Skip to content

Commit

Permalink
Merged automatically by CI pipeline
Browse files Browse the repository at this point in the history
SCALRCORE-28534 Service accounts > Owners: Provider support
  • Loading branch information
emocharnik committed Nov 27, 2023
2 parents 13fe246 + ef39771 commit 27becc6
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/service_account.md
Expand Up @@ -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.

<a id="nestedatt--created_by"></a>
Expand Down
1 change: 1 addition & 0 deletions docs/resources/service_account.md
Expand Up @@ -32,6 +32,7 @@ resource "scalr_service_account" "example" {

- `account_id` (String) ID of the account, in the format `acc-<RANDOM STRING>`.
- `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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -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=
Expand Down
16 changes: 15 additions & 1 deletion scalr/data_source_scalr_service_account.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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},
},
},
}
}
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 14 additions & 2 deletions scalr/data_source_scalr_service_account_test.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
),
),
},
{
Expand Down Expand Up @@ -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 {
Expand Down
35 changes: 34 additions & 1 deletion scalr/resource_scalr_service_account.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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},
},
},
}
}
Expand All @@ -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{}{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 15 additions & 1 deletion scalr/resource_scalr_service_account_test.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
),
),
},
},
Expand Down Expand Up @@ -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",
),
),
},
},
Expand All @@ -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)
}

Expand Down

0 comments on commit 27becc6

Please sign in to comment.