diff --git a/.changelog/29111.txt b/.changelog/29111.txt new file mode 100644 index 00000000000..18f1056616a --- /dev/null +++ b/.changelog/29111.txt @@ -0,0 +1,4 @@ +```release-note:new-data-source +aws_route53_resolver_query_log_config +``` + diff --git a/internal/service/route53resolver/query_log_config_data_source.go b/internal/service/route53resolver/query_log_config_data_source.go new file mode 100644 index 00000000000..4f60374b270 --- /dev/null +++ b/internal/service/route53resolver/query_log_config_data_source.go @@ -0,0 +1,132 @@ +package route53resolver + +import ( + "context" + "errors" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/route53resolver" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/create" + "github.com/hashicorp/terraform-provider-aws/internal/generate/namevaluesfilters" + tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/names" +) + +// @SDKDataSource("aws_route53_resolver_query_log_config") +func DataSourceQueryLogConfig() *schema.Resource { + return &schema.Resource{ + ReadWithoutTimeout: dataSourceQueryLogConfigRead, + + Schema: map[string]*schema.Schema{ + "arn": { + Type: schema.TypeString, + Computed: true, + }, + "destination_arn": { + Type: schema.TypeString, + Computed: true, + }, + "filter": namevaluesfilters.Schema(), + "name": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validResolverName, + }, + "owner_id": { + Type: schema.TypeString, + Computed: true, + }, + "resolver_query_log_config_id": { + Type: schema.TypeString, + Optional: true, + }, + "share_status": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tftags.TagsSchemaComputed(), + }, + } +} + +const ( + DSNameQueryLogConfig = "Query Log Config Data Source" +) + +func dataSourceQueryLogConfigRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).Route53ResolverConn() + + configID := d.Get("resolver_query_log_config_id").(string) + + input := &route53resolver.ListResolverQueryLogConfigsInput{} + + if v, ok := d.GetOk("filter"); ok && v.(*schema.Set).Len() > 0 { + input.Filters = namevaluesfilters.New(v.(*schema.Set)).Route53resolverFilters() + } + + var configs []*route53resolver.ResolverQueryLogConfig + + err := conn.ListResolverQueryLogConfigsPagesWithContext(ctx, input, func(page *route53resolver.ListResolverQueryLogConfigsOutput, lastPage bool) bool { + if page == nil { + return !lastPage + } + + for _, v := range page.ResolverQueryLogConfigs { + if configID != "" { + if aws.StringValue(v.Id) == configID { + configs = append(configs, v) + } + } else { + configs = append(configs, v) + } + } + + return !lastPage + }) + + if err != nil { + return diag.Errorf("listing Route53 resolver Query Logging Configurations: %s", err) + } + + if n := len(configs); n == 0 { + return create.DiagError(names.Route53Resolver, create.ErrActionReading, DSNameQueryLogConfig, configID, errors.New("your query returned no results, "+ + "please change your search criteria and try again")) + } else if n > 1 { + return create.DiagError(names.Route53Resolver, create.ErrActionReading, DSNameQueryLogConfig, configID, errors.New("your query returned more than one result, "+ + "please try more specific search criteria")) + } + + config := configs[0] + + d.SetId(aws.StringValue(config.Id)) + arn := aws.StringValue(config.Arn) + d.Set("arn", arn) + d.Set("destination_arn", config.DestinationArn) + d.Set("name", config.Name) + d.Set("owner_id", config.OwnerId) + d.Set("resolver_query_log_config_id", config.Id) + + shareStatus := aws.StringValue(config.ShareStatus) + d.Set("share_status", shareStatus) + + if shareStatus != route53resolver.ShareStatusSharedWithMe { + tags, err := ListTags(ctx, conn, arn) + + if err != nil { + return create.DiagError(names.AppConfig, create.ErrActionReading, DSNameQueryLogConfig, configID, err) + } + + ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig + tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig) + + //lintignore:AWSR002 + if err := d.Set("tags", tags.Map()); err != nil { + return create.DiagError(names.AppConfig, create.ErrActionSetting, DSNameQueryLogConfig, configID, err) + } + } + + return nil +} diff --git a/internal/service/route53resolver/query_log_config_data_source_test.go b/internal/service/route53resolver/query_log_config_data_source_test.go new file mode 100644 index 00000000000..6268548678c --- /dev/null +++ b/internal/service/route53resolver/query_log_config_data_source_test.go @@ -0,0 +1,96 @@ +package route53resolver_test + +import ( + "testing" + + "github.com/aws/aws-sdk-go/service/route53resolver" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" +) + +func TestAccRoute53ResolverQueryLogConfigDataSource_basic(t *testing.T) { + ctx := acctest.Context(t) + + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_route53_resolver_query_log_config.test" + dataSourceName := "data.aws_route53_resolver_query_log_config.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acctest.PreCheck(ctx, t) + acctest.PreCheckPartitionHasService(t, route53resolver.EndpointsID) + testAccPreCheck(ctx, t) + }, + ErrorCheck: acctest.ErrorCheck(t, route53resolver.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccQueryLogConfigDataSourceConfig_basic(rName, "key1", "value1"), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"), + resource.TestCheckResourceAttrPair(dataSourceName, "destination_arn", resourceName, "destination_arn"), + resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"), + resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"), + resource.TestCheckResourceAttrPair(dataSourceName, "owner_id", resourceName, "owner_id"), + resource.TestCheckResourceAttrPair(dataSourceName, "resolver_query_log_config_id", resourceName, "id"), + resource.TestCheckResourceAttrPair(dataSourceName, "share_status", resourceName, "share_status"), + resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"), + resource.TestCheckResourceAttrPair(dataSourceName, "tags.key1", resourceName, "tags.key1"), + ), + }, + }, + }) +} + +func TestAccRoute53ResolverQueryLogConfigDataSource_filter(t *testing.T) { + ctx := acctest.Context(t) + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_route53_resolver_query_log_config.test" + dataSourceName := "data.aws_route53_resolver_query_log_config.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acctest.PreCheck(ctx, t) + acctest.PreCheckPartitionHasService(t, route53resolver.EndpointsID) + testAccPreCheck(ctx, t) + }, + ErrorCheck: acctest.ErrorCheck(t, route53resolver.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccQueryLogConfigDataSourceConfig_filter(rName, "key1", "value1"), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"), + resource.TestCheckResourceAttrPair(dataSourceName, "destination_arn", resourceName, "destination_arn"), + resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"), + resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"), + resource.TestCheckResourceAttrPair(dataSourceName, "owner_id", resourceName, "owner_id"), + resource.TestCheckResourceAttrPair(dataSourceName, "resolver_query_log_config_id", resourceName, "id"), + resource.TestCheckResourceAttrPair(dataSourceName, "share_status", resourceName, "share_status"), + resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"), + resource.TestCheckResourceAttrPair(dataSourceName, "tags.key1", resourceName, "tags.key1"), + ), + }, + }, + }) +} + +func testAccQueryLogConfigDataSourceConfig_basic(rName string, tagKey string, tagValue string) string { + return acctest.ConfigCompose(testAccQueryLogConfigConfig_tags1(rName, tagKey, tagValue), ` +data "aws_route53_resolver_query_log_config" "test" { + resolver_query_log_config_id = aws_route53_resolver_query_log_config.test.id +} +`) +} + +func testAccQueryLogConfigDataSourceConfig_filter(rName string, tagKey string, tagValue string) string { + return acctest.ConfigCompose(testAccQueryLogConfigConfig_tags1(rName, tagKey, tagValue), ` +data "aws_route53_resolver_query_log_config" "test" { + filter { + name = "Name" + values = [aws_route53_resolver_query_log_config.test.name] + } +} +`) +} diff --git a/internal/service/route53resolver/service_package_gen.go b/internal/service/route53resolver/service_package_gen.go index 073ac5512ca..7a1361ba6f7 100644 --- a/internal/service/route53resolver/service_package_gen.go +++ b/internal/service/route53resolver/service_package_gen.go @@ -45,6 +45,10 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac Factory: DataSourceResolverFirewallRules, TypeName: "aws_route53_resolver_firewall_rules", }, + { + Factory: DataSourceQueryLogConfig, + TypeName: "aws_route53_resolver_query_log_config", + }, { Factory: DataSourceRule, TypeName: "aws_route53_resolver_rule", diff --git a/website/docs/d/route53_resolver_query_log_config.html.markdown b/website/docs/d/route53_resolver_query_log_config.html.markdown new file mode 100644 index 00000000000..23da30e6b01 --- /dev/null +++ b/website/docs/d/route53_resolver_query_log_config.html.markdown @@ -0,0 +1,52 @@ +--- +subcategory: "Route 53 Resolver" +layout: "aws" +page_title: "AWS: aws_route53_resolver_query_log_config" +description: |- + Provides details about a specific Route53 Resolver Query Logging Configuration. +--- + +# Data Source: aws_route53_resolver_query_log_config + +`aws_route53_resolver_query_log_config` provides details about a specific Route53 Resolver Query Logging Configuration. + +## Example Usage + +```terraform +data "aws_route53_resolver_query_log_config" "example" { + resolver_query_log_config_id = "rqlc-1abc2345ef678g91h" +} +``` + +```terraform +data "aws_route53_resolver_query_log_config" "example" { + filter { + name = "Name" + values = ["shared-query-log-config"] + } + + filter { + name = "ShareStatus" + values = ["SHARED_WITH_ME"] + } +} +``` + +## Argument Reference + +* `resolver_query_log_config_id` - (Optional) ID of the Route53 Resolver Query Logging Configuration. +* `filter` - (Optional) One or more name/value pairs to use as filters. There are +several valid keys, for a full reference, check out +[Route53resolver Filter value in the AWS API reference][1]. + +In addition to all arguments above, the following attributes are exported: + +* `id` - The ID for the query logging configuration. +* `arn` - Computed ARN of the Route53 Resolver Query Logging Configuration. +* `destination_arn` - The ARN of the resource that you want Resolver to send query logs: an Amazon S3 bucket, a CloudWatch Logs log group or a Kinesis Data Firehose delivery stream. +* `name` - The name of the query logging configuration. +* `owner_id` - The AWS account ID for the account that created the query logging configuration. +* `share_status` - An indication of whether the query logging configuration is shared with other AWS accounts or was shared with the current account by another AWS account. +* `tags` - Map of tags to assign to the service. + +[1]: https://docs.aws.amazon.com/Route53/latest/APIReference/API_route53resolver_Filter.html