Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert invoke results to ouputs when needed #11480

Merged
merged 1 commit into from Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: programgen/go
description: Convert the result of immediate invokes to ouputs when necessary.
4 changes: 1 addition & 3 deletions pkg/codegen/go/gen_program.go
Expand Up @@ -664,9 +664,7 @@ func (g *generator) genResource(w io.Writer, r *pcl.Resource) {
if len(r.Inputs) > 0 {
g.Fgenf(w, "&%s.%sArgs{\n", modOrAlias, typ)
for _, attr := range r.Inputs {
g.Fgenf(w, "%s: ", strings.Title(attr.Name))
g.Fgenf(w, "%.v,\n", attr.Value)

g.Fgenf(w, "%s: %.v,\n", strings.Title(attr.Name), attr.Value)
}
g.Fprint(w, "}")
} else {
Expand Down
28 changes: 20 additions & 8 deletions pkg/codegen/go/gen_program_expressions.go
Expand Up @@ -579,39 +579,51 @@ func (g *generator) genScopeTraversalExpression(
_, isInput = schemaType.(*schema.InputType)
}

if resource, ok := expr.Parts[0].(*pcl.Resource); ok {
var sourceIsPlain bool
switch root := expr.Parts[0].(type) {
case *pcl.Resource:
isInput = false
if _, ok := pcl.GetSchemaForType(resource.InputType); ok {
if _, ok := pcl.GetSchemaForType(root.InputType); ok {
// convert .id into .ID()
last := expr.Traversal[len(expr.Traversal)-1]
if attr, ok := last.(hcl.TraverseAttr); ok && attr.Name == "id" {
genIDCall = true
expr.Traversal = expr.Traversal[:len(expr.Traversal)-1]
}
}
case *pcl.LocalVariable:
if root, ok := root.Definition.Value.(*model.FunctionCallExpression); ok && !pcl.IsOutputVersionInvokeCall(root) {
sourceIsPlain = true
}
}

// TODO if it's an array type, we need a lowering step to turn []string -> pulumi.StringArray
if isInput {
argType := g.argumentTypeName(expr, expr.Type(), isInput)
if strings.HasSuffix(argType, "Array") {
argTypeName := g.argumentTypeName(expr, expr.Type(), isInput)
if strings.HasSuffix(argTypeName, "Array") {
destTypeName := g.argumentTypeName(expr, destType, isInput)
if argType != destTypeName {
// `argTypeName` == `destTypeName` and `argTypeName` ends with `Array`, we
// know that `destType` is an outputty type. If the source is plain (and thus
// not outputty), then the types can never line up and we will need a
// conversion helper method.
if argTypeName != destTypeName || sourceIsPlain {
// use a helper to transform prompt arrays into inputty arrays
var helper *promptToInputArrayHelper
if h, ok := g.arrayHelpers[argType]; ok {
if h, ok := g.arrayHelpers[argTypeName]; ok {
helper = h
} else {
// helpers are emitted at the end in the postamble step
helper = &promptToInputArrayHelper{
destType: argType,
destType: argTypeName,
}
g.arrayHelpers[argType] = helper
g.arrayHelpers[argTypeName] = helper
}
// Wrap the emitted expression in a call to the generated helper function.
g.Fgenf(w, "%s(", helper.getFnName())
defer g.Fgenf(w, ")")
}
} else {
// Wrap the emitted expression in a type conversion.
g.Fgenf(w, "%s(", g.argumentTypeName(expr, expr.Type(), isInput))
defer g.Fgenf(w, ")")
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/codegen/testing/test/program_driver.go
Expand Up @@ -81,9 +81,6 @@ var PulumiPulumiProgramTests = []ProgramTest{
{
Directory: "aws-fargate",
Description: "AWS Fargate",

// TODO[pulumi/pulumi#8440]
SkipCompile: codegen.NewStringSet("go"),
},
{
Directory: "aws-s3-logging",
Expand Down
Expand Up @@ -85,7 +85,7 @@ func main() {
return err
}
webLoadBalancer, err := elasticloadbalancingv2.NewLoadBalancer(ctx, "webLoadBalancer", &elasticloadbalancingv2.LoadBalancerArgs{
Subnets: subnets.Ids,
Subnets: toPulumiStringArray(subnets.Ids),
SecurityGroups: pulumi.StringArray{
webSecurityGroup.ID(),
},
Expand Down Expand Up @@ -153,7 +153,7 @@ func main() {
TaskDefinition: appTask.Arn,
NetworkConfiguration: &ecs.ServiceNetworkConfigurationArgs{
AssignPublicIp: pulumi.Bool(true),
Subnets: subnets.Ids,
Subnets: toPulumiStringArray(subnets.Ids),
SecurityGroups: pulumi.StringArray{
webSecurityGroup.ID(),
},
Expand All @@ -175,3 +175,10 @@ func main() {
return nil
})
}
func toPulumiStringArray(arr []string) pulumi.StringArray {
var pulumiArr pulumi.StringArray
for _, v := range arr {
pulumiArr = append(pulumiArr, pulumi.String(v))
}
return pulumiArr
}