busy-honey-73811
05/18/2022, 12:39 PMaws-native
) resources in Golang?
I have a real hard time to figure out how to do this (see all the ???
in below example). The main problem is that aws-native
uses no common (shared) types for tags and sets of tags, instead it uses generated interface
types specific to each taggable resource.
package aws
import (
"fmt"
"reflect"
"strings"
"<http://github.com/pulumi/pulumi/sdk/v3/go/pulumi|github.com/pulumi/pulumi/sdk/v3/go/pulumi>"
)
type Tagger struct {
tags map[string]string
}
func NewTagger() *Tagger {
return &Tagger{}
}
func (*Tagger) isTaggable(ctx *pulumi.Context, typeName string, t reflect.Type) bool {
if strings.HasPrefix(typeName, "aws-native:") {
if field, found := t.FieldByName("Tags"); found {
switch field.Type.Kind() {
case reflect.Slice:
return true
}
}
}
return false
}
func (t *Tagger) Apply(ctx *pulumi.Context) error {
return ctx.RegisterStackTransformation(
func(args *pulumi.ResourceTransformationArgs) *pulumi.ResourceTransformationResult {
if t.isTaggable(ctx, args.Type, args.Props.ElementType()) {
ptr := reflect.ValueOf(args.Props)
val := ptr.Elem()
tags := val.FieldByName("Tags")
var tagList ???
if !tags.IsZero() {
tagList = tags.Interface().(???)
} else {
tagList = ???{}
}
for k, v := range t.tags {
tagList = append(tagList, ???{ Key: pulumi.String(k), Value: pulumi.String(v)})
}
tags.Set(reflect.ValueOf(tagList))
return &pulumi.ResourceTransformationResult{
Props: args.Props,
Opts: args.Opts,
}
}
ctx.Log.Warn(fmt.Sprintf("isTaggable(%s) -> false", args.Type), nil)
return nil
},
)
}
tall-librarian-49374
06/03/2022, 2:56 PMbusy-honey-73811
06/03/2022, 3:06 PMaws-native
.tall-librarian-49374
06/03/2022, 3:12 PMthat does not work forCould you highlight the difference between two providers that makes it impossible for me?.aws-native
busy-honey-73811
06/03/2022, 3:31 PM???
in my example code snipped above.
var tagList ???
if !tags.IsZero() {
tagList = tags.Interface().(???)
} else {
tagList = ???{}
}
for k, v := range t.tags {
tagList = append(tagList, ???{ Key: pulumi.String(k), Value: pulumi.String(v)})
}
There is no common (Go struct) type for Tags in the (generated) aws-native
Go SDK. For each AWS Service (e.g. ec2
, …), service specific (struct) types are created for Tags. For example:
• for https://www.pulumi.com/registry/packages/aws-native/api-docs/kms/key/ there is KeyTagArray
and KeyTagArgs
• for https://www.pulumi.com/registry/packages/aws-native/api-docs/ecr/repository/there is PublicRepositoryTagArray
and PublicRepositoryTagArgs
tall-librarian-49374
06/03/2022, 3:33 PM