-
Notifications
You must be signed in to change notification settings - Fork 26
/
resource_stack.go
1090 lines (985 loc) · 37.1 KB
/
resource_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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package spacelift
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
"github.com/shurcooL/graphql"
"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 resourceStack() *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.",
CreateContext: resourceStackCreate,
ReadContext: resourceStackRead,
UpdateContext: resourceStackUpdate,
DeleteContext: resourceStackDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceStackImport,
},
Schema: map[string]*schema.Schema{
"administrative": {
Type: schema.TypeBool,
Description: "Indicates whether this stack can manage others. Defaults to `false`.",
Optional: true,
Default: false,
},
"ansible": {
Type: schema.TypeList,
ConflictsWith: []string{"cloudformation", "kubernetes", "pulumi", "terraform_version", "terraform_workflow_tool", "terraform_workspace", "terragrunt"},
Description: "Ansible-specific configuration. Presence means this Stack is an Ansible Stack.",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"playbook": {
Type: schema.TypeString,
Description: "The playbook Ansible should run.",
Required: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
},
},
},
"after_apply": {
Type: schema.TypeList,
Description: "List of after-apply scripts",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validations.DisallowEmptyString,
},
Optional: true,
},
"after_destroy": {
Type: schema.TypeList,
Description: "List of after-destroy scripts",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validations.DisallowEmptyString,
},
Optional: true,
},
"after_init": {
Type: schema.TypeList,
Description: "List of after-init scripts",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validations.DisallowEmptyString,
},
Optional: true,
},
"after_perform": {
Type: schema.TypeList,
Description: "List of after-perform scripts",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validations.DisallowEmptyString,
},
Optional: true,
},
"after_plan": {
Type: schema.TypeList,
Description: "List of after-plan scripts",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validations.DisallowEmptyString,
},
Optional: true,
},
"after_run": {
Type: schema.TypeList,
Description: "List of after-run scripts",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validations.DisallowEmptyString,
},
Optional: true,
},
"autodeploy": {
Type: schema.TypeBool,
Description: "Indicates whether changes to this stack can be automatically deployed. Defaults to `false`.",
Optional: true,
Default: false,
},
"autoretry": {
Type: schema.TypeBool,
Description: "Indicates whether obsolete proposed changes should automatically be retried. Defaults to `false`.",
Optional: true,
Default: false,
},
"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",
Optional: true,
ConflictsWith: conflictingVCSProviders("azure_devops"),
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Description: "The ID of the Azure Devops integration. If not specified, the default integration will be used.",
DiffSuppressFunc: func(_, _, new string, res *schema.ResourceData) bool {
isDefault := res.Get("azure_devops.0.is_default").(bool)
return isDefault && new == ""
},
},
"project": {
Type: schema.TypeString,
Required: true,
Description: "The name of the Azure DevOps project",
ValidateDiagFunc: validations.DisallowEmptyString,
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default Azure DevOps integration",
},
},
},
},
"before_apply": {
Type: schema.TypeList,
Description: "List of before-apply scripts",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validations.DisallowEmptyString,
},
Optional: true,
},
"before_destroy": {
Type: schema.TypeList,
Description: "List of before-destroy scripts",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validations.DisallowEmptyString,
},
Optional: true,
},
"before_init": {
Type: schema.TypeList,
Description: "List of before-init scripts",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validations.DisallowEmptyString,
},
Optional: true,
},
"before_perform": {
Type: schema.TypeList,
Description: "List of before-perform scripts",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validations.DisallowEmptyString,
},
Optional: true,
},
"before_plan": {
Type: schema.TypeList,
Description: "List of before-plan scripts",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validations.DisallowEmptyString,
},
Optional: true,
},
"branch": {
Type: schema.TypeString,
Description: "Git branch to apply changes to",
Required: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
"bitbucket_cloud": {
Type: schema.TypeList,
Description: "Bitbucket Cloud VCS settings",
Optional: true,
ConflictsWith: conflictingVCSProviders("bitbucket_cloud"),
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Description: "The ID of the Bitbucket Cloud integration. If not specified, the default integration will be used.",
DiffSuppressFunc: func(_, _, new string, res *schema.ResourceData) bool {
isDefault := res.Get("bitbucket_cloud.0.is_default").(bool)
return isDefault && new == ""
},
},
"namespace": {
Type: schema.TypeString,
Required: true,
Description: "The Bitbucket project containing the repository",
ValidateDiagFunc: validations.DisallowEmptyString,
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default Bitbucket Cloud integration",
},
},
},
},
"bitbucket_datacenter": {
Type: schema.TypeList,
Description: "Bitbucket Datacenter VCS settings",
Optional: true,
ConflictsWith: conflictingVCSProviders("bitbucket_datacenter"),
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Description: "The ID of the Bitbucket Datacenter integration. If not specified, the default integration will be used.",
DiffSuppressFunc: func(_, _, new string, res *schema.ResourceData) bool {
isDefault := res.Get("bitbucket_datacenter.0.is_default").(bool)
return isDefault && new == ""
},
},
"namespace": {
Type: schema.TypeString,
Required: true,
Description: "The Bitbucket project containing the repository",
ValidateDiagFunc: validations.DisallowEmptyString,
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default Bitbucket Datacenter integration",
},
},
},
},
"cloudformation": {
Type: schema.TypeList,
ConflictsWith: []string{"ansible", "kubernetes", "pulumi", "terraform_version", "terraform_workflow_tool", "terraform_workspace", "terragrunt"},
Description: "CloudFormation-specific configuration. Presence means this Stack is a CloudFormation Stack.",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"entry_template_file": {
Type: schema.TypeString,
Description: "Template file `cloudformation package` will be called on",
Required: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
"region": {
Type: schema.TypeString,
Description: "AWS region to use",
Required: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
"stack_name": {
Type: schema.TypeString,
Description: "CloudFormation stack name",
Required: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
"template_bucket": {
Type: schema.TypeString,
Description: "S3 bucket to save CloudFormation templates to",
Required: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
},
},
},
"description": {
Type: schema.TypeString,
Description: "Free-form stack description for users",
Optional: true,
},
"enable_local_preview": {
Type: schema.TypeBool,
Description: "Indicates whether local preview runs can be triggered on this Stack. Defaults to `false`.",
Optional: true,
Default: false,
},
"github_action_deploy": {
Type: schema.TypeBool,
Description: "Indicates whether GitHub users can deploy from the Checks API. Defaults to `true`. This is called allow run promotion in the UI.",
Optional: true,
Default: true,
},
"github_enterprise": {
Type: schema.TypeList,
Description: "VCS settings for [GitHub custom application](https://docs.spacelift.io/integrations/source-control/github#setting-up-the-custom-application)",
Optional: true,
ConflictsWith: conflictingVCSProviders("github_enterprise"),
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"namespace": {
Type: schema.TypeString,
Required: true,
Description: "The GitHub organization / user the repository belongs to",
ValidateDiagFunc: validations.DisallowEmptyString,
},
"id": {
Type: schema.TypeString,
Optional: true,
Description: "The ID of the GitHub Enterprise integration. If not specified, the default integration will be used.",
DiffSuppressFunc: func(_, _, new string, res *schema.ResourceData) bool {
isDefault := res.Get("github_enterprise.0.is_default").(bool)
return isDefault && new == ""
},
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default GitHub Enterprise integration",
},
},
},
},
"gitlab": {
Type: schema.TypeList,
Description: "GitLab VCS settings",
Optional: true,
ConflictsWith: conflictingVCSProviders("gitlab"),
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Description: "The ID of the Gitlab integration. If not specified, the default integration will be used.",
DiffSuppressFunc: func(_, _, new string, res *schema.ResourceData) bool {
isDefault := res.Get("gitlab.0.is_default").(bool)
return isDefault && new == ""
},
},
"namespace": {
Type: schema.TypeString,
Required: true,
Description: "The GitLab namespace containing the repository",
ValidateDiagFunc: validations.DisallowEmptyString,
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default GitLab integration",
},
},
},
},
"import_state": {
Type: schema.TypeString,
Description: "State file to upload when creating a new stack",
ConflictsWith: []string{"import_state_file"},
Optional: true,
DiffSuppressFunc: ignoreOnceCreated,
Sensitive: true,
},
"import_state_file": {
Type: schema.TypeString,
Description: "Path to the state file to upload when creating a new stack",
ConflictsWith: []string{"import_state"},
Optional: true,
DiffSuppressFunc: ignoreOnceCreated,
},
"kubernetes": {
Type: schema.TypeList,
ConflictsWith: []string{"ansible", "cloudformation", "pulumi", "terraform_version", "terraform_workflow_tool", "terraform_workspace", "terragrunt"},
Description: "Kubernetes-specific configuration. Presence means this Stack is a Kubernetes Stack.",
Optional: true,
MaxItems: 1,
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.",
Optional: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
"kubectl_version": {
Type: schema.TypeString,
Description: "Kubectl version.",
Optional: true,
Computed: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
},
},
},
"labels": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
"manage_state": {
Type: schema.TypeBool,
Description: "Determines if Spacelift should manage state for this stack. Defaults to `true`.",
Optional: true,
Default: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Description: "Name of the stack - should be unique in one account",
Required: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
"project_root": {
Type: schema.TypeString,
Description: "Project root is the optional directory relative to the workspace root containing the entrypoint to the Stack.",
Optional: 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. Defaults to `false`.",
Optional: true,
Default: false,
},
"pulumi": {
Type: schema.TypeList,
ConflictsWith: []string{"ansible", "cloudformation", "kubernetes", "terraform_version", "terraform_workflow_tool", "terraform_workspace", "terragrunt"},
Description: "Pulumi-specific configuration. Presence means this Stack is a Pulumi Stack.",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"login_url": {
Type: schema.TypeString,
Description: "State backend to log into on Run initialize.",
Required: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
"stack_name": {
Type: schema.TypeString,
Description: "Pulumi stack name to use with the state backend.",
Required: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
},
},
},
"slug": {
Type: schema.TypeString,
Description: "Allows setting the custom ID (slug) for the stack",
Optional: true,
ForceNew: true,
Computed: true,
},
"raw_git": {
Type: schema.TypeList,
Description: "One-way VCS integration using a raw Git repository link",
Optional: true,
ConflictsWith: conflictingVCSProviders("raw_git"),
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"namespace": {
Type: schema.TypeString,
Required: true,
Description: "User-friendly namespace for the repository, this is for cosmetic purposes only",
ValidateDiagFunc: validations.DisallowEmptyString,
},
"url": {
Type: schema.TypeString,
Required: true,
Description: "HTTPS URL of the Git repository",
ValidateDiagFunc: validations.DisallowEmptyString,
},
},
},
},
"repository": {
Type: schema.TypeString,
Description: "Name of the repository, without the owner part",
Required: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
"runner_image": {
Type: schema.TypeString,
Description: "Name of the Docker image used to process Runs",
Optional: true,
},
"showcase": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"namespace": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
},
},
},
"space_id": {
Type: schema.TypeString,
Description: "ID (slug) of the space the stack is in. Defaults to `legacy`.",
Optional: true,
Computed: true,
},
"terraform_external_state_access": {
Type: schema.TypeBool,
Description: "Indicates whether you can access the Stack state file from other stacks or outside of Spacelift. Defaults to `false`.",
Optional: true,
Default: false,
},
"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. Note: Requires the terraform version to be v1.0.1 or above. Defaults to `false`.",
Optional: true,
Default: false,
},
"terraform_version": {
Type: schema.TypeString,
Description: "Terraform version to use",
Optional: true,
DiffSuppressFunc: onceTheVersionIsSetDoNotUnset,
},
"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`. Defaults to `TERRAFORM_FOSS`.",
Optional: true,
Computed: true,
},
"terraform_workspace": {
Type: schema.TypeString,
Description: "Terraform workspace to select",
Optional: true,
},
"terragrunt": {
Type: schema.TypeList,
ConflictsWith: []string{"ansible", "cloudformation", "kubernetes", "pulumi", "terraform_version", "terraform_workflow_tool", "terraform_workspace"},
Description: "Terragrunt-specific configuration. Presence means this Stack is an Terragrunt Stack.",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"terraform_version": {
Type: schema.TypeString,
Description: "The Terraform version. Must not be provided when tool is set to MANUALLY_PROVISIONED. Defaults to the latest available OpenTofu/Terraform version.",
Optional: true,
Computed: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
"terragrunt_version": {
Type: schema.TypeString,
Description: "The Terragrunt version. Defaults to the latest Terragrunt version.",
Optional: true,
Computed: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
"use_run_all": {
Type: schema.TypeBool,
Description: "Whether to use `terragrunt run-all` instead of `terragrunt`.",
Optional: true,
Default: false,
},
"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.",
Optional: true,
Default: false,
},
"tool": {
Type: schema.TypeString,
Description: "The IaC tool used by Terragrunt. Valid values are OPEN_TOFU, TERRAFORM_FOSS or MANUALLY_PROVISIONED. Defaults to TERRAFORM_FOSS if not specified.",
Optional: true,
Computed: true,
},
},
},
},
"worker_pool_id": {
Type: schema.TypeString,
Description: "ID of the worker pool to use. NOTE: worker_pool_id is required when using a self-hosted instance of Spacelift.",
Optional: true,
},
},
}
}
func resourceStackCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var mutation struct {
CreateStack structs.Stack `graphql:"stackCreate(input: $input, manageState: $manageState, stackObjectID: $stackObjectID, slug: $slug)"`
}
manageState := d.Get("manage_state").(bool)
variables := map[string]interface{}{
"input": stackInput(d),
"manageState": graphql.Boolean(manageState),
"stackObjectID": (*graphql.String)(nil),
"slug": (*graphql.String)(nil),
}
if slug, ok := d.GetOk("slug"); ok {
variables["slug"] = toOptionalString(slug)
}
var stateContent string
content, ok := d.GetOk("import_state")
if ok && !manageState {
return diag.Errorf(`"import_state" requires "manage_state" to be true`)
} else if ok {
stateContent = content.(string)
}
path, ok := d.GetOk("import_state_file")
if ok && !manageState {
return diag.Errorf(`"import_state_file" requires "manage_state" to be true`)
} else if ok {
data, err := os.ReadFile(path.(string))
if err != nil {
return diag.Errorf("failed to read imported state file: %s", err)
}
stateContent = string(data)
}
if stateContent != "" {
objectID, err := uploadStateFile(ctx, stateContent, meta)
if err != nil {
return diag.FromErr(err)
}
variables["stackObjectID"] = toOptionalString(objectID)
}
if v, ok := d.GetOk("terraform_external_state_access"); ok {
if v.(bool) && !manageState {
return diag.Errorf(`"terraform_external_state_access" requires "manage_state" to be true`)
}
}
if err := meta.(*internal.Client).Mutate(ctx, "StackCreate", &mutation, variables); err != nil {
return diag.Errorf("could not create stack: %v", internal.FromSpaceliftError(err))
}
d.SetId(mutation.CreateStack.ID)
return resourceStackRead(ctx, d, meta)
}
func getStackByID(ctx context.Context, client *internal.Client, stackID string) (*structs.Stack, error) {
var query struct {
Stack *structs.Stack `graphql:"stack(id: $id)"`
}
variables := map[string]interface{}{"id": graphql.ID(stackID)}
if err := client.Query(ctx, "StackRead", &query, variables); err != nil {
return nil, errors.Wrap(err, "could not query for stack")
}
return query.Stack, nil
}
func resourceStackRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
stack, err := getStackByID(ctx, meta.(*internal.Client), d.Id())
if err != nil {
return diag.FromErr(err)
}
if stack == nil {
d.SetId("")
return nil
}
if err := structs.PopulateStack(d, stack); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceStackUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var mutation struct {
UpdateStack structs.Stack `graphql:"stackUpdate(id: $id, input: $input)"`
}
variables := map[string]interface{}{
"id": toID(d.Id()),
"input": stackInput(d),
}
var ret diag.Diagnostics
if err := meta.(*internal.Client).Mutate(ctx, "StackUpdate", &mutation, variables); err != nil {
ret = diag.Errorf("could not update stack: %v", internal.FromSpaceliftError(err))
}
return append(ret, resourceStackRead(ctx, d, meta)...)
}
func resourceStackDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var mutation struct {
DeleteStack *structs.Stack `graphql:"stackDelete(id: $id)"`
}
variables := map[string]interface{}{"id": toID(d.Id())}
if err := meta.(*internal.Client).Mutate(ctx, "StackDelete", &mutation, variables); err != nil {
return diag.Errorf("could not delete stack: %v", internal.FromSpaceliftError(err))
}
d.SetId("")
return nil
}
func stackInput(d *schema.ResourceData) structs.StackInput {
ret := structs.StackInput{
Administrative: graphql.Boolean(d.Get("administrative").(bool)),
Autodeploy: graphql.Boolean(d.Get("autodeploy").(bool)),
Autoretry: graphql.Boolean(d.Get("autoretry").(bool)),
Branch: toString(d.Get("branch")),
GitHubActionDeploy: graphql.Boolean(d.Get("github_action_deploy").(bool)),
LocalPreviewEnabled: graphql.Boolean(d.Get("enable_local_preview").(bool)),
Name: toString(d.Get("name")),
ProtectFromDeletion: graphql.Boolean(d.Get("protect_from_deletion").(bool)),
Repository: toString(d.Get("repository")),
}
afterApplies := getStrings(d, "after_apply")
ret.AfterApply = &afterApplies
afterDestroys := getStrings(d, "after_destroy")
ret.AfterDestroy = &afterDestroys
afterInits := getStrings(d, "after_init")
ret.AfterInit = &afterInits
afterPerforms := getStrings(d, "after_perform")
ret.AfterPerform = &afterPerforms
afterPlans := getStrings(d, "after_plan")
ret.AfterPlan = &afterPlans
afterRuns := getStrings(d, "after_run")
ret.AfterRun = &afterRuns
beforeApplies := getStrings(d, "before_apply")
ret.BeforeApply = &beforeApplies
beforeDestroys := getStrings(d, "before_destroy")
ret.BeforeDestroy = &beforeDestroys
beforeInits := getStrings(d, "before_init")
ret.BeforeInit = &beforeInits
beforePerforms := getStrings(d, "before_perform")
ret.BeforePerform = &beforePerforms
beforePlans := getStrings(d, "before_plan")
ret.BeforePlan = &beforePlans
description, ok := d.GetOk("description")
if ok {
ret.Description = toOptionalString(description)
}
ret.Provider = graphql.NewString("GITHUB")
if azureDevOps, ok := d.Get("azure_devops").([]interface{}); ok && len(azureDevOps) > 0 {
azureSettings := azureDevOps[0].(map[string]interface{})
if id, ok := azureSettings["id"]; ok && id != nil && id.(string) != "" {
ret.VCSIntegrationID = graphql.NewID(id.(string))
}
ret.Namespace = toOptionalString(azureDevOps[0].(map[string]interface{})["project"])
ret.Provider = graphql.NewString(graphql.String(structs.VCSProviderAzureDevOps))
}
if bitbucketCloud, ok := d.Get("bitbucket_cloud").([]interface{}); ok && len(bitbucketCloud) > 0 {
bitbucketCloudSettings := bitbucketCloud[0].(map[string]interface{})
if id, ok := bitbucketCloudSettings["id"]; ok && id != nil && id.(string) != "" {
ret.VCSIntegrationID = graphql.NewID(id.(string))
}
ret.Namespace = toOptionalString(bitbucketCloud[0].(map[string]interface{})["namespace"])
ret.Provider = graphql.NewString(graphql.String(structs.VCSProviderBitbucketCloud))
}
if bitbucketDatacenter, ok := d.Get("bitbucket_datacenter").([]interface{}); ok && len(bitbucketDatacenter) > 0 {
bitbucketDatacenterSettings := bitbucketDatacenter[0].(map[string]interface{})
if id, ok := bitbucketDatacenterSettings["id"]; ok && id != nil && id.(string) != "" {
ret.VCSIntegrationID = graphql.NewID(id.(string))
}
ret.Namespace = toOptionalString(bitbucketDatacenter[0].(map[string]interface{})["namespace"])
ret.Provider = graphql.NewString(graphql.String(structs.VCSProviderBitbucketDatacenter))
}
if githubEnterprise, ok := d.Get("github_enterprise").([]interface{}); ok && len(githubEnterprise) > 0 {
ghEnterpriseSettings := githubEnterprise[0].(map[string]interface{})
if id, ok := ghEnterpriseSettings["id"]; ok && id != nil && id.(string) != "" {
ret.VCSIntegrationID = graphql.NewID(id)
}
ret.Namespace = toOptionalString(ghEnterpriseSettings["namespace"])
ret.Provider = graphql.NewString(graphql.String(structs.VCSProviderGitHubEnterprise))
}
if gitlab, ok := d.Get("gitlab").([]interface{}); ok && len(gitlab) > 0 {
gitlabSettings := gitlab[0].(map[string]interface{})
if id, ok := gitlabSettings["id"]; ok && id != nil && id.(string) != "" {
ret.VCSIntegrationID = graphql.NewID(id.(string))
}
ret.Namespace = toOptionalString(gitlabSettings["namespace"])
ret.Provider = graphql.NewString(graphql.String(structs.VCSProviderGitlab))
}
if rawGit, ok := d.Get("raw_git").([]interface{}); ok && len(rawGit) > 0 {
ret.Provider = graphql.NewString(graphql.String(structs.VCSProviderRawGit))
ret.Namespace = toOptionalString(rawGit[0].(map[string]interface{})["namespace"])
ret.RepositoryURL = toOptionalString(rawGit[0].(map[string]interface{})["url"])
}
if showcase, ok := d.Get("showcase").([]interface{}); ok && len(showcase) > 0 {
ret.Namespace = toOptionalString(showcase[0].(map[string]interface{})["namespace"])
ret.Provider = graphql.NewString(graphql.String(structs.VCSProviderShowcases))
}
if labelSet, ok := d.Get("labels").(*schema.Set); ok {
var labels []graphql.String
for _, label := range labelSet.List() {
labels = append(labels, graphql.String(label.(string)))
}
ret.Labels = &labels
}
if space, ok := d.GetOk("space_id"); ok {
ret.Space = toOptionalString(space)
}
if projectRoot, ok := d.GetOk("project_root"); ok {
ret.ProjectRoot = toOptionalString(projectRoot)
}
if globsSet, ok := d.Get("additional_project_globs").(*schema.Set); ok {
var gbs []graphql.String
for _, gb := range globsSet.List() {
gbs = append(gbs, graphql.String(gb.(string)))
}
ret.AddditionalProjectGlobs = &gbs
}
if runnerImage, ok := d.GetOk("runner_image"); ok {
ret.RunnerImage = toOptionalString(runnerImage)
}
ret.VendorConfig = getVendorConfig(d)
if workerPoolID, ok := d.GetOk("worker_pool_id"); ok {
ret.WorkerPool = graphql.NewID(workerPoolID)
}
return ret
}
func getVendorConfig(d *schema.ResourceData) *structs.VendorConfigInput {
if cloudFormation, ok := d.Get("cloudformation").([]interface{}); ok && len(cloudFormation) > 0 {
return &structs.VendorConfigInput{
CloudFormationInput: &structs.CloudFormationInput{
EntryTemplateFile: toString(cloudFormation[0].(map[string]interface{})["entry_template_file"]),
Region: toString(cloudFormation[0].(map[string]interface{})["region"]),
StackName: toString(cloudFormation[0].(map[string]interface{})["stack_name"]),
TemplateBucket: toString(cloudFormation[0].(map[string]interface{})["template_bucket"]),
},
}
}
if kubernetes, ok := d.Get("kubernetes").([]interface{}); ok && len(kubernetes) > 0 {
vendorConfig := &structs.VendorConfigInput{
Kubernetes: &structs.KubernetesInput{},
}
if kubernetesSettings, ok := kubernetes[0].(map[string]interface{}); ok {
vendorConfig.Kubernetes.Namespace = toString(kubernetesSettings["namespace"])
if s := toOptionalString(kubernetesSettings["kubectl_version"]); *s != "" {
vendorConfig.Kubernetes.KubectlVersion = s
}
}
return vendorConfig
}
if pulumi, ok := d.Get("pulumi").([]interface{}); ok && len(pulumi) > 0 {
return &structs.VendorConfigInput{
Pulumi: &structs.PulumiInput{
LoginURL: toString(pulumi[0].(map[string]interface{})["login_url"]),
StackName: toString(pulumi[0].(map[string]interface{})["stack_name"]),
},
}
}
if ansible, ok := d.Get("ansible").([]interface{}); ok && len(ansible) > 0 {
return &structs.VendorConfigInput{
AnsibleInput: &structs.AnsibleInput{
Playbook: toString(ansible[0].(map[string]interface{})["playbook"]),
},
}
}
if terragrunt, ok := d.Get("terragrunt").([]interface{}); ok && len(terragrunt) > 0 {
terragruntConfig := structs.TerragruntInput{
UseRunAll: toBool(terragrunt[0].(map[string]interface{})["use_run_all"]),
UseSmartSanitization: toBool(terragrunt[0].(map[string]interface{})["use_smart_sanitization"]),
}
if version, ok := terragrunt[0].(map[string]interface{})["terraform_version"]; ok && version.(string) != "" {
terragruntConfig.TerraformVersion = toOptionalString(version)
}
if version, ok := terragrunt[0].(map[string]interface{})["terragrunt_version"]; ok && version.(string) != "" {
terragruntConfig.TerragruntVersion = toOptionalString(version)
}
if tool, ok := terragrunt[0].(map[string]interface{})["tool"]; ok && tool.(string) != "" {
terragruntConfig.Tool = toOptionalString(tool)
}
return &structs.VendorConfigInput{
TerragruntInput: &terragruntConfig,
}
}
terraformConfig := &structs.TerraformInput{}
if terraformVersion, ok := d.GetOk("terraform_version"); ok {
terraformConfig.Version = toOptionalString(terraformVersion)
}
if terraformWorkflowTool, ok := d.GetOk("terraform_workflow_tool"); ok {
terraformConfig.WorkflowTool = toOptionalString(terraformWorkflowTool)
}
if terraformWorkspace, ok := d.GetOk("terraform_workspace"); ok {
terraformConfig.Workspace = toOptionalString(terraformWorkspace)
}
if terraformSmartSanitization, ok := d.GetOk("terraform_smart_sanitization"); ok {
terraformConfig.UseSmartSanitization = toOptionalBool(terraformSmartSanitization)
} else {
terraformConfig.UseSmartSanitization = toOptionalBool(false)
}
if v, ok := d.GetOk("terraform_external_state_access"); ok {
terraformConfig.ExternalStateAccessEnabled = toOptionalBool(v)
} else {
terraformConfig.ExternalStateAccessEnabled = toOptionalBool(false)
}
return &structs.VendorConfigInput{Terraform: terraformConfig}