Skip to content

Commit

Permalink
SCALRCORE-28534 Service accounts > Owners: Provider support
Browse files Browse the repository at this point in the history
  • Loading branch information
DayS1eeper committed Nov 22, 2023
1 parent c357edb commit 3969999
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/service_account.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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-20231004160327-c0bbb43d3b4f
github.com/scalr/go-scalr v0.0.0-20231122145749-8ee412f018b9
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
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-20231004160327-c0bbb43d3b4f h1:2acK1M8YNfTe1rpn9UUhR0UIGMWCmHw4BMxQCCM7lPo=
github.com/scalr/go-scalr v0.0.0-20231004160327-c0bbb43d3b4f/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE=
github.com/scalr/go-scalr v0.0.0-20231122145749-8ee412f018b9 h1:a4B/RzwtHjOt8SakWtFieCoO4Pl/Wcr+UEpdxT5RKNo=
github.com/scalr/go-scalr v0.0.0-20231122145749-8ee412f018b9/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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 3969999

Please sign in to comment.