Skip to content

Commit

Permalink
Merge pull request #17974 from hashicorp/f-resource-default-tags-poc
Browse files Browse the repository at this point in the history
[Proof of Concept] Default tags implementation: provider and subnet/vpc resources
  • Loading branch information
anGie44 authored Mar 18, 2021
2 parents c513562 + 6560fef commit f77fe16
Show file tree
Hide file tree
Showing 14 changed files with 1,671 additions and 93 deletions.
15 changes: 15 additions & 0 deletions .changelog/17974.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```release-note:note
provider: New `default_tags` argument as a public preview for applying tags across all resources under a provider. Support for the functionality must be added to individual resources in the codebase and is only implemented for the `aws_subnet` and `aws_vpc` resources at this time. Until a general availability announcement, no compatibility promises are made with these provider arguments and their functionality.
```

```release-note:enhancement
provider: Add `default_tags` argument (in public preview, see note above)


```release-note:enhancement
resource/aws_subnet: Support provider-wide default tags (in public preview, see note above)
```

```release-note:enhancement
resource/aws_vpc: Support provider-wide default tags (in public preview, see note above)
```
9 changes: 6 additions & 3 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,10 @@ type Config struct {
AllowedAccountIds []string
ForbiddenAccountIds []string

Endpoints map[string]string
IgnoreTagsConfig *keyvaluetags.IgnoreConfig
Insecure bool
DefaultTagsConfig *keyvaluetags.DefaultConfig
Endpoints map[string]string
IgnoreTagsConfig *keyvaluetags.IgnoreConfig
Insecure bool

SkipCredsValidation bool
SkipGetEC2Platforms bool
Expand Down Expand Up @@ -251,6 +252,7 @@ type AWSClient struct {
datapipelineconn *datapipeline.DataPipeline
datasyncconn *datasync.DataSync
daxconn *dax.DAX
DefaultTagsConfig *keyvaluetags.DefaultConfig
devicefarmconn *devicefarm.DeviceFarm
dlmconn *dlm.DLM
dmsconn *databasemigrationservice.DatabaseMigrationService
Expand Down Expand Up @@ -495,6 +497,7 @@ func (c *Config) Client() (interface{}, error) {
datapipelineconn: datapipeline.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["datapipeline"])})),
datasyncconn: datasync.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["datasync"])})),
daxconn: dax.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["dax"])})),
DefaultTagsConfig: c.DefaultTagsConfig,
devicefarmconn: devicefarm.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["devicefarm"])})),
dlmconn: dlm.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["dlm"])})),
dmsconn: databasemigrationservice.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["dms"])})),
Expand Down
56 changes: 56 additions & 0 deletions aws/internal/keyvaluetags/key_value_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ const (
ServerlessApplicationRepositoryTagKeyPrefix = `serverlessrepo:`
)

// DefaultConfig contains tags to default across all resources.
type DefaultConfig struct {
Tags KeyValueTags
}

// IgnoreConfig contains various options for removing resource tags.
type IgnoreConfig struct {
Keys KeyValueTags
Expand All @@ -50,6 +55,36 @@ func (tags KeyValueTags) IgnoreAws() KeyValueTags {
return result
}

// MergeTags returns the result of keyvaluetags.Merge() on the given
// DefaultConfig.Tags with KeyValueTags provided as an argument,
// overriding the value of any tag with a matching key.
func (dc *DefaultConfig) MergeTags(tags KeyValueTags) KeyValueTags {
if dc == nil || dc.Tags == nil {
return tags
}

return dc.Tags.Merge(tags)
}

// TagsEqual returns true if the given configuration's Tags
// are equal to those passed in as an argument;
// otherwise returns false
func (dc *DefaultConfig) TagsEqual(tags KeyValueTags) bool {
if dc == nil || dc.Tags == nil {
return tags == nil
}

if tags == nil {
return false
}

if len(tags) == 0 {
return len(dc.Tags) == 0
}

return dc.Tags.ContainsAll(tags)
}

// IgnoreConfig returns any tags not removed by a given configuration.
func (tags KeyValueTags) IgnoreConfig(config *IgnoreConfig) KeyValueTags {
if config == nil {
Expand Down Expand Up @@ -401,6 +436,27 @@ func (tags KeyValueTags) Hash() int {
return hash
}

// RemoveDefaultConfig returns tags not present in a DefaultConfig object
// in addition to tags with key/value pairs that override those in a DefaultConfig;
// however, if all tags present in the DefaultConfig object are equivalent to those
// in the given KeyValueTags, then the KeyValueTags are returned, effectively
// bypassing the need to remove differing tags.
func (tags KeyValueTags) RemoveDefaultConfig(dc *DefaultConfig) KeyValueTags {
if dc == nil || dc.Tags == nil {
return tags
}

result := make(KeyValueTags)

for k, v := range tags {
if defaultVal, ok := dc.Tags[k]; !ok || !v.Equal(defaultVal) {
result[k] = v
}
}

return result
}

// String returns the default string representation of the KeyValueTags.
func (tags KeyValueTags) String() string {
var builder strings.Builder
Expand Down
Loading

0 comments on commit f77fe16

Please sign in to comment.