https://pulumi.com logo
g

great-postman-59271

04/28/2020, 11:50 AM
has anyone tried applying the tagging procedure found here: https://www.pulumi.com/blog/automatically-enforcing-aws-resource-tagging-policies/ to other providers, like azure? I tried expanding the auto-tagging to also include some azure resources (im deploying a dual-cloud solution), but it would throw exceptions, saying that it could not cast the new tagging object to the resource tagging property. This works for aws resources though, and the type of the "Tags" property is the same for both the azure resource (i was trying an azure batch account) and aws resources, so im wondering why it happens ūüôā
b

broad-dog-22463

04/28/2020, 12:01 PM
Hi @great-postman-59271 What does your autotagging code look like for Azure?
g

great-postman-59271

04/28/2020, 12:02 PM
i was basically trying to run the same code as from the link, and just adding the name of the azure resource under that taggable list of resources
i know its a pretty basic attempt, but i figured since the tags property was the same, it might work directly ūüôā
m

mysterious-australia-14256

05/14/2020, 8:00 PM
Hi @great-postman-59271, I'm not sure what language you are using but in c# I am using reflection to see if a type supports tags using the following
Copy code
static bool IsTaggable(Resource resource)
{
    try{
        string name = resource.GetType().GetProperty("Tags")!.Name;
        return true;
    }
    catch(NullReferenceException ex)
    {
        return false;
    }
}
I call this using
Copy code
static ResourceTransformation RegisterAutoTags(Dictionary<string, string> autoTags) {
    return args => {
        if (IsTaggable(args.Resource)) {
            // Use reflection to look up the Tags property and merge the auto-tags.
            var tagp = args.Args.GetType().GetProperty("Tags");
            var tags = (InputMap<string>)tagp?.GetValue(args.Args, null)! ?? new InputMap<string>();
            foreach (var tag in autoTags) {
                tags[tag.Key] = tag.Value;
            }
            tagp?.SetValue(args.Args, tags, null);
            return new ResourceTransformationResult(args.Args, args.Options);
        }
        return null;
    };
}
The above works with Azure but I haven't tried AWS
4 Views