Skip to content

Commit

Permalink
[program-gen] Emit the Deployment class when using PCL functions stac…
Browse files Browse the repository at this point in the history
…k() and projectName() (#1518)

Fixes pulumi/pulumi#16385
  • Loading branch information
Zaid-Ajaj authored Dec 24, 2024
1 parent d2a85f0 commit dca6f89
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
- Update to Pulumi 3.143.0

### Bug Fixes

- [Convert] Emit the `Deployment` class when using Pulumi built-in functions in PCL `stack()` and `projectName()`
33 changes: 23 additions & 10 deletions pkg/codegen/java/gen_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,10 @@ func (g *generator) collectFunctionCallImports(functionCall *model.FunctionCallE
imports = append(imports, collectObjectImports(argumentsExpr, argumentExprType)...)
}
}
case "stack", "projectName":
// stack() and projectName() functions are pulumi built-ins
// they require the Deployment class
imports = append(imports, "com.pulumi.deployment.Deployment")
}

return imports
Expand All @@ -678,24 +682,33 @@ func removeDuplicates(inputs []string) []string {
// configuration or used in function calls
func (g *generator) collectImports(nodes []pcl.Node) []string {
imports := make([]string, 0)

visitFunctionCalls := func(expr model.Expression) (model.Expression, hcl.Diagnostics) {
switch expr := expr.(type) {
case *model.FunctionCallExpression:
imports = append(imports, g.collectFunctionCallImports(expr)...)
}
return expr, nil
}

for _, node := range nodes {
switch node := node.(type) {
case *pcl.Resource:
// collect resource imports
resource := node
imports = append(imports, collectResourceImports(resource)...)
for _, prop := range resource.Inputs {
_, diags := model.VisitExpression(prop.Value, model.IdentityVisitor, visitFunctionCalls)
g.diagnostics = append(g.diagnostics, diags...)
}
case *pcl.LocalVariable:
localVariable := node
switch localVariable.Definition.Value.(type) {
case *model.FunctionCallExpression:
// collect function invoke imports
// traverse the args and inner objects recursively
functionCall := localVariable.Definition.Value.(*model.FunctionCallExpression)
_, isInvokeCall := g.isFunctionInvoke(localVariable)
if isInvokeCall {
imports = append(imports, g.collectFunctionCallImports(functionCall)...)
}
}
_, diags := model.VisitExpression(localVariable.Definition.Value, model.IdentityVisitor, visitFunctionCalls)
g.diagnostics = append(g.diagnostics, diags...)
case *pcl.OutputVariable:
outputVariable := node
_, diags := model.VisitExpression(outputVariable.Value, model.IdentityVisitor, visitFunctionCalls)
g.diagnostics = append(g.diagnostics, diags...)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.deployment.Deployment;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}

public static void stack(Context ctx) {
ctx.export("stackName", Deployment.getInstance().getStackName());
ctx.export("projectName", Deployment.getInstance().getProjectName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "stackName" {
value = stack()
}

output "projectName" {
value = project()
}

0 comments on commit dca6f89

Please sign in to comment.