6
6
"github.com/cloudflare/terraform-provider-cloudflare/internal/acctest"
7
7
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
8
8
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9
+ "github.com/hashicorp/terraform-plugin-testing/plancheck"
9
10
)
10
11
11
12
func TestAccAPIToken_Basic (t * testing.T ) {
@@ -21,12 +22,14 @@ func TestAccAPIToken_Basic(t *testing.T) {
21
22
Config : testAccCloudflareAPITokenWithoutCondition (rnd , rnd , permissionID ),
22
23
Check : resource .ComposeTestCheckFunc (
23
24
resource .TestCheckResourceAttr (resourceID , "name" , rnd ),
25
+ resource .TestCheckResourceAttr (resourceID , "policies.0.permission_groups.0.id" , permissionID ),
24
26
),
25
27
},
26
28
{
27
29
Config : testAccCloudflareAPITokenWithoutCondition (rnd , rnd + "-updated" , permissionID ),
28
30
Check : resource .ComposeTestCheckFunc (
29
31
resource .TestCheckResourceAttr (resourceID , "name" , rnd + "-updated" ),
32
+ resource .TestCheckResourceAttr (resourceID , "policies.0.permission_groups.0.id" , permissionID ),
30
33
),
31
34
},
32
35
},
@@ -132,3 +135,67 @@ func TestAccAPIToken_TokenTTL(t *testing.T) {
132
135
func testAccCloudflareAPITokenWithTTL (rnd string , permissionID string ) string {
133
136
return acctest .LoadTestCase ("apitokenwithttl.tf" , rnd , permissionID )
134
137
}
138
+
139
+ func TestAccAPIToken_PermissionGroupOrder (t * testing.T ) {
140
+ rnd := utils .GenerateRandomResourceName ()
141
+ name := "cloudflare_api_token." + rnd
142
+ permissionID1 := "82e64a83756745bbbb1c9c2701bf816b" // DNS read
143
+ permissionID2 := "e199d584e69344eba202452019deafe3" // Disable ESC read
144
+
145
+ resource .Test (t , resource.TestCase {
146
+ PreCheck : func () { acctest .TestAccPreCheck (t ) },
147
+ ProtoV6ProviderFactories : acctest .TestAccProtoV6ProviderFactories ,
148
+ Steps : []resource.TestStep {
149
+ {
150
+ Config : acctest .LoadTestCase ("api_token-permissiongroup-order.tf" , rnd , permissionID1 , permissionID2 ),
151
+ Check : resource .ComposeTestCheckFunc (
152
+ resource .TestCheckResourceAttr (name , "name" , rnd ),
153
+ resource .TestCheckResourceAttr (name , "policies.0.permission_groups.0.id" , permissionID1 ),
154
+ resource .TestCheckResourceAttr (name , "policies.0.permission_groups.1.id" , permissionID2 ),
155
+ ),
156
+ },
157
+ {
158
+ Config : acctest .LoadTestCase ("api_token-permissiongroup-order.tf" , rnd , permissionID2 , permissionID1 ),
159
+ // changing the order of permission groups should not affect plan
160
+ ConfigPlanChecks : resource.ConfigPlanChecks {
161
+ PreApply : []plancheck.PlanCheck {
162
+ plancheck .ExpectEmptyPlan (),
163
+ },
164
+ },
165
+ },
166
+ },
167
+ })
168
+
169
+ resource .Test (t , resource.TestCase {
170
+ PreCheck : func () { acctest .TestAccPreCheck (t ) },
171
+ ProtoV6ProviderFactories : acctest .TestAccProtoV6ProviderFactories ,
172
+ Steps : []resource.TestStep {
173
+ {
174
+ Config : acctest .LoadTestCase ("api_token-permissiongroup-order.tf" , rnd , permissionID2 , permissionID1 ),
175
+ Check : resource .ComposeTestCheckFunc (
176
+ resource .TestCheckResourceAttr (name , "name" , rnd ),
177
+ resource .TestCheckResourceAttr (name , "policies.0.permission_groups.0.id" , permissionID1 ),
178
+ resource .TestCheckResourceAttr (name , "policies.0.permission_groups.1.id" , permissionID2 ),
179
+ ),
180
+ },
181
+ {
182
+ Config : acctest .LoadTestCase ("api_token-permissiongroup-order.tf" , rnd , permissionID2 , permissionID1 ),
183
+ // re-applying same change does not produce drift
184
+ ConfigPlanChecks : resource.ConfigPlanChecks {
185
+ PreApply : []plancheck.PlanCheck {
186
+ plancheck .ExpectEmptyPlan (),
187
+ },
188
+ },
189
+ },
190
+ {
191
+ Config : acctest .LoadTestCase ("api_token-permissiongroup-order.tf" , rnd , permissionID1 , permissionID2 ),
192
+ // changing the order of permission groups should not affect plan
193
+ ConfigPlanChecks : resource.ConfigPlanChecks {
194
+ PreApply : []plancheck.PlanCheck {
195
+ plancheck .ExpectEmptyPlan (),
196
+ },
197
+ },
198
+ },
199
+ },
200
+ })
201
+ }
0 commit comments