-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdata_stack.go
575 lines (559 loc) · 18.3 KB
/
data_stack.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package spacelift
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal"
"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/structs"
"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/validations"
)
func dataStack() *schema.Resource {
return &schema.Resource{
Description: "" +
"`spacelift_stack` combines source code and configuration to create a " +
"runtime environment where resources are managed. In this way it's " +
"similar to a stack in AWS CloudFormation, or a project on generic " +
"CI/CD platforms.",
ReadContext: dataStackRead,
Schema: map[string]*schema.Schema{
"administrative": {
Type: schema.TypeBool,
Description: "indicates whether this stack can administer others",
Computed: true,
},
"ansible": {
Type: schema.TypeList,
Description: "Ansible-specific configuration. Presence means this Stack is an Ansible Stack.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"playbook": {
Type: schema.TypeString,
Description: "The playbook the Ansible stack should run.",
Computed: true,
},
},
},
},
"after_apply": {
Type: schema.TypeList,
Description: "List of after-apply scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"after_destroy": {
Type: schema.TypeList,
Description: "List of after-destroy scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"after_init": {
Type: schema.TypeList,
Description: "List of after-init scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"after_perform": {
Type: schema.TypeList,
Description: "List of after-perform scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"after_plan": {
Type: schema.TypeList,
Description: "List of after-plan scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"after_run": {
Type: schema.TypeList,
Description: "List of after-run scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
"autodeploy": {
Type: schema.TypeBool,
Description: "indicates whether changes to this stack can be automatically deployed",
Computed: true,
},
"autoretry": {
Type: schema.TypeBool,
Description: "indicates whether obsolete proposed changes should automatically be retried",
Computed: true,
},
"aws_assume_role_policy_statement": {
Type: schema.TypeString,
Description: "AWS IAM assume role policy statement setting up trust relationship",
Computed: true,
},
"azure_devops": {
Type: schema.TypeList,
Description: "Azure DevOps VCS settings",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "ID of the Azure Devops VCS integration",
Computed: true,
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default Azure Devops VCS integration",
},
"project": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the Azure DevOps project",
},
},
},
},
"before_apply": {
Type: schema.TypeList,
Description: "List of before-apply scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"before_destroy": {
Type: schema.TypeList,
Description: "List of before-destroy scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"before_init": {
Type: schema.TypeList,
Description: "List of before-init scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"before_perform": {
Type: schema.TypeList,
Description: "List of before-perform scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"before_plan": {
Type: schema.TypeList,
Description: "List of before-plan scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"bitbucket_cloud": {
Type: schema.TypeList,
Description: "Bitbucket Cloud VCS settings",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "ID of the Bitbucket Cloud integration",
Computed: true,
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default Bitbucket Cloud integration",
},
"namespace": {
Type: schema.TypeString,
Description: "Bitbucket Cloud namespace of the stack's repository",
Computed: true,
},
},
},
},
"bitbucket_datacenter": {
Type: schema.TypeList,
Description: "Bitbucket Datacenter VCS settings",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "ID of the Bitbucket Datacenter integration",
Computed: true,
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default Bitbucket Datacenter integration",
},
"namespace": {
Type: schema.TypeString,
Description: "Bitbucket Datacenter namespace of the stack's repository",
Computed: true,
},
},
},
},
"branch": {
Type: schema.TypeString,
Description: "Repository branch to treat as the default 'main' branch",
Computed: true,
},
"cloudformation": {
Type: schema.TypeList,
Description: "CloudFormation-specific configuration. Presence means this Stack is a CloudFormation Stack.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"entry_template_file": {
Type: schema.TypeString,
Description: "Template file `cloudformation package` will be called on",
Computed: true,
},
"region": {
Type: schema.TypeString,
Description: "AWS region to use",
Computed: true,
},
"stack_name": {
Type: schema.TypeString,
Description: "CloudFormation stack name",
Computed: true,
},
"template_bucket": {
Type: schema.TypeString,
Description: "S3 bucket to save CloudFormation templates to",
Computed: true,
},
},
},
},
"description": {
Type: schema.TypeString,
Description: "free-form stack description for users",
Computed: true,
},
"github_enterprise": {
Type: schema.TypeList,
Description: "GitHub Enterprise (self-hosted) VCS settings",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "ID of the GitHub Enterprise integration",
Computed: true,
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default GitHub Enterprise integration",
},
"namespace": {
Type: schema.TypeString,
Description: "GitHub Enterprise namespace of the stack's repository",
Computed: true,
},
},
},
},
"gitlab": {
Type: schema.TypeList,
Description: "GitLab VCS settings",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "ID of the Gitlab integration",
Computed: true,
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default Gitlab integration",
},
"namespace": {
Type: schema.TypeString,
Description: "GitLab namespace of the stack's repository",
Computed: true,
},
},
},
},
"labels": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"enable_local_preview": {
Type: schema.TypeBool,
Description: "Indicates whether local preview runs can be triggered on this Stack.",
Computed: true,
},
"enable_well_known_secret_masking": {
Type: schema.TypeBool,
Description: "Indicates whether well-known secret masking is enabled.",
Computed: true,
},
"kubernetes": {
Type: schema.TypeList,
Description: "Kubernetes-specific configuration. Presence means this Stack is a Kubernetes Stack.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"namespace": {
Type: schema.TypeString,
Description: "Namespace of the Kubernetes cluster to run commands on. Leave empty for multi-namespace Stacks.",
Computed: true,
},
"kubectl_version": {
Type: schema.TypeString,
Description: "Kubectl version.",
Computed: true,
},
},
},
},
"manage_state": {
Type: schema.TypeBool,
Description: "Determines if Spacelift should manage state for this stack",
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "Name of the stack - should be unique in one account",
Computed: true,
},
"project_root": {
Type: schema.TypeString,
Description: "Project root is the optional directory relative to the workspace root containing the entrypoint to the Stack.",
Computed: true,
},
"additional_project_globs": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "Project globs is an optional list of paths to track changes of in addition to the project root.",
},
"protect_from_deletion": {
Type: schema.TypeBool,
Description: "Protect this stack from accidental deletion. If set, attempts to delete this stack will fail.",
Computed: true,
},
"pulumi": {
Type: schema.TypeList,
Description: "Pulumi-specific configuration. Presence means this Stack is a Pulumi Stack.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"login_url": {
Type: schema.TypeString,
Description: "State backend to log into on Run initialize.",
Computed: true,
},
"stack_name": {
Type: schema.TypeString,
Description: "Pulumi stack name to use with the state backend.",
Computed: true,
},
},
},
},
"raw_git": {
Type: schema.TypeList,
Description: "One-way VCS integration using a raw Git repository link",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"namespace": {
Type: schema.TypeString,
Description: "User-friendly namespace for the repository, this is for cosmetic purposes only",
Computed: true,
},
"url": {
Type: schema.TypeString,
Description: "HTTPS URL of the Git repository",
Computed: true,
},
},
},
},
"repository": {
Type: schema.TypeString,
Description: "Name of the repository, without the owner part",
Computed: true,
},
"runner_image": {
Type: schema.TypeString,
Description: "Name of the Docker image used to process Runs",
Computed: true,
},
"showcase": {
Type: schema.TypeList,
Description: "Showcase-related attributes",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"namespace": {
Type: schema.TypeString,
Description: "GitHub namespace of the stack's repository",
Computed: true,
},
},
},
},
"space_id": {
Type: schema.TypeString,
Description: "ID (slug) of the space the stack is in",
Computed: true,
},
"stack_id": {
Type: schema.TypeString,
Description: "ID (slug) of the stack",
Required: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
"terraform_external_state_access": {
Type: schema.TypeBool,
Description: "Indicates whether you can access the Stack state file from other stacks or outside of Spacelift.",
Computed: true,
},
"terraform_smart_sanitization": {
Type: schema.TypeBool,
Description: "Indicates whether runs on this will use terraform's sensitive value system to sanitize the outputs of Terraform state and plans in spacelift instead of sanitizing all fields.",
Computed: true,
},
"terraform_version": {
Type: schema.TypeString,
Description: "Terraform version to use",
Computed: true,
},
"terraform_workflow_tool": {
Type: schema.TypeString,
Description: "Defines the tool that will be used to execute the workflow. This can be one of `OPEN_TOFU`, `TERRAFORM_FOSS` or `CUSTOM`.",
Computed: true,
},
"terraform_workspace": {
Type: schema.TypeString,
Description: "Terraform workspace to select",
Computed: true,
},
"terragrunt": {
Type: schema.TypeList,
Description: "Terragrunt-specific configuration. Presence means this Stack is a Terragrunt Stack.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"terraform_version": {
Type: schema.TypeString,
Description: "The Terraform version.",
Computed: true,
},
"terragrunt_version": {
Type: schema.TypeString,
Description: "The Terragrunt version.",
Computed: true,
},
"use_run_all": {
Type: schema.TypeBool,
Description: "Whether to use `terragrunt run-all` instead of `terragrunt`.",
Computed: true,
},
"use_smart_sanitization": {
Type: schema.TypeBool,
Description: "Indicates whether runs on this will use Terraform's sensitive value system to sanitize the outputs of Terraform state and plans in spacelift instead of sanitizing all fields.",
Computed: true,
},
"tool": {
Type: schema.TypeString,
Description: "The IaC tool used by Terragrunt. Will be either OPEN_TOFU, TERRAFORM_FOSS or MANUALLY_PROVISIONED.",
Computed: true,
},
},
},
},
"worker_pool_id": {
Type: schema.TypeString,
Description: "ID of the worker pool to use",
Computed: true,
},
},
}
}
func dataStackRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var query struct {
Stack *structs.Stack `graphql:"stack(id: $id)"`
}
stackID := d.Get("stack_id")
variables := map[string]interface{}{"id": toID(stackID)}
if err := meta.(*internal.Client).Query(ctx, "StackRead", &query, variables); err != nil {
return diag.Errorf("could not query for stack: %v", err)
}
stack := query.Stack
if stack == nil {
return diag.Errorf("stack not found")
}
d.SetId(stackID.(string))
d.Set("administrative", stack.Administrative)
d.Set("after_apply", stack.AfterApply)
d.Set("after_destroy", stack.AfterDestroy)
d.Set("after_init", stack.AfterInit)
d.Set("after_perform", stack.AfterPerform)
d.Set("after_plan", stack.AfterPlan)
d.Set("after_run", stack.AfterRun)
d.Set("autodeploy", stack.Autodeploy)
d.Set("autoretry", stack.Autoretry)
d.Set("aws_assume_role_policy_statement", stack.Integrations.AWS.AssumeRolePolicyStatement)
d.Set("before_apply", stack.BeforeApply)
d.Set("before_destroy", stack.BeforeDestroy)
d.Set("before_init", stack.BeforeInit)
d.Set("before_perform", stack.BeforePerform)
d.Set("before_plan", stack.BeforePlan)
d.Set("branch", stack.Branch)
d.Set("description", stack.Description)
d.Set("enable_local_preview", stack.LocalPreviewEnabled)
d.Set("enable_well_known_secret_masking", stack.EnableWellKnownSecretMasking)
d.Set("manage_state", stack.ManagesStateFile)
d.Set("name", stack.Name)
d.Set("project_root", stack.ProjectRoot)
d.Set("protect_from_deletion", stack.ProtectFromDeletion)
d.Set("repository", stack.Repository)
d.Set("runner_image", stack.RunnerImage)
d.Set("terraform_version", stack.TerraformVersion)
d.Set("space_id", stack.Space)
if err := stack.ExportVCSSettings(d); err != nil {
return diag.FromErr(err)
}
labels := schema.NewSet(schema.HashString, []interface{}{})
for _, label := range stack.Labels {
labels.Add(label)
}
d.Set("labels", labels)
globs := schema.NewSet(schema.HashString, []interface{}{})
for _, gb := range stack.AdditionalProjectGlobs {
globs.Add(gb)
}
d.Set("additional_project_globs", globs)
if iacKey, iacSettings := stack.IaCSettings(); iacKey != "" {
if err := d.Set(iacKey, []interface{}{iacSettings}); err != nil {
return diag.Errorf("could not set IaC settings: %v", err)
}
} else { // this is a Terraform stack
d.Set("terraform_version", stack.VendorConfig.Terraform.Version)
d.Set("terraform_workspace", stack.VendorConfig.Terraform.Workspace)
d.Set("terraform_smart_sanitization", stack.VendorConfig.Terraform.UseSmartSanitization)
d.Set("terraform_external_state_access", stack.VendorConfig.Terraform.ExternalStateAccessEnabled)
d.Set("terraform_workflow_tool", stack.VendorConfig.Terraform.WorkflowTool)
}
if workerPool := stack.WorkerPool; workerPool != nil {
d.Set("worker_pool_id", workerPool.ID)
} else {
d.Set("worker_pool_id", nil)
}
return nil
}