Skip to content

Commit

Permalink
Merge pull request #243 from Scalr/feature/SCALRCORE-26056
Browse files Browse the repository at this point in the history
SCALRCORE-26056 - Provider > Add sharing VCS to environments feature
  • Loading branch information
emocharnik authored May 12, 2023
2 parents 4c21a36 + b3730f8 commit c7eb005
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `data.scalr_vcs_provider`: added new attribute `agent_pool_id` ([#233](https://github.com/Scalr/terraform-provider-scalr/pull/233))
- `scalr_agent_pool`: added new attribute `vcs_enabled` ([#233](https://github.com/Scalr/terraform-provider-scalr/pull/233))
- `data.scalr_agent_pool`: added new attribute `vcs_enabled` ([#233](https://github.com/Scalr/terraform-provider-scalr/pull/232))
- `scalr_vcs_provider`: added new attribute `environments` ([#243](https://github.com/Scalr/terraform-provider-scalr/pull/243))
- `scalr_workspace`: added new attribute `deletion_protection_enabled` ([#242](https://github.com/Scalr/terraform-provider-scalr/pull/242))
- `data.scalr_workspace`: added new attribute `deletion_protection_enabled` ([#242](https://github.com/Scalr/terraform-provider-scalr/pull/242))

Expand Down
2 changes: 2 additions & 0 deletions docs/resources/scalr_vcs_provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ resource "scalr_vcs_provider" "example" {
* `url` - (Optional) This field is required for self-hosted vcs providers.
* `username` - (Optional) This field is required for `bitbucket_enterprise` provider type.
* `agent_pool_id` - (Optional) ID of the agent pool to communicate with VCS through.
* `environments` - (Optional) The list of environment identifiers that the VCS provider is shared to.
Use `["*"]` to share with all environments.


## Attribute Reference
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require (
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
github.com/scalr/go-scalr v0.0.0-20230504103205-022c2d21236f
github.com/scalr/go-scalr v0.0.0-20230511140835-ed400653430f
)

require (
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,12 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/scalr/go-scalr v0.0.0-20230503074839-6ee8fc52925f h1:3HctDP6Dt8F6UWEXG7Fwx4JMx21LlvH//rYNxSteMO8=
github.com/scalr/go-scalr v0.0.0-20230503074839-6ee8fc52925f/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE=
github.com/scalr/go-scalr v0.0.0-20230504103205-022c2d21236f h1:NX/BFNSAMB+SD8i1RsOcc71KT110UfSpVg+9W5LSkZY=
github.com/scalr/go-scalr v0.0.0-20230504103205-022c2d21236f/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE=
github.com/scalr/go-scalr v0.0.0-20230511140835-ed400653430f h1:TilyG5/f6rLxDIr0NzErEO8+E1fDRMaGAYJbO9wQ6VA=
github.com/scalr/go-scalr v0.0.0-20230511140835-ed400653430f/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE=
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
Expand Down
59 changes: 59 additions & 0 deletions scalr/resource_scalr_vcs_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ func resourceScalrVcsProvider() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"environments": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
Expand Down Expand Up @@ -107,6 +113,25 @@ func resourceScalrVcsProviderCreate(ctx context.Context, d *schema.ResourceData,
}
}

if environmentsI, ok := d.GetOk("environments"); ok {
environments := environmentsI.(*schema.Set).List()
if (len(environments) == 1) && (environments[0].(string) == "*") {
options.IsShared = scalr.Bool(true)
} else if len(environments) > 0 {
options.IsShared = scalr.Bool(false)
environmentValues := make([]*scalr.Environment, 0)
for _, env := range environments {
if env.(string) == "*" {
return diag.Errorf(
"You cannot simultaneously enable the VCS provider for all and a limited list of environments. Please remove either wildcard or environment identifiers.",
)
}
environmentValues = append(environmentValues, &scalr.Environment{ID: env.(string)})
}
options.Environments = environmentValues
}
}

log.Printf("[DEBUG] Create vcs provider: %s", name)
provider, err := scalrClient.VcsProviders.Create(ctx, options)
if err != nil {
Expand Down Expand Up @@ -141,6 +166,17 @@ func resourceScalrVcsProviderRead(ctx context.Context, d *schema.ResourceData, m
_ = d.Set("agent_pool_id", "")
}

if provider.IsShared {
allEnvironments := []string{"*"}
_ = d.Set("environments", allEnvironments)
} else {
environmentIDs := make([]string, 0)
for _, environment := range provider.Environments {
environmentIDs = append(environmentIDs, environment.ID)
}
_ = d.Set("environments", environmentIDs)
}

return nil
}

Expand All @@ -167,6 +203,29 @@ func resourceScalrVcsProviderUpdate(ctx context.Context, d *schema.ResourceData,
}
}

if environmentsI, ok := d.GetOk("environments"); ok {
environments := environmentsI.(*schema.Set).List()
if (len(environments) == 1) && (environments[0].(string) == "*") {
options.IsShared = scalr.Bool(true)
options.Environments = make([]*scalr.Environment, 0)
} else {
options.IsShared = scalr.Bool(false)
environmentValues := make([]*scalr.Environment, 0)
for _, env := range environments {
if env.(string) == "*" {
return diag.Errorf(
"You cannot simultaneously enable the VCS provider for all and a limited list of environments. Please remove either wildcard or environment identifiers.",
)
}
environmentValues = append(environmentValues, &scalr.Environment{ID: env.(string)})
}
options.Environments = environmentValues
}
} else {
options.IsShared = scalr.Bool(true)
options.Environments = make([]*scalr.Environment, 0)
}

log.Printf("[DEBUG] Update vcs provider: %s", d.Id())
_, err := scalrClient.VcsProviders.Update(ctx, d.Id(), options)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions scalr/resource_scalr_vcs_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestAccVcsProvider_basic(t *testing.T) {
resource.TestCheckResourceAttr("scalr_vcs_provider.test", "account_id", defaultAccount),
resource.TestCheckResourceAttr("scalr_vcs_provider.test", "vcs_type", string(scalr.Github)),
resource.TestCheckResourceAttr("scalr_vcs_provider.test", "url", "https://github.com"),
resource.TestCheckResourceAttr("scalr_vcs_provider.test", "environments.0", "*"),
),
},
{
Expand Down

0 comments on commit c7eb005

Please sign in to comment.