Skip to content

Commit

Permalink
Merge pull request #500 from spacelift-io/multivcs-bitbucket-datacenter
Browse files Browse the repository at this point in the history
MultiVCS: bitbucket Datacenter
  • Loading branch information
truszkowski authored Jan 17, 2024
2 parents 4be1a4f + cd09b20 commit 4b2f9a8
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 40 deletions.
11 changes: 10 additions & 1 deletion docs/data-sources/bitbucket_datacenter_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,19 @@ data "spacelift_bitbucket_datacenter_integration" "bitbucket_datacenter_integrat
<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `id` (String) Bitbucket Datacenter integration id. If not provided, the default integration will be returned

### Read-Only

- `api_host` (String) Bitbucket Datacenter integration api host
- `id` (String) The ID of this resource.
- `description` (String) Bitbucket Datacenter integration description
- `is_default` (Boolean) Bitbucket Datacenter integration is default
- `labels` (List of String) Bitbucket Datacenter integration labels
- `name` (String) Bitbucket Datacenter integration name
- `space_id` (String) Bitbucket Datacenter integration space id
- `user_facing_host` (String) Bitbucket Datacenter integration user facing host
- `username` (String) Bitbucket Datacenter username
- `webhook_secret` (String) Bitbucket Datacenter integration webhook secret
- `webhook_url` (String) Bitbucket Datacenter integration webhook URL
2 changes: 2 additions & 0 deletions docs/data-sources/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Read-Only:

Read-Only:

- `id` (String)
- `namespace` (String)


Expand All @@ -71,6 +72,7 @@ Read-Only:

Read-Only:

- `id` (String)
- `namespace` (String)


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

Read-Only:

- `id` (String)
- `namespace` (String)


Expand All @@ -106,6 +107,7 @@ Read-Only:

Read-Only:

- `id` (String)
- `namespace` (String)


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

Read-Only:

- `id` (String)
- `namespace` (String)


Expand All @@ -203,6 +204,7 @@ Read-Only:

Read-Only:

- `id` (String)
- `namespace` (String)


Expand Down
4 changes: 4 additions & 0 deletions docs/resources/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ Required:

- `namespace` (String) The Bitbucket project containing the repository

Optional:

- `id` (String) The ID of the Bitbucket Datacenter integration. If not specified, the default integration will be used.


<a id="nestedblock--github_enterprise"></a>
### Nested Schema for `github_enterprise`
Expand Down
4 changes: 4 additions & 0 deletions docs/resources/stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ Required:

- `namespace` (String) The Bitbucket project containing the repository

Optional:

- `id` (String) The ID of the Bitbucket Datacenter integration. If not specified, the default integration will be used.


<a id="nestedblock--cloudformation"></a>
### Nested Schema for `cloudformation`
Expand Down
121 changes: 95 additions & 26 deletions spacelift/data_bitbucket_datacenter_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ import (
"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal"
)

var bitbucketDatacenterFields = struct {
UserFacingHost string
APIHost string
WebhookSecret string
WebhookURL string
}{
UserFacingHost: "user_facing_host",
APIHost: "api_host",
WebhookSecret: "webhook_secret",
WebhookURL: "webhook_url",
}
const (
bitbucketDatacenterID = "id"
bitbucketDatacenterName = "name"
bitbucketDatacenterDescription = "description"
bitbucketDatacenterIsDefault = "is_default"
bitbucketDatacenterLabels = "labels"
bitbucketDatacenterSpaceID = "space_id"
bitbucketDatacenterUserFacingHost = "user_facing_host"
bitbucketDatacenterAPIHost = "api_host"
bitbucketDatacenterUsername = "username"
bitbucketDatacenterWebhookURL = "webhook_url"
bitbucketDatacenterWebhookSecret = "webhook_secret"

defaultBitbucketDatacenterIntegrationID = "bitbucket-datacenter-default-integration"
)

