@@ -14,39 +14,34 @@ import (
14
14
)
15
15
16
16
func TestAccAWSAPIGatewayDomainName_CertificateArn (t * testing.T ) {
17
- certificateArn := os .Getenv ("AWS_API_GATEWAY_DOMAIN_NAME_CERTIFICATE_ARN" )
18
- if certificateArn == "" {
19
- t .Skip (
20
- "Environment variable AWS_API_GATEWAY_DOMAIN_NAME_CERTIFICATE_ARN is not set. " +
21
- "This environment variable must be set to the ARN of " +
22
- "an ISSUED ACM certificate in us-east-1 to enable this test." )
23
- }
24
-
25
- // This test must always run in us-east-1
26
- // BadRequestException: Invalid certificate ARN: arn:aws:acm:us-west-2:123456789012:certificate/xxxxx. Certificate must be in 'us-east-1'.
27
- oldvar := os .Getenv ("AWS_DEFAULT_REGION" )
28
- os .Setenv ("AWS_DEFAULT_REGION" , "us-east-1" )
29
- defer os .Setenv ("AWS_DEFAULT_REGION" , oldvar )
17
+ rootDomain := testAccAwsAcmCertificateDomainFromEnv (t )
18
+ domain := testAccAwsAcmCertificateRandomSubDomain (rootDomain )
30
19
31
20
var domainName apigateway.DomainName
21
+ acmCertificateResourceName := "aws_acm_certificate.test"
32
22
resourceName := "aws_api_gateway_domain_name.test"
33
- rName := fmt .Sprintf ("tf-acc-%s.terraformtest.com" , acctest .RandString (8 ))
34
23
35
24
resource .ParallelTest (t , resource.TestCase {
36
- PreCheck : func () { testAccPreCheck (t ) },
37
- Providers : testAccProviders ,
38
- CheckDestroy : testAccCheckAWSAPIGatewayDomainNameDestroy ,
25
+ PreCheck : func () { testAccPreCheck ( t ); testAccPreCheckApigatewayEdgeDomainName (t ) },
26
+ ProviderFactories : testAccProviderFactories ,
27
+ CheckDestroy : testAccCheckAWSAPIGatewayDomainNameDestroy ,
39
28
Steps : []resource.TestStep {
40
29
{
41
- Config : testAccAWSAPIGatewayDomainNameConfig_CertificateArn (rName , certificateArn ),
30
+ Config : testAccAWSAPIGatewayDomainNameConfig_CertificateArn (rootDomain , domain ),
42
31
Check : resource .ComposeTestCheckFunc (
43
32
testAccCheckAWSAPIGatewayDomainNameExists (resourceName , & domainName ),
44
- testAccMatchResourceAttrRegionalARNNoAccount (resourceName , "arn" , "apigateway" , regexp .MustCompile (`/domainnames/+.` )),
45
- resource .TestCheckResourceAttrSet (resourceName , "cloudfront_domain_name" ),
33
+ testAccCheckResourceAttrRegionalARNApigatewayEdgeDomainName (resourceName , "arn" , "apigateway" , domain ),
34
+ resource .TestCheckResourceAttrPair (resourceName , "certificate_arn" , acmCertificateResourceName , "arn" ),
35
+ resource .TestMatchResourceAttr (resourceName , "cloudfront_domain_name" , regexp .MustCompile (`[a-z0-9]+.cloudfront.net` )),
46
36
resource .TestCheckResourceAttr (resourceName , "cloudfront_zone_id" , "Z2FDTNDATAQYW2" ),
47
- resource .TestCheckResourceAttr (resourceName , "domain_name" , rName ),
37
+ resource .TestCheckResourceAttrPair (resourceName , "domain_name" , acmCertificateResourceName , "domain_name" ),
48
38
),
49
39
},
40
+ {
41
+ ResourceName : resourceName ,
42
+ ImportState : true ,
43
+ ImportStateVerify : true ,
44
+ },
50
45
},
51
46
})
52
47
}
@@ -131,9 +126,9 @@ func TestAccAWSAPIGatewayDomainName_RegionalCertificateArn(t *testing.T) {
131
126
Config : testAccAWSAPIGatewayDomainNameConfig_RegionalCertificateArn (rName , key , certificate ),
132
127
Check : resource .ComposeTestCheckFunc (
133
128
testAccCheckAWSAPIGatewayDomainNameExists (resourceName , & domainName ),
134
- testAccMatchResourceAttrRegionalARNNoAccount (resourceName , "arn" , "apigateway" , regexp . MustCompile ( `/domainnames/+.` ) ),
129
+ testAccCheckResourceAttrRegionalARNApigatewayRegionalDomainName (resourceName , "arn" , "apigateway" , rName ),
135
130
resource .TestCheckResourceAttr (resourceName , "domain_name" , rName ),
136
- resource . TestMatchResourceAttr (resourceName , "regional_domain_name" , regexp .MustCompile (`.*\.execute-api\..* ` )),
131
+ testAccMatchResourceAttrRegionalHostname (resourceName , "regional_domain_name" , "execute-api" , regexp .MustCompile (`d-[a-z0-9]+ ` )),
137
132
resource .TestMatchResourceAttr (resourceName , "regional_zone_id" , regexp .MustCompile (`^Z` )),
138
133
),
139
134
},
@@ -174,14 +169,14 @@ func TestAccAWSAPIGatewayDomainName_RegionalCertificateName(t *testing.T) {
174
169
Config : testAccAWSAPIGatewayDomainNameConfig_RegionalCertificateName (rName , key , certificate , caCertificate ),
175
170
Check : resource .ComposeTestCheckFunc (
176
171
testAccCheckAWSAPIGatewayDomainNameExists (resourceName , & domainName ),
177
- testAccMatchResourceAttrRegionalARNNoAccount (resourceName , "arn" , "apigateway" , regexp . MustCompile ( `/domainnames/+.` ) ),
172
+ testAccCheckResourceAttrRegionalARNApigatewayRegionalDomainName (resourceName , "arn" , "apigateway" , rName ),
178
173
resource .TestCheckResourceAttr (resourceName , "certificate_body" , certificate ),
179
174
resource .TestCheckResourceAttr (resourceName , "certificate_chain" , caCertificate ),
180
175
resource .TestCheckResourceAttr (resourceName , "certificate_name" , "tf-acc-apigateway-domain-name" ),
181
176
resource .TestCheckResourceAttr (resourceName , "certificate_private_key" , key ),
182
177
resource .TestCheckResourceAttrSet (resourceName , "certificate_upload_date" ),
183
178
resource .TestCheckResourceAttr (resourceName , "domain_name" , rName ),
184
- resource . TestMatchResourceAttr (resourceName , "regional_domain_name" , regexp .MustCompile (`.*\.execute-api\..* ` )),
179
+ testAccMatchResourceAttrRegionalHostname (resourceName , "regional_domain_name" , "execute-api" , regexp .MustCompile (`d-[a-z0-9]+ ` )),
185
180
resource .TestMatchResourceAttr (resourceName , "regional_zone_id" , regexp .MustCompile (`^Z` )),
186
181
),
187
182
},
@@ -206,7 +201,6 @@ func TestAccAWSAPIGatewayDomainName_SecurityPolicy(t *testing.T) {
206
201
Config : testAccAWSAPIGatewayDomainNameConfig_SecurityPolicy (rName , key , certificate , apigateway .SecurityPolicyTls12 ),
207
202
Check : resource .ComposeTestCheckFunc (
208
203
testAccCheckAWSAPIGatewayDomainNameExists (resourceName , & domainName ),
209
- testAccMatchResourceAttrRegionalARNNoAccount (resourceName , "arn" , "apigateway" , regexp .MustCompile (`/domainnames/+.` )),
210
204
resource .TestCheckResourceAttr (resourceName , "security_policy" , apigateway .SecurityPolicyTls12 ),
211
205
),
212
206
},
@@ -236,7 +230,6 @@ func TestAccAWSAPIGatewayDomainName_Tags(t *testing.T) {
236
230
Config : testAccAWSAPIGatewayDomainNameConfigTags1 (rName , key , certificate , "key1" , "value1" ),
237
231
Check : resource .ComposeTestCheckFunc (
238
232
testAccCheckAWSAPIGatewayDomainNameExists (resourceName , & domainName ),
239
- testAccMatchResourceAttrRegionalARNNoAccount (resourceName , "arn" , "apigateway" , regexp .MustCompile (`/domainnames/+.` )),
240
233
resource .TestCheckResourceAttr (resourceName , "tags.%" , "1" ),
241
234
resource .TestCheckResourceAttr (resourceName , "tags.key1" , "value1" ),
242
235
),
@@ -348,17 +341,64 @@ func testAccCheckAWSAPIGatewayDomainNameDestroy(s *terraform.State) error {
348
341
return nil
349
342
}
350
343
351
- func testAccAWSAPIGatewayDomainNameConfig_CertificateArn (domainName , certificateArn string ) string {
352
- return fmt .Sprintf (`
344
+ func testAccAWSAPIGatewayDomainNameConfig_CertificateArn (rootDomain string , domain string ) string {
345
+ return composeConfig (
346
+ testAccApigatewayEdgeDomainNameRegionProviderConfig (),
347
+ fmt .Sprintf (`
348
+ data "aws_route53_zone" "test" {
349
+ name = %[1]q
350
+ private_zone = false
351
+ }
352
+
353
+ resource "aws_acm_certificate" "test" {
354
+ domain_name = %[2]q
355
+ validation_method = "DNS"
356
+ }
357
+
358
+ #
359
+ # for_each acceptance testing requires:
360
+ # https://github.com/hashicorp/terraform-plugin-sdk/issues/536
361
+ #
362
+ # resource "aws_route53_record" "test" {
363
+ # for_each = {
364
+ # for dvo in aws_acm_certificate.test.domain_validation_options: dvo.domain_name => {
365
+ # name = dvo.resource_record_name
366
+ # record = dvo.resource_record_value
367
+ # type = dvo.resource_record_type
368
+ # }
369
+ # }
370
+
371
+ # allow_overwrite = true
372
+ # name = each.value.name
373
+ # records = [each.value.record]
374
+ # ttl = 60
375
+ # type = each.value.type
376
+ # zone_id = data.aws_route53_zone.test.zone_id
377
+ # }
378
+
379
+ resource "aws_route53_record" "test" {
380
+ allow_overwrite = true
381
+ name = tolist(aws_acm_certificate.test.domain_validation_options)[0].resource_record_name
382
+ records = [tolist(aws_acm_certificate.test.domain_validation_options)[0].resource_record_value]
383
+ ttl = 60
384
+ type = tolist(aws_acm_certificate.test.domain_validation_options)[0].resource_record_type
385
+ zone_id = data.aws_route53_zone.test.zone_id
386
+ }
387
+
388
+ resource "aws_acm_certificate_validation" "test" {
389
+ certificate_arn = aws_acm_certificate.test.arn
390
+ validation_record_fqdns = [aws_route53_record.test.fqdn]
391
+ }
392
+
353
393
resource "aws_api_gateway_domain_name" "test" {
354
- domain_name = "%s"
355
- certificate_arn = "%s"
394
+ domain_name = aws_acm_certificate.test.domain_name
395
+ certificate_arn = aws_acm_certificate_validation.test.certificate_arn
356
396
357
397
endpoint_configuration {
358
398
types = ["EDGE"]
359
399
}
360
400
}
361
- ` , domainName , certificateArn )
401
+ ` , rootDomain , domain ) )
362
402
}
363
403
364
404
func testAccAWSAPIGatewayDomainNameConfig_CertificateName (domainName , key , certificate , chainCertificate string ) string {
0 commit comments