forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hashicorp#19391 from wjam/default_tag_data_source
Expose the default tags configured on a provider as a data source
- Loading branch information
Showing
5 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-notes:new-data-source | ||
aws_default_tags | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" {}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`). |