-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathresource_groups.go
342 lines (288 loc) · 9.92 KB
/
resource_groups.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
//
// Author:: Darren Murray (<[email protected]>)
// Copyright:: Copyright 2021, Lacework Inc.
// License:: Apache License, Version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package api
import (
_ "embed"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/pkg/errors"
)
type resourceGroupType int
const (
// type that defines a non-existing Resource Group
NoneResourceGroup resourceGroupType = iota
AwsResourceGroup
AzureResourceGroup
ContainerResourceGroup
GcpResourceGroup
MachineResourceGroup
OciResourceGroup
KubernetesResourceGroup
)
// query templates
var (
NoneResourceGroupQueryTemplate string = ""
//go:embed _templates/resource_groups/aws.json
AwsResourceGroupQueryTemplate string
//go:embed _templates/resource_groups/azure.json
AzureResourceGroupQueryTemplate string
//go:embed _templates/resource_groups/container.json
ContainerResourceGroupQueryTemplate string
//go:embed _templates/resource_groups/gcp.json
GcpResourceGroupQueryTemplate string
//go:embed _templates/resource_groups/machine.json
MachineResourceGroupQueryTemplate string
//go:embed _templates/resource_groups/oci.json
OciResourceGroupQueryTemplate string
//go:embed _templates/resource_groups/kubernetes.json
KubernetesResourceGroupQueryTemplate string
)
type resourceGroupContext struct {
resourceGroupType string
queryTemplate string
}
// ResourceGroupTypes is the list of available Resource Group types
var ResourceGroupTypes = map[resourceGroupType]resourceGroupContext{
NoneResourceGroup: {resourceGroupType: "None", queryTemplate: NoneResourceGroupQueryTemplate},
AwsResourceGroup: {resourceGroupType: "AWS", queryTemplate: AwsResourceGroupQueryTemplate},
AzureResourceGroup: {resourceGroupType: "AZURE", queryTemplate: AzureResourceGroupQueryTemplate},
ContainerResourceGroup: {resourceGroupType: "CONTAINER", queryTemplate: ContainerResourceGroupQueryTemplate},
GcpResourceGroup: {resourceGroupType: "GCP", queryTemplate: GcpResourceGroupQueryTemplate},
MachineResourceGroup: {resourceGroupType: "MACHINE", queryTemplate: MachineResourceGroupQueryTemplate},
OciResourceGroup: {resourceGroupType: "OCI", queryTemplate: OciResourceGroupQueryTemplate},
KubernetesResourceGroup: {resourceGroupType: "KUBERNETES", queryTemplate: KubernetesResourceGroupQueryTemplate},
}
func NewResourceGroup(name string, iType resourceGroupType,
description string, query *RGQuery) ResourceGroupData {
return ResourceGroupData{
Name: name,
Type: iType.String(),
Enabled: 1,
Query: query,
Description: description,
}
}
func (svc *ResourceGroupsService) List() (response ResourceGroupsResponse, err error) {
var rawResponse ResourceGroupsResponse
err = svc.client.RequestDecoder("GET", apiV2ResourceGroups, nil, &rawResponse)
if err != nil {
return rawResponse, err
}
err = sanitizeFieldsInRawResponseList(&rawResponse, &response)
if err != nil {
return rawResponse, err
}
return rawResponse, nil
}
func sanitizeFieldsInRawResponse(rawResponse *ResourceGroupResponse, response interface{}) error {
// update filters keys to match the query template
updateFiltersKeys(&rawResponse.Data)
j, err := json.Marshal(rawResponse)
if err != nil {
return err
}
return json.Unmarshal(j, &response)
}
func sanitizeFieldsInRawResponseList(rawResponse *ResourceGroupsResponse, response interface{}) error {
for i := range rawResponse.Data {
// update filters keys to match the query template
updateFiltersKeys(&rawResponse.Data[i])
}
j, err := json.Marshal(rawResponse)
if err != nil {
return err
}
return json.Unmarshal(j, &response)
}
func (svc *ResourceGroupsService) Create(group ResourceGroupData) (
response ResourceGroupResponse,
err error,
) {
var rawResponse ResourceGroupResponse
err = svc.create(group, &rawResponse)
if err != nil {
return
}
err = sanitizeFieldsInRawResponse(&rawResponse, &response)
return
}
func (svc *ResourceGroupsService) Update(data *ResourceGroupData) (
response ResourceGroupResponse,
err error,
) {
if data == nil {
err = errors.New("resource group must not be empty")
return
}
guid := data.ID()
data.ResetResourceGUID()
var rawResponse ResourceGroupResponse
err = svc.update(guid, data, &rawResponse)
if err != nil {
return
}
err = sanitizeFieldsInRawResponse(&rawResponse, &response)
return
}
func collectFilterNames(children []*RGChild, filterNames map[string]string) {
for _, child := range children {
if child.FilterName != "" {
normalizedKey := strings.ReplaceAll(strings.ToLower(child.FilterName), "_", "")
filterNames[normalizedKey] = child.FilterName
}
if len(child.Children) > 0 {
collectFilterNames(child.Children, filterNames)
}
}
}
/*
updateFiltersKeys updates the keys in the Filters map of ResourceGroupData to ensure they match the filter names
defined in the nested children of the query expression. This is necessary because JSON decoding/encoding can
convert keys to camel case, causing mismatches. The function normalizes the keys by removing underscores and
converting them to lower case, then compares them with the filter names. If a mismatch is found, the key is
updated to the value in RGExpression.Children
*/
func updateFiltersKeys(data *ResourceGroupData) {
if data.Query == nil || data.Query.Expression == nil {
return
}
filterNames := make(map[string]string)
collectFilterNames(data.Query.Expression.Children, filterNames)
updatedFilters := make(map[string]*RGFilter)
for key, value := range data.Query.Filters {
normalizedKey := strings.ReplaceAll(strings.ToLower(key), "_", "")
if _, exists := filterNames[normalizedKey]; exists {
updatedFilters[filterNames[normalizedKey]] = value
} else {
updatedFilters[key] = value
}
}
data.Query.Filters = updatedFilters
}
func (group *ResourceGroupData) ResetResourceGUID() {
group.ResourceGroupGuid = ""
group.UpdatedBy = ""
group.UpdatedTime = nil
group.CreatedBy = ""
group.CreatedTime = nil
group.IsDefaultBoolean = nil
}
func (svc *ResourceGroupsService) Delete(guid string) error {
if guid == "" {
return errors.New("specify a resourceGuid")
}
return svc.client.RequestDecoder(
"DELETE",
fmt.Sprintf(apiV2ResourceGroupsFromGUID, guid),
nil,
nil,
)
}
func (svc *ResourceGroupsService) Get(guid string, response interface{}) error {
var rawResponse ResourceGroupResponse
err := svc.get(guid, &rawResponse)
if err != nil {
return err
}
err = sanitizeFieldsInRawResponse(&rawResponse, response)
if err != nil {
return err
}
return nil
}
func (svc *ResourceGroupsService) create(data interface{}, response interface{}) error {
return svc.client.RequestEncoderDecoder("POST", apiV2ResourceGroups, data, response)
}
func (svc *ResourceGroupsService) get(guid string, response interface{}) error {
if guid == "" {
return errors.New("specify an resource group guid")
}
apiPath := fmt.Sprintf(apiV2ResourceGroupsFromGUID, guid)
return svc.client.RequestDecoder("GET", apiPath, nil, response)
}
func (svc *ResourceGroupsService) update(guid string, data interface{}, response interface{}) error {
if guid == "" {
return errors.New("specify a resource group guid")
}
apiPath := fmt.Sprintf(apiV2ResourceGroupsFromGUID, guid)
return svc.client.RequestEncoderDecoder("PATCH", apiPath, data, response)
}
type ResourceGroupsService struct {
client *Client
}
type RGExpression struct {
Operator string `json:"operator"`
Children []*RGChild `json:"children"`
}
type RGChild struct {
Operator string `json:"operator,omitempty"`
FilterName string `json:"filterName,omitempty"`
Children []*RGChild `json:"children,omitempty"`
}
type RGFilter struct {
Field string `json:"field"`
Operation string `json:"operation"`
Values []string `json:"values"`
Key string `json:"key,omitempty"`
}
type RGQuery struct {
Filters map[string]*RGFilter `json:"filters"`
Expression *RGExpression `json:"expression"`
}
// String returns the string representation of a Resource Group type
func (i resourceGroupType) String() string {
return ResourceGroupTypes[i].resourceGroupType
}
// QueryTemplate returns the resource group type's query template
func (i resourceGroupType) QueryTemplate() string {
return ResourceGroupTypes[i].queryTemplate
}
// FindResourceGroupType looks up inside the list of available resource group types
// the matching type from the provided string, if none, returns NoneResourceGroup
func FindResourceGroupType(typ string) (resourceGroupType, bool) {
for i, ctx := range ResourceGroupTypes {
if typ == ctx.resourceGroupType {
return i, true
}
}
return NoneResourceGroup, false
}
func (group *ResourceGroupData) ID() string {
return group.ResourceGroupGuid
}
type ResourceGroupResponse struct {
Data ResourceGroupData `json:"data"`
}
type ResourceGroupsResponse struct {
Data []ResourceGroupData `json:"data"`
}
type ResourceGroupData struct {
Name string `json:"name,omitempty"`
Query *RGQuery `json:"query,omitempty"`
Description string `json:"description,omitempty"`
ResourceGroupGuid string `json:"resourceGroupGuid,omitempty"`
CreatedTime *time.Time `json:"createdTime,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
UpdatedTime *time.Time `json:"updatedTime,omitempty"`
UpdatedBy string `json:"updatedBy,omitempty"`
IsDefaultBoolean *bool `json:"isDefaultBoolean,omitempty"`
Type string `json:"resourceType"`
Enabled int `json:"enabled"`
}