generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
data_source_gitops_cluster.go
464 lines (457 loc) · 16.9 KB
/
data_source_gitops_cluster.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package cluster
import (
"context"
"github.com/antihax/optional"
hh "github.com/harness/harness-go-sdk/harness/helpers"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceGitopsCluster() *schema.Resource {
resource := &schema.Resource{
Description: "Data source for fetching a Harness GitOps Cluster.",
ReadContext: dataSourceGitopsClusterRead,
Schema: map[string]*schema.Schema{
"account_id": {
Description: "Account identifier of the GitOps cluster.",
Type: schema.TypeString,
Required: true,
},
"project_id": {
Description: "Project identifier of the GitOps cluster.",
Type: schema.TypeString,
Optional: true,
},
"org_id": {
Description: "Organization identifier of the cluster.",
Type: schema.TypeString,
Optional: true,
},
"agent_id": {
Description: "Agent identifier of the GitOps cluster.",
Type: schema.TypeString,
Required: true,
},
"identifier": {
Description: "Identifier of the GitOps cluster.",
Type: schema.TypeString,
Required: true,
},
"query": {
Description: "Query for the GitOps cluster resources.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"server": {
Description: "Server of the GitOps cluster.",
Type: schema.TypeString,
Optional: true,
},
"name": {
Description: "Name of the GitOps cluster.",
Type: schema.TypeString,
Optional: true,
},
"id": {
Description: "Cluster server URL or the cluster name.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Description: "Type of the specified GitOps cluster identifier ( 'server' - default, 'name' ).",
Type: schema.TypeString,
Optional: true,
},
"value": {
Description: "Cluster server URL or the cluster name.",
Type: schema.TypeString,
Optional: true,
},
}},
},
},
},
},
"request": {
Description: "Cluster create or update request.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"upsert": {
Description: "Indicates if the GitOps cluster should be updated if existing and inserted if not.",
Type: schema.TypeBool,
Optional: true,
},
"updated_fields": {
Description: "Fields which are updated.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"update_mask": {
Description: "Update mask of the GitOps cluster.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"paths": {
Description: "The set of field mask paths.",
Optional: true,
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeList,
},
},
},
},
},
"id": {
Description: "Cluster server URL or the cluster name.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Description: "Type of the specified cluster identifier ( 'server' - default, 'name' ).",
Type: schema.TypeString,
Optional: true,
},
"value": {
Description: "Cluster server URL or the cluster name.",
Type: schema.TypeString,
Optional: true,
},
}},
},
"cluster": {
Description: "GitOps cluster details.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"server": {
Description: "API server URL of the kubernetes cluster.",
Type: schema.TypeString,
Optional: true,
},
"name": {
Description: "Name of the cluster. If omitted, will use the server address.",
Type: schema.TypeString,
Optional: true,
},
"config": {
Description: "GitOps cluster config.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"username": {
Description: "Username of the server of the cluster.",
Type: schema.TypeString,
Optional: true,
},
"password": {
Description: "Password of the server of the cluster.",
Type: schema.TypeString,
Optional: true,
},
"bearer_token": {
Description: "Bearer authentication token the cluster.",
Type: schema.TypeString,
Optional: true,
},
"tls_client_config": {
Description: "Settings to enable transport layer security.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"insecure": {
Description: "Indicates if the TLS connection to the cluster should be insecure.",
Type: schema.TypeBool,
Optional: true,
},
"server_name": {
Description: "Server name for SNI in the client to check server certificates against.",
Type: schema.TypeString,
Optional: true,
},
"cert_data": {
Description: "Certificate data holds PEM-encoded bytes (typically read from a client certificate file).",
Type: schema.TypeString,
Optional: true,
},
"key_data": {
Description: "Key data holds PEM-encoded bytes (typically read from a client certificate key file).",
Type: schema.TypeString,
Optional: true,
},
"ca_data": {
Description: "CA data holds PEM-encoded bytes (typically read from a root certificates bundle).",
Type: schema.TypeString,
Optional: true,
},
},
},
},
"aws_auth_config": {
Description: "IAM authentication configuration for AWS.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cluster_name": {
Description: "AWS cluster name.",
Type: schema.TypeString,
Optional: true,
},
"role_a_r_n": {
Description: "Optional role ARN. If set then used for AWS IAM Authenticator.",
Type: schema.TypeString,
Optional: true,
},
},
},
},
"exec_provider_config": {
Description: "Configuration for an exec provider.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"command": {
Description: "Command to execute.",
Type: schema.TypeString,
Optional: true,
},
"args": {
Description: "Arguments to pass to the command when executing it.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"env": {
Description: "Additional environment variables to expose to the process.",
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"api_version": {
Description: "Preferred input version of the ExecInfo.",
Type: schema.TypeString,
Optional: true,
},
"install_hint": {
Description: "Message displayed when the executable is not found.",
Type: schema.TypeString,
Optional: true,
},
},
},
},
"cluster_connection_type": {
Description: "Identifies the authentication method used to connect to the cluster.",
Type: schema.TypeString,
Optional: true,
},
},
},
},
"namespaces": {
Description: "List of namespaces which are accessible in that cluster. Cluster level resources will be ignored if namespace list is not empty.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"refresh_requested_at": {
Description: "Time when cluster cache refresh has been requested.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"seconds": {
Description: "Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.",
Type: schema.TypeString,
Optional: true,
},
"nanos": {
Description: "Non-negative fractions of a second at nanosecond resolution. Negative second values with fractions must still have non-negative nanos values that count forward in time. Must be from 0 to 999,999,999 inclusive. This field may be limited in precision depending on context.",
Type: schema.TypeInt,
Optional: true,
},
},
},
},
"info": {
Description: "Information about cluster cache and state.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"connection_state": {
Description: "Information about the connection to the cluster.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status": {
Description: "Current status indicator of the connection.",
Type: schema.TypeString,
Computed: true,
},
"message": {
Description: "Information about the connection status.",
Type: schema.TypeString,
Computed: true,
},
"attempted_at": {
Description: "Time when cluster cache refresh has been requested.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"seconds": {
Description: "Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.",
Type: schema.TypeString,
Optional: true,
},
"nanos": {
Description: "Non-negative fractions of a second at nanosecond resolution. Negative second values with fractions must still have non-negative nanos values that count forward in time. Must be from 0 to 999,999,999 inclusive. This field may be limited in precision depending on context.",
Type: schema.TypeInt,
Optional: true,
},
},
},
},
},
},
},
"server_version": {
Description: "Kubernetes version of the cluster.",
Type: schema.TypeString,
Computed: true,
},
"cache_info": {
Description: "Information about the cluster cache.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"resources_count": {
Description: "Number of observed kubernetes resources.",
Type: schema.TypeString,
Optional: true,
},
"apis_count": {
Description: "Number of observed kubernetes API count.",
Type: schema.TypeString,
Optional: true,
},
"last_cache_sync_time": {
Description: "Time of most recent cache synchronization.",
Type: schema.TypeString,
Optional: true,
},
},
},
},
"applications_count": {
Description: "Number of applications managed by Argo CD on the cluster.",
Type: schema.TypeString,
Optional: true,
},
"api_versions": {
Description: "List of API versions supported by the cluster.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"shard": {
Description: "Shard number. Calculated on the fly by the application controller if not specified.",
Type: schema.TypeString,
Optional: true,
},
"cluster_resources": {
Description: "Indicates if cluster level resources should be managed. This setting is used only if cluster is connected in a namespaced mode.",
Type: schema.TypeBool,
Optional: true,
},
"project": {
Description: "Reference between project and cluster that allow you automatically to be added as item inside Destinations project entity.",
Type: schema.TypeString,
Optional: true,
},
"labels": {
Description: "Labels for cluster secret metadata.",
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"annotations": {
Description: "Annotations for cluster secret metadata.",
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
},
},
},
}
return resource
}
func dataSourceGitopsClusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
ctx = context.WithValue(ctx, nextgen.ContextAccessToken, hh.EnvVars.BearerToken.Get())
agentIdentifier := d.Get("agent_id").(string)
identifier := d.Get("identifier").(string)
var queryName, queryServer string
if d.Get("query") != nil && len(d.Get("query").([]interface{})) > 0 {
query := d.Get("query").([]interface{})[0].(map[string]interface{})
queryServer = query["server"].(string)
queryName = query["name"].(string)
}
resp, httpResp, err := c.ClustersApi.AgentClusterServiceGet(ctx, agentIdentifier, identifier, c.AccountId, &nextgen.ClustersApiAgentClusterServiceGetOpts{
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
ProjectIdentifier: optional.NewString(d.Get("project_id").(string)),
QueryServer: optional.NewString(queryServer),
QueryName: optional.NewString(queryName),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
// Soft delete lookup error handling
// https://harness.atlassian.net/browse/PL-23765
if resp.Cluster == nil {
d.SetId("")
d.MarkNewResource()
return nil
}
setClusterDetails(d, &resp)
return nil
}