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

Emit imports for input types from external packages #15971

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: programgen/python
description: Emit imports for input types from external packages
17 changes: 17 additions & 0 deletions pkg/codegen/python/gen_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,24 @@ func (g *generator) genPreamble(w io.Writer, program *pcl.Program, preambleHelpe
}
}
importSet[packageName] = Import{ImportAs: true, Pkg: makeValidIdentifier(pkg)}

if r.Schema != nil {
codegen.VisitTypeClosure(r.Schema.InputProperties, func(t schema.Type) {
switch t := t.(type) {
case *schema.ObjectType:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to also handle EnumType here

components := strings.Split(t.Token, ":")
contract.Assertf(len(components) == 3, "malformed token %v", t.Token)
pkg := makeValidIdentifier(components[0])
packageName := "pulumi_" + pkg
importSet[packageName] = Import{
ImportAs: true,
Pkg: pkg,
}
}
})
}
}

diags := n.VisitExpressions(nil, func(n model.Expression) (model.Expression, hcl.Diagnostics) {
if call, ok := n.(*model.FunctionCallExpression); ok {
if i := g.getFunctionImports(call); len(i) > 0 && i[0] != "" {
Expand Down
5 changes: 5 additions & 0 deletions pkg/codegen/testing/test/program_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ var PulumiPulumiProgramTests = []ProgramTest{
Description: "Tests whether using inline invoke expressions works",
SkipCompile: codegen.NewStringSet("go"),
},
{
Directory: "python-import-external-type",
Description: "Tests importing external types in python",
Skip: allProgLanguages.Except("python"),
},
}

var PulumiPulumiYAMLProgramTests = []ProgramTest{
Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/testing/utils/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ func NewHost(schemaDirectoryPath string) plugin.Host {
SchemaProvider{"plain-properties", "1.0.0"},
SchemaProvider{"recursive", "1.0.0"},
SchemaProvider{"aws-static-website", "0.4.0"},
SchemaProvider{"external-type-ref", "1.0.0"},
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pulumi
import pulumi_aws as aws
import pulumi_aws_static_website as aws_static_website

website_resource = aws_static_website.Website("websiteResource",
Expand Down
24 changes: 24 additions & 0 deletions tests/testdata/codegen/external-type-ref-1.0.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "https://raw.githubusercontent.com/pulumi/pulumi/master/pkg/codegen/schema/pulumi.json",
"name": "external-type-ref",
"version": "1.0.0",
"resources": {
"external-type-ref:index:ExampleComponent": {
"isComponent": true,
"inputProperties": {
"externalInput": {
"type": "object",
"$ref": "/aws/v5.4.0/schema.json#/types/aws:s3/BucketWebsite:BucketWebsite"
}
}
}
},
"language": {
"python": {
"requires": {
"pulumi": ">=3.0.0,<4.0.0",
"pulumi-aws": ">=5.3.0,<6.0.0"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "main" "external-type-ref:index:ExampleComponent" {
externalInput = {
indexDocument = "index.html"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pulumi
import pulumi_aws as aws
import pulumi_external_type_ref as external_type_ref

main = external_type_ref.ExampleComponent("main", external_input=aws.s3.BucketWebsiteArgs(
index_document="index.html",
))
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pulumi
import pulumi_aws as aws
import pulumi_eks as eks
import pulumi_kubernetes as kubernetes

vpc_id = aws.ec2.get_vpc(default=True).id
subnet_ids = aws.ec2.get_subnet_ids(vpc_id=vpc_id).ids
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pulumi
import pulumi_aws as aws
import pulumi_eks as eks
import pulumi_kubernetes as kubernetes

rawkode = eks.Cluster("rawkode",
instance_type="t2.medium",
Expand Down