Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using anonymous objects instead of Dictionary<string, object?> results into compile-time error

I’m doing Pulumi C# Infrastructure as Code (IaC).

Pulumi docs uses Dictionary<string, object?> which I want to replace with anonymous objects because they look better.

The issue is that I’m getting a compile-time error with the anonymous objects.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

x is ImmutableArray<string>.

Fine

var xpolicyDoc = Output
    .All(tables.UserTradesTableArn)
    .Apply(x =>
        new Dictionary<string, object?>
        {
            { "Version", "2012-10-17" },
            {
                "Statement", new[]
                {
                    new Dictionary<string, object?>
                    {
                        { "Effect", "Allow" },
                        {
                            "Action", new[]
                            {
                                "dynamodb:BatchWriteItem",
                                "dynamodb:PutItem",
                                "dynamodb:DeleteItem",
                                "dynamodb:UpdateItem",
                                "dynamodb:DescribeTable",
                                "dynamodb:Query"
                            }
                        },
                        {
                            "Resource", new[] { x[0] }
                        }
                    },
                    new Dictionary<string, object?>
                    {
                        { "Effect", "Allow" },
                        { "Action", "dynamodb:ListTables" },
                        { "Resource", "*" }
                    }
                }
            }
        });

var policy = new Policy($"{tableName}-table-policy", new PolicyArgs
{
    PolicyDocument = policyDoc.Apply(x => JsonSerializer.Serialize(x))
});

Compile-time error

x[0]: Anonymous type projection initializer must be simple name or member access expression

var policyDoc = Output
    .All(tables.UserTradesTableArn)
    .Apply(x =>
        new
        {
            Version = "2012-10-17",
            Statement = new[]
            {
                new
                {
                    Effect = "Allow",
                    Action = new[]
                    {
                        "dynamodb:BatchWriteItem",
                        "dynamodb:PutItem",
                        "dynamodb:DeleteItem",
                        "dynamodb:UpdateItem",
                        "dynamodb:DescribeTable",
                        "dynamodb:Query"
                    },
                    Resource = new { x[0] }
                },
                new
                {
                    Effect = "Allow",
                    Action = "dynamodb:ListTables",
                    Resource = "*"
                }
            }
        });

var policy = new Policy($"{tableName}-table-policy", new PolicyArgs
{
    PolicyDocument = policyDoc.Apply(x => JsonSerializer.Serialize(x))
});

>Solution :

It should be either

Resource = new [] { x[0] }

Or

Resource = x[0]

Or

Resource = new { SomeProperty = x[0] }

What you have is missing a property name.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading