Skip to content

Commit

Permalink
Merge pull request #512 from spacelift-io/add-vcs-integration-is-defa…
Browse files Browse the repository at this point in the history
…ult-into-data-source

MultiVCS: Added is_default into stack/module data source and prevent panic on detached VCS integration
  • Loading branch information
truszkowski authored Feb 7, 2024
2 parents 6002e5b + 9db2ce0 commit eb023b9
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 18 deletions.
5 changes: 5 additions & 0 deletions docs/data-sources/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ data "spacelift_module" "k8s-module" {
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `project` (String)


Expand All @@ -64,6 +65,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `namespace` (String)


Expand All @@ -73,6 +75,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `namespace` (String)


Expand All @@ -82,6 +85,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `namespace` (String)


Expand All @@ -91,4 +95,5 @@ Read-Only:
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `namespace` (String)
5 changes: 5 additions & 0 deletions docs/data-sources/stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `project` (String)


Expand All @@ -99,6 +100,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `namespace` (String)


Expand All @@ -108,6 +110,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `namespace` (String)


Expand All @@ -128,6 +131,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `namespace` (String)


Expand All @@ -137,6 +141,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `namespace` (String)


Expand Down
5 changes: 5 additions & 0 deletions docs/data-sources/stacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `project` (String)


Expand All @@ -196,6 +197,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `namespace` (String)


Expand All @@ -205,6 +207,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `namespace` (String)


Expand All @@ -225,6 +228,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `namespace` (String)


Expand All @@ -234,6 +238,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `is_default` (Boolean)
- `namespace` (String)


Expand Down
25 changes: 25 additions & 0 deletions spacelift/data_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func dataModule() *schema.Resource {
Description: "ID of the Azure Devops integration",
Computed: true,
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default Azure Devops integration",
},
"project": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -65,6 +70,11 @@ func dataModule() *schema.Resource {
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",
Expand All @@ -84,6 +94,11 @@ func dataModule() *schema.Resource {
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",
Expand All @@ -108,6 +123,11 @@ func dataModule() *schema.Resource {
Description: "GitHub Enterprise namespace of the stack's repository",
Computed: true,
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default GitHub Enterprise integration",
},
"id": {
Type: schema.TypeString,
Description: "ID of the GitHub Enterprise integration",
Expand All @@ -126,6 +146,11 @@ func dataModule() *schema.Resource {
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 repository",
Expand Down
25 changes: 25 additions & 0 deletions spacelift/data_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func dataStack() *schema.Resource {
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,
Expand Down Expand Up @@ -162,6 +167,11 @@ func dataStack() *schema.Resource {
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",
Expand All @@ -181,6 +191,11 @@ func dataStack() *schema.Resource {
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",
Expand Down Expand Up @@ -239,6 +254,11 @@ func dataStack() *schema.Resource {
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",
Expand All @@ -258,6 +278,11 @@ func dataStack() *schema.Resource {
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",
Expand Down
6 changes: 5 additions & 1 deletion spacelift/data_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ func dataStacksRead(ctx context.Context, d *schema.ResourceData, meta interface{
}

if vcsKey, vcsSettings := node.VCSSettings(); vcsKey != "" {
stack[vcsKey] = []interface{}{vcsSettings}
vcsValue := []interface{}{vcsSettings}
if vcsSettings == nil {
vcsValue = nil
}
stack[vcsKey] = vcsValue
}

if iacKey, iacSettings := node.IaCSettings(); iacKey != "" {
Expand Down
52 changes: 36 additions & 16 deletions spacelift/internal/structs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,53 @@ type Module struct {
// ExportVCSSettings exports VCS settings into Terraform schema.
func (m *Module) ExportVCSSettings(d *schema.ResourceData) error {
var fieldName string
vcsSettings := make(map[string]interface{})
var vcsSettings map[string]interface{}

switch m.Provider {
case VCSProviderAzureDevOps:
vcsSettings["id"] = m.VCSIntegration.ID
vcsSettings["project"] = m.Namespace
vcsSettings["is_default"] = m.VCSIntegration.IsDefault
if m.VCSIntegration != nil {
vcsSettings = map[string]interface{}{
"id": m.VCSIntegration.ID,
"project": m.Namespace,
"is_default": m.VCSIntegration.IsDefault,
}
}
fieldName = "azure_devops"
case VCSProviderBitbucketCloud:
vcsSettings["id"] = m.VCSIntegration.ID
vcsSettings["namespace"] = m.Namespace
vcsSettings["is_default"] = m.VCSIntegration.IsDefault
if m.VCSIntegration != nil {
vcsSettings = map[string]interface{}{
"id": m.VCSIntegration.ID,
"namespace": m.Namespace,
"is_default": m.VCSIntegration.IsDefault,
}
}
fieldName = "bitbucket_cloud"
case VCSProviderBitbucketDatacenter:
vcsSettings["id"] = m.VCSIntegration.ID
vcsSettings["namespace"] = m.Namespace
vcsSettings["is_default"] = m.VCSIntegration.IsDefault
if m.VCSIntegration != nil {
vcsSettings = map[string]interface{}{
"id": m.VCSIntegration.ID,
"namespace": m.Namespace,
"is_default": m.VCSIntegration.IsDefault,
}
}
fieldName = "bitbucket_datacenter"
case VCSProviderGitHubEnterprise:
vcsSettings["id"] = m.VCSIntegration.ID
vcsSettings["namespace"] = m.Namespace
vcsSettings["is_default"] = m.VCSIntegration.IsDefault
if m.VCSIntegration != nil {
vcsSettings = map[string]interface{}{
"id": m.VCSIntegration.ID,
"namespace": m.Namespace,
"is_default": m.VCSIntegration.IsDefault,
}
}
fieldName = "github_enterprise"
case VCSProviderGitlab:
vcsSettings["id"] = m.VCSIntegration.ID
vcsSettings["namespace"] = m.Namespace
vcsSettings["is_default"] = m.VCSIntegration.IsDefault
if m.VCSIntegration != nil {
vcsSettings = map[string]interface{}{
"id": m.VCSIntegration.ID,
"namespace": m.Namespace,
"is_default": m.VCSIntegration.IsDefault,
}
}
fieldName = "gitlab"
}

Expand Down
21 changes: 20 additions & 1 deletion spacelift/internal/structs/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ type Stack struct {
// ExportVCSSettings exports VCS settings into Terraform schema.
func (s *Stack) ExportVCSSettings(d *schema.ResourceData) error {
if fieldName, vcsSettings := s.VCSSettings(); fieldName != "" {
if err := d.Set(fieldName, []interface{}{vcsSettings}); err != nil {
fieldValue := []interface{}{vcsSettings}
if vcsSettings == nil {
fieldValue = nil
}
if err := d.Set(fieldName, fieldValue); err != nil {
return errors.Wrapf(err, "error setting %s (resource)", fieldName)
}
}
Expand Down Expand Up @@ -135,30 +139,45 @@ func (s *Stack) IaCSettings() (string, map[string]interface{}) {
func (s *Stack) VCSSettings() (string, map[string]interface{}) {
switch s.Provider {
case VCSProviderAzureDevOps:
if s.VCSIntegration == nil {
return "azure_devops", nil
}
return "azure_devops", map[string]interface{}{
"id": s.VCSIntegration.ID,
"project": s.Namespace,
"is_default": s.VCSIntegration.IsDefault,
}
case VCSProviderBitbucketCloud:
if s.VCSIntegration == nil {
return "bitbucket_cloud", nil
}
return "bitbucket_cloud", map[string]interface{}{
"id": s.VCSIntegration.ID,
"namespace": s.Namespace,
"is_default": s.VCSIntegration.IsDefault,
}
case VCSProviderBitbucketDatacenter:
if s.VCSIntegration == nil {
return "bitbucket_datacenter", nil
}
return "bitbucket_datacenter", map[string]interface{}{
"id": s.VCSIntegration.ID,
"namespace": s.Namespace,
"is_default": s.VCSIntegration.IsDefault,
}
case VCSProviderGitHubEnterprise:
if s.VCSIntegration == nil {
return "github_enterprise", nil
}
return "github_enterprise", map[string]interface{}{
"id": s.VCSIntegration.ID,
"namespace": s.Namespace,
"is_default": s.VCSIntegration.IsDefault,
}
case VCSProviderGitlab:
if s.VCSIntegration == nil {
return "gitlab", nil
}
return "gitlab", map[string]interface{}{
"id": s.VCSIntegration.ID,
"namespace": s.Namespace,
Expand Down
Loading

0 comments on commit eb023b9

Please sign in to comment.