@@ -11,6 +11,7 @@ import (
1111 _ "embed"
1212
1313 "github.com/hashicorp/terraform-plugin-testing/config"
14+ "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1415 "github.com/hashicorp/terraform-plugin-testing/helper/resource"
1516 "github.com/hashicorp/terraform-plugin-testing/terraform"
1617 stackitSdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
@@ -33,12 +34,33 @@ var invalidRole string
3334//go:embed testfiles/organization-role.tf
3435var organizationRole string
3536
37+ //go:embed testfiles/custom-role.tf
38+ var customRole string
39+
3640var testConfigVars = config.Variables {
3741 "project_id" : config .StringVariable (testutil .ProjectId ),
3842 "test_service_account" : config .StringVariable (testutil .TestProjectServiceAccountEmail ),
3943 "organization_id" : config .StringVariable (testutil .OrganizationId ),
4044}
4145
46+ var testConfigVarsCustomRole = config.Variables {
47+ "project_id" : config .StringVariable (testutil .ProjectId ),
48+ "test_service_account" : config .StringVariable (testutil .TestProjectServiceAccountEmail ),
49+ "organization_id" : config .StringVariable (testutil .OrganizationId ),
50+ "role_name" : config .StringVariable (fmt .Sprintf ("tf-acc-%s" , acctest .RandStringFromCharSet (5 , acctest .CharSetAlpha ))),
51+ "role_description" : config .StringVariable ("Some description" ),
52+ "role_permissions_0" : config .StringVariable ("iam.role.list" ),
53+ }
54+
55+ var testConfigVarsCustomRoleUpdated = config.Variables {
56+ "project_id" : config .StringVariable (testutil .ProjectId ),
57+ "test_service_account" : config .StringVariable (testutil .TestProjectServiceAccountEmail ),
58+ "organization_id" : config .StringVariable (testutil .OrganizationId ),
59+ "role_name" : config .StringVariable (fmt .Sprintf ("tf-acc-%s" , acctest .RandStringFromCharSet (5 , acctest .CharSetAlpha ))),
60+ "role_description" : config .StringVariable ("Updated description" ),
61+ "role_permissions_0" : config .StringVariable ("iam.role.edit" ),
62+ }
63+
4264func TestAccProjectRoleAssignmentResource (t * testing.T ) {
4365 t .Log (testutil .AuthorizationProviderConfig ())
4466 resource .Test (t , resource.TestCase {
@@ -53,8 +75,7 @@ func TestAccProjectRoleAssignmentResource(t *testing.T) {
5375 return err
5476 }
5577
56- members , err := client .ListMembers (context .TODO (), "project" , testutil .ProjectId ).Execute ()
57-
78+ members , err := client .ListMembers (context .Background (), "project" , testutil .ProjectId ).Execute ()
5879 if err != nil {
5980 return err
6081 }
@@ -93,18 +114,105 @@ func TestAccProjectRoleAssignmentResource(t *testing.T) {
93114 },
94115 },
95116 })
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+ })
96205}
97206
98207func authApiClient () (* authorization.APIClient , error ) {
99208 var client * authorization.APIClient
100209 var err error
101- if testutil .AuthorizationCustomEndpoint == "" {
102- client , err = authorization .NewAPIClient (
103- stackitSdkConfig .WithRegion ("eu01" ),
104- )
210+ if testutil .AuthorizationCustomEndpoint == "" || testutil .TokenCustomEndpoint == "" {
211+ client , err = authorization .NewAPIClient ()
105212 } else {
106213 client , err = authorization .NewAPIClient (
107214 stackitSdkConfig .WithEndpoint (testutil .AuthorizationCustomEndpoint ),
215+ stackitSdkConfig .WithTokenEndpoint (testutil .TokenCustomEndpoint ),
108216 )
109217 }
110218 if err != nil {
0 commit comments