Skip to content

Commit

Permalink
Merge pull request hashicorp#19391 from wjam/default_tag_data_source
Browse files Browse the repository at this point in the history
Expose the default tags configured on a provider as a data source
  • Loading branch information
YakDriver authored May 21, 2021
2 parents 1c840ba + 0051a61 commit 4ed718a
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/19391.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-notes:new-data-source
aws_default_tags
```
36 changes: 36 additions & 0 deletions aws/data_source_aws_default_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package aws

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceAwsDefaultTags() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsDefaultTagsRead,

Schema: map[string]*schema.Schema{
"tags": tagsSchemaComputed(),
},
}
}

func dataSourceAwsDefaultTagsRead(d *schema.ResourceData, meta interface{}) error {
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

d.SetId(meta.(*AWSClient).partition)

tags := defaultTagsConfig.GetTags()

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

return nil
}
122 changes: 122 additions & 0 deletions aws/data_source_aws_default_tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package aws

import (
"testing"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func TestAccAWSDefaultTagsDataSource_basic(t *testing.T) {
var providers []*schema.Provider

dataSourceName := "data.aws_default_tags.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID),
ProviderFactories: testAccProviderFactoriesInternal(&providers),
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: composeConfig(
testAccAWSProviderConfigDefaultTags_Tags1("first", "value"),
testAccAWSDefaultTagsDataSource(),
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(dataSourceName, "tags.first", "value"),
),
},
},
})
}

func TestAccAWSDefaultTagsDataSource_empty(t *testing.T) {
var providers []*schema.Provider

dataSourceName := "data.aws_default_tags.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID),
ProviderFactories: testAccProviderFactoriesInternal(&providers),
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: composeConfig(
testAccAWSProviderConfigDefaultTags_Tags0(),
testAccAWSDefaultTagsDataSource(),
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"),
),
},
},
})
}

func TestAccAWSDefaultTagsDataSource_multiple(t *testing.T) {
var providers []*schema.Provider

dataSourceName := "data.aws_default_tags.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID),
ProviderFactories: testAccProviderFactoriesInternal(&providers),
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: composeConfig(
testAccAWSProviderConfigDefaultTags_Tags2("nuera", "hijo", "escalofrios", "calambres"),
testAccAWSDefaultTagsDataSource(),
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(dataSourceName, "tags.nuera", "hijo"),
resource.TestCheckResourceAttr(dataSourceName, "tags.escalofrios", "calambres"),
),
},
},
})
}

func TestAccAWSDefaultTagsDataSource_ignore(t *testing.T) {
var providers []*schema.Provider

dataSourceName := "data.aws_default_tags.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID),
ProviderFactories: testAccProviderFactoriesInternal(&providers),
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: composeConfig(
testAccAWSProviderConfigDefaultTags_Tags1("Tabac", "Louis Chiron"),
testAccAWSDefaultTagsDataSource(),
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(dataSourceName, "tags.Tabac", "Louis Chiron"),
),
},
{
Config: composeConfig(
testAccProviderConfigDefaultAndIgnoreTagsKeys1("Tabac", "Louis Chiron"),
testAccAWSDefaultTagsDataSource(),
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"),
),
},
},
})
}

func testAccAWSDefaultTagsDataSource() string {
return `data "aws_default_tags" "test" {}`
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ func Provider() *schema.Provider {
"aws_codecommit_repository": dataSourceAwsCodeCommitRepository(),
"aws_codestarconnections_connection": dataSourceAwsCodeStarConnectionsConnection(),
"aws_cur_report_definition": dataSourceAwsCurReportDefinition(),
"aws_default_tags": dataSourceAwsDefaultTags(),
"aws_db_cluster_snapshot": dataSourceAwsDbClusterSnapshot(),
"aws_db_event_categories": dataSourceAwsDbEventCategories(),
"aws_db_instance": dataSourceAwsDbInstance(),
Expand Down
63 changes: 63 additions & 0 deletions website/docs/d/default_tags.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
subcategory: ""
layout: "aws"
page_title: "AWS: aws_default_tags"
description: |-
Access the default tags configured on the provider.
---

# Data Source: aws_default_Tags

Use this data source to get the default tags configured on the provider.

With this data source, you can apply default tags to resources not _directly_ managed by a Terraform resource, such as the instances underneath an Auto Scaling group or the volumes created for an EC2 instance.

## Example Usage

### Basic Usage

```terraform
data "aws_default_tags" "example" {}
```

### Dynamically Apply Default Tags to Auto Scaling Group

```terraform
provider "aws" {
default_tags {
tags = {
Environment = "Test"
Name = "Provider Tag"
}
}
}
data "aws_default_tags" "example" {}
resource "aws_autoscaling_group" "example" {
# ...
dynamic "tag" {
for_each = data.aws_default_tags.example.tags
content {
key = tag.key
value = tag.value
propagate_at_launch = true
}
}
}
```

## Argument Reference

This data source has no arguments.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `tags` - Blocks of default tags set on the provider. See details below.

### tags

* `key` - Key name of the tag (i.e., `tags.#.key`).
* `value` - Value of the tag (i.e., `tags.#.value`).

0 comments on commit 4ed718a

Please sign in to comment.