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

[Proof of Concept] Default tags implementation: provider and subnet/vpc resources #17974

Merged
merged 4 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
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
aws_subnet: Support provider-wide default tags (in public preview, see note above)
anGie44 marked this conversation as resolved.
Show resolved Hide resolved
```

```release-note:enhancement
aws_vpc: Support provider-wide default tags (in public preview, see note above)
anGie44 marked this conversation as resolved.
Show resolved Hide resolved
```
9 changes: 6 additions & 3 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,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 @@ -249,6 +250,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 @@ -492,6 +494,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