Add Stack Transformations to .NET SDK (#4008)

Stack transformations and integration tests
This commit is contained in:
Mikhail Shilkov 2020-03-16 17:40:52 +01:00 committed by GitHub
parent fdd9a57ff8
commit dbe365376d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 334 additions and 6 deletions

View file

@ -4,6 +4,9 @@ CHANGELOG
## HEAD (Unreleased)
- Improve documentation of URL formats for `pulumi login`
[#4059](https://github.com/pulumi/pulumi/pull/4059)
- Add support for stack transformations in the .NET SDK.
[4008](https://github.com/pulumi/pulumi/pull/4008)
## 1.12.1 (2020-03-11)
- Fix Kubernetes YAML parsing error in .NET.

View file

@ -182,7 +182,11 @@ Pulumi.Serialization.OutputConstructorAttribute.OutputConstructorAttribute() ->
Pulumi.Serialization.OutputTypeAttribute
Pulumi.Serialization.OutputTypeAttribute.OutputTypeAttribute() -> void
Pulumi.Stack
Pulumi.Stack.Stack() -> void
Pulumi.Stack.Stack(Pulumi.StackOptions options = null) -> void
Pulumi.StackOptions
Pulumi.StackOptions.StackOptions() -> void
Pulumi.StackOptions.ResourceTransformations.get -> System.Collections.Generic.List<Pulumi.ResourceTransformation>
Pulumi.StackOptions.ResourceTransformations.set -> void
Pulumi.StackReference
Pulumi.StackReference.GetOutput(Pulumi.Input<string> name) -> Pulumi.Output<object>
Pulumi.StackReference.GetValueAsync(Pulumi.Input<string> name) -> System.Threading.Tasks.Task<object>

View file

@ -0,0 +1,25 @@
// Copyright 2016-2020, Pulumi Corporation
using System.Collections.Generic;
namespace Pulumi
{
/// <summary>
/// <see cref="StackOptions"/> is a bag of optional settings that control a stack's behavior.
/// </summary>
public class StackOptions
{
private List<ResourceTransformation>? _resourceTransformations;
/// <summary>
/// Optional list of transformations to apply to this stack's resources during construction.
/// The transformations are applied in order, and are applied after all the transformations of custom
/// and component resources in the stack.
/// </summary>
public List<ResourceTransformation> ResourceTransformations
{
get => _resourceTransformations ??= new List<ResourceTransformation>();
set => _resourceTransformations = value;
}
}
}

View file

@ -50,8 +50,10 @@ namespace Pulumi
/// <summary>
/// Create a Stack with stack resources defined in derived class constructor.
/// </summary>
public Stack()
: base(_rootPulumiStackTypeName, $"{Deployment.Instance.ProjectName}-{Deployment.Instance.StackName}")
public Stack(StackOptions? options = null)
: base(_rootPulumiStackTypeName,
$"{Deployment.Instance.ProjectName}-{Deployment.Instance.StackName}",
ConvertOptions(options))
{
Deployment.InternalInstance.Stack = this;
}
@ -121,5 +123,16 @@ namespace Pulumi
? ImmutableDictionary<string, object?>.Empty
: dictionary.ToImmutableDictionary();
}
private static ComponentResourceOptions? ConvertOptions(StackOptions? options)
{
if (options == null)
return null;
return new ComponentResourceOptions
{
ResourceTransformations = options.ResourceTransformations
};
}
}
}

View file

@ -0,0 +1,3 @@
/.pulumi/
[Bb]in/
[Oo]bj/

View file

@ -0,0 +1,183 @@
// Copyright 2016-2020, 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 { ResourceTransformations = {Scenario3} })
{
// Scenario #1 - apply a transformation to a CustomResource
var res1 = new RandomString("res1", new RandomStringArgs { Length = 5 }, new CustomResourceOptions
{
ResourceTransformations =
{
args =>
{
var options = CustomResourceOptions.Merge(
(CustomResourceOptions)args.Options,
new CustomResourceOptions {AdditionalSecretOutputs = {"length"}});
return new ResourceTransformationResult(args.Args, options);
}
}
});
// Scenario #2 - apply a transformation to a Component to transform its children
var res2 = new MyComponent("res2", new ComponentResourceOptions
{
ResourceTransformations =
{
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 ResourceTransformationResult(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
{
ResourceTransformations = { args => scenario4(args, "value1"), args => scenario4(args, "value2") }
});
ResourceTransformationResult? scenario4(ResourceTransformationArgs 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 ResourceTransformationResult(resultArgs, args.Options);
}
return null;
}
// Scenario #5 - cross-resource transformations that inject dependencies on one resource into another.
var res5 = new MyOtherComponent("res5", new ComponentResourceOptions
{
ResourceTransformations = { transformChild1DependsOnChild2() }
});
ResourceTransformation transformChild1DependsOnChild2()
{
// Create a task completion source that wil be resolved once we find child2.
// This is needed because we do not know what order we will see the resource
// registrations of child1 and child2.
var child2ArgsTaskSource = new TaskCompletionSource<RandomStringArgs>();
return transform;
ResourceTransformationResult? transform(ResourceTransformationArgs args)
{
// Return a transformation which will rewrite child1 to depend on the promise for child2, and
// will resolve that promise when it finds child2.
//return (args: pulumi.ResourceTransformationArgs) => {
if (args.Args is RandomStringArgs resourceArgs)
{
var resourceName = args.Resource.GetResourceName();
if (resourceName.EndsWith("-child2"))
{
// Resolve the child2 promise with the child2 resource.
child2ArgsTaskSource.SetResult(resourceArgs);
return null;
}
if (resourceName.EndsWith("-child1"))
{
var child2Length = resourceArgs.Length.ToOutput()
.Apply(async input =>
{
if (input != 5)
{
// Not strictly necessary - but shows we can confirm invariants we expect to be true.
throw new Exception("unexpected input value");
}
var child2Args = await child2ArgsTaskSource.Task;
return child2Args.Length;
})
.Apply(output => output);
var newArgs = new RandomStringArgs {Length = child2Length};
return new ResourceTransformationResult(newArgs, args.Options);
}
}
return null;
}
}
}
// Scenario #3 - apply a transformation to the Stack to transform all (future) resources in the stack
private static ResourceTransformationResult? Scenario3(ResourceTransformationArgs 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 ResourceTransformationResult(resultArgs, args.Options);
}
return null;
}
private const string RandomStringType = "random:index/randomString:RandomString";
}
class Program
{
static Task<int> Main(string[] args) => Deployment.RunAsync<TransformationsStack>();
}

