1
1
package api_token_test
2
2
3
3
import (
4
+ "fmt"
4
5
"testing"
5
6
6
7
"github.com/cloudflare/terraform-provider-cloudflare/internal/acctest"
7
8
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
8
9
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10
+ "github.com/hashicorp/terraform-plugin-testing/plancheck"
9
11
)
10
12
11
13
func TestAccAPIToken_Basic (t * testing.T ) {
12
14
rnd := utils .GenerateRandomResourceName ()
13
15
resourceID := "cloudflare_api_token." + rnd
14
16
permissionID := "82e64a83756745bbbb1c9c2701bf816b" // DNS read
15
17
18
+ var policyId string
19
+
16
20
resource .Test (t , resource.TestCase {
17
- PreCheck : func () { acctest .TestAccPreCheck (t ) },
21
+ PreCheck : func () { acctest .TestAccPreCheck_APIToken (t ) },
18
22
ProtoV6ProviderFactories : acctest .TestAccProtoV6ProviderFactories ,
19
23
Steps : []resource.TestStep {
20
24
{
21
25
Config : testAccCloudflareAPITokenWithoutCondition (rnd , rnd , permissionID ),
22
26
Check : resource .ComposeTestCheckFunc (
23
27
resource .TestCheckResourceAttr (resourceID , "name" , rnd ),
28
+ resource .TestCheckResourceAttrSet (resourceID , "policies.0.id" ),
29
+ resource .TestCheckResourceAttrWith (resourceID , "policies.0.id" , func (value string ) error {
30
+ policyId = value
31
+ return nil
32
+ }),
33
+ resource .TestCheckResourceAttr (resourceID , "policies.0.permission_groups.0.id" , permissionID ),
24
34
),
25
35
},
26
36
{
27
37
Config : testAccCloudflareAPITokenWithoutCondition (rnd , rnd + "-updated" , permissionID ),
28
38
Check : resource .ComposeTestCheckFunc (
29
39
resource .TestCheckResourceAttr (resourceID , "name" , rnd + "-updated" ),
40
+ resource .TestCheckResourceAttrSet (resourceID , "policies.0.id" ),
41
+ resource .TestCheckResourceAttrWith (resourceID , "policies.0.id" , func (value string ) error {
42
+ if value != policyId {
43
+ return fmt .Errorf ("policy ID changed from %s to %s" , policyId , value )
44
+ }
45
+ return nil
46
+ }),
47
+ resource .TestCheckResourceAttr (resourceID , "policies.0.permission_groups.0.id" , permissionID ),
30
48
),
31
49
},
32
50
},
@@ -39,7 +57,7 @@ func TestAccAPIToken_DoesNotSetConditions(t *testing.T) {
39
57
permissionID := "82e64a83756745bbbb1c9c2701bf816b" // DNS read
40
58
41
59
resource .Test (t , resource.TestCase {
42
- PreCheck : func () { acctest .TestAccPreCheck (t ) },
60
+ PreCheck : func () { acctest .TestAccPreCheck_APIToken (t ) },
43
61
ProtoV6ProviderFactories : acctest .TestAccProtoV6ProviderFactories ,
44
62
Steps : []resource.TestStep {
45
63
{
@@ -64,7 +82,7 @@ func TestAccAPIToken_SetIndividualCondition(t *testing.T) {
64
82
permissionID := "82e64a83756745bbbb1c9c2701bf816b" // DNS read
65
83
66
84
resource .Test (t , resource.TestCase {
67
- PreCheck : func () { acctest .TestAccPreCheck (t ) },
85
+ PreCheck : func () { acctest .TestAccPreCheck_APIToken (t ) },
68
86
ProtoV6ProviderFactories : acctest .TestAccProtoV6ProviderFactories ,
69
87
Steps : []resource.TestStep {
70
88
{
@@ -89,7 +107,7 @@ func TestAccAPIToken_SetAllCondition(t *testing.T) {
89
107
permissionID := "82e64a83756745bbbb1c9c2701bf816b" // DNS read
90
108
91
109
resource .Test (t , resource.TestCase {
92
- PreCheck : func () { acctest .TestAccPreCheck (t ) },
110
+ PreCheck : func () { acctest .TestAccPreCheck_APIToken (t ) },
93
111
ProtoV6ProviderFactories : acctest .TestAccProtoV6ProviderFactories ,
94
112
Steps : []resource.TestStep {
95
113
{
@@ -114,7 +132,7 @@ func TestAccAPIToken_TokenTTL(t *testing.T) {
114
132
permissionID := "82e64a83756745bbbb1c9c2701bf816b" // DNS read
115
133
116
134
resource .Test (t , resource.TestCase {
117
- PreCheck : func () { acctest .TestAccPreCheck (t ) },
135
+ PreCheck : func () { acctest .TestAccPreCheck_APIToken (t ) },
118
136
ProtoV6ProviderFactories : acctest .TestAccProtoV6ProviderFactories ,
119
137
Steps : []resource.TestStep {
120
138
{
@@ -132,3 +150,67 @@ func TestAccAPIToken_TokenTTL(t *testing.T) {
132
150
func testAccCloudflareAPITokenWithTTL (rnd string , permissionID string ) string {
133
151
return acctest .LoadTestCase ("apitokenwithttl.tf" , rnd , permissionID )
134
152
}
153
+
154
+ func TestAccAPIToken_PermissionGroupOrder (t * testing.T ) {
155
+ rnd := utils .GenerateRandomResourceName ()
156
+ name := "cloudflare_api_token." + rnd
157
+ permissionID1 := "82e64a83756745bbbb1c9c2701bf816b" // DNS read
158
+ permissionID2 := "e199d584e69344eba202452019deafe3" // Disable ESC read
159
+
160
+ resource .Test (t , resource.TestCase {
161
+ PreCheck : func () { acctest .TestAccPreCheck_APIToken (t ) },
162
+ ProtoV6ProviderFactories : acctest .TestAccProtoV6ProviderFactories ,
163
+ Steps : []resource.TestStep {
164
+ {
165
+ Config : acctest .LoadTestCase ("api_token-permissiongroup-order.tf" , rnd , permissionID1 , permissionID2 ),
166
+ Check : resource .ComposeTestCheckFunc (
167
+ resource .TestCheckResourceAttr (name , "name" , rnd ),
168
+ resource .TestCheckResourceAttr (name , "policies.0.permission_groups.0.id" , permissionID1 ),
169
+ resource .TestCheckResourceAttr (name , "policies.0.permission_groups.1.id" , permissionID2 ),
170
+ ),
171
+ },
172
+ {
173
+ Config : acctest .LoadTestCase ("api_token-permissiongroup-order.tf" , rnd , permissionID2 , permissionID1 ),
174
+ // changing the order of permission groups should not affect plan
175
+ ConfigPlanChecks : resource.ConfigPlanChecks {
176
+ PreApply : []plancheck.PlanCheck {
177
+ plancheck .ExpectEmptyPlan (),
178
+ },
179
+ },
180
+ },
181
+ },
182
+ })
183
+
184
+ resource .Test (t , resource.TestCase {
185
+ PreCheck : func () { acctest .TestAccPreCheck_APIToken (t ) },
186
+ ProtoV6ProviderFactories : acctest .TestAccProtoV6ProviderFactories ,
187
+ Steps : []resource.TestStep {
188
+ {
189
+ Config : acctest .LoadTestCase ("api_token-permissiongroup-order.tf" , rnd , permissionID2 , permissionID1 ),
190
+ Check : resource .ComposeTestCheckFunc (
191
+ resource .TestCheckResourceAttr (name , "name" , rnd ),
192
+ resource .TestCheckResourceAttr (name , "policies.0.permission_groups.0.id" , permissionID1 ),
193
+ resource .TestCheckResourceAttr (name , "policies.0.permission_groups.1.id" , permissionID2 ),
194
+ ),
195
+ },
196
+ {
197
+ Config : acctest .LoadTestCase ("api_token-permissiongroup-order.tf" , rnd , permissionID2 , permissionID1 ),
198
+ // re-applying same change does not produce drift
199
+ ConfigPlanChecks : resource.ConfigPlanChecks {
200
+ PreApply : []plancheck.PlanCheck {
201
+ plancheck .ExpectEmptyPlan (),
202
+ },
203
+ },
204
+ },
205
+ {
206
+ Config : acctest .LoadTestCase ("api_token-permissiongroup-order.tf" , rnd , permissionID1 , permissionID2 ),
207
+ // changing the order of permission groups should not affect plan
208
+ ConfigPlanChecks : resource.ConfigPlanChecks {
209
+ PreApply : []plancheck.PlanCheck {
210
+ plancheck .ExpectEmptyPlan (),
211
+ },
212
+ },
213
+ },
214
+ },
215
+ })
216
+ }
0 commit comments