Skip to content

Commit

Permalink
Merge pull request #14901 from ewbankkit/f-aws_route53_resolver_query…
Browse files Browse the repository at this point in the history
…_log_config_association-resource

r/aws_route53_resolver_query_log_config_association: New resource
  • Loading branch information
breathingdust authored Sep 21, 2020
2 parents a0470ce + 29bb5d0 commit 2f81e85
Show file tree
Hide file tree
Showing 8 changed files with 433 additions and 0 deletions.
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 @@ -5,6 +5,25 @@ import (
"github.com/aws/aws-sdk-go/service/route53resolver"
)

// ResolverQueryLogConfigAssociationByID returns the query logging configuration association corresponding to the specified ID.
// Returns nil if no configuration is found.
func ResolverQueryLogConfigAssociationByID(conn *route53resolver.Route53Resolver, queryLogConfigAssociationID string) (*route53resolver.ResolverQueryLogConfigAssociation, error) {
input := &route53resolver.GetResolverQueryLogConfigAssociationInput{
ResolverQueryLogConfigAssociationId: aws.String(queryLogConfigAssociationID),
}

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

if output == nil {
return nil, nil
}

return output.ResolverQueryLogConfigAssociation, nil
}

// ResolverQueryLogConfigByID returns the query logging configuration corresponding to the specified ID.
// Returns nil if no configuration is found.
func ResolverQueryLogConfigByID(conn *route53resolver.Route53Resolver, queryLogConfigID string) (*route53resolver.ResolverQueryLogConfig, error) {
Expand Down
24 changes: 24 additions & 0 deletions aws/internal/service/route53resolver/waiter/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,34 @@ import (
)

const (
resolverQueryLogConfigAssociationStatusNotFound = "NotFound"
resolverQueryLogConfigAssociationStatusUnknown = "Unknown"

resolverQueryLogConfigStatusNotFound = "NotFound"
resolverQueryLogConfigStatusUnknown = "Unknown"
)

// QueryLogConfigAssociationStatus fetches the QueryLogConfigAssociation and its Status
func QueryLogConfigAssociationStatus(conn *route53resolver.Route53Resolver, queryLogConfigAssociationID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
queryLogConfigAssociation, err := finder.ResolverQueryLogConfigAssociationByID(conn, queryLogConfigAssociationID)

if tfawserr.ErrCodeEquals(err, route53resolver.ErrCodeResourceNotFoundException) {
return nil, resolverQueryLogConfigAssociationStatusNotFound, nil
}

if err != nil {
return nil, resolverQueryLogConfigAssociationStatusUnknown, err
}

if queryLogConfigAssociation == nil {
return nil, resolverQueryLogConfigAssociationStatusNotFound, nil
}

return queryLogConfigAssociation, aws.StringValue(queryLogConfigAssociation.Status), nil
}
}

// QueryLogConfigStatus fetches the QueryLogConfig and its Status
func QueryLogConfigStatus(conn *route53resolver.Route53Resolver, queryLogConfigID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
Expand Down
42 changes: 42 additions & 0 deletions aws/internal/service/route53resolver/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,55 @@ import (
)

const (
// Maximum amount of time to wait for a QueryLogConfigAssociation to return ACTIVE
QueryLogConfigAssociationCreatedTimeout = 5 * time.Minute

// Maximum amount of time to wait for a QueryLogConfigAssociation to be deleted
QueryLogConfigAssociationDeletedTimeout = 5 * time.Minute

// Maximum amount of time to wait for a QueryLogConfig to return CREATED
QueryLogConfigCreatedTimeout = 5 * time.Minute

// Maximum amount of time to wait for a QueryLogConfig to be deleted
QueryLogConfigDeletedTimeout = 5 * time.Minute
)

// QueryLogConfigAssociationCreated waits for a QueryLogConfig to return ACTIVE
func QueryLogConfigAssociationCreated(conn *route53resolver.Route53Resolver, queryLogConfigAssociationID string) (*route53resolver.ResolverQueryLogConfigAssociation, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{route53resolver.ResolverQueryLogConfigAssociationStatusCreating},
Target: []string{route53resolver.ResolverQueryLogConfigAssociationStatusActive},
Refresh: QueryLogConfigAssociationStatus(conn, queryLogConfigAssociationID),
Timeout: QueryLogConfigAssociationCreatedTimeout,
}

outputRaw, err := stateConf.WaitForState()

if v, ok := outputRaw.(*route53resolver.ResolverQueryLogConfigAssociation); ok {
return v, err
}

return nil, err
}

