From deacf9f55efe3b7e5e739f7930521103c953ca6a Mon Sep 17 00:00:00 2001 From: Matt Ellis Date: Mon, 18 Jun 2018 15:31:34 -0700 Subject: [PATCH] Support string properties in CustomResource creation --- sdk/dotnet/Pulumi/Resource.cs | 28 ++++++++++++++++++++++++++-- sdk/dotnet/examples/main.csx | 5 ++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/sdk/dotnet/Pulumi/Resource.cs b/sdk/dotnet/Pulumi/Resource.cs index 8bfaaeaa3..fea0abd24 100644 --- a/sdk/dotnet/Pulumi/Resource.cs +++ b/sdk/dotnet/Pulumi/Resource.cs @@ -1,3 +1,4 @@ +using Google.Protobuf.WellKnownTypes; using Pulumirpc; using System; using System.Collections.Generic; @@ -27,8 +28,9 @@ namespace Pulumi } + // Figure out the parent URN. If an explicit parent was passed in, use that. Otherwise use the global root URN. In the case where that hasn't been set yet, we must be creating + // the ComponentResource that represents the global stack object, so pass along no parent. Task parentUrn; - if (options.Parent != null) { parentUrn = options.Parent.Urn; } else if (Runtime.Root != null) { @@ -44,7 +46,7 @@ namespace Pulumi Name = name, Custom = custom, Protect = false, - Object = new Google.Protobuf.WellKnownTypes.Struct(), + Object = SerializeProperties(properties), Parent = parentUrn.Result } ); @@ -52,5 +54,27 @@ namespace Pulumi Urn = res.ResponseAsync.ContinueWith((x) => x.Result.Urn); return res.ResponseAsync; } + + private Struct SerializeProperties(Dictionary properties) { + if (properties == null) { + return new Struct(); + } + + var s = new Struct(); + + foreach (var kvp in properties) { + s.Fields.Add(kvp.Key, SerializeProperty(kvp.Value)); + } + + return s; + } + + private Value SerializeProperty(object o) { + if (o.GetType() == typeof(string)) { + return Value.ForString((string)o); + } + + throw new NotImplementedException($"cannot marshal object of type ${o.GetType()}"); + } } } \ No newline at end of file diff --git a/sdk/dotnet/examples/main.csx b/sdk/dotnet/examples/main.csx index e2d3ab21b..ddf812908 100644 --- a/sdk/dotnet/examples/main.csx +++ b/sdk/dotnet/examples/main.csx @@ -1,7 +1,10 @@ #r "/home/matell/go/src/github.com/pulumi/pulumi/sdk/dotnet/Pulumi/bin/Debug/netstandard2.0/Pulumi.dll" using Pulumi; using System; +using System.Collections.Generic; Config config = new Config("hello-dotnet"); -CustomResource r = new CustomResource("aws:s3/bucket:Bucket", config["name"]); +CustomResource r = new CustomResource("aws:s3/bucket:Bucket", config["name"], new Dictionary { + { "acl", "public-read" } +}); Console.WriteLine($"provided assigned id: {r.Id.Result}");