-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathzone_test.go
768 lines (680 loc) · 22.8 KB
/
zone_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package route53_test
import (
"context"
"fmt"
"log"
"testing"
"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfroute53 "github.com/hashicorp/terraform-provider-aws/internal/service/route53"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)
func TestCleanZoneID(t *testing.T) {
t.Parallel()
cases := []struct {
Input, Output string
}{
{"/hostedzone/foo", "foo"},
{"/change/foo", "/change/foo"},
{"/bar", "/bar"},
}
for _, tc := range cases {
actual := tfroute53.CleanZoneID(tc.Input)
if actual != tc.Output {
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
}
}
}
func TestCleanChangeID(t *testing.T) {
t.Parallel()
cases := []struct {
Input, Output string
}{
{"/hostedzone/foo", "/hostedzone/foo"},
{"/change/foo", "foo"},
{"/bar", "/bar"},
}
for _, tc := range cases {
actual := tfroute53.CleanChangeID(tc.Input)
if actual != tc.Output {
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
}
}
}
func TestNormalizeZoneName(t *testing.T) {
t.Parallel()
cases := []struct {
Input interface{}
Output string
}{
{"example.com", "example.com"},
{"example.com.", "example.com"},
{"www.example.com.", "www.example.com"},
{"www.ExAmPlE.COM.", "www.example.com"},
{"", ""},
{".", "."},
{aws.String("example.com"), "example.com"},
{aws.String("example.com."), "example.com"},
{aws.String("www.example.com."), "www.example.com"},
{aws.String("www.ExAmPlE.COM."), "www.example.com"},
{aws.String(""), ""},
{aws.String("."), "."},
{(*string)(nil), ""},
{42, ""},
{nil, ""},
}
for _, tc := range cases {
actual := tfroute53.NormalizeZoneName(tc.Input)
if actual != tc.Output {
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
}
}
}
// add sweeper to delete resources
func TestAccRoute53Zone_basic(t *testing.T) {
ctx := acctest.Context(t)
var zone route53.GetHostedZoneOutput
resourceName := "aws_route53_zone.test"
zoneName := acctest.RandomDomainName()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.Route53ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckZoneDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccZoneConfig_basic(zoneName),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
acctest.MatchResourceAttrGlobalARNNoAccount(resourceName, "arn", "route53", regexache.MustCompile("hostedzone/.+")),
resource.TestCheckResourceAttr(resourceName, "name", zoneName),
resource.TestCheckResourceAttr(resourceName, "name_servers.#", "4"),
resource.TestCheckResourceAttrSet(resourceName, "primary_name_server"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttr(resourceName, "vpc.#", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
},
})
}
func TestAccRoute53Zone_disappears(t *testing.T) {
ctx := acctest.Context(t)
var zone route53.GetHostedZoneOutput
resourceName := "aws_route53_zone.test"
zoneName := acctest.RandomDomainName()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.Route53ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckZoneDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccZoneConfig_basic(zoneName),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
acctest.CheckResourceDisappears(ctx, acctest.Provider, tfroute53.ResourceZone(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}
func TestAccRoute53Zone_multiple(t *testing.T) {
ctx := acctest.Context(t)
var zone0, zone1, zone2, zone3, zone4 route53.GetHostedZoneOutput
domainName := acctest.RandomDomainName()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.Route53ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckZoneDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccZoneConfig_multiple(domainName),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, "aws_route53_zone.test.0", &zone0),
testAccCheckDomainName(&zone0, fmt.Sprintf("subdomain0.%s.", domainName)),
testAccCheckZoneExists(ctx, "aws_route53_zone.test.1", &zone1),
testAccCheckDomainName(&zone1, fmt.Sprintf("subdomain1.%s.", domainName)),
testAccCheckZoneExists(ctx, "aws_route53_zone.test.2", &zone2),
testAccCheckDomainName(&zone2, fmt.Sprintf("subdomain2.%s.", domainName)),
testAccCheckZoneExists(ctx, "aws_route53_zone.test.3", &zone3),
testAccCheckDomainName(&zone3, fmt.Sprintf("subdomain3.%s.", domainName)),
testAccCheckZoneExists(ctx, "aws_route53_zone.test.4", &zone4),
testAccCheckDomainName(&zone4, fmt.Sprintf("subdomain4.%s.", domainName)),
),
},
},
})
}
func TestAccRoute53Zone_comment(t *testing.T) {
ctx := acctest.Context(t)
var zone route53.GetHostedZoneOutput
resourceName := "aws_route53_zone.test"
zoneName := acctest.RandomDomainName()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.Route53ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckZoneDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccZoneConfig_comment(zoneName, "comment1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
resource.TestCheckResourceAttr(resourceName, "comment", "comment1"),
),
},
{
Config: testAccZoneConfig_comment(zoneName, "comment2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
resource.TestCheckResourceAttr(resourceName, "comment", "comment2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
},
})
}
func TestAccRoute53Zone_delegationSetID(t *testing.T) {
ctx := acctest.Context(t)
var zone route53.GetHostedZoneOutput
delegationSetResourceName := "aws_route53_delegation_set.test"
resourceName := "aws_route53_zone.test"
zoneName := acctest.RandomDomainName()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.Route53ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckZoneDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccZoneConfig_delegationSetID(zoneName),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
resource.TestCheckResourceAttrPair(resourceName, "delegation_set_id", delegationSetResourceName, "id"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
},
})
}
func TestAccRoute53Zone_forceDestroy(t *testing.T) {
ctx := acctest.Context(t)
var zone route53.GetHostedZoneOutput
resourceName := "aws_route53_zone.test"
zoneName := acctest.RandomDomainName()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.Route53ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckZoneDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccZoneConfig_forceDestroy(zoneName),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
// Add >100 records to verify pagination works ok
testAccCreateRandomRecordsInZoneID(ctx, &zone, 100),
testAccCreateRandomRecordsInZoneID(ctx, &zone, 5),
),
},
},
})
}
func TestAccRoute53Zone_ForceDestroy_trailingPeriod(t *testing.T) {
ctx := acctest.Context(t)
var zone route53.GetHostedZoneOutput
resourceName := "aws_route53_zone.test"
zoneName := acctest.RandomDomainName()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.Route53ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckZoneDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccZoneConfig_forceDestroyTrailingPeriod(zoneName),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
// Add >100 records to verify pagination works ok
testAccCreateRandomRecordsInZoneID(ctx, &zone, 100),
testAccCreateRandomRecordsInZoneID(ctx, &zone, 5),
),
},
},
})
}
func TestAccRoute53Zone_tags(t *testing.T) {
ctx := acctest.Context(t)
var zone route53.GetHostedZoneOutput
resourceName := "aws_route53_zone.test"
zoneName := acctest.RandomDomainName()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.Route53ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckZoneDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccZoneConfig_tags1(zoneName, "tag1key", "tag1value"),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.tag1key", "tag1value"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
{
Config: testAccZoneConfig_tags2(zoneName, "tag1key", "tag1valueupdated", "tag2key", "tag2value"),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.tag1key", "tag1valueupdated"),
resource.TestCheckResourceAttr(resourceName, "tags.tag2key", "tag2value"),
),
},
{
Config: testAccZoneConfig_tags1(zoneName, "tag2key", "tag2value"),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.tag2key", "tag2value"),
),
},
},
})
}
func TestAccRoute53Zone_VPC_single(t *testing.T) {
ctx := acctest.Context(t)
var zone route53.GetHostedZoneOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_route53_zone.test"
vpcResourceName := "aws_vpc.test1"
zoneName := acctest.RandomDomainName()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.Route53ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckZoneDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccZoneConfig_vpcSingle(rName, zoneName),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
resource.TestCheckResourceAttr(resourceName, "vpc.#", "1"),
testAccCheckZoneAssociatesVPC(vpcResourceName, &zone),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
},
})
}
func TestAccRoute53Zone_VPC_multiple(t *testing.T) {
ctx := acctest.Context(t)
var zone route53.GetHostedZoneOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_route53_zone.test"
vpcResourceName1 := "aws_vpc.test1"
vpcResourceName2 := "aws_vpc.test2"
zoneName := acctest.RandomDomainName()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.Route53ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckZoneDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccZoneConfig_vpcMultiple(rName, zoneName),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
resource.TestCheckResourceAttr(resourceName, "vpc.#", "2"),
testAccCheckZoneAssociatesVPC(vpcResourceName1, &zone),
testAccCheckZoneAssociatesVPC(vpcResourceName2, &zone),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
},
})
}
func TestAccRoute53Zone_VPC_updates(t *testing.T) {
ctx := acctest.Context(t)
var zone route53.GetHostedZoneOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_route53_zone.test"
vpcResourceName1 := "aws_vpc.test1"
vpcResourceName2 := "aws_vpc.test2"
zoneName := acctest.RandomDomainName()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.Route53ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckZoneDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccZoneConfig_vpcSingle(rName, zoneName),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
resource.TestCheckResourceAttr(resourceName, "vpc.#", "1"),
testAccCheckZoneAssociatesVPC(vpcResourceName1, &zone),
),
},
{
Config: testAccZoneConfig_vpcMultiple(rName, zoneName),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
resource.TestCheckResourceAttr(resourceName, "vpc.#", "2"),
testAccCheckZoneAssociatesVPC(vpcResourceName1, &zone),
testAccCheckZoneAssociatesVPC(vpcResourceName2, &zone),
),
},
{
Config: testAccZoneConfig_vpcSingle(rName, zoneName),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
resource.TestCheckResourceAttr(resourceName, "vpc.#", "1"),
testAccCheckZoneAssociatesVPC(vpcResourceName1, &zone),
),
},
},
})
}
// Excercises exception handling during forced destruction in partitions
// which do no support DNSSEC (e.g. GovCloud).
//
// Ref: https://github.com/hashicorp/terraform-provider-aws/issues/22334
func TestAccRoute53Zone_VPC_single_forceDestroy(t *testing.T) {
ctx := acctest.Context(t)
var zone route53.GetHostedZoneOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_route53_zone.test"
vpcResourceName := "aws_vpc.test1"
zoneName := acctest.RandomDomainName()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.Route53ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckZoneDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccZoneConfig_vpcSingle_forceDestroy(rName, zoneName),
Check: resource.ComposeTestCheckFunc(
testAccCheckZoneExists(ctx, resourceName, &zone),
resource.TestCheckResourceAttr(resourceName, "vpc.#", "1"),
testAccCheckZoneAssociatesVPC(vpcResourceName, &zone),
// Add >100 records to verify pagination works ok
testAccCreateRandomRecordsInZoneID(ctx, &zone, 100),
testAccCreateRandomRecordsInZoneID(ctx, &zone, 5),
),
},
},
})
}
func testAccCheckZoneDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).Route53Conn(ctx)
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_route53_zone" {
continue
}
_, err := tfroute53.FindHostedZoneByID(ctx, conn, rs.Primary.ID)
if tfresource.NotFound(err) {
continue
}
if err != nil {
return err
}
return fmt.Errorf("Route53 Hosted Zone %s still exists", rs.Primary.ID)
}
return nil
}
}
func testAccCreateRandomRecordsInZoneID(ctx context.Context, zone *route53.GetHostedZoneOutput, recordsCount int) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).Route53Conn(ctx)
var changes []*route53.Change
if recordsCount > 100 {
return fmt.Errorf("Route53 API only allows 100 record sets in a single batch")
}
for i := 0; i < recordsCount; i++ {
changes = append(changes, &route53.Change{
Action: aws.String("UPSERT"),
ResourceRecordSet: &route53.ResourceRecordSet{
Name: aws.String(fmt.Sprintf("%d-tf-acc-random.%s", sdkacctest.RandInt(), *zone.HostedZone.Name)),
Type: aws.String("CNAME"),
ResourceRecords: []*route53.ResourceRecord{
{Value: aws.String(fmt.Sprintf("random.%s", *zone.HostedZone.Name))},
},
TTL: aws.Int64(30),
},
})
}
req := &route53.ChangeResourceRecordSetsInput{
HostedZoneId: zone.HostedZone.Id,
ChangeBatch: &route53.ChangeBatch{
Comment: aws.String("Generated by Terraform"),
Changes: changes,
},
}
log.Printf("[DEBUG] Change set: %s\n", *req)
changeInfo, err := tfroute53.ChangeResourceRecordSets(ctx, conn, req)
if err != nil {
return err
}
err = tfroute53.WaitForRecordSetToSync(ctx, conn, tfroute53.CleanChangeID(*changeInfo.Id))
return err
}
}
func testAccCheckZoneExists(ctx context.Context, n string, v *route53.GetHostedZoneOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Route53 Hosted Zone ID is set")
}
conn := acctest.Provider.Meta().(*conns.AWSClient).Route53Conn(ctx)
output, err := tfroute53.FindHostedZoneByID(ctx, conn, rs.Primary.ID)
if err != nil {
return err
}
*v = *output
return nil
}
}
func testAccCheckZoneAssociatesVPC(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No VPC ID is set")
}
for _, vpc := range zone.VPCs {
if aws.StringValue(vpc.VPCId) == rs.Primary.ID {
return nil
}
}
return fmt.Errorf("VPC: %s is not associated to Zone: %v", n, tfroute53.CleanZoneID(aws.StringValue(zone.HostedZone.Id)))
}
}
func testAccCheckDomainName(zone *route53.GetHostedZoneOutput, domain string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if zone.HostedZone.Name == nil {
return fmt.Errorf("Empty name in HostedZone for domain %s", domain)
}
if aws.StringValue(zone.HostedZone.Name) == domain {
return nil
}
return fmt.Errorf("Invalid domain name. Expected %s is %s", domain, *zone.HostedZone.Name)
}
}
func testAccZoneConfig_basic(zoneName string) string {
return fmt.Sprintf(`
resource "aws_route53_zone" "test" {
name = "%[1]s."
}
`, zoneName)
}
func testAccZoneConfig_multiple(domainName string) string {
return fmt.Sprintf(`
resource "aws_route53_zone" "test" {
count = 5
name = "subdomain${count.index}.%[1]s"
}
`, domainName)
}
func testAccZoneConfig_comment(zoneName, comment string) string {
return fmt.Sprintf(`
resource "aws_route53_zone" "test" {
comment = %[1]q
name = "%[2]s."
}
`, comment, zoneName)
}
func testAccZoneConfig_delegationSetID(zoneName string) string {
return fmt.Sprintf(`
resource "aws_route53_delegation_set" "test" {}
resource "aws_route53_zone" "test" {
delegation_set_id = aws_route53_delegation_set.test.id
name = "%[1]s."
}
`, zoneName)
}
func testAccZoneConfig_forceDestroy(zoneName string) string {
return fmt.Sprintf(`
resource "aws_route53_zone" "test" {
force_destroy = true
name = "%[1]s"
}
`, zoneName)
}
func testAccZoneConfig_forceDestroyTrailingPeriod(zoneName string) string {
return fmt.Sprintf(`
resource "aws_route53_zone" "test" {
force_destroy = true
name = "%[1]s."
}
`, zoneName)
}
func testAccZoneConfig_tags1(zoneName, tag1Key, tag1Value string) string {
return fmt.Sprintf(`
resource "aws_route53_zone" "test" {
name = "%[1]s."
tags = {
%[2]q = %[3]q
}
}
`, zoneName, tag1Key, tag1Value)
}
func testAccZoneConfig_tags2(zoneName, tag1Key, tag1Value, tag2Key, tag2Value string) string {
return fmt.Sprintf(`
resource "aws_route53_zone" "test" {
name = "%[1]s."
tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, zoneName, tag1Key, tag1Value, tag2Key, tag2Value)
}
func testAccZoneConfig_vpcSingle(rName, zoneName string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test1" {
cidr_block = "10.1.0.0/16"
tags = {
Name = %[1]q
}
}
resource "aws_route53_zone" "test" {
name = "%[2]s."
vpc {
vpc_id = aws_vpc.test1.id
}
}
`, rName, zoneName)
}
func testAccZoneConfig_vpcMultiple(rName, zoneName string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test1" {
cidr_block = "10.1.0.0/16"
tags = {
Name = %[1]q
}
}
resource "aws_vpc" "test2" {
cidr_block = "10.2.0.0/16"
tags = {
Name = %[1]q
}
}
resource "aws_route53_zone" "test" {
name = "%[2]s."
vpc {
vpc_id = aws_vpc.test1.id
}
vpc {
vpc_id = aws_vpc.test2.id
}
}
`, rName, zoneName)
}
func testAccZoneConfig_vpcSingle_forceDestroy(rName, zoneName string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test1" {
cidr_block = "10.1.0.0/16"
tags = {
Name = %[1]q
}
}
resource "aws_route53_zone" "test" {
force_destroy = true
name = "%[2]s."
vpc {
vpc_id = aws_vpc.test1.id
}
}
`, rName, zoneName)
}