diff --git a/CHANGELOG.md b/CHANGELOG.md index 4de1c8e1..a259a8a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- `scalr_service_account`: new attribute `owners` ([#289](https://github.com/Scalr/terraform-provider-scalr/pull/289)) +- `data.scalr_service_account`: new attribute `owners` ([#289](https://github.com/Scalr/terraform-provider-scalr/pull/289)) + ### Changed - `scalr_policy_group`: `environments` attribute became optional instead of read-only ([#288](https://github.com/Scalr/terraform-provider-scalr/pull/288)) diff --git a/docs/data-sources/service_account.md b/docs/data-sources/service_account.md index d6080b89..1134d15e 100644 --- a/docs/data-sources/service_account.md +++ b/docs/data-sources/service_account.md @@ -38,6 +38,7 @@ data "scalr_service_account" "example2" { - `created_by` (List of Object) Details of the user that created the service account. (see [below for nested schema](#nestedatt--created_by)) - `description` (String) Description of the service account. - `name` (String) Name of the service account. +- `owners` (List of String) The teams, the service account belongs to. - `status` (String) The status of the service account. diff --git a/docs/resources/service_account.md b/docs/resources/service_account.md index 1a3a339f..9c6e1f76 100644 --- a/docs/resources/service_account.md +++ b/docs/resources/service_account.md @@ -32,6 +32,7 @@ resource "scalr_service_account" "example" { - `account_id` (String) ID of the account, in the format `acc-`. - `description` (String) Description of the service account. +- `owners` (Set of String) The teams, the service account belongs to. - `status` (String) The status of the service account. Valid values are `Active` and `Inactive`. Defaults to `Active`. ### Read-Only diff --git a/go.mod b/go.mod index 4e60ba46..97cb6ca1 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/hashicorp/terraform-plugin-docs v0.16.0 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-20231117090940-913594e4e135 + github.com/scalr/go-scalr v0.0.0-20231127161513-5526b467eebc ) require ( diff --git a/go.sum b/go.sum index 888c1a27..6c4250df 100644 --- a/go.sum +++ b/go.sum @@ -260,8 +260,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww= github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY= -github.com/scalr/go-scalr v0.0.0-20231117090940-913594e4e135 h1:EAfMV+rwOLld3pJPwUnrFRyt3jxYx/N0q+fjextco0s= -github.com/scalr/go-scalr v0.0.0-20231117090940-913594e4e135/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE= +github.com/scalr/go-scalr v0.0.0-20231127161513-5526b467eebc h1:TvEXZ34Q3rS8dab1zO5pNqTQDDjPZrKBBw44PycKV3A= +github.com/scalr/go-scalr v0.0.0-20231127161513-5526b467eebc/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE= github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ= github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= diff --git a/scalr/data_source_scalr_service_account.go b/scalr/data_source_scalr_service_account.go index 0feaa4a6..08d5edc6 100644 --- a/scalr/data_source_scalr_service_account.go +++ b/scalr/data_source_scalr_service_account.go @@ -2,11 +2,12 @@ package scalr import ( "context" + "log" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/scalr/go-scalr" - "log" ) func dataSourceScalrServiceAccount() *schema.Resource { @@ -75,6 +76,12 @@ func dataSourceScalrServiceAccount() *schema.Resource { }, }, }, + "owners": { + Description: "The teams, the service account belongs to.", + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, }, } } @@ -124,6 +131,13 @@ func dataSourceScalrServiceAccountRead(ctx context.Context, d *schema.ResourceDa "full_name": sa.CreatedBy.FullName, }) } + + owners := make([]string, 0) + for _, owner := range sa.Owners { + owners = append(owners, owner.ID) + } + _ = d.Set("owners", owners) + _ = d.Set("name", sa.Name) _ = d.Set("email", sa.Email) _ = d.Set("description", sa.Description) diff --git a/scalr/data_source_scalr_service_account_test.go b/scalr/data_source_scalr_service_account_test.go index b72419ca..4739b8e9 100644 --- a/scalr/data_source_scalr_service_account_test.go +++ b/scalr/data_source_scalr_service_account_test.go @@ -2,10 +2,11 @@ package scalr import ( "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - "github.com/scalr/go-scalr" "regexp" "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/scalr/go-scalr" ) func TestAccScalrServiceAccountDataSource_basic(t *testing.T) { @@ -50,6 +51,10 @@ func TestAccScalrServiceAccountDataSource_basic(t *testing.T) { resource.TestCheckResourceAttr( "data.scalr_service_account.test", "created_by.#", "1", ), + resource.TestCheckResourceAttrPair( + "data.scalr_service_account.test", "owners", + "scalr_service_account.test", "owners", + ), ), }, { @@ -125,6 +130,13 @@ resource scalr_service_account test { name = "test-sa-%d" description = "desc-%[1]d" status = "%[2]s" + owners = [scalr_iam_team.test.id] +} + +resource "scalr_iam_team" "test" { + name = "test-%[1]d-owner" + description = "Test team" + users = [] } data scalr_service_account test { diff --git a/scalr/resource_scalr_service_account.go b/scalr/resource_scalr_service_account.go index 7644b3e4..5fea4dfc 100644 --- a/scalr/resource_scalr_service_account.go +++ b/scalr/resource_scalr_service_account.go @@ -3,11 +3,12 @@ package scalr import ( "context" "errors" + "log" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/scalr/go-scalr" - "log" ) func resourceScalrServiceAccount() *schema.Resource { @@ -83,6 +84,12 @@ func resourceScalrServiceAccount() *schema.Resource { }, }, }, + "owners": { + Description: "The teams, the service account belongs to.", + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, }, } } @@ -109,6 +116,12 @@ func resourceScalrServiceAccountRead(ctx context.Context, d *schema.ResourceData _ = d.Set("status", sa.Status) _ = d.Set("account_id", sa.Account.ID) + owners := make([]string, 0) + for _, owner := range sa.Owners { + owners = append(owners, owner.ID) + } + _ = d.Set("owners", owners) + var createdBy []interface{} if sa.CreatedBy != nil { createdBy = append(createdBy, map[string]interface{}{ @@ -142,6 +155,14 @@ func resourceScalrServiceAccountCreate(ctx context.Context, d *schema.ResourceDa options.Status = scalr.ServiceAccountStatusPtr(saStatus) } + if owners, ok := d.GetOk("owners"); ok { + ownerResources := make([]*scalr.Team, 0) + for _, ownerId := range owners.(*schema.Set).List() { + ownerResources = append(ownerResources, &scalr.Team{ID: ownerId.(string)}) + } + options.Owners = ownerResources + } + log.Printf("[DEBUG] Create service account %s in account %s", name, accountID) sa, err := scalrClient.ServiceAccounts.Create(ctx, options) if err != nil { @@ -170,6 +191,18 @@ func resourceScalrServiceAccountUpdate(ctx context.Context, d *schema.ResourceDa options.Status = scalr.ServiceAccountStatusPtr(status) } + if d.HasChange("owners") { + ownerResources := make([]*scalr.Team, 0) + if owners, ok := d.GetOk("owners"); ok { + for _, ownerId := range owners.(*schema.Set).List() { + ownerResources = append(ownerResources, &scalr.Team{ID: ownerId.(string)}) + } + options.Owners = ownerResources + } else { + options.Owners = ownerResources + } + } + log.Printf("[DEBUG] Update service account %s", id) _, err := scalrClient.ServiceAccounts.Update(ctx, id, options) if err != nil { diff --git a/scalr/resource_scalr_service_account_test.go b/scalr/resource_scalr_service_account_test.go index 261cc4cf..00cd08ea 100644 --- a/scalr/resource_scalr_service_account_test.go +++ b/scalr/resource_scalr_service_account_test.go @@ -2,10 +2,11 @@ package scalr import ( "fmt" + "testing" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/scalr/go-scalr" - "testing" ) func TestAccScalrServiceAccount_basic(t *testing.T) { @@ -36,6 +37,9 @@ func TestAccScalrServiceAccount_basic(t *testing.T) { resource.TestCheckResourceAttr( "scalr_service_account.test", "created_by.#", "1", ), + resource.TestCheckResourceAttr( + "scalr_service_account.test", "owners.#", "1", + ), ), }, }, @@ -87,6 +91,9 @@ func TestAccScalrServiceAccount_update(t *testing.T) { resource.TestCheckResourceAttr( "scalr_service_account.test", "status", string(scalr.ServiceAccountStatusInactive), ), + resource.TestCheckResourceAttr( + "scalr_service_account.test", "owners.#", "0", + ), ), }, }, @@ -99,6 +106,13 @@ resource scalr_service_account test { name = "test-sa-%d" description = "desc-%[1]d" status = "%[2]s" + owners = [scalr_iam_team.test.id] +} + +resource "scalr_iam_team" "test" { + name = "test-%[1]d-owner" + description = "Test team" + users = [] }`, rInt, scalr.ServiceAccountStatusActive) }