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

[WIP] r/aws_route53_resolver_rule: Add resource #6571

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import (
"github.com/aws/aws-sdk-go/service/rds"
"github.com/aws/aws-sdk-go/service/redshift"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/service/route53resolver"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/aws/aws-sdk-go/service/servicecatalog"
Expand Down Expand Up @@ -202,6 +203,7 @@ type AWSClient struct {
stsconn *sts.STS
redshiftconn *redshift.Redshift
r53conn *route53.Route53
r53resolverconn *route53resolver.Route53Resolver
partition string
accountid string
supportedplatforms []string
Expand Down Expand Up @@ -554,6 +556,7 @@ func (c *Config) Client() (interface{}, error) {
client.opsworksconn = opsworks.New(sess)
client.organizationsconn = organizations.New(sess)
client.r53conn = route53.New(r53Sess)
client.r53resolverconn = route53resolver.New(sess)
client.rdsconn = rds.New(awsRdsSess)
client.redshiftconn = redshift.New(sess)
client.simpledbconn = simpledb.New(sess)
Expand Down
2 changes: 2 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ func Provider() terraform.ResourceProvider {
"aws_route53_zone_association": resourceAwsRoute53ZoneAssociation(),
"aws_route53_zone": resourceAwsRoute53Zone(),
"aws_route53_health_check": resourceAwsRoute53HealthCheck(),
"aws_route53_resolver_rule_association": resourceAwsRoute53ResolverRuleAssociation(),
"aws_route53_resolver_rule": resourceAwsRoute53ResolverRule(),
"aws_route": resourceAwsRoute(),
"aws_route_table": resourceAwsRouteTable(),
"aws_default_route_table": resourceAwsDefaultRouteTable(),
Expand Down
317 changes: 317 additions & 0 deletions aws/resource_aws_route53_resolver_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,317 @@
package aws

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53resolver"
)

const (
Route53ResolverRuleStatusCreating = "CREATING"
Route53ResolverRuleStatusDeleted = "DELETED"
)

func resourceAwsRoute53ResolverRule() *schema.Resource {
return &schema.Resource{
Create: resourceAwsRoute53ResolverRuleCreate,
Read: resourceAwsRoute53ResolverRuleRead,
Update: resourceAwsRoute53ResolverRuleUpdate,
Delete: resourceAwsRoute53ResolverRuleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},

"domain_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: suppressRoute53ZoneNameWithTrailingDot,
ValidateFunc: validation.StringLenBetween(1, 256),
},

"name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 64),
},

"resolver_endpoint_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 64),
},

"rule_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
route53resolver.RuleTypeOptionForward,
route53resolver.RuleTypeOptionSystem,
route53resolver.RuleTypeOptionRecursive,
}, false),
},

"target_ip": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.SingleIP(),
},
"port": {
Type: schema.TypeInt,
Optional: true,
Default: 53,
ValidateFunc: validation.IntBetween(0, 65535),
},
},
},
},

"tags": tagsSchema(),
},
}
}

func resourceAwsRoute53ResolverRuleCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).r53resolverconn

req := &route53resolver.CreateResolverRuleInput{
CreatorRequestId: aws.String(resource.UniqueId()),
DomainName: aws.String(d.Get("domain_name").(string)),
RuleType: aws.String(d.Get("rule_type").(string)),
}

if v, ok := d.GetOk("name"); ok {
req.Name = aws.String(v.(string))
}

if v, ok := d.GetOk("resolver_endpoint_id"); ok {
req.ResolverEndpointId = aws.String(v.(string))
}

if v, ok := d.GetOk("target_ip"); ok {
req.TargetIps = expandTargetIps(v.([]interface{}))
}

if v, ok := d.GetOk("tags"); ok {
req.Tags = tagsFromMapRoute53Resolver(v.(map[string]interface{}))
}

log.Printf("[DEBUG] Creating Route 53 Resolver rule: %s", req)

res, err := conn.CreateResolverRule(req)
if err != nil {
return fmt.Errorf("Error creating Route 53 Resolver rule: %s", err)
}

d.SetId(*res.ResolverRule.Id)

stateConf := &resource.StateChangeConf{
Pending: []string{Route53ResolverRuleStatusCreating},
Target: []string{route53resolver.ResolverRuleStatusComplete},
Refresh: resourceAwsRoute53ResolverRuleStateRefresh(conn, d.Id()),
Timeout: d.Timeout(schema.TimeoutCreate),
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for Route 53 Resolver rule (%s) to be created: %s", d.Id(), err)
}

return resourceAwsRoute53ResolverRuleRead(d, meta)
}

func resourceAwsRoute53ResolverRuleRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).r53resolverconn

req := &route53resolver.GetResolverRuleInput{
ResolverRuleId: aws.String(d.Id()),
}

