Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rd/route53_delegation_set - add arn attribute + remove resource from state when doesn't exist #20664

Merged
merged 3 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changelog/20664.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```release-note:enhancement
resource/aws_route53_delegation_set: Add `arn` attribute
```

```release-note:enhancement
resource/aws_route53_delegation_set: Add plan time validation for `reference_name`
```

```release-note:enhancement
data-source/aws_route53_delegation_set: Add `arn` attribute
```

```release-note:bug
resource/aws_route53_delegation_set: Properly remove from state when resource does not exist
```
14 changes: 13 additions & 1 deletion aws/data_source_aws_route53_delegation_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand All @@ -14,6 +15,10 @@ func dataSourceAwsDelegationSet() *schema.Resource {
Read: dataSourceAwsDelegationSetRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -50,9 +55,16 @@ func dataSourceAwsDelegationSetRead(d *schema.ResourceData, meta interface{}) er
d.SetId(dSetID)
d.Set("caller_reference", resp.DelegationSet.CallerReference)

if err := d.Set("name_servers", expandNameServers(resp.DelegationSet.NameServers)); err != nil {
if err := d.Set("name_servers", aws.StringValueSlice(resp.DelegationSet.NameServers)); err != nil {
return fmt.Errorf("error setting name_servers: %w", err)
}

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "route53",
Resource: fmt.Sprintf("delegationset/%s", d.Id()),
}.String()
d.Set("arn", arn)

return nil
}
1 change: 1 addition & 0 deletions aws/data_source_aws_route53_delegation_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestAccAWSRoute53DelegationSetDataSource_basic(t *testing.T) {
{
Config: testAccAWSDataSourceAWSRoute53DelegationSetConfig_basic(zoneName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "name_servers.#", resourceName, "name_servers.#"),
resource.TestMatchResourceAttr("data.aws_route53_delegation_set.dset", "caller_reference", regexp.MustCompile("DynDNS(.*)")),
),
Expand Down
50 changes: 32 additions & 18 deletions aws/resource_aws_route53_delegation_set.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package aws

import (
"fmt"
"log"
"sort"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func resourceAwsRoute53DelegationSet() *schema.Resource {
Expand All @@ -21,10 +23,15 @@ func resourceAwsRoute53DelegationSet() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"reference_name": {
"arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"reference_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(0, 128),
},

"name_servers": {
Expand Down Expand Up @@ -58,8 +65,8 @@ func resourceAwsRoute53DelegationSetCreate(d *schema.ResourceData, meta interfac

set := out.DelegationSet
d.SetId(cleanDelegationSetId(*set.Id))
d.Set("name_servers", expandNameServers(set.NameServers))
return nil

return resourceAwsRoute53DelegationSetRead(d, meta)
}

func resourceAwsRoute53DelegationSetRead(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -71,14 +78,24 @@ func resourceAwsRoute53DelegationSetRead(d *schema.ResourceData, meta interface{
log.Printf("[DEBUG] Reading Route53 reusable delegation set: %#v", input)
out, err := r53.GetReusableDelegationSet(input)
if err != nil {
if isAWSErr(err, route53.ErrCodeNoSuchDelegationSet, "") {
d.SetId("")
return nil

}
return err
}
log.Printf("[DEBUG] Route53 reusable delegation set received: %#v", out)

set := out.DelegationSet
d.Set("name_servers", aws.StringValueSlice(set.NameServers))

d.SetId(cleanDelegationSetId(*set.Id))
d.Set("name_servers", expandNameServers(set.NameServers))
arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "route53",
Resource: fmt.Sprintf("delegationset/%s", d.Id()),
}.String()
d.Set("arn", arn)

return nil
}
Expand All @@ -91,18 +108,15 @@ func resourceAwsRoute53DelegationSetDelete(d *schema.ResourceData, meta interfac
}
log.Printf("[DEBUG] Deleting Route53 reusable delegation set: %#v", input)
_, err := r53.DeleteReusableDelegationSet(input)
return err
}
if isAWSErr(err, route53.ErrCodeNoSuchDelegationSet, "") {
return nil
}

func expandNameServers(name_servers []*string) []string {
log.Printf("[DEBUG] Processing %d name servers: %#v...", len(name_servers), name_servers)
ns := make([]string, len(name_servers))
for i, server := range name_servers {
ns[i] = *server
if err != nil {
return fmt.Errorf("error deleting Route53 reusable delegation set (%s): %w", d.Id(), err)
}
sort.Strings(ns)
log.Printf("[DEBUG] Returning processed name servers: %#v", ns)
return ns

return nil
}

func cleanDelegationSetId(id string) string {
Expand Down
24 changes: 24 additions & 0 deletions aws/resource_aws_route53_delegation_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"reflect"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -26,6 +27,7 @@ func TestAccAWSRoute53DelegationSet_basic(t *testing.T) {
Config: testAccRoute53DelegationSetConfig(refName),
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53DelegationSetExists(resourceName),
testAccMatchResourceAttrGlobalARNNoAccount(resourceName, "arn", "route53", regexp.MustCompile("delegationset/.+")),
),
},
{
Expand Down Expand Up @@ -76,6 +78,28 @@ func TestAccAWSRoute53DelegationSet_withZones(t *testing.T) {
})
}

func TestAccAWSRoute53DelegationSet_disappears(t *testing.T) {
refName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_route53_delegation_set.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, route53.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckRoute53DelegationSetDestroy,
Steps: []resource.TestStep{
{
Config: testAccRoute53DelegationSetConfig(refName),
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53DelegationSetExists(resourceName),
testAccCheckResourceDisappears(testAccProvider, resourceAwsRoute53DelegationSet(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckRoute53DelegationSetDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).r53conn
for _, rs := range s.RootModule().Resources {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/route53_delegation_set.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ data "aws_route53_delegation_set" "dset" {

## Argument Reference


* `id` - (Required) The Hosted Zone id of the desired delegation set.

The following attribute is additionally exported:

* `arn` - The Amazon Resource Name (ARN) of the Delegation Set.
* `caller_reference` - Caller Reference of the delegation set.
* `name_servers` - The list of DNS name servers for the delegation set.
1 change: 1 addition & 0 deletions website/docs/r/route53_delegation_set.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The following arguments are supported:

In addition to all arguments above, the following attributes are exported:

* `arn` - The Amazon Resource Name (ARN) of the Delegation Set.
* `id` - The delegation set ID
* `name_servers` - A list of authoritative name servers for the hosted zone
(effectively a list of NS records).
Expand Down