great-postman-59271
04/28/2020, 11:50 AMbroad-dog-22463
04/28/2020, 12:01 PMgreat-postman-59271
04/28/2020, 12:02 PMmysterious-australia-14256
05/14/2020, 8:00 PMstatic bool IsTaggable(Resource resource)
{
try{
string name = resource.GetType().GetProperty("Tags")!.Name;
return true;
}
catch(NullReferenceException ex)
{
return false;
}
}
I call this using
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