res, err := conn.GetResolverRule(req)
if err != nil {
if isAWSErr(err, route53resolver.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] No Route 53 Resolver rule by Id (%s) found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Route 53 Resolver rule %s: %s", d.Id(), err)
}

rule := res.ResolverRule

d.Set("arn", rule.Arn)
d.Set("domain_name", rule.DomainName)
d.Set("name", rule.Name)
d.Set("resolver_endpoint_id", rule.ResolverEndpointId)
d.Set("rule_type", rule.RuleType)
d.Set("target_ip", flattenTargetIps(rule.TargetIps))

err = getTagsRoute53Resolver(conn, d, d.Get("arn").(string))
if err != nil {
return fmt.Errorf("Error reading Route 53 Resolver rule tags %s: %s", d.Id(), err)
}

return nil
}

func resourceAwsRoute53ResolverRuleUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).r53resolverconn

d.Partial(true)

if d.HasChange("name") || d.HasChange("resolver_endpoint_id") || d.HasChange("target_ip") {
config := &route53resolver.ResolverRuleConfig{}

if v, ok := d.GetOk("name"); ok {
config.Name = aws.String(v.(string))
}

if v, ok := d.GetOk("resolver_endpoint_id"); ok {
config.ResolverEndpointId = aws.String(v.(string))
}

if v, ok := d.GetOk("target_ip"); ok {
config.TargetIps = expandTargetIps(v.([]interface{}))
}

req := &route53resolver.UpdateResolverRuleInput{
ResolverRuleId: aws.String(d.Id()),
Config: config,
}

_, err := conn.UpdateResolverRule(req)
if err != nil {
if isAWSErr(err, route53resolver.ErrCodeUnknownResourceException, "") {
log.Printf("[WARN] No Route 53 Resolver rule by Id (%s) found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error updating Route 53 Resolver rule %s: %s", d.Id(), err)
}

d.SetPartial("name")
d.SetPartial("resolver_endpoint_id")
d.SetPartial("target_ip")
}

stateConf := &resource.StateChangeConf{
Pending: []string{route53resolver.ResolverRuleStatusUpdating},
Target: []string{route53resolver.ResolverRuleStatusComplete},
Refresh: resourceAwsRoute53ResolverRuleStateRefresh(conn, d.Id()),
Timeout: d.Timeout(schema.TimeoutUpdate),
}
_, err := stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for Route 53 Resolver rule (%s) to be updated: %s", d.Id(), err)
}

if d.HasChange("tags") {
err := setTagsRoute53Resolver(conn, d, d.Get("arn").(string))
if err != nil {
return fmt.Errorf("Error updating Route 53 Resolver rule tags: %s", err)
}
d.SetPartial("tags")
}

d.Partial(false)

return resourceAwsRoute53ResolverRuleRead(d, meta)
}

func resourceAwsRoute53ResolverRuleDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).r53resolverconn

req := &route53resolver.DeleteResolverRuleInput{
ResolverRuleId: aws.String(d.Id()),
}

_, err := conn.DeleteResolverRule(req)
if err != nil {
if isAWSErr(err, route53resolver.ErrCodeResourceNotFoundException, "") {
return nil
}
return fmt.Errorf("Error deleting Route 53 Resolver rule %s: %s", d.Id(), err)
}

stateConf := &resource.StateChangeConf{
Pending: []string{route53resolver.ResolverRuleStatusDeleting},
Target: []string{Route53ResolverRuleStatusDeleted},
Refresh: resourceAwsRoute53ResolverRuleStateRefresh(conn, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for Route 53 Resolver rule (%s) to be deleted: %s", d.Id(), err)
}

return nil
}

func resourceAwsRoute53ResolverRuleStateRefresh(conn *route53resolver.Route53Resolver, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {

req := &route53resolver.GetResolverRuleInput{
ResolverRuleId: aws.String(id),
}

res, err := conn.GetResolverRule(req)
if err != nil {
if isAWSErr(err, route53resolver.ErrCodeResourceNotFoundException, "") {
return "", Route53ResolverRuleStatusDeleted, nil
}
return nil, "", err
}

return res.ResolverRule, aws.StringValue(res.ResolverRule.Status), nil
}
}

func expandTargetIps(tips []interface{}) []*route53resolver.TargetAddress {
tas := make([]*route53resolver.TargetAddress, len(tips), len(tips))

for i, tip := range tips {
ta := tip.(map[string]interface{})
tas[i] = &route53resolver.TargetAddress{
Ip: aws.String(ta["ip"].(string)),
Port: aws.Int64(int64(ta["port"].(int))),
}
}

return tas
}

func flattenTargetIps(tas []*route53resolver.TargetAddress) []interface{} {
tips := make([]interface{}, len(tas), len(tas))

for i, ta := range tas {
tips[i] = map[string]interface{}{
"ip": *ta.Ip,
"port": *ta.Port,
}
}
return tips
}
Loading