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: Introduce tag resource generator, new aws_dynamodb_tag and aws_ecs_tag resources #13783

Merged
merged 41 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
e766fe8
Add 'namevaluesfilters' package.
ewbankkit May 13, 2020
0b010df
Add namevaluesfilters code generation.
ewbankkit May 13, 2020
af91aa9
Delete unused 'testNameValuesFiltersVerifyNames'.
ewbankkit May 13, 2020
97fa902
NameValuesFilters.Map eliminates duplicates removes empty values.
ewbankkit May 16, 2020
3ee1fb4
Additional AWS services.
ewbankkit May 16, 2020
c788eec
Support passing a '*schema.Set' to NameValuesFilters.New.
ewbankkit May 16, 2020
c48fbde
Add 'namevaluesfilters.Ec2Tags'.
ewbankkit May 17, 2020
e3a3027
Update documentation.
ewbankkit May 19, 2020
624ac04
r/aws_db_cluster_snapshot: Use 'namevaluefilters' in test sweeper.
ewbankkit May 29, 2020
2487452
d/aws_vpc: Use 'namevaluefilters'.
ewbankkit May 29, 2020
3e0088c
d/aws_autoscaling_groups: Use 'namevaluefilters'.
ewbankkit Jun 22, 2020
de2c3cf
Fix broken link in 'contribution-checklists.md'.
ewbankkit Aug 19, 2020
b6696b1
Additional AWS services.
ewbankkit Oct 23, 2020
c0af1e7
Run 'make gen'.
ewbankkit Oct 23, 2020
75dac12
Replace 'Merge' by 'Add'.
ewbankkit Oct 23, 2020
a76445b
Allow 'namevaluesfilters' internal package to implement functionality…
ewbankkit Jan 21, 2021
c2a121d
Call out that the 'namevaluesfilters' code is for server-side filtering.
ewbankkit Apr 28, 2021
87e866d
Add support for 'resourcegroupstaggingapi.TagFilters'.
ewbankkit Apr 28, 2021
2388347
Remove resource and data source changes.
ewbankkit Aug 19, 2021
2dbfb60
Fix typo.
ewbankkit Aug 19, 2021
d03884f
internal/tagresource: Initial generator for individual tag resources,…
bflad Jun 17, 2020
4ae3b73
New Resource: aws_ecs_tag
bflad Jun 17, 2020
819627f
docs/resource/aws_ecs_tag: Show better real world example
bflad Jun 17, 2020
64fc37a
New Resource: aws_dynamodb_tag
bflad Jun 17, 2020
5096374
docs/resource/aws_ecs_tag: Fix resource type in example
bflad Jun 17, 2020
ddca357
docs/provider: Add New Tag Resource section to Contributing Guide
bflad Jun 17, 2020
cb53bf7
internal/tagresource: Fix typo in testing
bflad Jun 17, 2020
487648a
Update CHANGELOG for #13783
bflad Mar 24, 2021
c968998
provider: Update tag generation to Terraform Plugin SDK v2 and add Er…
bflad Mar 24, 2021
83c615e
docs/provider: tfproviderdocs modernization for new tag resources
bflad Mar 24, 2021
8b81518
docs/contributing: Modernize New Tag Resource section
bflad Mar 24, 2021
51c03bc
provider: Use ServiceID constant in generated tag resource errors and…
bflad Mar 24, 2021
b989ec0
docs/provider: Modernize tag resource example configurations
bflad Mar 25, 2021
d598f5a
tests/resource/aws_dynamodb_tag: Modernize cross-region test configur…
bflad Mar 25, 2021
882dff2
make gen
bflad Mar 25, 2021
98b76e8
Add 'ErrorCheck' to acceptance tests.
ewbankkit Aug 20, 2021
68cf07f
Return 'NotFoundError' when a tag is not found.
ewbankkit Aug 20, 2021
0adfe92
Return 'NotFoundError' from list tags function when parent resource i…
ewbankkit Aug 20, 2021
454c2dd
Acceptance test output:
ewbankkit Aug 20, 2021
5689893
Simplify CreateTags generation.
ewbankkit Aug 20, 2021
49cc3e2
Use 'tfresource.RetryWhenNotFound' in CreateTags.
ewbankkit Aug 20, 2021
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
7 changes: 7 additions & 0 deletions .changelog/13783.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-resource
aws_dynamodb_tag
```

```release-note:new-resource
aws_ecs_tag
```
1 change: 1 addition & 0 deletions .semgrep.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rules:
- aws/resource_aws_transfer_server.go
- aws/*_test.go
- aws/internal/keyvaluetags/
- aws/internal/namevaluesfilters/
- aws/internal/service/wafregional/
# Legacy resource handling
- aws/resource_aws_autoscaling_group.go
Expand Down
2 changes: 2 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ build: fmtcheck
go install

gen:
rm -f aws/*_gen.go aws/*_gen_test.go
rm -f aws/internal/keyvaluetags/*_gen.go
rm -f aws/internal/namevaluesfilters/*_gen.go
rm -f aws/internal/service/**/lister/*_gen.go
go generate ./...

Expand Down
19 changes: 7 additions & 12 deletions aws/internal/keyvaluetags/create_tags_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 17 additions & 36 deletions aws/internal/keyvaluetags/generators/createtags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,19 @@ func main() {
ServiceNames: serviceNames,
}
templateFuncMap := template.FuncMap{
"ClientType": keyvaluetags.ServiceClientType,
"ResourceNotFoundErrorCode": keyvaluetags.ServiceResourceNotFoundErrorCode,
"ResourceNotFoundErrorCodeContains": keyvaluetags.ServiceResourceNotFoundErrorCodeContains,
"RetryCreationOnResourceNotFound": keyvaluetags.ServiceRetryCreationOnResourceNotFound,
"TagFunction": keyvaluetags.ServiceTagFunction,
"TagFunctionBatchSize": keyvaluetags.ServiceTagFunctionBatchSize,
"TagInputCustomValue": keyvaluetags.ServiceTagInputCustomValue,
"TagInputIdentifierField": keyvaluetags.ServiceTagInputIdentifierField,
"TagInputIdentifierRequiresSlice": keyvaluetags.ServiceTagInputIdentifierRequiresSlice,
"TagInputTagsField": keyvaluetags.ServiceTagInputTagsField,
"TagPackage": keyvaluetags.ServiceTagPackage,
"TagResourceTypeField": keyvaluetags.ServiceTagResourceTypeField,
"TagTypeIdentifierField": keyvaluetags.ServiceTagTypeIdentifierField,
"Title": strings.Title,
"ClientType": keyvaluetags.ServiceClientType,
"ParentResourceNotFoundError": keyvaluetags.ServiceParentResourceNotFoundError,
"RetryCreationOnResourceNotFound": keyvaluetags.ServiceRetryCreationOnResourceNotFound,
"TagFunction": keyvaluetags.ServiceTagFunction,
"TagFunctionBatchSize": keyvaluetags.ServiceTagFunctionBatchSize,
"TagInputCustomValue": keyvaluetags.ServiceTagInputCustomValue,
"TagInputIdentifierField": keyvaluetags.ServiceTagInputIdentifierField,
"TagInputIdentifierRequiresSlice": keyvaluetags.ServiceTagInputIdentifierRequiresSlice,
"TagInputTagsField": keyvaluetags.ServiceTagInputTagsField,
"TagPackage": keyvaluetags.ServiceTagPackage,
"TagResourceTypeField": keyvaluetags.ServiceTagResourceTypeField,
"TagTypeIdentifierField": keyvaluetags.ServiceTagTypeIdentifierField,
"Title": strings.Title,
}

tmpl, err := template.New("createtags").Funcs(templateFuncMap).Parse(templateBody)
Expand Down Expand Up @@ -133,31 +132,13 @@ func {{ . | Title }}CreateTags(conn {{ . | ClientType }}, identifier string{{ if

{{- if . | RetryCreationOnResourceNotFound }}

err := resource.Retry(EventualConsistencyTimeout, func() *resource.RetryError {
_, err := conn.{{ . | TagFunction }}(input)
_, err := tfresource.RetryWhenNotFound(EventualConsistencyTimeout, func() (interface{}, error) {
output, err := conn.{{ . | TagFunction }}(input)

{{- if . | ResourceNotFoundErrorCodeContains }}
{{ . | ParentResourceNotFoundError }}

if tfawserr.ErrCodeContains(err, "{{ . | ResourceNotFoundErrorCodeContains }}") {

{{- else }}

if tfawserr.ErrCodeEquals(err, {{ . | ResourceNotFoundErrorCode }}) {

{{- end }}
return resource.RetryableError(err)
}

if err != nil {
return resource.NonRetryableError(err)
}

return nil
return output, err
})

if tfresource.TimedOut(err) {
_, err = conn.{{ . | TagFunction }}(input)
}
{{- else }}
_, err := conn.{{ . | TagFunction }}(input)
{{- end }}
Expand Down
19 changes: 12 additions & 7 deletions aws/internal/keyvaluetags/generators/gettag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,20 @@ import (
{{- range .ServiceNames }}
"github.com/aws/aws-sdk-go/service/{{ . }}"
{{- end }}
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

{{- range .ServiceNames }}

// {{ . | Title }}GetTag fetches an individual {{ . }} service tag for a resource.
// Returns whether the key exists, the key value, and any errors.
// Returns whether the key value and any errors. A NotFoundError is used to signal that no value was found.
// This function will optimise the handling over {{ . | Title }}ListTags, if possible.
// The identifier is typically the Amazon Resource Name (ARN), although
// it may also be a different identifier depending on the service.
{{- if or ( . | TagTypeIdentifierField ) ( . | TagTypeAdditionalBoolFields) }}
func {{ . | Title }}GetTag(conn {{ . | ClientType }}, identifier string{{ if . | TagResourceTypeField }}, resourceType string{{ end }}, key string) (bool, *TagData, error) {
func {{ . | Title }}GetTag(conn {{ . | ClientType }}, identifier string{{ if . | TagResourceTypeField }}, resourceType string{{ end }}, key string) (*TagData, error) {
{{- else }}
func {{ . | Title }}GetTag(conn {{ . | ClientType }}, identifier string{{ if . | TagResourceTypeField }}, resourceType string{{ end }}, key string) (bool, *string, error) {
func {{ . | Title }}GetTag(conn {{ . | ClientType }}, identifier string{{ if . | TagResourceTypeField }}, resourceType string{{ end }}, key string) (*string, error) {
{{- end }}
{{- if . | ListTagsInputFilterIdentifierName }}
input := &{{ . | TagPackage }}.{{ . | ListTagsFunction }}Input{
Expand All @@ -123,22 +124,26 @@ func {{ . | Title }}GetTag(conn {{ . | ClientType }}, identifier string{{ if . |
output, err := conn.{{ . | ListTagsFunction }}(input)

if err != nil {
return false, nil, err
return nil, err
}

listTags := {{ . | Title }}KeyValueTags(output.{{ . | ListTagsOutputTagsField }}{{ if . | TagTypeIdentifierField }}, identifier{{ if . | TagResourceTypeField }}, resourceType{{ end }}{{ end }})
{{- else }}
listTags, err := {{ . | Title }}ListTags(conn, identifier{{ if . | TagResourceTypeField }}, resourceType{{ end }})

if err != nil {
return false, nil, err
return nil, err
}
{{- end }}

if !listTags.KeyExists(key) {
return nil, tfresource.NewEmptyResultError(nil)
}

{{ if or ( . | TagTypeIdentifierField ) ( . | TagTypeAdditionalBoolFields) }}
return listTags.KeyExists(key), listTags.KeyTagData(key), nil
return listTags.KeyTagData(key), nil
{{- else }}
return listTags.KeyExists(key), listTags.KeyValue(key), nil
return listTags.KeyValue(key), nil
{{- end }}
}
{{- end }}
Expand Down
5 changes: 5 additions & 0 deletions aws/internal/keyvaluetags/generators/listtags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func main() {
"ListTagsInputIdentifierField": keyvaluetags.ServiceListTagsInputIdentifierField,
"ListTagsInputIdentifierRequiresSlice": keyvaluetags.ServiceListTagsInputIdentifierRequiresSlice,
"ListTagsOutputTagsField": keyvaluetags.ServiceListTagsOutputTagsField,
"ParentResourceNotFoundError": keyvaluetags.ServiceParentResourceNotFoundError,
"TagPackage": keyvaluetags.ServiceTagPackage,
"TagResourceTypeField": keyvaluetags.ServiceTagResourceTypeField,
"TagTypeIdentifierField": keyvaluetags.ServiceTagTypeIdentifierField,
Expand Down Expand Up @@ -197,6 +198,8 @@ import (
{{- range .ServiceNames }}
"github.com/aws/aws-sdk-go/service/{{ . }}"
{{- end }}
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
{{ range .ServiceNames }}

Expand Down Expand Up @@ -226,6 +229,8 @@ func {{ . | Title }}ListTags(conn {{ . | ClientType }}, identifier string{{ if .

output, err := conn.{{ . | ListTagsFunction }}(input)

{{ . | ParentResourceNotFoundError }}

if err != nil {
return New(nil), err
}
Expand Down
73 changes: 49 additions & 24 deletions aws/internal/keyvaluetags/get_tag_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading