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_health_check - add arn attribute + validations #20653

Merged
merged 3 commits into from
Aug 23, 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/20653.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_route53_health_check: Add plan time validation for `failure_threshold`, `ip_address`, `fqdn`, `port`, `resource_path`, `search_string`, `child_healthchecks`.
```

```release-note:enhancement
resource/aws_route53_health_check: Add `arn` attribute.
```
93 changes: 55 additions & 38 deletions aws/resource_aws_route53_health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"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"
Expand All @@ -26,26 +26,24 @@ func resourceAwsRoute53HealthCheck() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: func(val interface{}) string {
return strings.ToUpper(val.(string))
},
ValidateFunc: validation.StringInSlice([]string{
route53.HealthCheckTypeCalculated,
route53.HealthCheckTypeCloudwatchMetric,
route53.HealthCheckTypeHttp,
route53.HealthCheckTypeHttpStrMatch,
route53.HealthCheckTypeHttps,
route53.HealthCheckTypeHttpsStrMatch,
route53.HealthCheckTypeTcp,
}, true),
ValidateFunc: validation.StringInSlice(route53.HealthCheckType_Values(), true),
},
"failure_threshold": {
Type: schema.TypeInt,
Optional: true,
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validation.IntBetween(1, 10),
},
"request_interval": {
Type: schema.TypeInt,
Expand All @@ -54,20 +52,23 @@ func resourceAwsRoute53HealthCheck() *schema.Resource {
ValidateFunc: validation.IntInSlice([]int{10, 30}),
},
"ip_address": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IsIPAddress,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return net.ParseIP(old).Equal(net.ParseIP(new))
},
},
"fqdn": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 255),
},
"port": {
Type: schema.TypeInt,
Optional: true,
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IsPortNumber,
},

"invert_healthcheck": {
Expand All @@ -76,13 +77,15 @@ func resourceAwsRoute53HealthCheck() *schema.Resource {
},

"resource_path": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 255),
},

"search_string": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 255),
},

"measure_latency": {
Expand All @@ -94,9 +97,12 @@ func resourceAwsRoute53HealthCheck() *schema.Resource {

"child_healthchecks": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
MaxItems: 256,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 64),
},
Optional: true,
Set: schema.HashString,
},
"child_health_threshold": {
Type: schema.TypeInt,
Expand All @@ -115,13 +121,9 @@ func resourceAwsRoute53HealthCheck() *schema.Resource {
},

"insufficient_data_health_status": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
route53.InsufficientDataHealthStatusHealthy,
route53.InsufficientDataHealthStatusLastKnownStatus,
route53.InsufficientDataHealthStatusUnhealthy,
}, true),
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(route53.InsufficientDataHealthStatus_Values(), true),
},
"reference_name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -234,7 +236,7 @@ func resourceAwsRoute53HealthCheckUpdate(d *schema.ResourceData, meta interface{
o, n := d.GetChange("tags_all")

if err := keyvaluetags.Route53UpdateTags(conn, d.Id(), route53.TagResourceTypeHealthcheck, o, n); err != nil {
return fmt.Errorf("error updating Route53 Health Check (%s) tags: %s", d.Id(), err)
return fmt.Errorf("error updating Route53 Health Check (%s) tags: %w", d.Id(), err)
}
}

Expand Down Expand Up @@ -347,7 +349,7 @@ func resourceAwsRoute53HealthCheckCreate(d *schema.ResourceData, meta interface{
d.SetId(aws.StringValue(resp.HealthCheck.Id))

if err := keyvaluetags.Route53UpdateTags(conn, d.Id(), route53.TagResourceTypeHealthcheck, nil, tags); err != nil {
return fmt.Errorf("error setting Route53 Health Check (%s) tags: %s", d.Id(), err)
return fmt.Errorf("error setting Route53 Health Check (%s) tags: %w", d.Id(), err)
}

return resourceAwsRoute53HealthCheckRead(d, meta)
Expand All @@ -360,7 +362,7 @@ func resourceAwsRoute53HealthCheckRead(d *schema.ResourceData, meta interface{})

read, err := conn.GetHealthCheck(&route53.GetHealthCheckInput{HealthCheckId: aws.String(d.Id())})
if err != nil {
if r53err, ok := err.(awserr.Error); ok && r53err.Code() == "NoSuchHealthCheck" {
if isAWSErr(err, route53.ErrCodeNoSuchHealthCheck, "") {
d.SetId("")
return nil

Expand All @@ -386,7 +388,7 @@ func resourceAwsRoute53HealthCheckRead(d *schema.ResourceData, meta interface{})
d.Set("disabled", updated.Disabled)

if err := d.Set("child_healthchecks", flattenStringList(updated.ChildHealthChecks)); err != nil {
return fmt.Errorf("error setting child_healthchecks: %s", err)
return fmt.Errorf("error setting child_healthchecks: %w", err)
}

d.Set("child_health_threshold", updated.HealthThreshold)
Expand All @@ -403,7 +405,7 @@ func resourceAwsRoute53HealthCheckRead(d *schema.ResourceData, meta interface{})
tags, err := keyvaluetags.Route53ListTags(conn, d.Id(), route53.TagResourceTypeHealthcheck)

if err != nil {
return fmt.Errorf("error listing tags for Route53 Health Check (%s): %s", d.Id(), err)
return fmt.Errorf("error listing tags for Route53 Health Check (%s): %w", d.Id(), err)
}

tags = tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig)
Expand All @@ -417,6 +419,13 @@ func resourceAwsRoute53HealthCheckRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("error setting tags_all: %w", err)
}

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

return nil
}

Expand All @@ -425,5 +434,13 @@ func resourceAwsRoute53HealthCheckDelete(d *schema.ResourceData, meta interface{

log.Printf("[DEBUG] Deleting Route53 health check: %s", d.Id())
_, err := conn.DeleteHealthCheck(&route53.DeleteHealthCheckInput{HealthCheckId: aws.String(d.Id())})
return err
if isAWSErr(err, route53.ErrCodeNoSuchHealthCheck, "") {
return nil
}

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

return nil
}
18 changes: 3 additions & 15 deletions aws/resource_aws_route53_health_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package aws

import (
"fmt"
"log"
"regexp"
"strings"
"testing"

Expand All @@ -25,6 +25,7 @@ func TestAccAWSRoute53HealthCheck_basic(t *testing.T) {
Config: testAccRoute53HealthCheckConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53HealthCheckExists(resourceName, &check),
testAccMatchResourceAttrGlobalARNNoAccount(resourceName, "arn", "route53", regexp.MustCompile("healthcheck/.+")),
resource.TestCheckResourceAttr(resourceName, "measure_latency", "true"),
resource.TestCheckResourceAttr(resourceName, "port", "80"),
resource.TestCheckResourceAttr(resourceName, "invert_healthcheck", "true"),
Expand Down Expand Up @@ -345,7 +346,7 @@ func TestAccAWSRoute53HealthCheck_disappears(t *testing.T) {
Config: testAccRoute53HealthCheckConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53HealthCheckExists(resourceName, &check),
testAccCheckRoute53HealthCheckDisappears(&check),
testAccCheckResourceDisappears(testAccProvider, resourceAwsRoute53HealthCheck(), resourceName),
),
ExpectNonEmptyPlan: true,
},
Expand Down Expand Up @@ -414,19 +415,6 @@ func testAccCheckRoute53HealthCheckExists(n string, hCheck *route53.HealthCheck)
}
}

func testAccCheckRoute53HealthCheckDisappears(hCheck *route53.HealthCheck) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).r53conn
input := &route53.DeleteHealthCheckInput{
HealthCheckId: hCheck.Id,
}
log.Printf("[DEBUG] Deleting Route53 Health Check: %#v", input)
_, err := conn.DeleteHealthCheck(input)

return err
}
}

const testAccRoute53HealthCheckConfig = `
resource "aws_route53_health_check" "test" {
fqdn = "dev.example.com"
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/route53_health_check.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,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 Health Check.
* `id` - The id of the health check
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block).

Expand Down