@@ -12,7 +12,10 @@ import (
12
12
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
13
13
"github.com/hashicorp/terraform-plugin-log/tflog"
14
14
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
15
+ "github.com/hashicorp/terraform-plugin-testing/knownvalue"
16
+ "github.com/hashicorp/terraform-plugin-testing/statecheck"
15
17
"github.com/hashicorp/terraform-plugin-testing/terraform"
18
+ "github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
16
19
)
17
20
18
21
func init () {
@@ -62,7 +65,6 @@ func testSweepCloudflareAccessMutualTLSCertificate(r string) error {
62
65
}
63
66
64
67
func TestAccCloudflareAccessMutualTLSBasic (t * testing.T ) {
65
- t .Skip (`FIXME: "DELETE, 409 Conflict, access.api.error.conflict: certificate has active associations"` )
66
68
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Access
67
69
// service does not yet support the API tokens and it results in
68
70
// misleading state error messages.
@@ -71,7 +73,7 @@ func TestAccCloudflareAccessMutualTLSBasic(t *testing.T) {
71
73
}
72
74
73
75
rnd := utils .GenerateRandomResourceName ()
74
- name := fmt .Sprintf ("cloudflare_zero_trust_access_mtls_certificate.%s" , rnd )
76
+ resourceName := fmt .Sprintf ("cloudflare_zero_trust_access_mtls_certificate.%s" , rnd )
75
77
cert := os .Getenv ("CLOUDFLARE_MUTUAL_TLS_CERTIFICATE" )
76
78
domain := os .Getenv ("CLOUDFLARE_DOMAIN" )
77
79
accountID := os .Getenv ("CLOUDFLARE_ACCOUNT_ID" )
@@ -86,12 +88,22 @@ func TestAccCloudflareAccessMutualTLSBasic(t *testing.T) {
86
88
Steps : []resource.TestStep {
87
89
{
88
90
Config : testAccessMutualTLSCertificateConfigBasic (rnd , cloudflare .AccountIdentifier (accountID ), cert , domain ),
89
- Check : resource .ComposeTestCheckFunc (
90
- resource .TestCheckResourceAttr (name , consts .AccountIDSchemaKey , accountID ),
91
- resource .TestCheckResourceAttr (name , "name" , rnd ),
92
- resource .TestCheckResourceAttrSet (name , "certificate" ),
93
- resource .TestCheckResourceAttr (name , "associated_hostnames.#" , "2" ),
94
- ),
91
+ ConfigStateChecks : []statecheck.StateCheck {
92
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New (consts .AccountIDSchemaKey ), knownvalue .StringExact (accountID )),
93
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("name" ), knownvalue .StringExact (rnd )),
94
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("certificate" ), knownvalue .NotNull ()),
95
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("associated_hostnames" ), knownvalue .ListSizeExact (2 )),
96
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("id" ), knownvalue .NotNull ()),
97
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("fingerprint" ), knownvalue .NotNull ()),
98
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("expires_on" ), knownvalue .NotNull ()),
99
+ },
100
+ },
101
+ {
102
+ ResourceName : resourceName ,
103
+ ImportState : true ,
104
+ ImportStateVerify : true ,
105
+ ImportStateIdPrefix : fmt .Sprintf ("accounts/%s/" , accountID ),
106
+ ImportStateVerifyIgnore : []string {"certificate" },
95
107
},
96
108
{
97
109
// Ensures no diff on last plan
@@ -100,12 +112,15 @@ func TestAccCloudflareAccessMutualTLSBasic(t *testing.T) {
100
112
},
101
113
{
102
114
Config : testAccessMutualTLSCertificateUpdated (rnd , cloudflare .AccountIdentifier (accountID ), cert ),
103
- Check : resource .ComposeTestCheckFunc (
104
- resource .TestCheckResourceAttr (name , consts .AccountIDSchemaKey , accountID ),
105
- resource .TestCheckResourceAttr (name , "name" , rnd ),
106
- resource .TestCheckResourceAttrSet (name , "certificate" ),
107
- resource .TestCheckResourceAttr (name , "associated_hostnames.#" , "0" ),
108
- ),
115
+ ConfigStateChecks : []statecheck.StateCheck {
116
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New (consts .AccountIDSchemaKey ), knownvalue .StringExact (accountID )),
117
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("name" ), knownvalue .StringExact (rnd )),
118
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("certificate" ), knownvalue .NotNull ()),
119
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("associated_hostnames" ), knownvalue .ListSizeExact (0 )),
120
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("id" ), knownvalue .NotNull ()),
121
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("fingerprint" ), knownvalue .NotNull ()),
122
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("expires_on" ), knownvalue .NotNull ()),
123
+ },
109
124
},
110
125
{
111
126
// Ensures no diff on last plan
@@ -117,7 +132,6 @@ func TestAccCloudflareAccessMutualTLSBasic(t *testing.T) {
117
132
}
118
133
119
134
func TestAccCloudflareAccessMutualTLSBasicWithZoneID (t * testing.T ) {
120
- t .Skip (`FIXME: "POST, 409 Conflict, access.api.error.conflict: certificate already exists"` )
121
135
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Access
122
136
// service does not yet support the API tokens and it results in
123
137
// misleading state error messages.
@@ -126,7 +140,7 @@ func TestAccCloudflareAccessMutualTLSBasicWithZoneID(t *testing.T) {
126
140
}
127
141
128
142
rnd := utils .GenerateRandomResourceName ()
129
- name := fmt .Sprintf ("cloudflare_zero_trust_access_mtls_certificate.%s" , rnd )
143
+ resourceName := fmt .Sprintf ("cloudflare_zero_trust_access_mtls_certificate.%s" , rnd )
130
144
cert := os .Getenv ("CLOUDFLARE_MUTUAL_TLS_CERTIFICATE" )
131
145
domain := os .Getenv ("CLOUDFLARE_DOMAIN" )
132
146
zoneID := os .Getenv ("CLOUDFLARE_ZONE_ID" )
@@ -140,12 +154,22 @@ func TestAccCloudflareAccessMutualTLSBasicWithZoneID(t *testing.T) {
140
154
Steps : []resource.TestStep {
141
155
{
142
156
Config : testAccessMutualTLSCertificateConfigBasic (rnd , cloudflare .ZoneIdentifier (zoneID ), cert , domain ),
143
- Check : resource .ComposeTestCheckFunc (
144
- resource .TestCheckResourceAttr (name , consts .ZoneIDSchemaKey , zoneID ),
145
- resource .TestCheckResourceAttr (name , "name" , rnd ),
146
- resource .TestCheckResourceAttrSet (name , "certificate" ),
147
- resource .TestCheckResourceAttr (name , "associated_hostnames.#" , "2" ),
148
- ),
157
+ ConfigStateChecks : []statecheck.StateCheck {
158
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New (consts .ZoneIDSchemaKey ), knownvalue .StringExact (zoneID )),
159
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("name" ), knownvalue .StringExact (rnd )),
160
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("certificate" ), knownvalue .NotNull ()),
161
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("associated_hostnames" ), knownvalue .ListSizeExact (2 )),
162
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("id" ), knownvalue .NotNull ()),
163
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("fingerprint" ), knownvalue .NotNull ()),
164
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("expires_on" ), knownvalue .NotNull ()),
165
+ },
166
+ },
167
+ {
168
+ ResourceName : resourceName ,
169
+ ImportState : true ,
170
+ ImportStateVerify : true ,
171
+ ImportStateIdPrefix : fmt .Sprintf ("zones/%s/" , zoneID ),
172
+ ImportStateVerifyIgnore : []string {"certificate" },
149
173
},
150
174
{
151
175
// Ensures no diff on last plan
@@ -154,12 +178,15 @@ func TestAccCloudflareAccessMutualTLSBasicWithZoneID(t *testing.T) {
154
178
},
155
179
{
156
180
Config : testAccessMutualTLSCertificateUpdated (rnd , cloudflare .ZoneIdentifier (zoneID ), cert ),
157
- Check : resource .ComposeTestCheckFunc (
158
- resource .TestCheckResourceAttr (name , consts .ZoneIDSchemaKey , zoneID ),
159
- resource .TestCheckResourceAttr (name , "name" , rnd ),
160
- resource .TestCheckResourceAttrSet (name , "certificate" ),
161
- resource .TestCheckResourceAttr (name , "associated_hostnames.#" , "0" ),
162
- ),
181
+ ConfigStateChecks : []statecheck.StateCheck {
182
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New (consts .ZoneIDSchemaKey ), knownvalue .StringExact (zoneID )),
183
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("name" ), knownvalue .StringExact (rnd )),
184
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("certificate" ), knownvalue .NotNull ()),
185
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("associated_hostnames" ), knownvalue .ListSizeExact (0 )),
186
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("id" ), knownvalue .NotNull ()),
187
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("fingerprint" ), knownvalue .NotNull ()),
188
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("expires_on" ), knownvalue .NotNull ()),
189
+ },
163
190
},
164
191
{
165
192
// Ensures no diff on last plan
@@ -170,6 +197,107 @@ func TestAccCloudflareAccessMutualTLSBasicWithZoneID(t *testing.T) {
170
197
})
171
198
}
172
199
200
+ func TestAccCloudflareAccessMutualTLSMinimal (t * testing.T ) {
201
+ // Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Access
202
+ // service does not yet support the API tokens and it results in
203
+ // misleading state error messages.
204
+ if os .Getenv ("CLOUDFLARE_API_TOKEN" ) != "" {
205
+ t .Setenv ("CLOUDFLARE_API_TOKEN" , "" )
206
+ }
207
+
208
+ rnd := utils .GenerateRandomResourceName ()
209
+ resourceName := fmt .Sprintf ("cloudflare_zero_trust_access_mtls_certificate.%s" , rnd )
210
+ cert := os .Getenv ("CLOUDFLARE_MUTUAL_TLS_CERTIFICATE" )
211
+ accountID := os .Getenv ("CLOUDFLARE_ACCOUNT_ID" )
212
+
213
+ resource .Test (t , resource.TestCase {
214
+ PreCheck : func () {
215
+ acctest .TestAccPreCheck (t )
216
+ acctest .TestAccPreCheck_AccountID (t )
217
+ },
218
+ ProtoV6ProviderFactories : acctest .TestAccProtoV6ProviderFactories ,
219
+ CheckDestroy : testAccCheckCloudflareAccessMutualTLSCertificateDestroy ,
220
+ Steps : []resource.TestStep {
221
+ {
222
+ Config : testAccessMutualTLSCertificateMinimal (rnd , cloudflare .AccountIdentifier (accountID ), cert ),
223
+ ConfigStateChecks : []statecheck.StateCheck {
224
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New (consts .AccountIDSchemaKey ), knownvalue .StringExact (accountID )),
225
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("name" ), knownvalue .StringExact (rnd )),
226
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("certificate" ), knownvalue .NotNull ()),
227
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("associated_hostnames" ), knownvalue .Null ()),
228
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("id" ), knownvalue .NotNull ()),
229
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("fingerprint" ), knownvalue .NotNull ()),
230
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("expires_on" ), knownvalue .NotNull ()),
231
+ },
232
+ },
233
+ {
234
+ ResourceName : resourceName ,
235
+ ImportState : true ,
236
+ ImportStateVerify : true ,
237
+ ImportStateIdPrefix : fmt .Sprintf ("accounts/%s/" , accountID ),
238
+ ImportStateVerifyIgnore : []string {"certificate" },
239
+ },
240
+ },
241
+ })
242
+ }
243
+
244
+ func TestAccCloudflareAccessMutualTLSNameUpdate (t * testing.T ) {
245
+ // Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Access
246
+ // service does not yet support the API tokens and it results in
247
+ // misleading state error messages.
248
+ if os .Getenv ("CLOUDFLARE_API_TOKEN" ) != "" {
249
+ t .Setenv ("CLOUDFLARE_API_TOKEN" , "" )
250
+ }
251
+
252
+ rnd := utils .GenerateRandomResourceName ()
253
+ resourceName := fmt .Sprintf ("cloudflare_zero_trust_access_mtls_certificate.%s" , rnd )
254
+ cert := os .Getenv ("CLOUDFLARE_MUTUAL_TLS_CERTIFICATE" )
255
+ domain := os .Getenv ("CLOUDFLARE_DOMAIN" )
256
+ accountID := os .Getenv ("CLOUDFLARE_ACCOUNT_ID" )
257
+
258
+ resource .Test (t , resource.TestCase {
259
+ PreCheck : func () {
260
+ acctest .TestAccPreCheck (t )
261
+ acctest .TestAccPreCheck_AccountID (t )
262
+ },
263
+ ProtoV6ProviderFactories : acctest .TestAccProtoV6ProviderFactories ,
264
+ CheckDestroy : testAccCheckCloudflareAccessMutualTLSCertificateDestroy ,
265
+ Steps : []resource.TestStep {
266
+ {
267
+ Config : testAccessMutualTLSCertificateConfigBasic (rnd , cloudflare .AccountIdentifier (accountID ), cert , domain ),
268
+ ConfigStateChecks : []statecheck.StateCheck {
269
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New (consts .AccountIDSchemaKey ), knownvalue .StringExact (accountID )),
270
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("name" ), knownvalue .StringExact (rnd )),
271
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("certificate" ), knownvalue .NotNull ()),
272
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("associated_hostnames" ), knownvalue .ListSizeExact (2 )),
273
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("id" ), knownvalue .NotNull ()),
274
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("fingerprint" ), knownvalue .NotNull ()),
275
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("expires_on" ), knownvalue .NotNull ()),
276
+ },
277
+ },
278
+ {
279
+ Config : testAccessMutualTLSCertificateNameUpdated (rnd , cloudflare .AccountIdentifier (accountID ), cert , domain ),
280
+ ConfigStateChecks : []statecheck.StateCheck {
281
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New (consts .AccountIDSchemaKey ), knownvalue .StringExact (accountID )),
282
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("name" ), knownvalue .StringExact (rnd + "-updated" )),
283
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("certificate" ), knownvalue .NotNull ()),
284
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("associated_hostnames" ), knownvalue .ListSizeExact (1 )),
285
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("id" ), knownvalue .NotNull ()),
286
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("fingerprint" ), knownvalue .NotNull ()),
287
+ statecheck .ExpectKnownValue (resourceName , tfjsonpath .New ("expires_on" ), knownvalue .NotNull ()),
288
+ },
289
+ },
290
+ {
291
+ ResourceName : resourceName ,
292
+ ImportState : true ,
293
+ ImportStateVerify : true ,
294
+ ImportStateIdPrefix : fmt .Sprintf ("accounts/%s/" , accountID ),
295
+ ImportStateVerifyIgnore : []string {"certificate" },
296
+ },
297
+ },
298
+ })
299
+ }
300
+
173
301
func testAccCheckCloudflareAccessMutualTLSCertificateDestroy (s * terraform.State ) error {
174
302
client , clientErr := acctest .SharedV1Client () // TODO(terraform): replace with SharedV2Clent
175
303
if clientErr != nil {
@@ -206,3 +334,11 @@ func testAccessMutualTLSCertificateConfigBasic(rnd string, identifier *cloudflar
206
334
func testAccessMutualTLSCertificateUpdated (rnd string , identifier * cloudflare.ResourceContainer , cert string ) string {
207
335
return acctest .LoadTestCase ("accessmutualtlscertificateupdated.tf" , rnd , identifier .Type , identifier .Identifier , cert )
208
336
}
337
+
338
+ func testAccessMutualTLSCertificateMinimal (rnd string , identifier * cloudflare.ResourceContainer , cert string ) string {
339
+ return acctest .LoadTestCase ("accessmutualtlscertificateminimal.tf" , rnd , identifier .Type , identifier .Identifier , cert )
340
+ }
341
+
342
+ func testAccessMutualTLSCertificateNameUpdated (rnd string , identifier * cloudflare.ResourceContainer , cert , domain string ) string {
343
+ return acctest .LoadTestCase ("accessmutualtlscertificatenameupdated.tf" , rnd , identifier .Type , identifier .Identifier , cert , domain )
344
+ }
0 commit comments