-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure.go
348 lines (278 loc) · 8.53 KB
/
azure.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-Present Datadog, Inc.
package cloudcraft
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/DataDog/cloudcraft-go/internal/xerrors"
)
// azureAccountPath is the path to the Azure endpoint of the Cloudcraft API.
const azureAccountPath string = "azure/account"
const (
// ErrEmptyApplicationID is returned when an Azure account is created with
// an empty application ID.
ErrEmptyApplicationID xerrors.Error = "field 'ApplicationID' cannot be empty"
// ErrEmptyDirectoryID is returned when an Azure account is created with
// an empty directory ID.
ErrEmptyDirectoryID xerrors.Error = "field 'DirectoryID' cannot be empty"
// ErrEmptySubscriptionID is returned when an Azure account is created with
// an empty subscription ID.
ErrEmptySubscriptionID xerrors.Error = "field 'SubscriptionID' cannot be empty"
// ErrEmptyClientSecret is returned when an Azure account is created with
// an empty client secret.
ErrEmptyClientSecret xerrors.Error = "field 'ClientSecret' cannot be empty"
)
// AzureService handles communication with the "/azure" endpoint of Cloudcraft's
// developer API.
type AzureService service
// AzureAccount represents an Azure account registered with Cloudcraft.
type AzureAccount struct {
CreatedAt time.Time `json:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt,omitempty"`
ReadAccess *[]string `json:"readAccess,omitempty"`
WriteAccess *[]string `json:"writeAccess,omitempty"`
CustomerID *string `json:"CustomerId,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
ApplicationID string `json:"applicationId,omitempty"`
DirectoryID string `json:"directoryId,omitempty"`
SubscriptionID string `json:"subscriptionId,omitempty"`
ClientSecret string `json:"clientSecret,omitempty"`
CreatorID string `json:"CreatorId,omitempty"`
Hint string `json:"hint,omitempty"`
Source string `json:"source,omitempty"`
}
// List returns a list of Azure accounts linked with Cloudcraft.
//
// [API reference].
//
// [API reference]: https://docs.datadoghq.com/cloudcraft/api/azure-accounts/#list-azure-accounts
func (s *AzureService) List(ctx context.Context) ([]*AzureAccount, *Response, error) {
if ctx == nil {
return nil, nil, ErrNilContext
}
var (
baseURL = s.client.cfg.endpoint.String()
endpoint strings.Builder
)
endpoint.Grow(len(baseURL) + len(azureAccountPath))
endpoint.WriteString(baseURL)
endpoint.WriteString(azureAccountPath)
req, err := s.client.request(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return nil, nil, fmt.Errorf("%w", err)
}
resp, err := s.client.do(req)
if err != nil {
return nil, resp, fmt.Errorf("%w", err)
}
var result map[string][]*AzureAccount
if err := json.Unmarshal(resp.Body, &result); err != nil {
return nil, resp, fmt.Errorf("%w", err)
}
accounts, ok := result["accounts"]
if !ok {
return nil, resp, fmt.Errorf("%w", ErrAccountsKey)
}
return accounts, resp, nil
}
// Create registers a new Azure account with Cloudcraft.
//
// [API reference].
//
// [API reference]: https://docs.datadoghq.com/cloudcraft/api/azure-accounts/#add-an-azure-account
func (s *AzureService) Create(ctx context.Context, account *AzureAccount) (*AzureAccount, *Response, error) {
if ctx == nil {
return nil, nil, ErrNilContext
}
if account == nil {
return nil, nil, ErrNilAccount
}
if account.Name == "" {
return nil, nil, ErrEmptyAccountName
}
if account.ApplicationID == "" {
return nil, nil, ErrEmptyApplicationID
}
if account.DirectoryID == "" {
return nil, nil, ErrEmptyDirectoryID
}
if account.SubscriptionID == "" {
return nil, nil, ErrEmptySubscriptionID
}
if account.ClientSecret == "" {
return nil, nil, ErrEmptyClientSecret
}
var (
baseURL = s.client.cfg.endpoint.String()
endpoint strings.Builder
)
endpoint.Grow(len(baseURL) + len(azureAccountPath))
endpoint.WriteString(baseURL)
endpoint.WriteString(azureAccountPath)
payload, err := json.Marshal(account)
if err != nil {
return nil, nil, fmt.Errorf("%w", err)
}
req, err := s.client.request(ctx, http.MethodPost, endpoint.String(), bytes.NewReader(payload))
if err != nil {
return nil, nil, fmt.Errorf("%w", err)
}
resp, err := s.client.do(req)
if err != nil {
return nil, resp, fmt.Errorf("%w", err)
}
var result *AzureAccount
if err := json.Unmarshal(resp.Body, &result); err != nil {
return nil, resp, fmt.Errorf("%w", err)
}
return result, resp, nil
}
// Update updates an AWS account registered in Cloudcraft.
//
// [API reference].
//
// [API reference]: https://docs.datadoghq.com/cloudcraft/api/azure-accounts/#update-an-azure-account
func (s *AzureService) Update(ctx context.Context, account *AzureAccount) (*Response, error) {
if ctx == nil {
return nil, ErrNilContext
}
if account == nil {
return nil, ErrNilAccount
}
if account.ID == "" {
return nil, ErrEmptyAccountID
}
if account.Name == "" {
return nil, ErrEmptyAccountName
}
if account.ApplicationID == "" {
return nil, ErrEmptyApplicationID
}
if account.DirectoryID == "" {
return nil, ErrEmptyDirectoryID
}
if account.SubscriptionID == "" {
return nil, ErrEmptySubscriptionID
}
if account.ClientSecret == "" {
return nil, ErrEmptyClientSecret
}
var (
baseURL = s.client.cfg.endpoint.String()
endpoint strings.Builder
)
endpoint.Grow(len(baseURL) + len(azureAccountPath) + len(account.ID) + 1)
endpoint.WriteString(baseURL)
endpoint.WriteString(azureAccountPath)
endpoint.WriteByte('/')
endpoint.WriteString(account.ID)
payload, err := json.Marshal(account)
if err != nil {
return nil, fmt.Errorf("%w", err)
}
req, err := s.client.request(ctx, http.MethodPut, endpoint.String(), bytes.NewReader(payload))
if err != nil {
return nil, fmt.Errorf("%w", err)
}
resp, err := s.client.do(req)
if err != nil {
return resp, fmt.Errorf("%w", err)
}
return resp, nil
}
// Delete deletes a registered AWS account from Cloudcraft by ID.
//
// [API reference].
//
// [API reference]: https://docs.datadoghq.com/cloudcraft/api/azure-accounts/#delete-an-azure-account
func (s *AzureService) Delete(ctx context.Context, id string) (*Response, error) {
if ctx == nil {
return nil, ErrNilContext
}
if id == "" {
return nil, ErrEmptyAccountID
}
var (
baseURL = s.client.cfg.endpoint.String()
endpoint strings.Builder
)
endpoint.Grow(len(baseURL) + len(azureAccountPath) + len(id) + 1)
endpoint.WriteString(baseURL)
endpoint.WriteString(azureAccountPath)
endpoint.WriteByte('/')
endpoint.WriteString(id)
req, err := s.client.request(ctx, http.MethodDelete, endpoint.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("%w", err)
}
resp, err := s.client.do(req)
if err != nil {
return resp, fmt.Errorf("%w", err)
}
return resp, nil
}
// Snapshot scans and render a region of an Azure account into a blueprint in
// JSON, SVG, PNG, PDF or MxGraph format.
//
// [API reference].
//
// [API reference]: https://docs.datadoghq.com/cloudcraft/api/azure-accounts/#snapshot-an-azure-account
func (s *AzureService) Snapshot(
ctx context.Context,
id, region, format string,
params *SnapshotParams,
) ([]byte, *Response, error) {
if ctx == nil {
return nil, nil, ErrNilContext
}
if id == "" {
return nil, nil, ErrEmptyAccountID
}
if region == "" {
return nil, nil, ErrEmptyRegion
}
if format == "" {
format = DefaultSnapshotFormat
}
if params == nil {
params = &SnapshotParams{
Width: DefaultSnapshotWidth,
Height: DefaultSnapshotHeight,
}
}
var (
baseURL = s.client.cfg.endpoint.String()
endpoint strings.Builder
)
endpoint.Grow(len(baseURL) + len(azureAccountPath) + len(id) + len(region) + len(format) + 3)
endpoint.WriteString(baseURL)
endpoint.WriteString(azureAccountPath)
endpoint.WriteByte('/')
endpoint.WriteString(id)
endpoint.WriteByte('/')
endpoint.WriteString(region)
endpoint.WriteByte('/')
endpoint.WriteString(format)
u, err := url.Parse(endpoint.String())
if err != nil {
return nil, nil, fmt.Errorf("%w", err)
}
u.RawQuery = params.query().Encode()
req, err := s.client.request(ctx, http.MethodGet, u.String(), http.NoBody)
if err != nil {
return nil, nil, fmt.Errorf("%w", err)
}
resp, err := s.client.do(req)
if err != nil {
return nil, resp, fmt.Errorf("%w", err)
}
return resp.Body, resp, nil
}