Skip to content

Commit 392523e

Browse files
Service Account tests: Use OpenAPI client (#1143)
Gets rid of the manually written checkers in favor of the new ones that use the OpenAPI client
1 parent 535da01 commit 392523e

File tree

3 files changed

+45
-95
lines changed

3 files changed

+45
-95
lines changed

internal/resources/grafana/common_check_exists_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/grafana/grafana-openapi-client-go/client/datasources"
1111
"github.com/grafana/grafana-openapi-client-go/client/folders"
1212
"github.com/grafana/grafana-openapi-client-go/client/library_elements"
13+
"github.com/grafana/grafana-openapi-client-go/client/service_accounts"
1314
"github.com/grafana/grafana-openapi-client-go/client/teams"
1415
"github.com/grafana/grafana-openapi-client-go/client/users"
1516
"github.com/grafana/grafana-openapi-client-go/models"
@@ -55,6 +56,14 @@ var (
5556
return payloadOrError(resp, err)
5657
},
5758
)
59+
serviceAccountCheckExists = newCheckExistsHelper(
60+
func(t *models.ServiceAccountDTO) string { return strconv.FormatInt(t.ID, 10) },
61+
func(client *goapi.GrafanaHTTPAPI, id string) (*models.ServiceAccountDTO, error) {
62+
params := service_accounts.NewRetrieveServiceAccountParams().WithServiceAccountID(mustParseInt64(id))
63+
resp, err := client.ServiceAccounts.RetrieveServiceAccount(params, nil)
64+
return payloadOrError(resp, err)
65+
},
66+
)
5867
teamCheckExists = newCheckExistsHelper(
5968
func(t *models.TeamDTO) string { return strconv.FormatInt(t.ID, 10) },
6069
func(client *goapi.GrafanaHTTPAPI, id string) (*models.TeamDTO, error) {

internal/resources/grafana/resource_service_account_test.go

Lines changed: 21 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,31 @@ import (
44
"errors"
55
"fmt"
66
"regexp"
7-
"strconv"
87
"testing"
98

109
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1110
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1211
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1312

14-
gapi "github.com/grafana/grafana-api-golang-client"
15-
"github.com/grafana/terraform-provider-grafana/internal/common"
16-
"github.com/grafana/terraform-provider-grafana/internal/resources/grafana"
13+
"github.com/grafana/grafana-openapi-client-go/models"
1714
"github.com/grafana/terraform-provider-grafana/internal/testutils"
1815
)
1916

2017
func TestAccServiceAccount_basic(t *testing.T) {
2118
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")
2219

23-
var sa gapi.ServiceAccountDTO
24-
var updatedSA gapi.ServiceAccountDTO
20+
var sa models.ServiceAccountDTO
21+
var updatedSA models.ServiceAccountDTO
2522
name := acctest.RandString(10)
2623

2724
resource.ParallelTest(t, resource.TestCase{
2825
ProviderFactories: testutils.ProviderFactories,
29-
CheckDestroy: testAccServiceAccountCheckDestroy,
26+
CheckDestroy: serviceAccountCheckExists.destroyed(&updatedSA, nil),
3027
Steps: []resource.TestStep{
3128
{
3229
Config: testServiceAccountConfig(name, "Editor"),
3330
Check: resource.ComposeTestCheckFunc(
34-
testAccServiceAccountCheckExists(&sa),
31+
serviceAccountCheckExists.exists("grafana_service_account.test", &sa),
3532
resource.TestCheckResourceAttr("grafana_service_account.test", "name", name),
3633
resource.TestCheckResourceAttr("grafana_service_account.test", "org_id", "1"),
3734
resource.TestCheckResourceAttr("grafana_service_account.test", "role", "Editor"),
@@ -43,7 +40,7 @@ func TestAccServiceAccount_basic(t *testing.T) {
4340
{
4441
Config: testServiceAccountConfig(name+"-updated", "Editor"),
4542
Check: resource.ComposeTestCheckFunc(
46-
testAccServiceAccountCheckExists(&updatedSA),
43+
serviceAccountCheckExists.exists("grafana_service_account.test", &updatedSA),
4744
func(s *terraform.State) error {
4845
if sa.ID != updatedSA.ID {
4946
return errors.New("ID changed")
@@ -65,16 +62,16 @@ func TestAccServiceAccount_NoneRole(t *testing.T) {
6562
testutils.CheckOSSTestsEnabled(t, ">=10.2.0")
6663

6764
name := acctest.RandString(10)
68-
var sa gapi.ServiceAccountDTO
65+
var sa models.ServiceAccountDTO
6966

7067
resource.ParallelTest(t, resource.TestCase{
7168
ProviderFactories: testutils.ProviderFactories,
72-
CheckDestroy: testAccServiceAccountCheckDestroy,
69+
CheckDestroy: serviceAccountCheckExists.destroyed(&sa, nil),
7370
Steps: []resource.TestStep{
7471
{
7572
Config: testServiceAccountConfig(name, "None"),
7673
Check: resource.ComposeTestCheckFunc(
77-
testAccServiceAccountCheckExists(&sa),
74+
serviceAccountCheckExists.exists("grafana_service_account.test", &sa),
7875
resource.TestCheckResourceAttr("grafana_service_account.test", "name", name),
7976
resource.TestCheckResourceAttr("grafana_service_account.test", "org_id", "1"),
8077
resource.TestCheckResourceAttr("grafana_service_account.test", "role", "None"),
@@ -94,16 +91,23 @@ func TestAccServiceAccount_many_longtest(t *testing.T) {
9491

9592
name := acctest.RandString(10)
9693

94+
// For each SA, check that it exists and has the correct name, then check that it is properly destroyed
95+
createdServiceAccounts := make([]models.ServiceAccountDTO, 60)
96+
checks := []resource.TestCheckFunc{}
97+
destroyedChecks := []resource.TestCheckFunc{}
98+
for i := 0; i < 60; i++ {
99+
checks = append(checks, serviceAccountCheckExists.exists(fmt.Sprintf("grafana_service_account.test_%d", i), &createdServiceAccounts[i]))
100+
checks = append(checks, resource.TestCheckResourceAttr(fmt.Sprintf("grafana_service_account.test_%d", i), "name", fmt.Sprintf("%s-%d", name, i)))
101+
destroyedChecks = append(destroyedChecks, serviceAccountCheckExists.destroyed(&createdServiceAccounts[i], nil))
102+
}
103+
97104
resource.ParallelTest(t, resource.TestCase{
98105
ProviderFactories: testutils.ProviderFactories,
99-
CheckDestroy: testAccServiceAccountCheckDestroy,
106+
CheckDestroy: resource.ComposeAggregateTestCheckFunc(destroyedChecks...),
100107
Steps: []resource.TestStep{
101108
{
102109
Config: testManyServiceAccountsConfig(name, 60),
103-
Check: resource.ComposeTestCheckFunc(
104-
resource.TestCheckResourceAttr("grafana_service_account.test_1", "name", name+"-1"),
105-
resource.TestCheckResourceAttr("grafana_service_account.test_2", "name", name+"-2"),
106-
),
110+
Check: resource.ComposeAggregateTestCheckFunc(checks...),
107111
},
108112
},
109113
})
@@ -114,7 +118,6 @@ func TestAccServiceAccount_invalid_role(t *testing.T) {
114118

115119
resource.ParallelTest(t, resource.TestCase{
116120
ProviderFactories: testutils.ProviderFactories,
117-
CheckDestroy: testAccServiceAccountCheckDestroy,
118121
Steps: []resource.TestStep{
119122
{
120123
ExpectError: regexp.MustCompile(`.*expected role to be one of \[.+\], got InvalidRole`),
@@ -140,75 +143,6 @@ func testManyServiceAccountsConfig(prefix string, count int) string {
140143
return config
141144
}
142145

143-
func testAccServiceAccountCheckExists(sa *gapi.ServiceAccountDTO) resource.TestCheckFunc {
144-
return func(s *terraform.State) error {
145-
foundSA, err := testAccServiceAccountCheckExistsBool(s, true)
146-
if err != nil {
147-
return err
148-
}
149-
*sa = *foundSA
150-
return nil
151-
}
152-
}
153-
154-
func testAccServiceAccountCheckDestroy(s *terraform.State) error {
155-
_, err := testAccServiceAccountCheckExistsBool(s, false)
156-
return err
157-
}
158-
159-
func testAccServiceAccountCheckExistsBool(s *terraform.State, shouldExist bool) (*gapi.ServiceAccountDTO, error) {
160-
c := testutils.Provider.Meta().(*common.Client).GrafanaAPI
161-
162-
for _, rs := range s.RootModule().Resources {
163-
if rs.Type != "grafana_service_account" {
164-
continue
165-
}
166-
167-
orgID, idStr := grafana.SplitOrgResourceID(rs.Primary.ID)
168-
id, err := strconv.ParseInt(idStr, 10, 32)
169-
if err != nil {
170-
return nil, err
171-
}
172-
173-
// If orgID > 1, always check that the SA doesn't exist in the default org
174-
if orgID > 1 {
175-
sas, err := c.GetServiceAccounts()
176-
if err != nil {
177-
return nil, err
178-
}
179-
180-
for _, sa := range sas {
181-
if sa.ID == id {
182-
return nil, errors.New("Service account exists in the default org")
183-
}
184-
}
185-
186-
c = c.WithOrgID(orgID)
187-
}
188-
189-
sas, err := c.GetServiceAccounts()
190-
if err != nil {
191-
return nil, err
192-
}
193-
194-
for _, sa := range sas {
195-
if sa.ID == id {
196-
if shouldExist {
197-
return &sa, nil
198-
} else {
199-
return nil, errors.New("Service account still exists")
200-
}
201-
}
202-
}
203-
204-
if shouldExist {
205-
return nil, errors.New("Service account was not found")
206-
}
207-
}
208-
209-
return nil, nil
210-
}
211-
212146
func testServiceAccountConfig(name, role string) string {
213147
return fmt.Sprintf(`
214148
resource "grafana_service_account" "test" {

internal/resources/grafana/resource_service_account_token_test.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
gapi "github.com/grafana/grafana-api-golang-client"
9+
"github.com/grafana/grafana-openapi-client-go/models"
910
"github.com/grafana/terraform-provider-grafana/internal/common"
1011
"github.com/grafana/terraform-provider-grafana/internal/testutils"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -17,16 +18,19 @@ func TestAccServiceAccountToken_basic(t *testing.T) {
1718
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")
1819

1920
name := acctest.RandString(10)
20-
var sa gapi.ServiceAccountDTO
21+
var sa models.ServiceAccountDTO
2122

2223
resource.ParallelTest(t, resource.TestCase{
2324
ProviderFactories: testutils.ProviderFactories,
24-
CheckDestroy: testAccServiceAccountTokenCheckDestroy,
25+
CheckDestroy: resource.ComposeTestCheckFunc(
26+
serviceAccountCheckExists.destroyed(&sa, nil),
27+
testAccServiceAccountTokenCheckDestroy,
28+
),
2529
Steps: []resource.TestStep{
2630
{
2731
Config: testAccServiceAccountTokenConfig(name, "Editor", 0, false),
2832
Check: resource.ComposeTestCheckFunc(
29-
testAccServiceAccountCheckExists(&sa),
33+
serviceAccountCheckExists.exists("grafana_service_account.test", &sa),
3034
resource.TestCheckResourceAttr("grafana_service_account.test", "name", name),
3135
resource.TestCheckResourceAttr("grafana_service_account.test", "role", "Editor"),
3236
resource.TestCheckResourceAttr("grafana_service_account_token.test", "name", name),
@@ -36,7 +40,7 @@ func TestAccServiceAccountToken_basic(t *testing.T) {
3640
{
3741
Config: testAccServiceAccountTokenConfig(name+"-updated", "Viewer", 300, false),
3842
Check: resource.ComposeTestCheckFunc(
39-
testAccServiceAccountCheckExists(&sa),
43+
serviceAccountCheckExists.exists("grafana_service_account.test", &sa),
4044
resource.TestCheckResourceAttr("grafana_service_account.test", "name", name+"-updated"),
4145
resource.TestCheckResourceAttr("grafana_service_account.test", "role", "Viewer"),
4246
resource.TestCheckResourceAttr("grafana_service_account_token.test", "name", name+"-updated"),
@@ -52,16 +56,19 @@ func TestAccServiceAccountToken_inOrg(t *testing.T) {
5256

5357
name := acctest.RandString(10)
5458
var org gapi.Org
55-
var sa gapi.ServiceAccountDTO
59+
var sa models.ServiceAccountDTO
5660

5761
resource.ParallelTest(t, resource.TestCase{
5862
ProviderFactories: testutils.ProviderFactories,
59-
CheckDestroy: testAccServiceAccountTokenCheckDestroy,
63+
CheckDestroy: resource.ComposeTestCheckFunc(
64+
serviceAccountCheckExists.destroyed(&sa, &org),
65+
testAccServiceAccountTokenCheckDestroy,
66+
),
6067
Steps: []resource.TestStep{
6168
{
6269
Config: testAccServiceAccountTokenConfig(name, "Editor", 0, true),
6370
Check: resource.ComposeTestCheckFunc(
64-
testAccServiceAccountCheckExists(&sa),
71+
serviceAccountCheckExists.exists("grafana_service_account.test", &sa),
6572
resource.TestCheckResourceAttr("grafana_service_account.test", "name", name),
6673
resource.TestCheckResourceAttr("grafana_service_account.test", "role", "Editor"),
6774
resource.TestCheckResourceAttr("grafana_service_account_token.test", "name", name),
@@ -76,7 +83,7 @@ func TestAccServiceAccountToken_inOrg(t *testing.T) {
7683
{
7784
Config: testAccServiceAccountTokenConfig(name+"-updated", "Viewer", 300, true),
7885
Check: resource.ComposeTestCheckFunc(
79-
testAccServiceAccountCheckExists(&sa),
86+
serviceAccountCheckExists.exists("grafana_service_account.test", &sa),
8087
resource.TestCheckResourceAttr("grafana_service_account.test", "name", name+"-updated"),
8188
resource.TestCheckResourceAttr("grafana_service_account.test", "role", "Viewer"),
8289
resource.TestCheckResourceAttr("grafana_service_account_token.test", "name", name+"-updated"),

0 commit comments

Comments
 (0)