Skip to content

Commit

Permalink
Auto tagging with ResourceTransformation (#33)
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr <[email protected]>

Signed-off-by: Piotr <[email protected]>
Co-authored-by: Piotr <[email protected]>
  • Loading branch information
karpikpl and Piotr authored Sep 13, 2022
1 parent 91a8623 commit d7eb0eb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
22 changes: 22 additions & 0 deletions samples/My.Hr/My.Hr.Infra.Tests/CoreExStackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ public async Task ResourceGroupHasNameTag()
tags.Should().ContainKey("App");
}

[Test]
public async Task AllResourcesHaveNameTag()
{
// unfortunately this doesn't test tags created by auto-tagging dove via ResourceTransformation
var (resources, outputs, dbOperationsMock) = await Testing.RunAsync();

var rs = resources.Select(async r =>
{
var tagsProp = r.GetType().GetProperty("Tags");

return tagsProp != null
? (resource: r, tags: await tagsProp!.GetValue(r)!.GetValueAsync<System.Collections.Immutable.ImmutableDictionary<string, string>?>())
: (resource: r, tags: null);
});

var result = (await Task.WhenAll(rs)).Where(anyResource => anyResource.tags != null);

// Assert
result.Should().HaveCountGreaterThan(5);
result.Should().AllSatisfy(r => r.tags!.ContainsKey("App"), because: "All resources should be tagged");
}

[Test]
public async Task FunctionIsCreatedWithAUrl()
{
Expand Down
27 changes: 24 additions & 3 deletions samples/My.Hr/My.Hr.Infra/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
// build CoreEx stack
using My.Hr.Infra.Services;
using Pulumi;

return await Pulumi.Deployment.RunAsync(() =>
return await Deployment.RunAsync(() =>
{
// create ans use actual instance of DB Operations service
// create and use actual instance of DB Operations service
return My.Hr.Infra.CoreExStack.ExecuteStackAsync(new DbOperations());
}, null);
}, new StackOptions
{
// apply auto-tagging transformation
// https://gist.github.com/dbeattie71/1f8a1a9264ceb8161ad4c49de1ee3bb3
ResourceTransformations = new System.Collections.Generic.List<ResourceTransformation>{
(args) => {
var tagsProp = args.Args.GetType().GetProperty("Tags");

if(tagsProp?.GetValue(args.Args) is InputMap<string> tags)
{
Log.Debug("Adding tags to " + args.Resource.GetResourceName());

tags.Add("user:Stack", Deployment.Instance.StackName);
tags.Add("user:Project", Deployment.Instance.ProjectName);
tags.Add("App:Name", "CoreEx");
}

return new ResourceTransformationResult(args.Args, args.Options);
}
}
});

0 comments on commit d7eb0eb

Please sign in to comment.