Skip to content

Commit

Permalink
dotnet remote transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Frassle committed Mar 4, 2024
1 parent 7fc5b5c commit 7252edc
Show file tree
Hide file tree
Showing 16 changed files with 1,319 additions and 52 deletions.
3 changes: 3 additions & 0 deletions integration_tests/transformations_remote/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.pulumi/
[Bb]in/
[Oo]bj/
129 changes: 129 additions & 0 deletions integration_tests/transformations_remote/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright 2016-2024, Pulumi Corporation. All rights reserved.

using System;
using System.Threading.Tasks;
using Pulumi;
using Pulumi.Random;

class MyComponent : ComponentResource
{
public RandomString Child { get; }

public MyComponent(string name, ComponentResourceOptions? options = null)
: base("my:component:MyComponent", name, options)
{
this.Child = new RandomString($"{name}-child",
new RandomStringArgs { Length = 5 },
new CustomResourceOptions {Parent = this, AdditionalSecretOutputs = {"special"} });
}
}

// Scenario #5 - cross-resource transformations that inject the output of one resource to the input
// of the other one.
class MyOtherComponent : ComponentResource
{
public RandomString Child1 { get; }
public RandomString Child2 { get; }

public MyOtherComponent(string name, ComponentResourceOptions? options = null)
: base("my:component:MyComponent", name, options)
{
this.Child1 = new RandomString($"{name}-child1",
new RandomStringArgs { Length = 5 },
new CustomResourceOptions { Parent = this });

this.Child2 = new RandomString($"{name}-child2",
new RandomStringArgs { Length = 6 },
new CustomResourceOptions { Parent = this });
}
}

class TransformationsStack : Stack
{
public TransformationsStack() : base(new StackOptions { ResourceTransforms = {Scenario3} })
{
// Scenario #1 - apply a transformation to a CustomResource
var res1 = new RandomString("res1", new RandomStringArgs { Length = 5 }, new CustomResourceOptions
{
ResourceTransforms =
{
args =>
{
var options = CustomResourceOptions.Merge(
(CustomResourceOptions)args.Options,
new CustomResourceOptions {AdditionalSecretOutputs = {"length"}});
return new ResourceTransformResult(args.Args, options);
}
}
});

// Scenario #2 - apply a transformation to a Component to transform its children
var res2 = new MyComponent("res2", new ComponentResourceOptions
{
ResourceTransforms =
{
args =>
{
if (args.Resource.GetResourceType() == RandomStringType && args.Args is RandomStringArgs oldArgs)
{
var resultArgs = new RandomStringArgs {Length = oldArgs.Length, MinUpper = 2};
var resultOpts = CustomResourceOptions.Merge((CustomResourceOptions)args.Options,
new CustomResourceOptions {AdditionalSecretOutputs = {"length"}});
return new ResourceTransformResult(resultArgs, resultOpts);
}

return null;
}
}
});

// Scenario #3 - apply a transformation to the Stack to transform all resources in the stack.
var res3 = new RandomString("res3", new RandomStringArgs { Length = 5 });

// Scenario #4 - transformations are applied in order of decreasing specificity
// 1. (not in this example) Child transformation
// 2. First parent transformation
// 3. Second parent transformation
// 4. Stack transformation
var res4 = new MyComponent("res4", new ComponentResourceOptions
{
ResourceTransforms = { args => scenario4(args, "value1"), args => scenario4(args, "value2") }
});

ResourceTransformResult? scenario4(ResourceTransformArgs args, string v)
{
if (args.Resource.GetResourceType() == RandomStringType && args.Args is RandomStringArgs oldArgs)
{
var resultArgs = new RandomStringArgs
{Length = oldArgs.Length, OverrideSpecial = Output.Format($"{oldArgs.OverrideSpecial}{v}")};
return new ResourceTransformResult(resultArgs, args.Options);
}

return null;
}
}

// Scenario #3 - apply a transformation to the Stack to transform all (future) resources in the stack
private static ResourceTransformResult? Scenario3(ResourceTransformArgs args)
{
if (args.Resource.GetResourceType() == RandomStringType && args.Args is RandomStringArgs oldArgs)
{
var resultArgs = new RandomStringArgs
{
Length = oldArgs.Length,
MinUpper = oldArgs.MinUpper,
OverrideSpecial = Output.Format($"{oldArgs.OverrideSpecial}stackvalue")
};
return new ResourceTransformResult(resultArgs, args.Options);
}

return null;
}

private const string RandomStringType = "random:index/randomString:RandomString";
}

