1/*
2Copyright 2018 The Doctl Authors All rights reserved.
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6    http://www.apache.org/licenses/LICENSE-2.0
7Unless required by applicable law or agreed to in writing, software
8distributed under the License is distributed on an "AS IS" BASIS,
9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10See the License for the specific language governing permissions and
11limitations under the License.
12*/
13
14package do
15
16import (
17	"context"
18
19	"github.com/digitalocean/godo"
20)
21
22// AlertPolicy is a wrapper for godo.AlertPolicy
23type AlertPolicy struct {
24	*godo.AlertPolicy
25}
26
27// AlertPolicies is a slice of AlertPolicy.
28type AlertPolicies []AlertPolicy
29
30// MonitoringService is an interface for interacting with DigitalOcean's monitoring api.
31type MonitoringService interface {
32	ListAlertPolicies() (AlertPolicies, error)
33	GetAlertPolicy(string) (*AlertPolicy, error)
34	CreateAlertPolicy(request *godo.AlertPolicyCreateRequest) (*AlertPolicy, error)
35	UpdateAlertPolicy(uuid string, request *godo.AlertPolicyUpdateRequest) (*AlertPolicy, error)
36	DeleteAlertPolicy(string) error
37}
38
39type monitoringService struct {
40	client *godo.Client
41}
42
43var _ MonitoringService = (*monitoringService)(nil)
44
45// NewMonitoringService builds a MonitoringService instance.
46func NewMonitoringService(godoClient *godo.Client) MonitoringService {
47	return &monitoringService{
48		client: godoClient,
49	}
50}
51
52func (ms *monitoringService) ListAlertPolicies() (AlertPolicies, error) {
53	f := func(opt *godo.ListOptions) ([]interface{}, *godo.Response, error) {
54		list, resp, err := ms.client.Monitoring.ListAlertPolicies(context.TODO(), opt)
55		if err != nil {
56			return nil, nil, err
57		}
58
59		si := make([]interface{}, len(list))
60		for i := range list {
61			si[i] = list[i]
62		}
63
64		return si, resp, err
65	}
66
67	si, err := PaginateResp(f)
68	if err != nil {
69		return nil, err
70	}
71
72	list := make(AlertPolicies, len(si))
73	for i := range si {
74		a := si[i].(godo.AlertPolicy)
75		list[i] = AlertPolicy{AlertPolicy: &a}
76	}
77
78	return list, nil
79}
80
81func (ms *monitoringService) GetAlertPolicy(uuid string) (*AlertPolicy, error) {
82	p, _, err := ms.client.Monitoring.GetAlertPolicy(context.TODO(), uuid)
83	if err != nil {
84		return nil, err
85	}
86
87	return &AlertPolicy{AlertPolicy: p}, nil
88}
89
90func (ms *monitoringService) CreateAlertPolicy(apcr *godo.AlertPolicyCreateRequest) (*AlertPolicy, error) {
91	p, _, err := ms.client.Monitoring.CreateAlertPolicy(context.TODO(), apcr)
92	if err != nil {
93		return nil, err
94	}
95
96	return &AlertPolicy{AlertPolicy: p}, nil
97}
98
99func (ms *monitoringService) UpdateAlertPolicy(uuid string, apur *godo.AlertPolicyUpdateRequest) (*AlertPolicy, error) {
100	p, _, err := ms.client.Monitoring.UpdateAlertPolicy(context.TODO(), uuid, apur)
101	if err != nil {
102		return nil, err
103	}
104
105	return &AlertPolicy{AlertPolicy: p}, nil
106}
107
108func (ms *monitoringService) DeleteAlertPolicy(uuid string) error {
109	_, err := ms.client.Monitoring.DeleteAlertPolicy(context.TODO(), uuid)
110	return err
111}
112