From 1d377d683dba125d68a6cfc5a9d34e62f78af51c Mon Sep 17 00:00:00 2001 From: Piotr Truszkowski Date: Wed, 7 Feb 2024 10:11:37 +0100 Subject: [PATCH 1/3] Fix: add stack/module -> vcs integration (github/gitlab/azure/bitbucket) -> is_default (boolean) --- docs/data-sources/module.md | 5 +++++ docs/data-sources/stack.md | 5 +++++ docs/data-sources/stacks.md | 5 +++++ spacelift/data_module.go | 25 +++++++++++++++++++++++++ spacelift/data_stack.go | 25 +++++++++++++++++++++++++ 5 files changed, 65 insertions(+) diff --git a/docs/data-sources/module.md b/docs/data-sources/module.md index ee2c4af8..8c173548 100644 --- a/docs/data-sources/module.md +++ b/docs/data-sources/module.md @@ -55,6 +55,7 @@ data "spacelift_module" "k8s-module" { Read-Only: - `id` (String) +- `is_default` (Boolean) - `project` (String) @@ -64,6 +65,7 @@ Read-Only: Read-Only: - `id` (String) +- `is_default` (Boolean) - `namespace` (String) @@ -73,6 +75,7 @@ Read-Only: Read-Only: - `id` (String) +- `is_default` (Boolean) - `namespace` (String) @@ -82,6 +85,7 @@ Read-Only: Read-Only: - `id` (String) +- `is_default` (Boolean) - `namespace` (String) @@ -91,4 +95,5 @@ Read-Only: Read-Only: - `id` (String) +- `is_default` (Boolean) - `namespace` (String) diff --git a/docs/data-sources/stack.md b/docs/data-sources/stack.md index 870f7a89..5158ef1d 100644 --- a/docs/data-sources/stack.md +++ b/docs/data-sources/stack.md @@ -90,6 +90,7 @@ Read-Only: Read-Only: - `id` (String) +- `is_default` (Boolean) - `project` (String) @@ -99,6 +100,7 @@ Read-Only: Read-Only: - `id` (String) +- `is_default` (Boolean) - `namespace` (String) @@ -108,6 +110,7 @@ Read-Only: Read-Only: - `id` (String) +- `is_default` (Boolean) - `namespace` (String) @@ -128,6 +131,7 @@ Read-Only: Read-Only: - `id` (String) +- `is_default` (Boolean) - `namespace` (String) @@ -137,6 +141,7 @@ Read-Only: Read-Only: - `id` (String) +- `is_default` (Boolean) - `namespace` (String) diff --git a/docs/data-sources/stacks.md b/docs/data-sources/stacks.md index 55e7dfbd..b8df3cc5 100644 --- a/docs/data-sources/stacks.md +++ b/docs/data-sources/stacks.md @@ -187,6 +187,7 @@ Read-Only: Read-Only: - `id` (String) +- `is_default` (Boolean) - `project` (String) @@ -196,6 +197,7 @@ Read-Only: Read-Only: - `id` (String) +- `is_default` (Boolean) - `namespace` (String) @@ -205,6 +207,7 @@ Read-Only: Read-Only: - `id` (String) +- `is_default` (Boolean) - `namespace` (String) @@ -225,6 +228,7 @@ Read-Only: Read-Only: - `id` (String) +- `is_default` (Boolean) - `namespace` (String) @@ -234,6 +238,7 @@ Read-Only: Read-Only: - `id` (String) +- `is_default` (Boolean) - `namespace` (String) diff --git a/spacelift/data_module.go b/spacelift/data_module.go index be7848a3..41b6b112 100644 --- a/spacelift/data_module.go +++ b/spacelift/data_module.go @@ -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, @@ -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", @@ -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", @@ -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", @@ -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", diff --git a/spacelift/data_stack.go b/spacelift/data_stack.go index 60401603..79f77f35 100644 --- a/spacelift/data_stack.go +++ b/spacelift/data_stack.go @@ -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, @@ -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", @@ -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", @@ -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", @@ -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", From b73f5d1573ce6d5e65eb3e82388104956f475c27 Mon Sep 17 00:00:00 2001 From: Piotr Truszkowski Date: Wed, 7 Feb 2024 11:28:36 +0100 Subject: [PATCH 2/3] Fix: edge case when vcs integration is detached --- spacelift/data_stacks.go | 6 +++- spacelift/internal/structs/module.go | 52 +++++++++++++++++++--------- spacelift/internal/structs/stack.go | 21 ++++++++++- 3 files changed, 61 insertions(+), 18 deletions(-) diff --git a/spacelift/data_stacks.go b/spacelift/data_stacks.go index 01d2cbb0..72643626 100644 --- a/spacelift/data_stacks.go +++ b/spacelift/data_stacks.go @@ -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 != "" { diff --git a/spacelift/internal/structs/module.go b/spacelift/internal/structs/module.go index 8a34ee25..67f3b33d 100644 --- a/spacelift/internal/structs/module.go +++ b/spacelift/internal/structs/module.go @@ -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" } diff --git a/spacelift/internal/structs/stack.go b/spacelift/internal/structs/stack.go index 358787a6..9acf96b4 100644 --- a/spacelift/internal/structs/stack.go +++ b/spacelift/internal/structs/stack.go @@ -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) } } @@ -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, From 9db2ce0af60460761d403143cb8ba683edb9f6c0 Mon Sep 17 00:00:00 2001 From: Piotr Truszkowski Date: Wed, 7 Feb 2024 12:39:24 +0100 Subject: [PATCH 3/3] added "Computed: true" for vcs integration id --- spacelift/resource_module.go | 5 +++++ spacelift/resource_stack.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/spacelift/resource_module.go b/spacelift/resource_module.go index 048e089b..80dbd985 100644 --- a/spacelift/resource_module.go +++ b/spacelift/resource_module.go @@ -53,6 +53,7 @@ func resourceModule() *schema.Resource { Type: schema.TypeString, Description: "ID of the Azure Devops integration. If not specified, the default integration will be used.", Optional: true, + Computed: true, DiffSuppressFunc: func(_, _, new string, res *schema.ResourceData) bool { isDefault := res.Get("azure_devops.0.is_default").(bool) @@ -84,6 +85,7 @@ func resourceModule() *schema.Resource { "id": { Type: schema.TypeString, Optional: true, + Computed: 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) @@ -116,6 +118,7 @@ func resourceModule() *schema.Resource { "id": { Type: schema.TypeString, Optional: true, + Computed: 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) @@ -170,6 +173,7 @@ func resourceModule() *schema.Resource { "id": { Type: schema.TypeString, Optional: true, + Computed: 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) @@ -197,6 +201,7 @@ func resourceModule() *schema.Resource { Type: schema.TypeString, Description: "ID of the Gitlab integration. If not specified, the default integration will be used.", Optional: true, + Computed: true, DiffSuppressFunc: func(_, _, new string, res *schema.ResourceData) bool { isDefault := res.Get("gitlab.0.is_default").(bool) diff --git a/spacelift/resource_stack.go b/spacelift/resource_stack.go index 9bbce877..9a97406e 100644 --- a/spacelift/resource_stack.go +++ b/spacelift/resource_stack.go @@ -140,6 +140,7 @@ func resourceStack() *schema.Resource { "id": { Type: schema.TypeString, Optional: true, + Computed: 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) @@ -223,6 +224,7 @@ func resourceStack() *schema.Resource { "id": { Type: schema.TypeString, Optional: true, + Computed: 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) @@ -255,6 +257,7 @@ func resourceStack() *schema.Resource { "id": { Type: schema.TypeString, Optional: true, + Computed: 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) @@ -345,6 +348,7 @@ func resourceStack() *schema.Resource { "id": { Type: schema.TypeString, Optional: true, + Computed: 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) @@ -371,6 +375,7 @@ func resourceStack() *schema.Resource { "id": { Type: schema.TypeString, Optional: true, + Computed: 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)