-
Notifications
You must be signed in to change notification settings - Fork 19
/
data_source_kubernetes_clusters.go
302 lines (277 loc) · 8.05 KB
/
data_source_kubernetes_clusters.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
package provider
import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"wiz.io/hashicorp/terraform-provider-wiz/internal"
"wiz.io/hashicorp/terraform-provider-wiz/internal/client"
"wiz.io/hashicorp/terraform-provider-wiz/internal/utils"
"wiz.io/hashicorp/terraform-provider-wiz/internal/wiz"
)
// ReadKubernetesClusters struct
type ReadKubernetesClusters struct {
KubernetesClusters wiz.KubernetesClusterConnection `json:"kubernetesClusters"`
}
func dataSourceWizKubernetesClusters() *schema.Resource {
return &schema.Resource{
Description: "Get the details for Kubernetes clusters.",
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Internal identifier for the data.",
},
"first": {
Type: schema.TypeInt,
Optional: true,
Default: 50,
Description: "How many matches to return, maximum is `500` per page.",
},
"max_pages": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
Description: "How many pages to return. 0 means all pages.",
},
"search": {
Type: schema.TypeString,
Optional: true,
Description: "Free text search. Specify empty string to return all kubernetes clusters",
},
"external_ids": {
Type: schema.TypeList,
Optional: true,
Description: "The ID(s) to search by. i.e `Azure Subscription ID` or `AWS account number`.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"kind": {
Type: schema.TypeList,
Optional: true,
Description: fmt.Sprintf("Query Kubernetes Cluster of specific kind(s) or cloud provider(s).\n - Allowed values: %s",
utils.SliceOfStringToMDUList(
wiz.KubernetesClusterKind,
),
),
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(
validation.StringInSlice(
wiz.KubernetesClusterKind,
false,
),
),
},
},
"cloud_provider": {
Type: schema.TypeList,
Optional: true,
Description: fmt.Sprintf("Query cloud accounts of specific cloud provider.\n - Allowed values: %s",
utils.SliceOfStringToMDUList(
wiz.CloudProvider,
),
),
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(
validation.StringInSlice(
wiz.CloudProvider,
false,
),
),
},
},
"kubernetes_clusters": {
Type: schema.TypeSet,
Computed: true,
Description: "The returned kubernetes clusters.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Description: "Internal Wiz ID of Kubernetes Cluster.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the Kubernetes Cluster.",
},
"cloud_account": {
Type: schema.TypeSet,
Optional: true,
Description: "The cloud account details for the kubernetes cluster.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Description: "Internal Wiz ID of Cloud Account.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the cloud account.",
},
"cloud_provider": {
Type: schema.TypeString,
Optional: true,
Description: "The cloud provider of the cloud account.",
},
"external_id": {
Type: schema.TypeString,
Computed: true,
Description: "The external ID of the cloud account.",
},
},
},
},
},
},
},
},
ReadContext: dataSourceWizKubernetesClustersRead,
}
}
func dataSourceWizKubernetesClustersRead(ctx context.Context, d *schema.ResourceData, m interface{}) (diags diag.Diagnostics) {
tflog.Info(ctx, "dataSourceWizKubernetesClustersRead called...")
var identifier bytes.Buffer
a, b := d.GetOk("first")
if b {
identifier.WriteString(utils.PrettyPrint(a))
}
a, b = d.GetOk("id")
if b {
identifier.WriteString(utils.PrettyPrint(a))
}
a, b = d.GetOk("search")
if b {
identifier.WriteString(utils.PrettyPrint(a))
}
a, b = d.GetOk("kind")
if b {
identifier.WriteString(utils.PrettyPrint(a))
}
a, b = d.GetOk("external_ids")
if b {
identifier.WriteString(utils.PrettyPrint(a))
}
maxPages, b := d.GetOk("max_pages")
if b {
identifier.WriteString(utils.PrettyPrint(maxPages))
}
h := sha1.New()
h.Write([]byte(identifier.String()))
hashID := hex.EncodeToString(h.Sum(nil))
// Set the id
d.SetId(hashID)
query := `query ClustersPage($filterBy: KubernetesClusterFilters, $first: Int, $after: String) {
kubernetesClusters(filterBy: $filterBy, first: $first, after: $after) {
nodes {
id
externalId
name
kind
cloudAccount {
id
name
cloudProvider
externalId
}
projects {
id
name
slug
riskProfile {
businessImpact
}
}
}
pageInfo {
endCursor
hasNextPage
}
totalCount
}
}`
// set the resource parameters
err := d.Set("search", d.Get("search").(string))
if err != nil {
return append(diags, diag.FromErr(err)...)
}
err = d.Set("id", d.Get("id").(string))
if err != nil {
return append(diags, diag.FromErr(err)...)
}
err = d.Set("kind", d.Get("kind").([]interface{}))
if err != nil {
return append(diags, diag.FromErr(err)...)
}
err = d.Set("external_ids", d.Get("external_ids").([]interface{}))
if err != nil {
return append(diags, diag.FromErr(err)...)
}
// populate the graphql variables
vars := &internal.QueryVariables{}
vars.First = d.Get("first").(int)
filterBy := &wiz.KubernetesClusterFilters{}
a, b = d.GetOk("search")
if b {
filterBy.Search = a.(string)
}
a, b = d.GetOk("kind")
if b {
filterBy.Kind = utils.ConvertListToString(a.([]interface{}))
}
a, b = d.GetOk("external_ids")
if b {
filterBy.CloudAccount = utils.ConvertListToString(a.([]interface{}))
}
vars.FilterBy = filterBy
// process the request
data := &ReadKubernetesClusters{}
requestDiags, allData := client.ProcessPagedRequest(ctx, m, vars, data, query, "kubernetesClusters", "read", maxPages.(int))
tflog.Debug(ctx, fmt.Sprintf("allData: %s", utils.PrettyPrint(allData)))
diags = append(diags, requestDiags...)
if len(diags) > 0 {
return diags
}
clusters := flattenClusters(ctx, allData)
if err := d.Set("kubernetes_clusters", clusters); err != nil {
return append(diags, diag.FromErr(err)...)
}
return diags
}
func flattenClusters(ctx context.Context, clusters []interface{}) []interface{} {
tflog.Info(ctx, "flattenClusters called...")
tflog.Debug(ctx, fmt.Sprintf("Clusters: %s", utils.PrettyPrint(clusters)))
// walk the slice and construct the list
var output = make([]interface{}, 0)
for _, b := range clusters {
readClusters := b.(*ReadKubernetesClusters)
for _, cluster := range readClusters.KubernetesClusters.Nodes {
tflog.Debug(ctx, fmt.Sprintf("cluster: %s", utils.PrettyPrint(cluster)))
rootMap := make(map[string]interface{})
rootMap["id"] = cluster.ID
rootMap["name"] = cluster.Name
clusterMap := make(map[string]interface{})
clusterMap["cloud_provider"] = cluster.CloudAccount.CloudProvider
clusterMap["external_id"] = cluster.CloudAccount.ExternalID
clusterMap["id"] = cluster.CloudAccount.ID
clusterMap["name"] = cluster.CloudAccount.Name
cloudAccountMap := make([]interface{}, 0)
cloudAccountMap = append(cloudAccountMap, clusterMap)
rootMap["cloud_account"] = cloudAccountMap
output = append(output, rootMap)
}
}
tflog.Debug(ctx, fmt.Sprintf("flattenClusters output: %s", utils.PrettyPrint(output)))
return output
}