@@ -10,6 +10,7 @@ import (
1010 "testing"
1111
1212 "github.com/hashicorp/terraform-plugin-testing/config"
13+ "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1314 "github.com/hashicorp/terraform-plugin-testing/helper/resource"
1415 "github.com/hashicorp/terraform-plugin-testing/terraform"
1516 stackitSdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
@@ -32,12 +33,33 @@ var invalidRole string
3233//go:embed testfiles/organization-role.tf
3334var organizationRole string
3435
36+ //go:embed testfiles/custom-role.tf
37+ var customRole string
38+
3539var testConfigVars = config.Variables {
3640 "project_id" : config .StringVariable (testutil .ProjectId ),
3741 "test_service_account" : config .StringVariable (testutil .TestProjectServiceAccountEmail ),
3842 "organization_id" : config .StringVariable (testutil .OrganizationId ),
3943}
4044
45+ var testConfigVarsCustomRole = config.Variables {
46+ "project_id" : config .StringVariable (testutil .ProjectId ),
47+ "test_service_account" : config .StringVariable (testutil .TestProjectServiceAccountEmail ),
48+ "organization_id" : config .StringVariable (testutil .OrganizationId ),
49+ "role_name" : config .StringVariable (fmt .Sprintf ("tf-acc-%s" , acctest .RandStringFromCharSet (5 , acctest .CharSetAlpha ))),
50+ "role_description" : config .StringVariable ("Some description" ),
51+ "role_permissions_0" : config .StringVariable ("iam.role.list" ),
52+ }
53+
54+ var testConfigVarsCustomRoleUpdated = config.Variables {
55+ "project_id" : config .StringVariable (testutil .ProjectId ),
56+ "test_service_account" : config .StringVariable (testutil .TestProjectServiceAccountEmail ),
57+ "organization_id" : config .StringVariable (testutil .OrganizationId ),
58+ "role_name" : config .StringVariable (fmt .Sprintf ("tf-acc-%s" , acctest .RandStringFromCharSet (5 , acctest .CharSetAlpha ))),
59+ "role_description" : config .StringVariable ("Updated description" ),
60+ "role_permissions_0" : config .StringVariable ("iam.role.edit" ),
61+ }
62+
4163func TestAccProjectRoleAssignmentResource (t * testing.T ) {
4264 t .Log (testutil .AuthorizationProviderConfig ())
4365 resource .Test (t , resource.TestCase {
@@ -52,7 +74,7 @@ func TestAccProjectRoleAssignmentResource(t *testing.T) {
5274 return err
5375 }
5476
55- members , err := client .ListMembers (context .TODO (), "project" , testutil .ProjectId ).Execute ()
77+ members , err := client .ListMembers (context .Background (), "project" , testutil .ProjectId ).Execute ()
5678 if err != nil {
5779 return err
5880 }
@@ -92,19 +114,106 @@ func TestAccProjectRoleAssignmentResource(t *testing.T) {
92114 },
93115 },
94116 })
117+
118+ resource .Test (t , resource.TestCase {
119+ ProtoV6ProviderFactories : testutil .TestAccProtoV6ProviderFactories ,
120+ Steps : []resource.TestStep {
121+ {
122+ ConfigVariables : testConfigVarsCustomRole ,
123+ Config : testutil .AuthorizationProviderConfig () + customRole ,
124+ Check : resource .ComposeAggregateTestCheckFunc (
125+ resource .TestCheckResourceAttr ("stackit_authorization_project_custom_role.custom-role" , "resource_id" , testutil .ConvertConfigVariable (testConfigVarsCustomRole ["project_id" ])),
126+ resource .TestCheckResourceAttr ("stackit_authorization_project_custom_role.custom-role" , "name" , testutil .ConvertConfigVariable (testConfigVarsCustomRole ["role_name" ])),
127+ resource .TestCheckResourceAttr ("stackit_authorization_project_custom_role.custom-role" , "description" , testutil .ConvertConfigVariable (testConfigVarsCustomRole ["role_description" ])),
128+ resource .TestCheckResourceAttr ("stackit_authorization_project_custom_role.custom-role" , "permissions.#" , "1" ),
129+ resource .TestCheckTypeSetElemAttr ("stackit_authorization_project_custom_role.custom-role" , "permissions.*" , testutil .ConvertConfigVariable (testConfigVarsCustomRole ["role_permissions_0" ])),
130+ resource .TestCheckResourceAttrSet ("stackit_authorization_project_custom_role.custom-role" , "role_id" ),
131+ ),
132+ },
133+ // Data source
134+ {
135+ ConfigVariables : testConfigVarsCustomRole ,
136+ Config : fmt .Sprintf (`
137+ %s
138+
139+ data "stackit_authorization_project_custom_role" "custom-role" {
140+ resource_id = stackit_authorization_project_custom_role.custom-role.resource_id
141+ role_id = stackit_authorization_project_custom_role.custom-role.role_id
142+ }
143+ ` ,
144+ testutil .AuthorizationProviderConfig ()+ customRole ,
145+ ),
146+ Check : resource .ComposeAggregateTestCheckFunc (
147+ resource .TestCheckResourceAttr ("data.stackit_authorization_project_custom_role.custom-role" , "resource_id" , testutil .ConvertConfigVariable (testConfigVarsCustomRole ["project_id" ])),
148+ resource .TestCheckResourceAttrPair (
149+ "stackit_authorization_project_custom_role.custom-role" , "resource_id" ,
150+ "data.stackit_authorization_project_custom_role.custom-role" , "resource_id" ,
151+ ),
152+ resource .TestCheckResourceAttrPair (
153+ "stackit_authorization_project_custom_role.custom-role" , "role_id" ,
154+ "data.stackit_authorization_project_custom_role.custom-role" , "role_id" ,
155+ ),
156+ resource .TestCheckResourceAttrPair (
157+ "stackit_authorization_project_custom_role.custom-role" , "name" ,
158+ "data.stackit_authorization_project_custom_role.custom-role" , "name" ,
159+ ),
160+ resource .TestCheckResourceAttrPair (
161+ "stackit_authorization_project_custom_role.custom-role" , "description" ,
162+ "data.stackit_authorization_project_custom_role.custom-role" , "description" ,
163+ ),
164+ resource .TestCheckResourceAttrPair (
165+ "stackit_authorization_project_custom_role.custom-role" , "permissions" ,
166+ "data.stackit_authorization_project_custom_role.custom-role" , "permissions" ,
167+ ),
168+ ),
169+ },
170+ // Import
171+ {
172+ ConfigVariables : testConfigVarsCustomRole ,
173+ ResourceName : "stackit_authorization_project_custom_role.custom-role" ,
174+ ImportStateIdFunc : func (s * terraform.State ) (string , error ) {
175+ r , ok := s .RootModule ().Resources ["stackit_authorization_project_custom_role.custom-role" ]
176+ if ! ok {
177+ return "" , fmt .Errorf ("couldn't find resource stackit_authorization_project_custom_role.custom-role" )
178+ }
179+ roleId , ok := r .Primary .Attributes ["role_id" ]
180+ if ! ok {
181+ return "" , fmt .Errorf ("couldn't find attribute role_id" )
182+ }
183+
184+ return fmt .Sprintf ("%s,%s" , testutil .ProjectId , roleId ), nil
185+ },
186+ ImportState : true ,
187+ ImportStateVerify : true ,
188+ },
189+ // Update
190+ {
191+ ConfigVariables : testConfigVarsCustomRoleUpdated ,
192+ Config : testutil .AuthorizationProviderConfig () + customRole ,
193+ Check : resource .ComposeAggregateTestCheckFunc (
194+ resource .TestCheckResourceAttr ("stackit_authorization_project_custom_role.custom-role" , "resource_id" , testutil .ConvertConfigVariable (testConfigVarsCustomRoleUpdated ["project_id" ])),
195+ resource .TestCheckResourceAttr ("stackit_authorization_project_custom_role.custom-role" , "name" , testutil .ConvertConfigVariable (testConfigVarsCustomRoleUpdated ["role_name" ])),
196+ resource .TestCheckResourceAttr ("stackit_authorization_project_custom_role.custom-role" , "description" , testutil .ConvertConfigVariable (testConfigVarsCustomRoleUpdated ["role_description" ])),
197+ resource .TestCheckResourceAttr ("stackit_authorization_project_custom_role.custom-role" , "permissions.#" , "1" ),
198+ resource .TestCheckTypeSetElemAttr ("stackit_authorization_project_custom_role.custom-role" , "permissions.*" , testutil .ConvertConfigVariable (testConfigVarsCustomRoleUpdated ["role_permissions_0" ])),
199+ resource .TestCheckResourceAttrSet ("stackit_authorization_project_custom_role.custom-role" , "role_id" ),
200+ ),
201+ },
202+ // Deletion is done by the framework implicitly
203+ },
204+ })
95205}
96206
97207func authApiClient () (* authorization.APIClient , error ) {
98208 var client * authorization.APIClient
99209
100210 var err error
101- if testutil .AuthorizationCustomEndpoint == "" {
102- client , err = authorization .NewAPIClient (
103- stackitSdkConfig .WithRegion ("eu01" ),
104- )
211+ if testutil .AuthorizationCustomEndpoint == "" || testutil .TokenCustomEndpoint == "" {
212+ client , err = authorization .NewAPIClient ()
105213 } else {
106214 client , err = authorization .NewAPIClient (
107215 stackitSdkConfig .WithEndpoint (testutil .AuthorizationCustomEndpoint ),
216+ stackitSdkConfig .WithTokenEndpoint (testutil .TokenCustomEndpoint ),
108217 )
109218 }
110219
0 commit comments