// QueryLogConfigAssociationCreated waits for a QueryLogConfig to be deleted
func QueryLogConfigAssociationDeleted(conn *route53resolver.Route53Resolver, queryLogConfigAssociationID string) (*route53resolver.ResolverQueryLogConfigAssociation, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{route53resolver.ResolverQueryLogConfigAssociationStatusDeleting},
Target: []string{},
Refresh: QueryLogConfigAssociationStatus(conn, queryLogConfigAssociationID),
Timeout: QueryLogConfigAssociationDeletedTimeout,
}

outputRaw, err := stateConf.WaitForState()

if v, ok := outputRaw.(*route53resolver.ResolverQueryLogConfigAssociation); ok {
return v, err
}

return nil, err
}

// QueryLogConfigCreated waits for a QueryLogConfig to return CREATED
func QueryLogConfigCreated(conn *route53resolver.Route53Resolver, queryLogConfigID string) (*route53resolver.ResolverQueryLogConfig, error) {
stateConf := &resource.StateChangeConf{
Expand Down
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ func Provider() *schema.Provider {
"aws_route53_health_check": resourceAwsRoute53HealthCheck(),
"aws_route53_resolver_endpoint": resourceAwsRoute53ResolverEndpoint(),
"aws_route53_resolver_query_log_config": resourceAwsRoute53ResolverQueryLogConfig(),
"aws_route53_resolver_query_log_config_association": resourceAwsRoute53ResolverQueryLogConfigAssociation(),
"aws_route53_resolver_rule_association": resourceAwsRoute53ResolverRuleAssociation(),
"aws_route53_resolver_rule": resourceAwsRoute53ResolverRule(),
"aws_route": resourceAwsRoute(),
Expand Down
116 changes: 116 additions & 0 deletions aws/resource_aws_route53_resolver_query_log_config_association.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
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/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/route53resolver/finder"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/route53resolver/waiter"
)

func resourceAwsRoute53ResolverQueryLogConfigAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceAwsRoute53ResolverQueryLogConfigAssociationCreate,
Read: resourceAwsRoute53ResolverQueryLogConfigAssociationRead,
Delete: resourceAwsRoute53ResolverQueryLogConfigAssociationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"resolver_query_log_config_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"resource_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

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

input := &route53resolver.AssociateResolverQueryLogConfigInput{
ResolverQueryLogConfigId: aws.String(d.Get("resolver_query_log_config_id").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
}

log.Printf("[DEBUG] Creating Route53 Resolver Query Log Config Association: %s", input)
output, err := conn.AssociateResolverQueryLogConfig(input)

if err != nil {
return fmt.Errorf("error creating Route53 Resolver Query Log Config Association: %w", err)
}

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

_, err = waiter.QueryLogConfigAssociationCreated(conn, d.Id())

if err != nil {
return fmt.Errorf("error waiting for Route53 Resolver Query Log Config Association (%s) to become available: %w", d.Id(), err)
}

return resourceAwsRoute53ResolverQueryLogConfigAssociationRead(d, meta)
}

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

queryLogConfigAssociation, err := finder.ResolverQueryLogConfigAssociationByID(conn, d.Id())

if isAWSErr(err, route53resolver.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] Route53 Resolver Query Log Config Association (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading Route53 Resolver Query Log Config Association (%s): %w", d.Id(), err)
}

if queryLogConfigAssociation == nil {
log.Printf("[WARN] Route53 Resolver Query Log Config Association (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

d.Set("resolver_query_log_config_id", queryLogConfigAssociation.ResolverQueryLogConfigId)
d.Set("resource_id", queryLogConfigAssociation.ResourceId)

return nil
}

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

log.Printf("[DEBUG] Deleting Route53 Resolver Query Log Config Association (%s)", d.Id())
_, err := conn.DisassociateResolverQueryLogConfig(&route53resolver.DisassociateResolverQueryLogConfigInput{
ResolverQueryLogConfigId: aws.String(d.Get("resolver_query_log_config_id").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
})

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

if err != nil {
return fmt.Errorf("error deleting Route53 Resolver Query Log Config Association (%s): %w", d.Id(), err)
}

_, err = waiter.QueryLogConfigAssociationDeleted(conn, d.Id())

if err != nil {
return fmt.Errorf("error waiting for Route53 Resolver Query Log Config Association (%s) to be deleted: %w", d.Id(), err)
}

return nil
}
Loading

0 comments on commit 2f81e85

Please sign in to comment.