Would anyone have an example of use of the various...
# dotnet
l
Would anyone have an example of use of the various string interpolation functions and operators? InputJson, OutputJson, Output.Format(), etc.? I'm coming from Typescript world and I'm being bamboozled, mostly because the dotnet/C# style API docs make no sense to me (and never have, even since NET Framework 2 days...).
FWIW, I'm trying to build requestTemplates for AWS ApiGateway integrations. There's examples in https://github.com/pulumi/examples for some languages, but not C#.
e
ChatGPT actually summarises this fairly well: https://chatgpt.com/s/t_699807e4b33c8191905615357201226d
InputJson is just an Input<JsonElement> with some conversion helpers to implicit convert from string and JsonDocument.
l
Ah so it's Output.JsonSerialize I want. I haven't managed to get an example that's even close to compiling from any of the AI tools. All the examples for policy documents use the fairly icky GetPolicyDocument +
@"${{ ...
syntax. I'm trying to find something using
Output.JsonSerialize<PolicyDocumentArgs>
that works, but my C# is still letting me down -- too many years of TypeScript, I guess. Even my simplest MVCE is complaining:
Copy code
new RolePolicy("test", new Iam.RolePolicyArgs {
        Role = role.Id,
        Policy = Output.JsonSerialize<PolicyDocumentArgs>(new PolicyDocumentArgs {})
    });
With
Copy code
error CS1503: Argument 1: cannot convert from 'Pulumi.Aws.Iam.Inputs.PolicyDocumentArgs' to 'Pulumi.Output<Pulumi.Aws.Iam.Inputs.PolicyDocumentArgs>'
I did find the AssumeRolePolicyArgs etc. code in the example GitHub;s aws-cs-assume-role project, but that seems to be missing a trick too. Isn't AssumeRolePolicyArgs in CreateRoleStack.cs just a re-implementation of Pulumi.Aws.Iam.Inputs.PolicyDocumentArgs?
This is in the API examples, and it's a bit better.. but I thought I was getting closer before, not having to use the
["Version"] = "2012-10-17",
syntax and being able to use the much nicer
Version = PolicyDocumentVersion.PolicyDocumentVersion_2012_10_17
syntax.
Untitled
I'm trying to get something close to the examples in Fraser's ChatGPT conversation:
Copy code
var policy = Output.JsonSerialize(new
{
    Version = "2012-10-17",
    Statement = new[]
    {
        new
        {
            Effect = "Allow",
            Action = "s3:GetObject",
            Resource = Output.Format("{0}/*", bucket.Arn)
        }
    }
});
But that doesn't compile for me, JsonSerialize insists on having an Output as a parameter.
This is compiling (haven't tried running it yet) but it seems to work because it's converting the InvokeResult to a string, rather than natively using the PolicyDocumentArgs. Is what I'm attempting simply not how it works?
Copy code
var policy = new Policy("test", new Iam.PolicyArgs
    {
        PolicyDocument = Output.JsonSerialize<Iam.GetPolicyDocumentResult>(GetPolicyDocument.Invoke(new()
        {
            Statements = new[]
            {
                new GetPolicyDocumentStatementInputArgs
                {
                    Effect = PolicyStatementEffect.ALLOW.ToString(),
                    Actions = [
                         "dynamodb:PutItem" ,
                         "dynamodb:GetItem"
                     ],
                    Resources = table.Arn
                }
            }
        }))
    });
I'm looking for something in C# that is as strongly-typed and elegant as this valid TypeScript code:
e
But that doesn't compile for me, JsonSerialize insists on having an Output as a parameter.
Any value can be lifted up to be an Output version of that value with
Output.Create(val)
.
👍 1
We should probably make this an implicit conversion. It is for Input<T> already.