Skip to content

Commit 9af2374

Browse files
F21julienduchesne
andauthored
Fix grafana_cloud_stack_service_account resource to support creating service accounts without a basic role (#1464)
* Fix grafana_cloud_stack_service_account resource to support creating service accounts without a basic role * Fix tests * Generate docs --------- Co-authored-by: Julien Duchesne <[email protected]>
1 parent a2eee0f commit 9af2374

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

docs/resources/cloud_stack_service_account.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ resource "grafana_cloud_stack_service_account" "cloud_sa" {
4141
### Required
4242

4343
- `name` (String) The name of the service account.
44+
- `role` (String) The basic role of the service account in the organization.
4445
- `stack_slug` (String)
4546

4647
### Optional
4748

4849
- `is_disabled` (Boolean) The disabled status for the service account. Defaults to `false`.
49-
- `role` (String) The basic role of the service account in the organization.
5050

5151
### Read-Only
5252

internal/resources/cloud/resource_cloud_stack_service_account.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ Required access policy scopes:
5858
},
5959
"role": {
6060
Type: schema.TypeString,
61-
Optional: true,
62-
ValidateFunc: validation.StringInSlice([]string{"Viewer", "Editor", "Admin"}, false),
61+
Required: true,
62+
ValidateFunc: validation.StringInSlice([]string{"Viewer", "Editor", "Admin", "None"}, false),
6363
Description: "The basic role of the service account in the organization.",
6464
ForceNew: true, // The grafana API does not support updating the service account
6565
},

internal/resources/cloud/resource_cloud_stack_service_account_test.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestAccGrafanaServiceAccountFromCloud(t *testing.T) {
2929
CheckDestroy: testAccStackCheckDestroy(&stack),
3030
Steps: []resource.TestStep{
3131
{
32-
Config: testAccGrafanaServiceAccountFromCloud(slug, slug, true),
32+
Config: testAccGrafanaServiceAccountFromCloud(slug, slug, true, "Admin"),
3333
Check: resource.ComposeTestCheckFunc(
3434
testAccStackCheckExists("grafana_cloud_stack.test", &stack),
3535
testAccGrafanaAuthCheckServiceAccounts(&stack, []string{"management-sa"}),
@@ -42,9 +42,10 @@ func TestAccGrafanaServiceAccountFromCloud(t *testing.T) {
4242
),
4343
},
4444
{
45-
Config: testAccGrafanaServiceAccountFromCloud(slug, slug, false),
45+
Config: testAccGrafanaServiceAccountFromCloud(slug, slug, false, "Editor"),
4646
Check: resource.ComposeTestCheckFunc(
4747
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "is_disabled", "false"),
48+
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "role", "Editor"),
4849
),
4950
},
5051
{
@@ -60,6 +61,37 @@ func TestAccGrafanaServiceAccountFromCloud(t *testing.T) {
6061
})
6162
}
6263

64+
func TestAccGrafanaServiceAccountFromCloudNoneRole(t *testing.T) {
65+
testutils.CheckCloudAPITestsEnabled(t)
66+
67+
var stack gcom.FormattedApiInstance
68+
prefix := "tfsanone"
69+
slug := GetRandomStackName(prefix)
70+
71+
resource.ParallelTest(t, resource.TestCase{
72+
PreCheck: func() {
73+
testAccDeleteExistingStacks(t, prefix)
74+
},
75+
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
76+
CheckDestroy: testAccStackCheckDestroy(&stack),
77+
Steps: []resource.TestStep{
78+
{
79+
Config: testAccGrafanaServiceAccountFromCloud(slug, slug, true, "None"),
80+
Check: resource.ComposeTestCheckFunc(
81+
testAccStackCheckExists("grafana_cloud_stack.test", &stack),
82+
testAccGrafanaAuthCheckServiceAccounts(&stack, []string{"management-sa"}),
83+
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "name", "management-sa"),
84+
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "role", "None"),
85+
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "is_disabled", "true"),
86+
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account_token.management_token", "name", "management-sa-token"),
87+
resource.TestCheckNoResourceAttr("grafana_cloud_stack_service_account_token.management_token", "expiration"),
88+
resource.TestCheckResourceAttrSet("grafana_cloud_stack_service_account_token.management_token", "key"),
89+
),
90+
},
91+
},
92+
})
93+
}
94+
6395
// Tests that the ID change from 2.13.0 to latest works
6496
// Remove on next major release
6597
func TestAccGrafanaServiceAccountFromCloud_MigrateFrom213(t *testing.T) {
@@ -112,12 +144,12 @@ func TestAccGrafanaServiceAccountFromCloud_MigrateFrom213(t *testing.T) {
112144
})
113145
}
114146

115-
func testAccGrafanaServiceAccountFromCloud(name, slug string, disabled bool) string {
147+
func testAccGrafanaServiceAccountFromCloud(name, slug string, disabled bool, role string) string {
116148
return testAccStackConfigBasic(name, slug, "description") + fmt.Sprintf(`
117149
resource "grafana_cloud_stack_service_account" "management" {
118150
stack_slug = grafana_cloud_stack.test.slug
119151
name = "management-sa"
120-
role = "Admin"
152+
role = "%s"
121153
is_disabled = %t
122154
}
123155
@@ -126,11 +158,11 @@ func testAccGrafanaServiceAccountFromCloud(name, slug string, disabled bool) str
126158
service_account_id = grafana_cloud_stack_service_account.management.id
127159
name = "management-sa-token"
128160
}
129-
`, disabled)
161+
`, role, disabled)
130162
}
131163

132164
func testAccGrafanaServiceAccountWithStackFolder(name, slug string, withFolder bool) string {
133-
return testAccGrafanaServiceAccountFromCloud(name, slug, false) + fmt.Sprintf(`
165+
return testAccGrafanaServiceAccountFromCloud(name, slug, false, "Admin") + fmt.Sprintf(`
134166
provider "grafana" {
135167
alias = "stack"
136168
auth = grafana_cloud_stack_service_account_token.management_token.key

0 commit comments

Comments
 (0)