pulumi/pkg/codegen/pcl/rewrite_apply_test.go

218 lines
6.0 KiB
Go
Raw Permalink Normal View History

package pcl
import (
"fmt"
"testing"
"github.com/hashicorp/hcl/v2"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
"github.com/stretchr/testify/assert"
)
type nameInfo int
func (nameInfo) Format(name string) string {
return name
}
sdk/go: Remove 'nolint' directives from package docs Go treats comments that match the following regex as directives. //[a-z0-9]+:[a-z0-9] Comments that are directives don't show in an entity's documentation. https://github.com/golang/go/commit/5a550b695117f07a4f2454039a4871250cd3ed09#diff-f56160fd9fcea272966a8a1d692ad9f49206fdd8dbcbfe384865a98cd9bc2749R165 Our code has `//nolint` directives that now show in the API Reference. This is because these directives are in one of the following forms, which don't get this special treatment. // nolint:foo //nolint: foo This change fixes all such directives found by the regex: `// nolint|//nolint: `. See bottom of commit for command used for the fix. Verification: Here's the output of `go doc` on some entities before and after this change. Before ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi | head -n8 package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" nolint: lll, interfacer nolint: lll, interfacer const EnvOrganization = "PULUMI_ORGANIZATION" ... var ErrPlugins = errors.New("pulumi: plugins requested") ``` After ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi | head -n8 package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" const EnvOrganization = "PULUMI_ORGANIZATION" ... var ErrPlugins = errors.New("pulumi: plugins requested") func BoolRef(v bool) *bool func Float64Ref(v float64) *float64 func IntRef(v int) *int func IsSecret(o Output) bool ``` Before ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi URN_ package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" func URN_(o string) ResourceOption URN_ is an optional URN of a previously-registered resource of this type to read from the engine. nolint: revive ``` After: ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi URN_ package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" func URN_(o string) ResourceOption URN_ is an optional URN of a previously-registered resource of this type to read from the engine. ``` Note that golangci-lint offers a 'nolintlint' linter that finds such miuses of nolint, but it also finds other issues so I've deferred that to a follow up PR. Resolves #11785 Related: https://github.com/golangci/golangci-lint/issues/892 [git-generate] FILES=$(mktemp) rg -l '// nolint|//nolint: ' | tee "$FILES" | xargs perl -p -i -e ' s|// nolint|//nolint|g; s|//nolint: |//nolint:|g; ' rg '.go$' < "$FILES" | xargs gofmt -w -s
2023-01-06 00:07:45 +00:00
//nolint:lll
func TestApplyRewriter(t *testing.T) {
t.Parallel()
cases := []struct {
input, output string
skipPromises bool
}{
{
input: `"v: ${resource.foo.bar}"`,
output: `__apply(resource.foo,eval(foo, "v: ${foo.bar}"))`,
},
{
input: `"v: ${resource.baz[0]}"`,
output: `__apply(resource.baz,eval(baz, "v: ${baz[0]}"))`,
},
{
input: `"v: ${resources[0].foo.bar}"`,
output: `__apply(resources[0].foo,eval(foo, "v: ${foo.bar}"))`,
},
{
input: `"v: ${resources.*.id[0]}"`,
output: `__apply(resources.*.id[0],eval(id, "v: ${id}"))`,
},
{
input: `"v: ${element(resources.*.id, 0)}"`,
output: `__apply(element(resources.*.id, 0),eval(ids, "v: ${ids}"))`,
},
{
input: `"v: ${[for r in resources: r.id][0]}"`,
output: `__apply([for r in resources: r.id][0],eval(id, "v: ${id}"))`,
},
{
input: `"v: ${element([for r in resources: r.id], 0)}"`,
output: `__apply(element([for r in resources: r.id], 0),eval(ids, "v: ${ids}"))`,
},
{
input: `"v: ${resource[key]}"`,
output: `__apply(resource[key],eval(key, "v: ${key}"))`,
},
{
input: `"v: ${resource[resource.id]}"`,
output: `__apply(__apply(resource.id,eval(id, resource[id])),eval(id, "v: ${id}"))`,
},
{
input: `resourcesPromise.*.id`,
output: `__apply(resourcesPromise, eval(resourcesPromise, resourcesPromise.*.id))`,
},
{
input: `[for r in resourcesPromise: r.id]`,
output: `__apply(resourcesPromise,eval(resourcesPromise, [for r in resourcesPromise: r.id]))`,
},
{
input: `resourcesOutput.*.id`,
output: `__apply(resourcesOutput, eval(resourcesOutput, resourcesOutput.*.id))`,
},
{
input: `[for r in resourcesOutput: r.id]`,
output: `__apply(resourcesOutput,eval(resourcesOutput, [for r in resourcesOutput: r.id]))`,
},
{
input: `"v: ${[for r in resourcesPromise: r.id]}"`,
output: `__apply(__apply(resourcesPromise,eval(resourcesPromise, [for r in resourcesPromise: r.id])),eval(ids, "v: ${ids}"))`,
},
{
input: `toJSON({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = "*"
Action = [ "s3:GetObject" ]
Resource = [ "arn:aws:s3:::${resource.id}/*" ]
}]
})`,
output: `__apply(resource.id,eval(id, toJSON({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = "*"
Action = [ "s3:GetObject" ]
Resource = [ "arn:aws:s3:::${id}/*" ]
}]
})))`,
},
{
input: `getPromise().property`,
output: `__apply(getPromise(), eval(getPromise, getPromise.property))`,
},
{
input: `getPromise().object.foo`,
output: `__apply(getPromise(), eval(getPromise, getPromise.object.foo))`,
},
{
input: `getPromise().property`,
output: `getPromise().property`,
skipPromises: true,
},
{
input: `getPromise().object.foo`,
output: `getPromise().object.foo`,
skipPromises: true,
},
{
input: `getPromise(resource.id).property`,
output: `__apply(__apply(resource.id,eval(id, getPromise(id))), eval(getPromise, getPromise.property))`,
},
}
resourceType := model.NewObjectType(map[string]model.Type{
"id": model.NewOutputType(model.StringType),
"foo": model.NewOutputType(model.NewObjectType(map[string]model.Type{
"bar": model.StringType,
})),
"baz": model.NewOutputType(model.NewListType(model.StringType)),
})
scope := model.NewRootScope(syntax.None)
scope.Define("key", &model.Variable{
Name: "key",
VariableType: model.StringType,
})
scope.Define("resource", &model.Variable{
Name: "resource",
VariableType: resourceType,
})
scope.Define("resources", &model.Variable{
Name: "resources",
VariableType: model.NewListType(resourceType),
})
scope.Define("resourcesPromise", &model.Variable{
Name: "resourcesPromise",
VariableType: model.NewPromiseType(model.NewListType(resourceType)),
})
scope.Define("resourcesOutput", &model.Variable{
Name: "resourcesOutput",
VariableType: model.NewOutputType(model.NewListType(resourceType)),
})
functions := pulumiBuiltins(bindOptions{})
scope.DefineFunction("element", functions["element"])
scope.DefineFunction("toJSON", functions["toJSON"])
scope.DefineFunction("getPromise", model.NewFunction(model.StaticFunctionSignature{
Parameters: []model.Parameter{{
Name: "p",
Type: model.NewOptionalType(model.StringType),
}},
ReturnType: model.NewPromiseType(model.NewObjectType(map[string]model.Type{
"property": model.StringType,
"object": model.NewObjectType(map[string]model.Type{
"foo": model.StringType,
}),
})),
}))
for _, c := range cases {
c := c
t.Run(c.input, func(t *testing.T) {
t.Parallel()
expr, diags := model.BindExpressionText(c.input, scope, hcl.Pos{})
assert.Len(t, diags, 0)
expr, diags = RewriteApplies(expr, nameInfo(0), !c.skipPromises)
assert.Len(t, diags, 0)
assert.Equal(t, c.output, fmt.Sprintf("%v", expr))
})
}
[program-gen] Emit Output-returning JSON serialization methods without rewriting applies (#15371) ### Description A while ago we started implementing [specialized JSON serialization methods](https://github.com/pulumi/pulumi/issues/12519) for Pulumi programs which can accept nested outputs without having to rewrite and combine applies. - `Output.SerializeJson` in .NET - `pulumi.jsonStringify` in nodejs - `pulumi.Output.json_dumps` in Python This PR extends program-gen for TypeScript, C# and Python to start emitting these JSON serialization functions (when necessary). The PR special-cases the `toJSON` PCL function when rewriting applies so that nested outputs aren't rewritted. Example PCL program and generated results: > Also check out the downstream codegen tests to see improved generated examples ``` resource vpc "aws:ec2:Vpc" { cidrBlock = "10.100.0.0/16" instanceTenancy = "default" } resource policy "aws:iam/policy:Policy" { description = "test" policy = toJSON({ "Version" = "2012-10-17" "Interpolated" = "arn:${vpc.arn}:value" "Value" = vpc.id }) } ``` ### Generated TypeScript Before ```typescript import * as pulumi from "@pulumi/pulumi"; import * as aws from "@pulumi/aws"; const vpc = new aws.ec2.Vpc("vpc", { cidrBlock: "10.100.0.0/16", instanceTenancy: "default", }); const policy = new aws.iam.Policy("policy", { description: "test", policy: pulumi.all([vpc.arn, vpc.id]).apply(([arn, id]) => JSON.stringify({ Version: "2012-10-17", Interpolated: `arn:${arn}:value`, Value: id, })), }); ``` ### Generated TypeScript After ```typescript import * as pulumi from "@pulumi/pulumi"; import * as aws from "@pulumi/aws"; const vpc = new aws.ec2.Vpc("vpc", { cidrBlock: "10.100.0.0/16", instanceTenancy: "default", }); const policy = new aws.iam.Policy("policy", { description: "test", policy: pulumi.jsonStringify({ Version: "2012-10-17", Interpolated: pulumi.interpolate`arn:${vpc.arn}:value`, Value: vpc.id, }), }); ``` ### Generated Python Before ```python import pulumi import json import pulumi_aws as aws vpc = aws.ec2.Vpc("vpc", cidr_block="10.100.0.0/16", instance_tenancy="default") policy = aws.iam.Policy("policy", description="test", policy=pulumi.Output.all(vpc.arn, vpc.id).apply(lambda arn, id: json.dumps({ "Version": "2012-10-17", "Interpolated": f"arn:{arn}:value", "Value": id, }))) ``` ### Generated Python After ```python import pulumi import json import pulumi_aws as aws vpc = aws.ec2.Vpc("vpc", cidr_block="10.100.0.0/16", instance_tenancy="default") policy = aws.iam.Policy("policy", description="test", policy=pulumi.Output.json_dumps({ "Version": "2012-10-17", "Interpolated": vpc.arn.apply(lambda arn: f"arn:{arn}:value"), "Value": vpc.id, })) ``` ### Generated C# Before ```csharp using System.Collections.Generic; using System.Linq; using System.Text.Json; using Pulumi; using Aws = Pulumi.Aws; return await Deployment.RunAsync(() => { var vpc = new Aws.Ec2.Vpc("vpc", new() { CidrBlock = "10.100.0.0/16", InstanceTenancy = "default", }); var policy = new Aws.Iam.Policy("policy", new() { Description = "test", PolicyDocument = Output.Tuple(vpc.Arn, vpc.Id).Apply(values => { var arn = values.Item1; var id = values.Item2; return JsonSerializer.Serialize(new Dictionary<string, object?> { ["Version"] = "2012-10-17", ["Interpolated"] = $"arn:{arn}:value", ["Value"] = id, }); }), }); }); ``` ### Generated C# After ```csharp using System.Collections.Generic; using System.Linq; using System.Text.Json; using Pulumi; using Aws = Pulumi.Aws; return await Deployment.RunAsync(() => { var vpc = new Aws.Ec2.Vpc("vpc", new() { CidrBlock = "10.100.0.0/16", InstanceTenancy = "default", }); var policy = new Aws.Iam.Policy("policy", new() { Description = "test", PolicyDocument = Output.JsonSerialize(Output.Create(new Dictionary<string, object?> { ["Version"] = "2012-10-17", ["Interpolated"] = vpc.Arn.Apply(arn => $"arn:{arn}:value"), ["Value"] = vpc.Id, })), }); }); ``` ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-20 15:48:46 +00:00
t.Run("skip rewriting applies with toJSON", func(t *testing.T) {
input := `toJSON({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = "*"
Action = [ "s3:GetObject" ]
Resource = [ "arn:aws:s3:::${resource.id}/*" ]
}]
})`
expectedOutput := `toJSON({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = "*"
Action = [ "s3:GetObject" ]
Resource = [
__apply(resource.id,eval(id, "arn:aws:s3:::${id}/*")) ]
}]
})`
expr, diags := model.BindExpressionText(input, scope, hcl.Pos{})
assert.Len(t, diags, 0)
expr, diags = RewriteAppliesWithSkipToJSON(expr, nameInfo(0), false, true /* skiToJson */)
assert.Len(t, diags, 0)
output := fmt.Sprintf("%v", expr)
assert.Equal(t, expectedOutput, output)
})
}