View file

@ -0,0 +1,3 @@
name: transformations_dotnet
description: A simple .NET program that uses transformations.
runtime: dotnet

View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Pulumi.Random" Version="1.6.0-preview" />
</ItemGroup>
</Project>

View file

@ -25,7 +25,7 @@ func TestNodejsTransformations(t *testing.T) {
Dir: d,
Dependencies: []string{"@pulumi/pulumi"},
Quick: true,
ExtraRuntimeValidation: Validator("nodejs"),
ExtraRuntimeValidation: validator("nodejs"),
})
})
}
@ -41,13 +41,13 @@ func TestPythonTransformations(t *testing.T) {
filepath.Join("..", "..", "..", "sdk", "python", "env", "src"),
},
Quick: true,
ExtraRuntimeValidation: Validator("python"),
ExtraRuntimeValidation: validator("python"),
})
})
}
}
func Validator(language string) func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
func validator(language string) func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
dynamicResName := "pulumi-" + language + ":dynamic:Resource"
return func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
foundRes1 := false
@ -114,3 +114,84 @@ func Validator(language string) func(t *testing.T, stack integration.RuntimeVali
assert.True(t, foundRes5Child)
}
}
func TestDotNetTransformations(t *testing.T) {
for _, dir := range dirs {
d := filepath.Join("dotnet", dir)
t.Run(d, func(t *testing.T) {
integration.ProgramTest(t, &integration.ProgramTestOptions{
Dir: d,
Dependencies: []string{"Pulumi"},
Quick: true,
ExtraRuntimeValidation: dotNetValidator(),
})
})
}
}
// .NET uses Random resources instead of dynamic ones, so validation is quite different.
func dotNetValidator() func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
resName := "random:index/randomString:RandomString"
return func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
foundRes1 := false
foundRes2Child := false
foundRes3 := false
foundRes4Child := false
foundRes5Child := 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(resName))
assert.Contains(t, res.AdditionalSecretOutputs, resource.PropertyKey("length"))
}
// "res2" has a transformation which adds additionalSecretOutputs to it's
// "child" and sets minUpper to 2
if res.URN.Name() == "res2-child" {
foundRes2Child = true
assert.Equal(t, res.Type, tokens.Type(resName))
assert.Equal(t, res.Parent.Type(), tokens.Type("my:component:MyComponent"))
assert.Contains(t, res.AdditionalSecretOutputs, resource.PropertyKey("length"))
assert.Contains(t, res.AdditionalSecretOutputs, resource.PropertyKey("special"))
minUpper := res.Inputs["minUpper"]
assert.NotNil(t, minUpper)
assert.Equal(t, 2.0, minUpper.(float64))
}
// "res3" is impacted by a global stack transformation which sets
// overrideSpecial to "stackvalue"
if res.URN.Name() == "res3" {
foundRes3 = true
assert.Equal(t, res.Type, tokens.Type(resName))
overrideSpecial := res.Inputs["overrideSpecial"]
assert.NotNil(t, overrideSpecial)
assert.Equal(t, "stackvalue", overrideSpecial.(string))
}
// "res4" is impacted by two component parent transformations which appends
// to overrideSpecial "value1" and then "value2" and also a global stack
// transformation which appends "stackvalue" to overrideSpecial. The end
// result should be "value1value2stackvalue".
if res.URN.Name() == "res4-child" {
foundRes4Child = true
assert.Equal(t, res.Type, tokens.Type(resName))
assert.Equal(t, res.Parent.Type(), tokens.Type("my:component:MyComponent"))
overrideSpecial := res.Inputs["overrideSpecial"]
assert.NotNil(t, overrideSpecial)
assert.Equal(t, "value1value2stackvalue", overrideSpecial.(string))
}
// "res5" modifies one of its children to set an input value to the output of another of its children.
if res.URN.Name() == "res5-child1" {
foundRes5Child = true
assert.Equal(t, res.Type, tokens.Type(resName))
assert.Equal(t, res.Parent.Type(), tokens.Type("my:component:MyComponent"))
length := res.Inputs["length"]
assert.NotNil(t, length)
assert.Equal(t, 6.0, length.(float64))
}
}
assert.True(t, foundRes1)
assert.True(t, foundRes2Child)
assert.True(t, foundRes3)
assert.True(t, foundRes4Child)
assert.True(t, foundRes5Child)
}
}