Skip to content

Commit eb023b9

Browse files
authored
Merge pull request #512 from spacelift-io/add-vcs-integration-is-default-into-data-source
MultiVCS: Added is_default into stack/module data source and prevent panic on detached VCS integration
2 parents 6002e5b + 9db2ce0 commit eb023b9

File tree

10 files changed

+136
-18
lines changed

10 files changed

+136
-18
lines changed

docs/data-sources/module.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ data "spacelift_module" "k8s-module" {
5555
Read-Only:
5656

5757
- `id` (String)
58+
- `is_default` (Boolean)
5859
- `project` (String)
5960

6061

@@ -64,6 +65,7 @@ Read-Only:
6465
Read-Only:
6566

6667
- `id` (String)
68+
- `is_default` (Boolean)
6769
- `namespace` (String)
6870

6971

@@ -73,6 +75,7 @@ Read-Only:
7375
Read-Only:
7476

7577
- `id` (String)
78+
- `is_default` (Boolean)
7679
- `namespace` (String)
7780

7881

@@ -82,6 +85,7 @@ Read-Only:
8285
Read-Only:
8386

8487
- `id` (String)
88+
- `is_default` (Boolean)
8589
- `namespace` (String)
8690

8791

@@ -91,4 +95,5 @@ Read-Only:
9195
Read-Only:
9296

9397
- `id` (String)
98+
- `is_default` (Boolean)
9499
- `namespace` (String)

docs/data-sources/stack.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Read-Only:
9090
Read-Only:
9191

9292
- `id` (String)
93+
- `is_default` (Boolean)
9394
- `project` (String)
9495

9596

@@ -99,6 +100,7 @@ Read-Only:
99100
Read-Only:
100101

101102
- `id` (String)
103+
- `is_default` (Boolean)
102104
- `namespace` (String)
103105

104106

@@ -108,6 +110,7 @@ Read-Only:
108110
Read-Only:
109111

110112
- `id` (String)
113+
- `is_default` (Boolean)
111114
- `namespace` (String)
112115

113116

@@ -128,6 +131,7 @@ Read-Only:
128131
Read-Only:
129132

130133
- `id` (String)
134+
- `is_default` (Boolean)
131135
- `namespace` (String)
132136

133137

@@ -137,6 +141,7 @@ Read-Only:
137141
Read-Only:
138142

139143
- `id` (String)
144+
- `is_default` (Boolean)
140145
- `namespace` (String)
141146

142147

docs/data-sources/stacks.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Read-Only:
187187
Read-Only:
188188

189189
- `id` (String)
190+
- `is_default` (Boolean)
190191
- `project` (String)
191192

192193

@@ -196,6 +197,7 @@ Read-Only:
196197
Read-Only:
197198

198199
- `id` (String)
200+
- `is_default` (Boolean)
199201
- `namespace` (String)
200202

201203

@@ -205,6 +207,7 @@ Read-Only:
205207
Read-Only:
206208

207209
- `id` (String)
210+
- `is_default` (Boolean)
208211
- `namespace` (String)
209212

210213

@@ -225,6 +228,7 @@ Read-Only:
225228
Read-Only:
226229

227230
- `id` (String)
231+
- `is_default` (Boolean)
228232
- `namespace` (String)
229233

230234

@@ -234,6 +238,7 @@ Read-Only:
234238
Read-Only:
235239

236240
- `id` (String)
241+
- `is_default` (Boolean)
237242
- `namespace` (String)
238243

239244

spacelift/data_module.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ func dataModule() *schema.Resource {
4646
Description: "ID of the Azure Devops integration",
4747
Computed: true,
4848
},
49+
"is_default": {
50+
Type: schema.TypeBool,
51+
Computed: true,
52+
Description: "Indicates whether this is the default Azure Devops integration",
53+
},
4954
"project": {
5055
Type: schema.TypeString,
5156
Computed: true,
@@ -65,6 +70,11 @@ func dataModule() *schema.Resource {
6570
Description: "ID of the Bitbucket Cloud integration",
6671
Computed: true,
6772
},
73+
"is_default": {
74+
Type: schema.TypeBool,
75+
Computed: true,
76+
Description: "Indicates whether this is the default Bitbucket Cloud integration",
77+
},
6878
"namespace": {
6979
Type: schema.TypeString,
7080
Description: "Bitbucket Cloud namespace of the stack's repository",
@@ -84,6 +94,11 @@ func dataModule() *schema.Resource {
8494
Description: "ID of the Bitbucket Datacenter integration",
8595
Computed: true,
8696
},
97+
"is_default": {
98+
Type: schema.TypeBool,
99+
Computed: true,
100+
Description: "Indicates whether this is the default Bitbucket Datacenter integration",
101+
},
87102
"namespace": {
88103
Type: schema.TypeString,
89104
Description: "Bitbucket Datacenter namespace of the stack's repository",
@@ -108,6 +123,11 @@ func dataModule() *schema.Resource {
108123
Description: "GitHub Enterprise namespace of the stack's repository",
109124
Computed: true,
110125
},
126+
"is_default": {
127+
Type: schema.TypeBool,
128+
Computed: true,
129+
Description: "Indicates whether this is the default GitHub Enterprise integration",
130+
},
111131
"id": {
112132
Type: schema.TypeString,
113133
Description: "ID of the GitHub Enterprise integration",
@@ -126,6 +146,11 @@ func dataModule() *schema.Resource {
126146
Description: "ID of the Gitlab integration",
127147
Computed: true,
128148
},
149+
"is_default": {
150+
Type: schema.TypeBool,
151+
Computed: true,
152+
Description: "Indicates whether this is the default Gitlab integration",
153+
},
129154
"namespace": {
130155
Type: schema.TypeString,
131156
Description: "GitLab namespace of the repository",

spacelift/data_stack.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ func dataStack() *schema.Resource {
108108
Description: "ID of the Azure Devops VCS integration",
109109
Computed: true,
110110
},
111+
"is_default": {
112+
Type: schema.TypeBool,
113+
Computed: true,
114+
Description: "Indicates whether this is the default Azure Devops VCS integration",
115+
},
111116
"project": {
112117
Type: schema.TypeString,
113118
Computed: true,
@@ -162,6 +167,11 @@ func dataStack() *schema.Resource {
162167
Description: "ID of the Bitbucket Cloud integration",
163168
Computed: true,
164169
},
170+
"is_default": {
171+
Type: schema.TypeBool,
172+
Computed: true,
173+
Description: "Indicates whether this is the default Bitbucket Cloud integration",
174+
},
165175
"namespace": {
166176
Type: schema.TypeString,
167177
Description: "Bitbucket Cloud namespace of the stack's repository",
@@ -181,6 +191,11 @@ func dataStack() *schema.Resource {
181191
Description: "ID of the Bitbucket Datacenter integration",
182192
Computed: true,
183193
},
194+
"is_default": {
195+
Type: schema.TypeBool,
196+
Computed: true,
197+
Description: "Indicates whether this is the default Bitbucket Datacenter integration",
198+
},
184199
"namespace": {
185200
Type: schema.TypeString,
186201
Description: "Bitbucket Datacenter namespace of the stack's repository",
@@ -239,6 +254,11 @@ func dataStack() *schema.Resource {
239254
Description: "ID of the GitHub Enterprise integration",
240255
Computed: true,
241256
},
257+
"is_default": {
258+
Type: schema.TypeBool,
259+
Computed: true,
260+
Description: "Indicates whether this is the default GitHub Enterprise integration",
261+
},
242262
"namespace": {
243263
Type: schema.TypeString,
244264
Description: "GitHub Enterprise namespace of the stack's repository",
@@ -258,6 +278,11 @@ func dataStack() *schema.Resource {
258278
Description: "ID of the Gitlab integration",
259279
Computed: true,
260280
},
281+
"is_default": {
282+
Type: schema.TypeBool,
283+
Computed: true,
284+
Description: "Indicates whether this is the default Gitlab integration",
285+
},
261286
"namespace": {
262287
Type: schema.TypeString,
263288
Description: "GitLab namespace of the stack's repository",

spacelift/data_stacks.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ func dataStacksRead(ctx context.Context, d *schema.ResourceData, meta interface{
135135
}
136136

137137
if vcsKey, vcsSettings := node.VCSSettings(); vcsKey != "" {
138-
stack[vcsKey] = []interface{}{vcsSettings}
138+
vcsValue := []interface{}{vcsSettings}
139+
if vcsSettings == nil {
140+
vcsValue = nil
141+
}
142+
stack[vcsKey] = vcsValue
139143
}
140144

141145
if iacKey, iacSettings := node.IaCSettings(); iacKey != "" {

spacelift/internal/structs/module.go

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,53 @@ type Module struct {
3636
// ExportVCSSettings exports VCS settings into Terraform schema.
3737
func (m *Module) ExportVCSSettings(d *schema.ResourceData) error {
3838
var fieldName string
39-
vcsSettings := make(map[string]interface{})
39+
var vcsSettings map[string]interface{}
4040

4141
switch m.Provider {
4242
case VCSProviderAzureDevOps:
43-
vcsSettings["id"] = m.VCSIntegration.ID
44-
vcsSettings["project"] = m.Namespace
45-
vcsSettings["is_default"] = m.VCSIntegration.IsDefault
43+
if m.VCSIntegration != nil {
44+
vcsSettings = map[string]interface{}{
45+
"id": m.VCSIntegration.ID,
46+
"project": m.Namespace,
47+
"is_default": m.VCSIntegration.IsDefault,
48+
}
49+
}
4650
fieldName = "azure_devops"
4751
case VCSProviderBitbucketCloud:
48-
vcsSettings["id"] = m.VCSIntegration.ID
49-
vcsSettings["namespace"] = m.Namespace
50-
vcsSettings["is_default"] = m.VCSIntegration.IsDefault
52+
if m.VCSIntegration != nil {
53+
vcsSettings = map[string]interface{}{
54+
"id": m.VCSIntegration.ID,
55+
"namespace": m.Namespace,
56+
"is_default": m.VCSIntegration.IsDefault,
57+
}
58+
}
5159
fieldName = "bitbucket_cloud"
5260
case VCSProviderBitbucketDatacenter:
53-
vcsSettings["id"] = m.VCSIntegration.ID
54-
vcsSettings["namespace"] = m.Namespace
55-
vcsSettings["is_default"] = m.VCSIntegration.IsDefault
61+
if m.VCSIntegration != nil {
62+
vcsSettings = map[string]interface{}{
63+
"id": m.VCSIntegration.ID,
64+
"namespace": m.Namespace,
65+
"is_default": m.VCSIntegration.IsDefault,
66+
}
67+
}
5668
fieldName = "bitbucket_datacenter"
5769
case VCSProviderGitHubEnterprise:
58-
vcsSettings["id"] = m.VCSIntegration.ID
59-
vcsSettings["namespace"] = m.Namespace
60-
vcsSettings["is_default"] = m.VCSIntegration.IsDefault
70+
if m.VCSIntegration != nil {
71+
vcsSettings = map[string]interface{}{
72+
"id": m.VCSIntegration.ID,
73+
"namespace": m.Namespace,
74+
"is_default": m.VCSIntegration.IsDefault,
75+
}
76+
}
6177
fieldName = "github_enterprise"
6278
case VCSProviderGitlab:
63-
vcsSettings["id"] = m.VCSIntegration.ID
64-
vcsSettings["namespace"] = m.Namespace
65-
vcsSettings["is_default"] = m.VCSIntegration.IsDefault
79+
if m.VCSIntegration != nil {
80+
vcsSettings = map[string]interface{}{
81+
"id": m.VCSIntegration.ID,
82+
"namespace": m.Namespace,
83+
"is_default": m.VCSIntegration.IsDefault,
84+
}
85+
}
6686
fieldName = "gitlab"
6787
}
6888

spacelift/internal/structs/stack.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ type Stack struct {
9696
// ExportVCSSettings exports VCS settings into Terraform schema.
9797
func (s *Stack) ExportVCSSettings(d *schema.ResourceData) error {
9898
if fieldName, vcsSettings := s.VCSSettings(); fieldName != "" {
99-
if err := d.Set(fieldName, []interface{}{vcsSettings}); err != nil {
99+
fieldValue := []interface{}{vcsSettings}
100+
if vcsSettings == nil {
101+
fieldValue = nil
102+
}
103+
if err := d.Set(fieldName, fieldValue); err != nil {
100104
return errors.Wrapf(err, "error setting %s (resource)", fieldName)
101105
}
102106
}
@@ -135,30 +139,45 @@ func (s *Stack) IaCSettings() (string, map[string]interface{}) {
135139
func (s *Stack) VCSSettings() (string, map[string]interface{}) {
136140
switch s.Provider {
137141
case VCSProviderAzureDevOps:
142+
if s.VCSIntegration == nil {
143+
return "azure_devops", nil
144+
}
138145
return "azure_devops", map[string]interface{}{
139146
"id": s.VCSIntegration.ID,
140147
"project": s.Namespace,
141148
"is_default": s.VCSIntegration.IsDefault,
142149
}
143150
case VCSProviderBitbucketCloud:
151+
if s.VCSIntegration == nil {
152+
return "bitbucket_cloud", nil
153+
}
144154
return "bitbucket_cloud", map[string]interface{}{
145155
"id": s.VCSIntegration.ID,
146156
"namespace": s.Namespace,
147157
"is_default": s.VCSIntegration.IsDefault,
148158
}
149159
case VCSProviderBitbucketDatacenter:
160+
if s.VCSIntegration == nil {
161+
return "bitbucket_datacenter", nil
162+
}
150163
return "bitbucket_datacenter", map[string]interface{}{
151164
"id": s.VCSIntegration.ID,
152165
"namespace": s.Namespace,
153166
"is_default": s.VCSIntegration.IsDefault,
154167
}
155168
case VCSProviderGitHubEnterprise:
169+
if s.VCSIntegration == nil {
170+
return "github_enterprise", nil
171+
}
156172
return "github_enterprise", map[string]interface{}{
157173
"id": s.VCSIntegration.ID,
158174
"namespace": s.Namespace,
159175
"is_default": s.VCSIntegration.IsDefault,
160176
}
161177
case VCSProviderGitlab:
178+
if s.VCSIntegration == nil {
179+
return "gitlab", nil
180+
}
162181
return "gitlab", map[string]interface{}{
163182
"id": s.VCSIntegration.ID,
164183
"namespace": s.Namespace,

0 commit comments

Comments
 (0)