@@ -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
2017func 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-
212146func testServiceAccountConfig (name , role string ) string {
213147 return fmt .Sprintf (`
214148resource "grafana_service_account" "test" {
0 commit comments