Skip to content

Commit

Permalink
Merge pull request #27293 from sjparkinson/main
Browse files Browse the repository at this point in the history
Add `primary_name_server` attribute to `aws_route53_zone`
  • Loading branch information
ewbankkit authored Oct 18, 2022
2 parents cee1836 + 2abb764 commit edbe69a
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 112 deletions.
7 changes: 7 additions & 0 deletions .changelog/27293.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_route53_zone: Add `primary_name_server` attribute
```

```release-note:enhancement
data-source/aws_route53_zone: Add `primary_name_server` attribute
```
21 changes: 21 additions & 0 deletions internal/service/route53/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ func FindHealthCheckByID(conn *route53.Route53, id string) (*route53.HealthCheck
return output.HealthCheck, nil
}

func FindHostedZoneByID(conn *route53.Route53, id string) (*route53.GetHostedZoneOutput, error) {
input := &route53.GetHostedZoneInput{
Id: aws.String(id),
}

output, err := conn.GetHostedZone(input)

if tfawserr.ErrCodeEquals(err, route53.ErrCodeNoSuchHostedZone) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if output == nil || output.HostedZone == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output, nil
}

func FindHostedZoneDNSSEC(conn *route53.Route53, hostedZoneID string) (*route53.GetDNSSECOutput, error) {
input := &route53.GetDNSSECInput{
HostedZoneId: aws.String(hostedZoneID),
Expand Down
82 changes: 36 additions & 46 deletions internal/service/route53/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ func ResourceZone() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"comment": {
Type: schema.TypeString,
Optional: true,
Default: "Managed by Terraform",
ValidateFunc: validation.StringLenBetween(0, 256),
},
"delegation_set_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"vpc"},
ValidateFunc: validation.StringLenBetween(0, 32),
},
"force_destroy": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"name": {
// AWS Provider 3.0.0 - trailing period removed from name
// returned from API, no longer requiring custom DiffSuppressFunc;
Expand All @@ -57,14 +75,17 @@ func ResourceZone() *schema.Resource {
StateFunc: TrimTrailingPeriod,
ValidateFunc: validation.StringLenBetween(1, 1024),
},

"comment": {
Type: schema.TypeString,
Optional: true,
Default: "Managed by Terraform",
ValidateFunc: validation.StringLenBetween(0, 256),
"name_servers": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},

"primary_name_server": {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
"vpc": {
Type: schema.TypeSet,
Optional: true,
Expand All @@ -86,34 +107,10 @@ func ResourceZone() *schema.Resource {
},
Set: hostedZoneVPCHash,
},

"zone_id": {
Type: schema.TypeString,
Computed: true,
},

"delegation_set_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"vpc"},
ValidateFunc: validation.StringLenBetween(0, 32),
},

"name_servers": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},

"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),

"force_destroy": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},

CustomizeDiff: verify.SetTagsDiff,
Expand Down Expand Up @@ -185,27 +182,16 @@ func resourceZoneRead(d *schema.ResourceData, meta interface{}) error {
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

input := &route53.GetHostedZoneInput{
Id: aws.String(d.Id()),
}
output, err := FindHostedZoneByID(conn, d.Id())

log.Printf("[DEBUG] Getting Route53 Hosted Zone: %s", input)
output, err := conn.GetHostedZone(input)

if tfawserr.ErrCodeEquals(err, route53.ErrCodeNoSuchHostedZone) {
log.Printf("[WARN] Route53 Hosted Zone (%s) not found, removing from state", d.Id())
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Route53 Hosted Zone %s not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("getting Route53 Hosted Zone (%s): %s", d.Id(), err)
}

if output == nil || output.HostedZone == nil {
log.Printf("[WARN] Route53 Hosted Zone (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
return fmt.Errorf("reading Route53 Hosted Zone (%s): %w", d.Id(), err)
}

d.Set("comment", "")
Expand Down Expand Up @@ -236,6 +222,10 @@ func resourceZoneRead(d *schema.ResourceData, meta interface{}) error {
}
}

if err := d.Set("primary_name_server", nameServers[0]); err != nil {
return fmt.Errorf("setting primary_name_server: %s", err)
}

sort.Strings(nameServers)
if err := d.Set("name_servers", nameServers); err != nil {
return fmt.Errorf("setting name_servers: %s", err)
Expand Down
48 changes: 28 additions & 20 deletions internal/service/route53/zone_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,24 @@ func DataSourceZone() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"zone_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
"caller_reference": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"private_zone": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"comment": {
Type: schema.TypeString,
Computed: true,
},
"caller_reference": {
"linked_service_description": {
Type: schema.TypeString,
Computed: true,
},
"vpc_id": {
"linked_service_principal": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"tags": tftags.TagsSchemaComputed(),
"resource_record_set_count": {
Type: schema.TypeInt,
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
Expand All @@ -60,12 +47,29 @@ func DataSourceZone() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"linked_service_principal": {
"primary_name_server": {
Type: schema.TypeString,
Computed: true,
},
"linked_service_description": {
"private_zone": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"resource_record_set_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"tags": tftags.TagsSchemaComputed(),
"vpc_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"zone_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
Expand Down Expand Up @@ -182,6 +186,10 @@ func dataSourceZoneRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("getting Route 53 Hosted Zone (%s) name servers: %w", idHostedZone, err)
}

if err := d.Set("primary_name_server", nameServers[0]); err != nil {
return fmt.Errorf("setting primary_name_server: %w", err)
}

if err := d.Set("name_servers", nameServers); err != nil {
return fmt.Errorf("setting name_servers: %w", err)
}
Expand Down
4 changes: 4 additions & 0 deletions internal/service/route53/zone_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestAccRoute53ZoneDataSource_id(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, "id", dataSourceName, "id"),
resource.TestCheckResourceAttrPair(resourceName, "name", dataSourceName, "name"),
resource.TestCheckResourceAttrPair(resourceName, "name_servers.#", dataSourceName, "name_servers.#"),
resource.TestCheckResourceAttrPair(resourceName, "primary_name_server", dataSourceName, "primary_name_server"),
resource.TestCheckResourceAttrPair(resourceName, "tags", dataSourceName, "tags"),
),
},
Expand All @@ -54,6 +55,7 @@ func TestAccRoute53ZoneDataSource_name(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, "id", dataSourceName, "id"),
resource.TestCheckResourceAttrPair(resourceName, "name", dataSourceName, "name"),
resource.TestCheckResourceAttrPair(resourceName, "name_servers.#", dataSourceName, "name_servers.#"),
resource.TestCheckResourceAttrPair(resourceName, "primary_name_server", dataSourceName, "primary_name_server"),
resource.TestCheckResourceAttrPair(resourceName, "tags", dataSourceName, "tags"),
),
},
Expand All @@ -80,6 +82,7 @@ func TestAccRoute53ZoneDataSource_tags(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, "id", dataSourceName, "id"),
resource.TestCheckResourceAttrPair(resourceName, "name", dataSourceName, "name"),
resource.TestCheckResourceAttrPair(resourceName, "name_servers.#", dataSourceName, "name_servers.#"),
resource.TestCheckResourceAttrPair(resourceName, "primary_name_server", dataSourceName, "primary_name_server"),
resource.TestCheckResourceAttrPair(resourceName, "tags", dataSourceName, "tags"),
),
},
Expand All @@ -104,6 +107,7 @@ func TestAccRoute53ZoneDataSource_vpc(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, "id", dataSourceName, "id"),
resource.TestCheckResourceAttrPair(resourceName, "name", dataSourceName, "name"),
resource.TestCheckResourceAttrPair(resourceName, "name_servers.#", dataSourceName, "name_servers.#"),
resource.TestCheckResourceAttrPair(resourceName, "primary_name_server", dataSourceName, "primary_name_server"),
resource.TestCheckResourceAttrPair(resourceName, "tags", dataSourceName, "tags"),
),
},
Expand Down
Loading

0 comments on commit edbe69a

Please sign in to comment.