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

resource/aws_route53_resolver_firewall_rule_group: Add new resource #18554

Merged
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
19 changes: 19 additions & 0 deletions aws/internal/service/route53resolver/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,22 @@ func ResolverDnssecConfigByID(conn *route53resolver.Route53Resolver, dnssecConfi

return config, nil
}

// FirewallRuleGroupByID returns the DNS Firewall rule group corresponding to the specified ID.
// Returns nil if no DNS Firewall rule group is found.
func FirewallRuleGroupByID(conn *route53resolver.Route53Resolver, firewallGroupId string) (*route53resolver.FirewallRuleGroup, error) {
input := &route53resolver.GetFirewallRuleGroupInput{
FirewallRuleGroupId: aws.String(firewallGroupId),
}

output, err := conn.GetFirewallRuleGroup(input)
if err != nil {
return nil, err
}

if output == nil {
return nil, nil
}

return output.FirewallRuleGroup, nil
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,7 @@ func Provider() *schema.Provider {
"aws_route53_health_check": resourceAwsRoute53HealthCheck(),
"aws_route53_resolver_dnssec_config": resourceAwsRoute53ResolverDnssecConfig(),
"aws_route53_resolver_endpoint": resourceAwsRoute53ResolverEndpoint(),
"aws_route53_resolver_firewall_rule_group": resourceAwsRoute53ResolverFirewallRuleGroup(),
"aws_route53_resolver_query_log_config": resourceAwsRoute53ResolverQueryLogConfig(),
"aws_route53_resolver_query_log_config_association": resourceAwsRoute53ResolverQueryLogConfigAssociation(),
"aws_route53_resolver_rule_association": resourceAwsRoute53ResolverRuleAssociation(),
Expand Down
150 changes: 150 additions & 0 deletions aws/resource_aws_route53_resolver_firewall_rule_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53resolver"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/route53resolver/finder"
)

func resourceAwsRoute53ResolverFirewallRuleGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAwsRoute53ResolverFirewallRuleGroupCreate,
Read: resourceAwsRoute53ResolverFirewallRuleGroupRead,
Update: resourceAwsRoute53ResolverFirewallRuleGroupUpdate,
Delete: resourceAwsRoute53ResolverFirewallRuleGroupDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

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

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

"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateRoute53ResolverName,
},

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

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

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

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

input := &route53resolver.CreateFirewallRuleGroupInput{
CreatorRequestId: aws.String(resource.PrefixedUniqueId("tf-r53-resolver-firewall-rule-group-")),
Name: aws.String(d.Get("name").(string)),
}
if v, ok := d.GetOk("tags"); ok && len(v.(map[string]interface{})) > 0 {
input.Tags = keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().Route53resolverTags()
}

log.Printf("[DEBUG] Creating Route 53 Resolver DNS Firewall rule group: %#v", input)
output, err := conn.CreateFirewallRuleGroup(input)
if err != nil {
return fmt.Errorf("error creating Route 53 Resolver DNS Firewall rule group: %w", err)
}

d.SetId(aws.StringValue(output.FirewallRuleGroup.Id))

return resourceAwsRoute53ResolverFirewallRuleGroupRead(d, meta)
}

func resourceAwsRoute53ResolverFirewallRuleGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).route53resolverconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

ruleGroup, err := finder.FirewallRuleGroupByID(conn, d.Id())

if isAWSErr(err, route53resolver.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] Route53 Resolver DNS Firewall rule group (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error getting Route 53 Resolver DNS Firewall rule group (%s): %w", d.Id(), err)
}

if ruleGroup == nil {
log.Printf("[WARN] Route 53 Resolver DNS Firewall rule group (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

arn := aws.StringValue(ruleGroup.Arn)
d.Set("arn", arn)
d.Set("id", ruleGroup.Id)
d.Set("name", ruleGroup.Name)
d.Set("owner_id", ruleGroup.OwnerId)
d.Set("share_status", ruleGroup.ShareStatus)

tags, err := keyvaluetags.Route53resolverListTags(conn, arn)
if err != nil {
return fmt.Errorf("error listing tags for Route53 Resolver DNS Firewall rule group (%s): %w", arn, err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

return nil
}

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

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if err := keyvaluetags.Route53resolverUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating Route53 Resolver DNS Firewall rule group (%s) tags: %w", d.Get("arn").(string), err)
}
}

return resourceAwsRoute53ResolverFirewallRuleGroupRead(d, meta)
}

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

_, err := conn.DeleteFirewallRuleGroup(&route53resolver.DeleteFirewallRuleGroupInput{
FirewallRuleGroupId: aws.String(d.Id()),
})

if isAWSErr(err, route53resolver.ErrCodeResourceNotFoundException, "") {
return nil
}

if err != nil {
return fmt.Errorf("error deleting Route 53 Resolver DNS Firewall rule group (%s): %w", d.Id(), err)
}

return nil
}
Loading