Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCALRCORE-28534 Service accounts > Owners: Provider support #137

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type ServiceAccount struct {
// Relations
Account *Account `jsonapi:"relation,account,omitempty"`
CreatedBy *User `jsonapi:"relation,created-by,omitempty"`
Owners []*Team `jsonapi:"relation,owners"`
}

// ServiceAccountListOptions represents the options for listing service accounts.
Expand All @@ -79,6 +80,7 @@ type ServiceAccountCreateOptions struct {
Description *string `jsonapi:"attr,description,omitempty"`
Status *ServiceAccountStatus `jsonapi:"attr,status,omitempty"`
Account *Account `jsonapi:"relation,account"`
Owners []*Team `jsonapi:"relation,owners"`
}

func (o ServiceAccountCreateOptions) valid() error {
Expand All @@ -101,6 +103,7 @@ type ServiceAccountUpdateOptions struct {

Description *string `jsonapi:"attr,description,omitempty"`
Status *ServiceAccountStatus `jsonapi:"attr,status,omitempty"`
Owners []*Team `jsonapi:"relation,owners"`
}

// Read a service account by its ID.
Expand Down
40 changes: 40 additions & 0 deletions service_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ func TestServiceAccountsCreate(t *testing.T) {
ctx := context.Background()

t.Run("with valid options", func(t *testing.T) {
ownerTeam, ownerTeamCleanup := createTeam(t, client, nil)
defer ownerTeamCleanup()

options := ServiceAccountCreateOptions{
Name: String("tst-" + randomString(t)),
Description: String("tst-description-" + randomString(t)),
Status: ServiceAccountStatusPtr(ServiceAccountStatusActive),
Account: &Account{ID: defaultAccountID},
Owners: []*Team{{ID: ownerTeam.ID}},
}

sa, err := client.ServiceAccounts.Create(ctx, options)
Expand All @@ -95,6 +99,7 @@ func TestServiceAccountsCreate(t *testing.T) {
assert.Equal(t, options.Description, &item.Description)
assert.Equal(t, options.Status, &item.Status)
assert.Equal(t, &options.Account, &item.Account)
assert.Equal(t, &options.Owners, &item.Owners)
}
})

Expand Down Expand Up @@ -181,6 +186,9 @@ func TestServiceAccountsUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()

ownerTeam, ownerTeamCleanup := createTeam(t, client, nil)
defer ownerTeamCleanup()

saTest, saTestCleanup := createServiceAccount(
t, client, &Account{ID: defaultAccountID}, ServiceAccountStatusPtr(ServiceAccountStatusActive),
)
Expand All @@ -207,6 +215,38 @@ func TestServiceAccountsUpdate(t *testing.T) {
assert.Equal(t, options.Status, &item.Status)
}
})
t.Run("with owners", func(t *testing.T) {
options := ServiceAccountUpdateOptions{
Owners: []*Team{{ID: ownerTeam.ID}},
}
sa, err := client.ServiceAccounts.Update(ctx, saTest.ID, options)
require.NoError(t, err)
refreshed, err := client.ServiceAccounts.Read(ctx, saTest.ID)
require.NoError(t, err)

for _, item := range []*ServiceAccount{
sa,
refreshed,
} {
assert.Equal(t, &options.Owners, &item.Owners)
}

// unset the owner
options = ServiceAccountUpdateOptions{
Owners: []*Team{},
}
sa, err = client.ServiceAccounts.Update(ctx, saTest.ID, options)
require.NoError(t, err)
refreshed, err = client.ServiceAccounts.Read(ctx, saTest.ID)
require.NoError(t, err)

for _, item := range []*ServiceAccount{
sa,
refreshed,
} {
assert.Empty(t, item.Owners)
}
})
}

func TestServiceAccountsDelete(t *testing.T) {
Expand Down