class Program
{
static Task<int> Main(string[] args) => Deployment.RunAsync<TransformationsStack>();
}
3 changes: 3 additions & 0 deletions integration_tests/transformations_remote/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: transformations_dotnet
description: A simple .NET program that uses transformations.
runtime: dotnet
13 changes: 13 additions & 0 deletions integration_tests/transformations_remote/Transformations.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pulumi.Random" Version="4.8.2" />
</ItemGroup>

</Project>
81 changes: 81 additions & 0 deletions integration_tests/transformations_simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,84 @@ func dotNetValidator() func(t *testing.T, stack integration.RuntimeValidationSta
assert.True(t, foundRes5Child)
}
}

func TestDotNetTransforms(t *testing.T) {
testDotnetProgram(t, &integration.ProgramTestOptions{
Dir: "transformations_remote",
Quick: true,
ExtraRuntimeValidation: Validator,
LocalProviders: []integration.LocalDependency{
{
Package: "testprovider",
Path: "testprovider",
},
},
})
}

func Validator(t *testing.T, stack integration.RuntimeValidationStackInfo) {
randomResName := "testprovider:index:Random"
foundRes1 := false
foundRes2Child := false
foundRes3 := false
foundRes4Child := false
foundRes5 := false
for _, res := range stack.Deployment.Resources {
// "res1" has a transformation which adds additionalSecretOutputs
if res.URN.Name() == "res1" {
foundRes1 = true
assert.Equal(t, res.Type, tokens.Type(randomResName))
assert.Contains(t, res.AdditionalSecretOutputs, resource.PropertyKey("result"))
}
// "res2" has a transformation which adds additionalSecretOutputs to it's
// "child"
if res.URN.Name() == "res2-child" {
foundRes2Child = true
assert.Equal(t, res.Type, tokens.Type(randomResName))
assert.Equal(t, res.Parent.Type(), tokens.Type("my:component:MyComponent"))
assert.Contains(t, res.AdditionalSecretOutputs, resource.PropertyKey("result"))
assert.Contains(t, res.AdditionalSecretOutputs, resource.PropertyKey("length"))
}
// "res3" is impacted by a global stack transformation which sets
// optionalDefault to "stackDefault"
if res.URN.Name() == "res3" {
foundRes3 = true
assert.Equal(t, res.Type, tokens.Type(randomResName))
optionalPrefix := res.Inputs["prefix"]
assert.NotNil(t, optionalPrefix)
assert.Equal(t, "stackDefault", optionalPrefix.(string))
length := res.Inputs["length"]
assert.NotNil(t, length)
// length should be secret
secret, ok := length.(map[string]interface{})
assert.True(t, ok, "length should be a secret")
assert.Equal(t, resource.SecretSig, secret[resource.SigKey])
assert.Contains(t, res.AdditionalSecretOutputs, resource.PropertyKey("result"))
}
// "res4" is impacted by two component parent transformations which set
// optionalDefault to "default1" and then "default2" and also a global stack
// transformation which sets optionalDefault to "stackDefault". The end
// result should be "stackDefault".
if res.URN.Name() == "res4-child" {
foundRes4Child = true
assert.Equal(t, res.Type, tokens.Type(randomResName))
assert.Equal(t, res.Parent.Type(), tokens.Type("my:component:MyComponent"))
optionalPrefix := res.Inputs["prefix"]
assert.NotNil(t, optionalPrefix)
assert.Equal(t, "stackDefault", optionalPrefix.(string))
}
// "res5" should have mutated the length
if res.URN.Name() == "res5" {
foundRes5 = true
assert.Equal(t, res.Type, tokens.Type(randomResName))
length := res.Inputs["length"]
assert.NotNil(t, length)
assert.Equal(t, 20.0, length.(float64))
}
}
assert.True(t, foundRes1)
assert.True(t, foundRes2Child)
assert.True(t, foundRes3)
assert.True(t, foundRes4Child)
assert.True(t, foundRes5)
}
53 changes: 53 additions & 0 deletions sdk/Pulumi.Automation/Pulumi.Automation.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7252edc

Please sign in to comment.