/
contactpoint-handler.go
191 lines (165 loc) · 5.34 KB
/
contactpoint-handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package grafana
import (
"encoding/json"
"fmt"
"github.com/grafana/grafana-openapi-client-go/client/provisioning"
"github.com/grafana/grafana-openapi-client-go/models"
"github.com/grafana/grizzly/pkg/grizzly"
)
// AlertContactPointHandler is a Grizzly Handler for Grafana contactPoints
type AlertContactPointHandler struct {
grizzly.BaseHandler
}
// NewAlertContactPointHandler returns a new Grizzly Handler for Grafana contactPoints
func NewAlertContactPointHandler(provider grizzly.Provider) *AlertContactPointHandler {
return &AlertContactPointHandler{
BaseHandler: grizzly.NewBaseHandler(provider, "AlertContactPoint", false),
}
}
const (
contactPointPattern = "alert-contact-points/contactPoint-%s.%s"
)
// ResourceFilePath returns the location on disk where a resource should be updated
func (h *AlertContactPointHandler) ResourceFilePath(resource grizzly.Resource, filetype string) string {
return fmt.Sprintf(contactPointPattern, resource.Name(), filetype)
}
// Parse parses a manifest object into a struct for this resource type
func (h *AlertContactPointHandler) Parse(m map[string]any) (*grizzly.Resource, error) {
resource, err := grizzly.ResourceFromMap(m)
if err != nil {
return nil, err
}
resource.SetSpecString("uid", resource.Name())
return resource, nil
}
// Validate returns the uid of resource
func (h *AlertContactPointHandler) Validate(resource grizzly.Resource) error {
uid, exist := resource.GetSpecString("uid")
if exist {
if uid != resource.Name() {
return fmt.Errorf("uid '%s' and name '%s', don't match", uid, resource.Name())
}
}
return nil
}
func (h *AlertContactPointHandler) GetSpecUID(resource grizzly.Resource) (string, error) {
uid, ok := resource.GetSpecString("uid")
if !ok {
return "", fmt.Errorf("UID not specified")
}
return uid, nil
}
// GetByUID retrieves JSON for a resource from an endpoint, by UID
func (h *AlertContactPointHandler) GetByUID(UID string) (*grizzly.Resource, error) {
return h.getRemoteContactPoint(UID)
}
// GetRemote retrieves a contactPoint as a Resource
func (h *AlertContactPointHandler) GetRemote(resource grizzly.Resource) (*grizzly.Resource, error) {
return h.getRemoteContactPoint(resource.Name())
}
// ListRemote retrieves as list of UIDs of all remote resources
func (h *AlertContactPointHandler) ListRemote() ([]string, error) {
return h.getRemoteContactPointList()
}
// Add pushes a contactPoint to Grafana via the API
func (h *AlertContactPointHandler) Add(resource grizzly.Resource) error {
return h.postContactPoint(resource)
}
// Update pushes a contactPoint to Grafana via the API
func (h *AlertContactPointHandler) Update(existing, resource grizzly.Resource) error {
return h.putContactPoint(resource)
}
// getRemoteContactPoint retrieves a contactPoint object from Grafana
func (h *AlertContactPointHandler) getRemoteContactPoint(uid string) (*grizzly.Resource, error) {
client, err := h.Provider.(ClientProvider).Client()
if err != nil {
return nil, err
}
params := provisioning.NewGetContactpointsParams()
contactPoints, err := client.Provisioning.GetContactpoints(params)
if err != nil {
return nil, err
}
var point *models.EmbeddedContactPoint
for _, c := range contactPoints.GetPayload() {
if c.UID == uid {
point = c
break
}
}
if point == nil {
return nil, grizzly.ErrNotFound
}
// TODO: Turn spec into a real models.ContactPoint object
spec, err := structToMap(point)
if err != nil {
return nil, err
}
resource, err := grizzly.NewResource(h.APIVersion(), h.Kind(), uid, spec)
if err != nil {
return nil, err
}
return &resource, nil
}
func (h *AlertContactPointHandler) getRemoteContactPointList() ([]string, error) {
client, err := h.Provider.(ClientProvider).Client()
if err != nil {
return nil, err
}
params := provisioning.NewGetContactpointsParams()
contactPointsOk, err := client.Provisioning.GetContactpoints(params)
if err != nil {
return nil, err
}
contactPoints := contactPointsOk.GetPayload()
uids := make([]string, len(contactPoints))
for i, contactPoint := range contactPoints {
uids[i] = contactPoint.UID
}
return uids, nil
}
func (h *AlertContactPointHandler) postContactPoint(resource grizzly.Resource) error {
var contactPoint models.EmbeddedContactPoint
// TODO: Turn spec into a real models.ContactPoint object
data, err := json.Marshal(resource.Spec())
if err != nil {
return err
}
err = json.Unmarshal(data, &contactPoint)
if err != nil {
return err
}
client, err := h.Provider.(ClientProvider).Client()
if err != nil {
return err
}
stringtrue := "true"
params := provisioning.NewPostContactpointsParams().
WithBody(&contactPoint).
WithXDisableProvenance(&stringtrue)
_, err = client.Provisioning.PostContactpoints(params)
return err
}
func (h *AlertContactPointHandler) putContactPoint(resource grizzly.Resource) error {
// TODO: Turn spec into a real models.ContactPoint object
data, err := json.Marshal(resource.Spec())
if err != nil {
return err
}
var modelContactPoint models.EmbeddedContactPoint
err = json.Unmarshal(data, &modelContactPoint)
if err != nil {
return err
}
client, err := h.Provider.(ClientProvider).Client()
if err != nil {
return err
}
stringtrue := "true"
params := provisioning.NewPutContactpointParams().
WithUID(resource.Name()).
WithBody(&modelContactPoint).
WithXDisableProvenance(&stringtrue)
_, err = client.Provisioning.PutContactpoint(params)
return err
}