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

provider: ignore cloud: system tag key prefix as well as aws: #56

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions internal/service/ec2/ec2_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

func ResourceInstance() *schema.Resource {
//lintignore:R011
// lintignore:R011
return &schema.Resource{
Create: resourceInstanceCreate,
Read: resourceInstanceRead,
Expand Down Expand Up @@ -1091,7 +1091,7 @@ func resourceInstanceRead(d *schema.ResourceData, meta interface{}) error {

tags := KeyValueTags(instance.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
// lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}
Expand Down Expand Up @@ -3183,7 +3183,7 @@ func flattenInstanceLaunchTemplate(conn *ec2.EC2, instanceID, previousLaunchTemp
}

func findInstanceLaunchTemplateID(conn *ec2.EC2, id string) (string, error) {
launchTemplateID, err := findInstanceTagValue(conn, id, "aws:ec2launchtemplate:id")
launchTemplateID, err := findInstanceTagValue(conn, id, "cloud:ec2launchtemplate:id")

if err != nil {
return "", fmt.Errorf("reading EC2 Instance (%s) launch template ID tag: %w", id, err)
Expand All @@ -3193,7 +3193,7 @@ func findInstanceLaunchTemplateID(conn *ec2.EC2, id string) (string, error) {
}

func findInstanceLaunchTemplateVersion(conn *ec2.EC2, id string) (string, error) {
launchTemplateVersion, err := findInstanceTagValue(conn, id, "aws:ec2launchtemplate:version")
launchTemplateVersion, err := findInstanceTagValue(conn, id, "cloud:ec2launchtemplate:version")

if err != nil {
return "", fmt.Errorf("reading EC2 Instance (%s) launch template version tag: %w", id, err)
Expand Down
17 changes: 13 additions & 4 deletions internal/tags/key_value_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
const (
AwsTagKeyPrefix = `aws:`
SystemTagKeyPrefix = `system:`
CloudTagKeyPrefix = `cloud:`
ElasticbeanstalkTagKeyPrefix = `elasticbeanstalk:`
NameTagKey = `Name`
RdsTagKeyPrefix = `rds:`
Expand Down Expand Up @@ -42,7 +43,9 @@ func (tags KeyValueTags) IgnoreAWS() KeyValueTags {
result := make(KeyValueTags)

for k, v := range tags {
if !strings.HasPrefix(k, AwsTagKeyPrefix) && !strings.HasPrefix(k, SystemTagKeyPrefix) {
if !strings.HasPrefix(k, AwsTagKeyPrefix) &&
!strings.HasPrefix(k, SystemTagKeyPrefix) &&
!strings.HasPrefix(k, CloudTagKeyPrefix) {
result[k] = v
}
}
Expand Down Expand Up @@ -106,7 +109,9 @@ func (tags KeyValueTags) IgnoreElasticbeanstalk() KeyValueTags {
result := make(KeyValueTags)

for k, v := range tags {
if strings.HasPrefix(k, AwsTagKeyPrefix) || strings.HasPrefix(k, SystemTagKeyPrefix) {
if strings.HasPrefix(k, AwsTagKeyPrefix) ||
strings.HasPrefix(k, SystemTagKeyPrefix) ||
strings.HasPrefix(k, CloudTagKeyPrefix) {
continue
}

Expand Down Expand Up @@ -153,7 +158,9 @@ func (tags KeyValueTags) IgnoreRds() KeyValueTags {
result := make(KeyValueTags)

for k, v := range tags {
if strings.HasPrefix(k, AwsTagKeyPrefix) || strings.HasPrefix(k, SystemTagKeyPrefix) {
if strings.HasPrefix(k, AwsTagKeyPrefix) ||
strings.HasPrefix(k, SystemTagKeyPrefix) ||
strings.HasPrefix(k, CloudTagKeyPrefix) {
continue
}

Expand All @@ -172,7 +179,9 @@ func (tags KeyValueTags) IgnoreServerlessApplicationRepository() KeyValueTags {
result := make(KeyValueTags)

for k, v := range tags {
if strings.HasPrefix(k, AwsTagKeyPrefix) || strings.HasPrefix(k, SystemTagKeyPrefix) {
if strings.HasPrefix(k, AwsTagKeyPrefix) ||
strings.HasPrefix(k, SystemTagKeyPrefix) ||
strings.HasPrefix(k, CloudTagKeyPrefix) {
continue
}

Expand Down
64 changes: 36 additions & 28 deletions internal/tags/key_value_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,24 +305,28 @@ func TestKeyValueTagsIgnoreAWS(t *testing.T) {
{
name: "all",
tags: New(map[string]string{
"aws:cloudformation:key1": "value1",
"aws:cloudformation:key2": "value2",
"aws:cloudformation:key3": "value3",
"system:key4": "value4",
"aws:cloudformation:key1": "value1",
"aws:cloudformation:key2": "value2",
"aws:cloudformation:key3": "value3",
"cloud:cloudformation:key4": "value4",
"cloud:cloudformation:key5": "value5",
"cloud:cloudformation:key6": "value6",
"system:key7": "value7",
}),
want: map[string]string{},
},
{
name: "mixed",
tags: New(map[string]string{
"aws:cloudformation:key1": "value1",
"system:key2": "value2",
"key3": "value3",
"key4": "value4",
"aws:cloudformation:key1": "value1",
"cloud:cloudformation:key2": "value2",
"system:key3": "value3",
"key4": "value4",
"key5": "value5",
}),
want: map[string]string{
"key3": "value3",
"key4": "value4",
"key5": "value5",
},
},
{
Expand Down Expand Up @@ -560,26 +564,28 @@ func TestKeyValueTagsIgnoreElasticbeanstalk(t *testing.T) {
{
name: "all",
tags: New(map[string]string{
"aws:cloudformation:key1": "value1",
"system:key2": "value2",
"elasticbeanstalk:key3": "value3",
"Name": "value4",
"aws:cloudformation:key1": "value1",
"cloud:cloudformation:key2": "value2",
"system:key3": "value3",
"elasticbeanstalk:key4": "value4",
"Name": "value5",
}),
want: map[string]string{},
},
{
name: "mixed",
tags: New(map[string]string{
"aws:cloudformation:key1": "value1",
"system:key2": "value2",
"key3": "value3",
"elasticbeanstalk:key4": "value4",
"key5": "value5",
"Name": "value6",
"cloud:cloudformation:key2": "value2",
"system:key3": "value3",
"key4": "value4",
"elasticbeanstalk:key5": "value5",
"key6": "value6",
"Name": "value7",
}),
want: map[string]string{
"key3": "value3",
"key5": "value5",
"key4": "value4",
"key6": "value6",
},
},
{
Expand Down Expand Up @@ -708,23 +714,25 @@ func TestKeyValueTagsIgnoreRds(t *testing.T) {
name: "all",
tags: New(map[string]string{
"aws:cloudformation:key1": "value1",
"system:key2": "value2",
"rds:key3": "value3",
"cloud:cloudformation:key2": "value2",
"system:key3": "value3",
"rds:key4": "value4",
}),
want: map[string]string{},
},
{
name: "mixed",
tags: New(map[string]string{
"aws:cloudformation:key1": "value1",
"system:key2": "value2",
"key3": "value3",
"rds:key4": "value4",
"key5": "value5",
"cloud:cloudformation:key2": "value2",
"system:key3": "value3",
"key4": "value4",
"rds:key5": "value5",
"key6": "value6",
}),
want: map[string]string{
"key3": "value3",
"key5": "value5",
"key4": "value4",
"key6": "value6",
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion internal/verify/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func ValidLaunchTemplateID(v interface{}, k string) (ws []string, errors []error
errors = append(errors, fmt.Errorf("%q cannot be shorter than 1 character", k))
} else if len(value) > 255 {
errors = append(errors, fmt.Errorf("%q cannot be longer than 255 characters", k))
} else if !regexp.MustCompile(`^lt\-[a-z0-9]+$`).MatchString(value) {
} else if !regexp.MustCompile(`^lt\-[a-zA-Z0-9]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must begin with 'lt-' and be comprised of only alphanumeric characters: %v", k, value))
}
Expand Down
1 change: 1 addition & 0 deletions internal/verify/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ func TestValidLaunchTemplateName(t *testing.T) {
func TestValidLaunchTemplateID(t *testing.T) {
validIds := []string{
"lt-foobar123456",
"lt-FOOBAR123456",
}
for _, v := range validIds {
_, errors := ValidLaunchTemplateID(v, "id")
Expand Down
5 changes: 1 addition & 4 deletions website/docs/r/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ Any other instance parameters that you specify will override the same parameters

The `launch_template` block supports the following:

* `id` - The ID of the launch template.
* `name` - The name of the launch template.
* `version` - Template version. Valid values are a specific version number, `$Latest`, `$Default`. Defaults to `$Default`.

Expand All @@ -205,10 +206,6 @@ For `ebs_block_device`, in addition to the arguments above, the following attrib

* `volume_id` - ID of the volume. For example, the ID can be accessed like this, `aws_instance.web.ebs_block_device.2.volume_id`.

For `launch_template`, in addition to the arguments above, the following attribute is exported:

* `id` - The ID of the launch template.

For `root_block_device`, in addition to the arguments above, the following attributes are exported:

* `volume_id` - ID of the volume. For example, the ID can be accessed like this, `aws_instance.web.root_block_device.0.volume_id`.
Expand Down