@@ -3,7 +3,6 @@ package aws
3
3
import (
4
4
"fmt"
5
5
"log"
6
- "os"
7
6
"regexp"
8
7
"testing"
9
8
@@ -262,14 +261,12 @@ func TestAccAWSAPIGatewayV2DomainName_UpdateCertificate(t *testing.T) {
262
261
}
263
262
264
263
func TestAccAWSAPIGatewayV2DomainName_MutualTlsAuthentication (t * testing.T ) {
265
- key := "AWS_APIGATEWAYV2_CERTIFICATE_DOMAIN_NAME"
266
- domainName := os .Getenv (key )
267
- if domainName == "" {
268
- t .Skipf ("Environment variable %s is not set" , key )
269
- }
264
+ rootDomain := testAccAwsAcmCertificateDomainFromEnv (t )
265
+ domain := testAccAwsAcmCertificateRandomSubDomain (rootDomain )
270
266
271
267
var v apigatewayv2.GetDomainNameOutput
272
268
resourceName := "aws_apigatewayv2_domain_name.test"
269
+ acmCertificateResourceName := "aws_acm_certificate.test"
273
270
s3BucketObjectResourceName := "aws_s3_bucket_object.test"
274
271
rName := acctest .RandomWithPrefix ("tf-acc-test" )
275
272
@@ -279,13 +276,13 @@ func TestAccAWSAPIGatewayV2DomainName_MutualTlsAuthentication(t *testing.T) {
279
276
CheckDestroy : testAccCheckAWSAPIGatewayV2DomainNameDestroy ,
280
277
Steps : []resource.TestStep {
281
278
{
282
- Config : testAccAWSAPIGatewayV2DomainNameConfigMututalTlsAuthentication (rName , domainName ),
279
+ Config : testAccAWSAPIGatewayV2DomainNameConfigMututalTlsAuthentication (rootDomain , domain , rName ),
283
280
Check : resource .ComposeTestCheckFunc (
284
281
testAccCheckAWSAPIGatewayV2DomainNameExists (resourceName , & v ),
285
282
testAccMatchResourceAttrRegionalARNNoAccount (resourceName , "arn" , "apigateway" , regexp .MustCompile (`/domainnames/.+` )),
286
- resource .TestCheckResourceAttr (resourceName , "domain_name" , domainName ),
283
+ resource .TestCheckResourceAttrPair (resourceName , "domain_name" , acmCertificateResourceName , "domain_name" ),
287
284
resource .TestCheckResourceAttr (resourceName , "domain_name_configuration.#" , "1" ),
288
- resource .TestCheckResourceAttrSet (resourceName , "domain_name_configuration.0.certificate_arn" ),
285
+ resource .TestCheckResourceAttrPair (resourceName , "domain_name_configuration.0.certificate_arn" , acmCertificateResourceName , "arn " ),
289
286
resource .TestCheckResourceAttr (resourceName , "domain_name_configuration.0.endpoint_type" , "REGIONAL" ),
290
287
resource .TestCheckResourceAttrSet (resourceName , "domain_name_configuration.0.hosted_zone_id" ),
291
288
resource .TestCheckResourceAttr (resourceName , "domain_name_configuration.0.security_policy" , "TLS_1_2" ),
@@ -297,13 +294,13 @@ func TestAccAWSAPIGatewayV2DomainName_MutualTlsAuthentication(t *testing.T) {
297
294
),
298
295
},
299
296
{
300
- Config : testAccAWSAPIGatewayV2DomainNameConfigMututalTlsAuthenticationUpdated (rName , domainName ),
297
+ Config : testAccAWSAPIGatewayV2DomainNameConfigMututalTlsAuthenticationUpdated (rootDomain , domain , rName ),
301
298
Check : resource .ComposeTestCheckFunc (
302
299
testAccCheckAWSAPIGatewayV2DomainNameExists (resourceName , & v ),
303
300
testAccMatchResourceAttrRegionalARNNoAccount (resourceName , "arn" , "apigateway" , regexp .MustCompile (`/domainnames/.+` )),
304
- resource .TestCheckResourceAttr (resourceName , "domain_name" , domainName ),
301
+ resource .TestCheckResourceAttrPair (resourceName , "domain_name" , acmCertificateResourceName , "domain_name" ),
305
302
resource .TestCheckResourceAttr (resourceName , "domain_name_configuration.#" , "1" ),
306
- resource .TestCheckResourceAttrSet (resourceName , "domain_name_configuration.0.certificate_arn" ),
303
+ resource .TestCheckResourceAttrPair (resourceName , "domain_name_configuration.0.certificate_arn" , acmCertificateResourceName , "arn " ),
307
304
resource .TestCheckResourceAttr (resourceName , "domain_name_configuration.0.endpoint_type" , "REGIONAL" ),
308
305
resource .TestCheckResourceAttrSet (resourceName , "domain_name_configuration.0.hosted_zone_id" ),
309
306
resource .TestCheckResourceAttr (resourceName , "domain_name_configuration.0.security_policy" , "TLS_1_2" ),
@@ -321,13 +318,13 @@ func TestAccAWSAPIGatewayV2DomainName_MutualTlsAuthentication(t *testing.T) {
321
318
},
322
319
// Test disabling mutual TLS authentication.
323
320
{
324
- Config : testAccAWSAPIGatewayV2DomainNameConfigMututalTlsAuthenticationMissing (domainName ),
321
+ Config : testAccAWSAPIGatewayV2DomainNameConfigMututalTlsAuthenticationMissing (rootDomain , domain ),
325
322
Check : resource .ComposeTestCheckFunc (
326
323
testAccCheckAWSAPIGatewayV2DomainNameExists (resourceName , & v ),
327
324
testAccMatchResourceAttrRegionalARNNoAccount (resourceName , "arn" , "apigateway" , regexp .MustCompile (`/domainnames/.+` )),
328
- resource .TestCheckResourceAttr (resourceName , "domain_name" , domainName ),
325
+ resource .TestCheckResourceAttrPair (resourceName , "domain_name" , acmCertificateResourceName , "domain_name" ),
329
326
resource .TestCheckResourceAttr (resourceName , "domain_name_configuration.#" , "1" ),
330
- resource .TestCheckResourceAttrSet (resourceName , "domain_name_configuration.0.certificate_arn" ),
327
+ resource .TestCheckResourceAttrPair (resourceName , "domain_name_configuration.0.certificate_arn" , acmCertificateResourceName , "arn " ),
331
328
resource .TestCheckResourceAttr (resourceName , "domain_name_configuration.0.endpoint_type" , "REGIONAL" ),
332
329
resource .TestCheckResourceAttrSet (resourceName , "domain_name_configuration.0.hosted_zone_id" ),
333
330
resource .TestCheckResourceAttr (resourceName , "domain_name_configuration.0.security_policy" , "TLS_1_2" ),
@@ -405,8 +402,58 @@ resource "aws_acm_certificate" "test" {
405
402
` , rName , certificate , key , count )
406
403
}
407
404
405
+ func testAccAWSAPIGatewayV2DomainNameConfigPublicCert (rootDomain , domain string ) string {
406
+ return fmt .Sprintf (`
407
+ data "aws_route53_zone" "test" {
408
+ name = %[1]q
409
+ private_zone = false
410
+ }
411
+
412
+ resource "aws_acm_certificate" "test" {
413
+ domain_name = %[2]q
414
+ validation_method = "DNS"
415
+ }
416
+
417
+ #
418
+ # for_each acceptance testing requires:
419
+ # https://github.com/hashicorp/terraform-plugin-sdk/issues/536
420
+ #
421
+ # resource "aws_route53_record" "test" {
422
+ # for_each = {
423
+ # for dvo in aws_acm_certificate.test.domain_validation_options: dvo.domain_name => {
424
+ # name = dvo.resource_record_name
425
+ # record = dvo.resource_record_value
426
+ # type = dvo.resource_record_type
427
+ # }
428
+ # }
429
+ # allow_overwrite = true
430
+ # name = each.value.name
431
+ # records = [each.value.record]
432
+ # ttl = 60
433
+ # type = each.value.type
434
+ # zone_id = data.aws_route53_zone.test.zone_id
435
+ # }
436
+
437
+ resource "aws_route53_record" "test" {
438
+ allow_overwrite = true
439
+ name = tolist(aws_acm_certificate.test.domain_validation_options)[0].resource_record_name
440
+ records = [tolist(aws_acm_certificate.test.domain_validation_options)[0].resource_record_value]
441
+ ttl = 60
442
+ type = tolist(aws_acm_certificate.test.domain_validation_options)[0].resource_record_type
443
+ zone_id = data.aws_route53_zone.test.zone_id
444
+ }
445
+
446
+ resource "aws_acm_certificate_validation" "test" {
447
+ certificate_arn = aws_acm_certificate.test.arn
448
+ validation_record_fqdns = [aws_route53_record.test.fqdn]
449
+ }
450
+ ` , rootDomain , domain )
451
+ }
452
+
408
453
func testAccAWSAPIGatewayV2DomainNameConfig_basic (rName , certificate , key string , count , index int ) string {
409
- return composeConfig (testAccAWSAPIGatewayV2DomainNameConfigImportedCerts (rName , certificate , key , count ), fmt .Sprintf (`
454
+ return composeConfig (
455
+ testAccAWSAPIGatewayV2DomainNameConfigImportedCerts (rName , certificate , key , count ),
456
+ fmt .Sprintf (`
410
457
resource "aws_apigatewayv2_domain_name" "test" {
411
458
domain_name = "%[1]s.example.com"
412
459
@@ -420,7 +467,9 @@ resource "aws_apigatewayv2_domain_name" "test" {
420
467
}
421
468
422
469
func testAccAWSAPIGatewayV2DomainNameConfig_tags (rName , certificate , key string , count , index int ) string {
423
- return composeConfig (testAccAWSAPIGatewayV2DomainNameConfigImportedCerts (rName , certificate , key , count ), fmt .Sprintf (`
470
+ return composeConfig (
471
+ testAccAWSAPIGatewayV2DomainNameConfigImportedCerts (rName , certificate , key , count ),
472
+ fmt .Sprintf (`
424
473
resource "aws_apigatewayv2_domain_name" "test" {
425
474
domain_name = "%[1]s.example.com"
426
475
@@ -438,14 +487,10 @@ resource "aws_apigatewayv2_domain_name" "test" {
438
487
` , rName , index ))
439
488
}
440
489
441
- func testAccAWSAPIGatewayV2DomainNameConfigMututalTlsAuthentication (rName , domainName string ) string {
442
- return fmt .Sprintf (`
443
- data "aws_acm_certificate" "test" {
444
- domain = %[2]q
445
- types = ["AMAZON_ISSUED"]
446
- most_recent = true
447
- }
448
-
490
+ func testAccAWSAPIGatewayV2DomainNameConfigMututalTlsAuthentication (rootDomain , domain , rName string ) string {
491
+ return composeConfig (
492
+ testAccAWSAPIGatewayV2DomainNameConfigPublicCert (rootDomain , domain ),
493
+ fmt .Sprintf (`
449
494
resource "aws_s3_bucket" "test" {
450
495
bucket = %[1]q
451
496
@@ -459,10 +504,10 @@ resource "aws_s3_bucket_object" "test" {
459
504
}
460
505
461
506
resource "aws_apigatewayv2_domain_name" "test" {
462
- domain_name = %[2]q
507
+ domain_name = aws_acm_certificate.test.domain_name
463
508
464
509
domain_name_configuration {
465
- certificate_arn = data.aws_acm_certificate. test.arn
510
+ certificate_arn = aws_acm_certificate_validation. test.certificate_arn
466
511
endpoint_type = "REGIONAL"
467
512
security_policy = "TLS_1_2"
468
513
}
@@ -471,17 +516,13 @@ resource "aws_apigatewayv2_domain_name" "test" {
471
516
truststore_uri = "s3://${aws_s3_bucket_object.test.bucket}/${aws_s3_bucket_object.test.key}"
472
517
}
473
518
}
474
- ` , rName , domainName )
475
- }
476
-
477
- func testAccAWSAPIGatewayV2DomainNameConfigMututalTlsAuthenticationUpdated (rName , domainName string ) string {
478
- return fmt .Sprintf (`
479
- data "aws_acm_certificate" "test" {
480
- domain = %[2]q
481
- types = ["AMAZON_ISSUED"]
482
- most_recent = true
519
+ ` , rName ))
483
520
}
484
521
522
+ func testAccAWSAPIGatewayV2DomainNameConfigMututalTlsAuthenticationUpdated (rootDomain , domain , rName string ) string {
523
+ return composeConfig (
524
+ testAccAWSAPIGatewayV2DomainNameConfigPublicCert (rootDomain , domain ),
525
+ fmt .Sprintf (`
485
526
resource "aws_s3_bucket" "test" {
486
527
bucket = %[1]q
487
528
@@ -499,10 +540,10 @@ resource "aws_s3_bucket_object" "test" {
499
540
}
500
541
501
542
resource "aws_apigatewayv2_domain_name" "test" {
502
- domain_name = %[2]q
543
+ domain_name = aws_acm_certificate.test.domain_name
503
544
504
545
domain_name_configuration {
505
- certificate_arn = data.aws_acm_certificate. test.arn
546
+ certificate_arn = aws_acm_certificate_validation. test.certificate_arn
506
547
endpoint_type = "REGIONAL"
507
548
security_policy = "TLS_1_2"
508
549
}
@@ -512,25 +553,21 @@ resource "aws_apigatewayv2_domain_name" "test" {
512
553
truststore_version = aws_s3_bucket_object.test.version_id
513
554
}
514
555
}
515
- ` , rName , domainName )
516
- }
517
-
518
- func testAccAWSAPIGatewayV2DomainNameConfigMututalTlsAuthenticationMissing (domainName string ) string {
519
- return fmt .Sprintf (`
520
- data "aws_acm_certificate" "test" {
521
- domain = %[1]q
522
- types = ["AMAZON_ISSUED"]
523
- most_recent = true
556
+ ` , rName ))
524
557
}
525
558
559
+ func testAccAWSAPIGatewayV2DomainNameConfigMututalTlsAuthenticationMissing (rootDomain , domain string ) string {
560
+ return composeConfig (
561
+ testAccAWSAPIGatewayV2DomainNameConfigPublicCert (rootDomain , domain ),
562
+ `
526
563
resource "aws_apigatewayv2_domain_name" "test" {
527
- domain_name = %[1]q
564
+ domain_name = aws_acm_certificate.test.domain_name
528
565
529
566
domain_name_configuration {
530
- certificate_arn = data.aws_acm_certificate. test.arn
567
+ certificate_arn = aws_acm_certificate_validation. test.certificate_arn
531
568
endpoint_type = "REGIONAL"
532
569
security_policy = "TLS_1_2"
533
570
}
534
571
}
535
- ` , domainName )
572
+ ` )
536
573
}
0 commit comments