func dataBitbucketDatacenterIntegration() *schema.Resource {
return &schema.Resource{
Expand All @@ -28,22 +32,60 @@ func dataBitbucketDatacenterIntegration() *schema.Resource {
ReadContext: dataBitbucketDatacenterIntegrationRead,

Schema: map[string]*schema.Schema{
bitbucketDatacenterFields.APIHost: {
bitbucketDatacenterID: {
Type: schema.TypeString,
Description: "Bitbucket Datacenter integration id. If not provided, the default integration will be returned",
Optional: true,
},
bitbucketDatacenterName: {
Type: schema.TypeString,
Description: "Bitbucket Datacenter integration name",
Computed: true,
},
bitbucketDatacenterDescription: {
Type: schema.TypeString,
Description: "Bitbucket Datacenter integration description",
Computed: true,
},
bitbucketDatacenterIsDefault: {
Type: schema.TypeBool,
Description: "Bitbucket Datacenter integration is default",
Computed: true,
},
bitbucketDatacenterLabels: {
Type: schema.TypeList,
Description: "Bitbucket Datacenter integration labels",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
bitbucketDatacenterSpaceID: {
Type: schema.TypeString,
Description: "Bitbucket Datacenter integration space id",
Computed: true,
},
bitbucketDatacenterUsername: {
Type: schema.TypeString,
Description: "Bitbucket Datacenter username",
Computed: true,
},
bitbucketDatacenterAPIHost: {
Type: schema.TypeString,
Description: "Bitbucket Datacenter integration api host",
Computed: true,
},
bitbucketDatacenterFields.WebhookSecret: {
bitbucketDatacenterWebhookSecret: {
Type: schema.TypeString,
Description: "Bitbucket Datacenter integration webhook secret",
Computed: true,
},
bitbucketDatacenterFields.WebhookURL: {
bitbucketDatacenterWebhookURL: {
Type: schema.TypeString,
Description: "Bitbucket Datacenter integration webhook URL",
Computed: true,
},
bitbucketDatacenterFields.UserFacingHost: {
bitbucketDatacenterUserFacingHost: {
Type: schema.TypeString,
Description: "Bitbucket Datacenter integration user facing host",
Computed: true,
Expand All @@ -55,14 +97,28 @@ func dataBitbucketDatacenterIntegration() *schema.Resource {
func dataBitbucketDatacenterIntegrationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var query struct {
BitbucketDataCenterIntegration *struct {
APIHost string `graphql:"apiHost"`
WebhookSecret string `graphql:"webhookSecret"`
UserFacingHost string `graphql:"userFacingHost"`
WebhookURL string `graphql:"webhookURL"`
} `graphql:"bitbucketDatacenterIntegration"`
ID string `graphql:"id"`
Name string `graphql:"name"`
Description string `graphql:"description"`
IsDefault bool `graphql:"isDefault"`
Space struct {
ID string `graphql:"id"`
} `graphql:"space"`
Labels []string `graphql:"labels"`
APIHost string `graphql:"apiHost"`
WebhookSecret string `graphql:"webhookSecret"`
UserFacingHost string `graphql:"userFacingHost"`
WebhookURL string `graphql:"webhookURL"`
Username string `graphql:"username"`
} `graphql:"bitbucketDatacenterIntegration(id: $id)"`
}

variables := map[string]interface{}{"id": defaultBitbucketDatacenterIntegrationID}
if id, ok := d.GetOk(bitbucketDatacenterID); ok && id != "" {
variables["id"] = toID(id)
}

if err := meta.(*internal.Client).Query(ctx, "BitbucketDatacenterIntegrationRead", &query, map[string]interface{}{}); err != nil {
if err := meta.(*internal.Client).Query(ctx, "BitbucketDatacenterIntegrationRead", &query, variables); err != nil {
return diag.Errorf("could not query for bitbucket datacenter integration: %v", err)
}

Expand All @@ -71,11 +127,24 @@ func dataBitbucketDatacenterIntegrationRead(ctx context.Context, d *schema.Resou
return diag.Errorf("bitbucket datacenter integration not found")
}

d.SetId("spacelift_bitbucket_datacenter_integration_id") // TF expects id to be set otherwise it will fail
d.Set(bitbucketDatacenterFields.APIHost, bitbucketDatacenterIntegration.APIHost)
d.Set(bitbucketDatacenterFields.WebhookSecret, bitbucketDatacenterIntegration.WebhookSecret)
d.Set(bitbucketDatacenterFields.WebhookURL, bitbucketDatacenterIntegration.WebhookURL)
d.Set(bitbucketDatacenterFields.UserFacingHost, bitbucketDatacenterIntegration.UserFacingHost)
d.SetId(bitbucketDatacenterIntegration.ID)
d.Set(bitbucketDatacenterID, bitbucketDatacenterIntegration.ID)
d.Set(bitbucketDatacenterName, bitbucketDatacenterIntegration.Name)
d.Set(bitbucketDatacenterDescription, bitbucketDatacenterIntegration.Description)
d.Set(bitbucketDatacenterIsDefault, bitbucketDatacenterIntegration.IsDefault)
d.Set(bitbucketDatacenterSpaceID, bitbucketDatacenterIntegration.Space.ID)
d.Set(bitbucketDatacenterAPIHost, bitbucketDatacenterIntegration.APIHost)
d.Set(bitbucketDatacenterWebhookSecret, bitbucketDatacenterIntegration.WebhookSecret)
d.Set(bitbucketDatacenterWebhookURL, bitbucketDatacenterIntegration.WebhookURL)
d.Set(bitbucketDatacenterUserFacingHost, bitbucketDatacenterIntegration.UserFacingHost)
d.Set(bitbucketDatacenterUsername, bitbucketDatacenterIntegration.Username)

labels := schema.NewSet(schema.HashString, []interface{}{})
for _, label := range bitbucketDatacenterIntegration.Labels {
labels.Add(label)
}

d.Set(bitbucketDatacenterLabels, labels)

return nil
}
52 changes: 40 additions & 12 deletions spacelift/data_bitbucket_datacenter_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,44 @@ import (
)

func TestBitbucketDataCenterIntegrationData(t *testing.T) {
testSteps(t, []resource.TestStep{{
Config: `
data "spacelift_bitbucket_datacenter_integration" "test" {}
`,
Check: Resource(
"data.spacelift_bitbucket_datacenter_integration.test",
Attribute("api_host", Equals(os.Getenv("SPACELIFT_PROVIDER_TEST_BITBUCKET_DATACENTER_API_HOST"))),
Attribute("webhook_secret", Equals(os.Getenv("SPACELIFT_PROVIDER_TEST_BITBUCKET_DATACENTER_WEBHOOK_SECRET"))),
Attribute("webhook_url", Equals(os.Getenv("SPACELIFT_PROVIDER_TEST_BITBUCKET_DATACENTER_WEBHOOK_URL"))),
Attribute("user_facing_host", Equals(os.Getenv("SPACELIFT_PROVIDER_TEST_BITBUCKET_DATACENTER_USER_FACING_HOST"))),
),
}})
t.Run("without the id specified", func(t *testing.T) {
testSteps(t, []resource.TestStep{{
Config: `
data "spacelift_bitbucket_datacenter_integration" "test" {}
`,
Check: Resource(
"data.spacelift_bitbucket_datacenter_integration.test",
Attribute("id", IsNotEmpty()),
Attribute("name", IsNotEmpty()),
Attribute("is_default", Equals("true")),
Attribute("space_id", IsNotEmpty()),
Attribute("api_host", Equals(os.Getenv("SPACELIFT_PROVIDER_TEST_BITBUCKET_DATACENTER_API_HOST"))),
Attribute("webhook_secret", Equals(os.Getenv("SPACELIFT_PROVIDER_TEST_BITBUCKET_DATACENTER_WEBHOOK_SECRET"))),
Attribute("webhook_url", Equals(os.Getenv("SPACELIFT_PROVIDER_TEST_BITBUCKET_DATACENTER_WEBHOOK_URL"))),
Attribute("user_facing_host", Equals(os.Getenv("SPACELIFT_PROVIDER_TEST_BITBUCKET_DATACENTER_USER_FACING_HOST"))),
),
}})
})

t.Run("with the id specified", func(t *testing.T) {
testSteps(t, []resource.TestStep{{
Config: `
data "spacelift_bitbucket_datacenter_integration" "test" {
id = "bitbucket-datacenter-default-integration"
}
`,
Check: Resource(
"data.spacelift_bitbucket_datacenter_integration.test",
Attribute("id", IsNotEmpty()),
Attribute("name", IsNotEmpty()),
Attribute("is_default", Equals("true")),
Attribute("space_id", IsNotEmpty()),
Attribute("api_host", Equals(os.Getenv("SPACELIFT_PROVIDER_TEST_BITBUCKET_DATACENTER_API_HOST"))),
Attribute("webhook_secret", Equals(os.Getenv("SPACELIFT_PROVIDER_TEST_BITBUCKET_DATACENTER_WEBHOOK_SECRET"))),
Attribute("webhook_url", Equals(os.Getenv("SPACELIFT_PROVIDER_TEST_BITBUCKET_DATACENTER_WEBHOOK_URL"))),
Attribute("user_facing_host", Equals(os.Getenv("SPACELIFT_PROVIDER_TEST_BITBUCKET_DATACENTER_USER_FACING_HOST"))),
),
}})
})

}
10 changes: 10 additions & 0 deletions spacelift/data_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func dataModule() *schema.Resource {
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "ID of the Bitbucket Cloud integration",
Computed: true,
},
"namespace": {
Type: schema.TypeString,
Description: "Bitbucket Cloud namespace of the stack's repository",
Expand All @@ -74,6 +79,11 @@ func dataModule() *schema.Resource {
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "ID of the Bitbucket Datacenter integration",
Computed: true,
},
"namespace": {
Type: schema.TypeString,
Description: "Bitbucket Datacenter namespace of the stack's repository",
Expand Down
10 changes: 10 additions & 0 deletions spacelift/data_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ func dataStack() *schema.Resource {
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "ID of the Bitbucket Cloud integration",
Computed: true,
},
"namespace": {
Type: schema.TypeString,
Description: "Bitbucket Cloud namespace of the stack's repository",
Expand All @@ -171,6 +176,11 @@ func dataStack() *schema.Resource {
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "ID of the Bitbucket Datacenter integration",
Computed: true,
},
"namespace": {
Type: schema.TypeString,
Description: "Bitbucket Datacenter namespace of the stack's repository",
Expand Down
2 changes: 2 additions & 0 deletions spacelift/internal/structs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ func (m *Module) ExportVCSSettings(d *schema.ResourceData) error {
vcsSettings["project"] = m.Namespace
fieldName = "azure_devops"
case VCSProviderBitbucketCloud:
vcsSettings["id"] = m.VCSIntegration.ID
vcsSettings["namespace"] = m.Namespace
fieldName = "bitbucket_cloud"
case VCSProviderBitbucketDatacenter:
vcsSettings["id"] = m.VCSIntegration.ID
vcsSettings["namespace"] = m.Namespace
fieldName = "bitbucket_datacenter"
case VCSProviderGitHubEnterprise:
Expand Down
5 changes: 4 additions & 1 deletion spacelift/internal/structs/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ func (s *Stack) VCSSettings() (string, map[string]interface{}) {
"namespace": s.Namespace,
}
case VCSProviderBitbucketDatacenter:
return "bitbucket_datacenter", singleKeyMap("namespace", s.Namespace)
return "bitbucket_datacenter", map[string]interface{}{
"id": s.VCSIntegration.ID,
"namespace": s.Namespace,
}
case VCSProviderGitHubEnterprise:
return "github_enterprise", map[string]interface{}{
"id": s.VCSIntegration.ID,
Expand Down
Loading

0 comments on commit 4b2f9a8

Please sign in to comment.