Skip to content

Commit

Permalink
generate/tags: Handle 'ServiceTagsMap' in paginated ListTags.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Feb 8, 2024
1 parent 50d192b commit a1c583f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
44 changes: 23 additions & 21 deletions internal/generate/tags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,34 +116,34 @@ func newTemplateBody(version int, kvtValues bool) *TemplateBody {
switch version {
case sdkV1:
return &TemplateBody{
"\n" + v1.GetTagBody,
v1.HeaderBody,
"\n" + v1.ListTagsBody,
"\n" + v1.ServiceTagsMapBody,
"\n" + v1.ServiceTagsSliceBody,
"\n" + v1.UpdateTagsBody,
"\n" + v1.WaitTagsPropagatedBody,
getTag: "\n" + v1.GetTagBody,
header: v1.HeaderBody,
listTags: "\n" + v1.ListTagsBody,
serviceTagsMap: "\n" + v1.ServiceTagsMapBody,
serviceTagsSlice: "\n" + v1.ServiceTagsSliceBody,
updateTags: "\n" + v1.UpdateTagsBody,
waitTagsPropagated: "\n" + v1.WaitTagsPropagatedBody,
}
case sdkV2:
if kvtValues {
return &TemplateBody{
"\n" + v2.GetTagBody,
v2.HeaderBody,
"\n" + v2.ListTagsBody,
"\n" + v2.ServiceTagsValueMapBody,
"\n" + v2.ServiceTagsSliceBody,
"\n" + v2.UpdateTagsBody,
"\n" + v2.WaitTagsPropagatedBody,
getTag: "\n" + v2.GetTagBody,
header: v2.HeaderBody,
listTags: "\n" + v2.ListTagsBody,
serviceTagsMap: "\n" + v2.ServiceTagsValueMapBody,
serviceTagsSlice: "\n" + v2.ServiceTagsSliceBody,
updateTags: "\n" + v2.UpdateTagsBody,
waitTagsPropagated: "\n" + v2.WaitTagsPropagatedBody,
}
}
return &TemplateBody{
"\n" + v2.GetTagBody,
v2.HeaderBody,
"\n" + v2.ListTagsBody,
"\n" + v2.ServiceTagsMapBody,
"\n" + v2.ServiceTagsSliceBody,
"\n" + v2.UpdateTagsBody,
"\n" + v2.WaitTagsPropagatedBody,
getTag: "\n" + v2.GetTagBody,
header: v2.HeaderBody,
listTags: "\n" + v2.ListTagsBody,
serviceTagsMap: "\n" + v2.ServiceTagsMapBody,
serviceTagsSlice: "\n" + v2.ServiceTagsSliceBody,
updateTags: "\n" + v2.UpdateTagsBody,
waitTagsPropagated: "\n" + v2.WaitTagsPropagatedBody,
}
default:
return nil
Expand Down Expand Up @@ -172,6 +172,7 @@ type TemplateData struct {
ParentNotFoundErrCode string
ParentNotFoundErrMsg string
RetryCreateOnNotFound string
ServiceTagsMap bool
SetTagsOutFunc string
TagInCustomVal string
TagInIDElem string
Expand Down Expand Up @@ -333,6 +334,7 @@ func main() {
ListTagsOutTagsElem: *listTagsOutTagsElem,
ParentNotFoundErrCode: *parentNotFoundErrCode,
ParentNotFoundErrMsg: *parentNotFoundErrMsg,
ServiceTagsMap: *serviceTagsMap,
SetTagsOutFunc: *setTagsOutFunc,
TagInCustomVal: *tagInCustomVal,
TagInIDElem: *tagInIDElem,
Expand Down
3 changes: 3 additions & 0 deletions internal/generate/tags/templates/v1/header_body.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ import (
{{- if .NamesPkg }}
"github.com/hashicorp/terraform-provider-aws/names"
{{- end }}
{{- if and .ListTagsOpPaginated .ServiceTagsMap }}
"golang.org/x/exp/maps"
{{- end }}
)
8 changes: 8 additions & 0 deletions internal/generate/tags/templates/v1/list_tags_body.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,26 @@ func {{ .ListTagsFunc }}(ctx context.Context, conn {{ .ClientType }}, identifier
{{- end }}
}
{{- if .ListTagsOpPaginated }}
{{- if .ServiceTagsMap }}
output := make(map[string]*string)
{{- else }}
var output []*{{ .TagPackage }}.{{ or .TagType2 .TagType }}
{{- end }}

err := conn.{{ .ListTagsOp }}PagesWithContext(ctx, input, func(page *{{ .TagPackage }}.{{ .ListTagsOp }}Output, lastPage bool) bool {
if page == nil {
return !lastPage
}

{{ if .ServiceTagsMap }}
maps.Copy(output, page.{{ .ListTagsOutTagsElem }})
{{- else }}
for _, v := range page.{{ .ListTagsOutTagsElem }} {
if v != nil {
output = append(output, v)
}
}
{{- end }}

return !lastPage
})
Expand Down

0 comments on commit a1c583f

Please sign in to comment.