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

r/route53_query_log - add arn attribute + remove resource from state when doesn't exist #20666

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
7 changes: 7 additions & 0 deletions .changelog/20666.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_route53_query_log: Add `arn` attribute.
```

```release-note:bug
resource/aws_route53_query_log: Properly remove from state when resource does not exist
```
23 changes: 22 additions & 1 deletion aws/resource_aws_route53_query_log.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 @@ -19,6 +20,10 @@ func resourceAwsRoute53QueryLog() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"cloudwatch_log_group_arn": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -64,13 +69,25 @@ func resourceAwsRoute53QueryLogRead(d *schema.ResourceData, meta interface{}) er
log.Printf("[DEBUG] Reading Route53 query logging configuration: %#v", input)
out, err := r53.GetQueryLoggingConfig(input)
if err != nil {
if isAWSErr(err, route53.ErrCodeNoSuchQueryLoggingConfig, "") || isAWSErr(err, route53.ErrCodeNoSuchHostedZone, "") {
log.Printf("[WARN] Route53 Query Logging Config (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Route53 query logging configuration: %s", err)
}
log.Printf("[DEBUG] Route53 query logging configuration received: %#v", out)

d.Set("cloudwatch_log_group_arn", out.QueryLoggingConfig.CloudWatchLogsLogGroupArn)
d.Set("zone_id", out.QueryLoggingConfig.HostedZoneId)

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

return nil
}

Expand All @@ -82,8 +99,12 @@ func resourceAwsRoute53QueryLogDelete(d *schema.ResourceData, meta interface{})
}
log.Printf("[DEBUG] Deleting Route53 query logging configuration: %#v", input)
_, err := r53.DeleteQueryLoggingConfig(input)
if isAWSErr(err, route53.ErrCodeNoSuchQueryLoggingConfig, "") {
return nil
}

if err != nil {
return fmt.Errorf("Error deleting Route53 query logging configuration: %s", err)
return fmt.Errorf("error deleting Route53 query logging configuration (%s): %w", d.Id(), err)
}

return nil
Expand Down
64 changes: 57 additions & 7 deletions aws/resource_aws_route53_query_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -37,13 +38,10 @@ func testSweepRoute53QueryLogs(region string) error {
for _, queryLoggingConfig := range page.QueryLoggingConfigs {
id := aws.StringValue(queryLoggingConfig.Id)

log.Printf("[INFO] Deleting Route53 query logging configuration: %s", id)
_, err := conn.DeleteQueryLoggingConfig(&route53.DeleteQueryLoggingConfigInput{
Id: aws.String(id),
})
if isAWSErr(err, route53.ErrCodeNoSuchQueryLoggingConfig, "") {
continue
}
r := resourceAwsRoute53QueryLog()
d := r.Data(nil)
d.SetId(id)
err := r.Delete(d, client)
if err != nil {
sweeperErr := fmt.Errorf("error deleting Route53 query logging configuration (%s): %w", id, err)
log.Printf("[ERROR] %s", sweeperErr)
Expand Down Expand Up @@ -86,6 +84,7 @@ func TestAccAWSRoute53QueryLog_basic(t *testing.T) {
Config: testAccCheckAWSRoute53QueryLogResourceConfigBasic1(rName, domainName),
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53QueryLogExists(resourceName, &queryLoggingConfig),
testAccMatchResourceAttrGlobalARNNoAccount(resourceName, "arn", "route53", regexp.MustCompile("queryloggingconfig/.+")),
resource.TestCheckResourceAttrPair(resourceName, "cloudwatch_log_group_arn", cloudwatchLogGroupResourceName, "arn"),
resource.TestCheckResourceAttrPair(resourceName, "zone_id", route53ZoneResourceName, "zone_id"),
),
Expand All @@ -99,6 +98,57 @@ func TestAccAWSRoute53QueryLog_basic(t *testing.T) {
})
}

func TestAccAWSRoute53QueryLog_disappears(t *testing.T) {
resourceName := "aws_route53_query_log.test"

rName := acctest.RandomWithPrefix("tf-acc-test")
domainName := testAccRandomDomainName()

var queryLoggingConfig route53.QueryLoggingConfig
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckRoute53QueryLog(t) },
ErrorCheck: testAccErrorCheck(t, route53.EndpointsID),
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckRoute53QueryLogDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckAWSRoute53QueryLogResourceConfigBasic1(rName, domainName),
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53QueryLogExists(resourceName, &queryLoggingConfig),
testAccCheckResourceDisappears(testAccProvider, resourceAwsRoute53QueryLog(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSRoute53QueryLog_disappears_hostedZone(t *testing.T) {
resourceName := "aws_route53_query_log.test"
route53ZoneResourceName := "aws_route53_zone.test"

rName := acctest.RandomWithPrefix("tf-acc-test")
domainName := testAccRandomDomainName()

var queryLoggingConfig route53.QueryLoggingConfig
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckRoute53QueryLog(t) },
ErrorCheck: testAccErrorCheck(t, route53.EndpointsID),
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckRoute53QueryLogDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckAWSRoute53QueryLogResourceConfigBasic1(rName, domainName),
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53QueryLogExists(resourceName, &queryLoggingConfig),
testAccCheckResourceDisappears(testAccProvider, resourceAwsRoute53Zone(), route53ZoneResourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckRoute53QueryLogExists(pr string, queryLoggingConfig *route53.QueryLoggingConfig) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProviderRoute53QueryLog.Meta().(*AWSClient).r53conn
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/route53_query_log.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,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 Query Logging Config.
* `id` - The query logging configuration ID

## Import
Expand Down