Skip to content

Commit

Permalink
[codegen/go] Fix Go resource registrations (#6641)
Browse files Browse the repository at this point in the history
We've been emitting calls to `New<Resource>` for resource registrations
in Go, passing `nil` for args. However, some of those `New<Resource>`
functions actually check for `nil` args and return an error if the
resource has required arguments.

At first, I was looking for a way to check inside `New<Resource>` if
the `URN` option was specified and in that case not error on
`nil` args (like we do in other languages), but we don't provide a way
to access the resource option values outside the Go SDK), so I don't
think there is a way to do it this way for Go.

So instead, this change updates the registration code to call
`ctx.RegisterResource` directly instead of `New<Resource>`, where we can
pass a `nil` args.
  • Loading branch information
justinvp committed Mar 31, 2021
1 parent 656ed79 commit a9f0aea
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
7 changes: 5 additions & 2 deletions pkg/codegen/go/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1839,11 +1839,12 @@ func (pkg *pkgContext) genResourceModule(w io.Writer) {

registrations.Add(tokenToModule(r.Token))
fmt.Fprintf(w, "\tcase %q:\n", r.Token)
fmt.Fprintf(w, "\t\tr, err = New%s(ctx, name, nil, pulumi.URN_(urn))\n", resourceName(r))
fmt.Fprintf(w, "\t\tr = &%s{}\n", resourceName(r))
}
fmt.Fprintf(w, "\tdefault:\n")
fmt.Fprintf(w, "\t\treturn nil, fmt.Errorf(\"unknown resource type: %%s\", typ)\n")
fmt.Fprintf(w, "\t}\n\n")
fmt.Fprintf(w, "\terr = ctx.RegisterResource(typ, name, nil, r, pulumi.URN_(urn))\n")
fmt.Fprintf(w, "\treturn\n")
fmt.Fprintf(w, "}\n\n")
}
Expand All @@ -1861,7 +1862,9 @@ func (pkg *pkgContext) genResourceModule(w io.Writer) {
fmt.Fprintf(w, "\tif typ != \"pulumi:providers:%s\" {\n", pkg.pkg.Name)
fmt.Fprintf(w, "\t\treturn nil, fmt.Errorf(\"unknown provider type: %%s\", typ)\n")
fmt.Fprintf(w, "\t}\n\n")
fmt.Fprintf(w, "\treturn NewProvider(ctx, name, nil, pulumi.URN_(urn))\n")
fmt.Fprintf(w, "\tr := &Provider{}\n")
fmt.Fprintf(w, "\terr := ctx.RegisterResource(typ, name, nil, r, pulumi.URN_(urn))\n")
fmt.Fprintf(w, "\treturn r, err\n")
fmt.Fprintf(w, "}\n\n")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ func (m *module) Version() semver.Version {
func (m *module) Construct(ctx *pulumi.Context, name, typ, urn string) (r pulumi.Resource, err error) {
switch typ {
case "example::Cat":
r, err = NewCat(ctx, name, nil, pulumi.URN_(urn))
r = &Cat{}
case "example::Component":
r, err = NewComponent(ctx, name, nil, pulumi.URN_(urn))
r = &Component{}
case "example::Workload":
r, err = NewWorkload(ctx, name, nil, pulumi.URN_(urn))
r = &Workload{}
default:
return nil, fmt.Errorf("unknown resource type: %s", typ)
}

err = ctx.RegisterResource(typ, name, nil, r, pulumi.URN_(urn))
return
}

Expand All @@ -46,7 +47,9 @@ func (p *pkg) ConstructProvider(ctx *pulumi.Context, name, typ, urn string) (pul
return nil, fmt.Errorf("unknown provider type: %s", typ)
}

return NewProvider(ctx, name, nil, pulumi.URN_(urn))
r := &Provider{}
err := ctx.RegisterResource(typ, name, nil, r, pulumi.URN_(urn))
return r, err
}

func init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ func (p *pkg) ConstructProvider(ctx *pulumi.Context, name, typ, urn string) (pul
return nil, fmt.Errorf("unknown provider type: %s", typ)
}

return NewProvider(ctx, name, nil, pulumi.URN_(urn))
r := &Provider{}
err := ctx.RegisterResource(typ, name, nil, r, pulumi.URN_(urn))
return r, err
}

func init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ func (m *module) Version() semver.Version {
func (m *module) Construct(ctx *pulumi.Context, name, typ, urn string) (r pulumi.Resource, err error) {
switch typ {
case "plant:tree/v1:Nursery":
r, err = NewNursery(ctx, name, nil, pulumi.URN_(urn))
r = &Nursery{}
case "plant:tree/v1:RubberTree":
r, err = NewRubberTree(ctx, name, nil, pulumi.URN_(urn))
r = &RubberTree{}
default:
return nil, fmt.Errorf("unknown resource type: %s", typ)
}

err = ctx.RegisterResource(typ, name, nil, r, pulumi.URN_(urn))
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ func (m *module) Version() semver.Version {
func (m *module) Construct(ctx *pulumi.Context, name, typ, urn string) (r pulumi.Resource, err error) {
switch typ {
case "example::Component":
r, err = NewComponent(ctx, name, nil, pulumi.URN_(urn))
r = &Component{}
default:
return nil, fmt.Errorf("unknown resource type: %s", typ)
}

err = ctx.RegisterResource(typ, name, nil, r, pulumi.URN_(urn))
return
}

Expand All @@ -42,7 +43,9 @@ func (p *pkg) ConstructProvider(ctx *pulumi.Context, name, typ, urn string) (pul
return nil, fmt.Errorf("unknown provider type: %s", typ)
}

return NewProvider(ctx, name, nil, pulumi.URN_(urn))
r := &Provider{}
err := ctx.RegisterResource(typ, name, nil, r, pulumi.URN_(urn))
return r, err
}

func init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ func (m *module) Version() semver.Version {
func (m *module) Construct(ctx *pulumi.Context, name, typ, urn string) (r pulumi.Resource, err error) {
switch typ {
case "example::OtherResource":
r, err = NewOtherResource(ctx, name, nil, pulumi.URN_(urn))
r = &OtherResource{}
case "example::Resource":
r, err = NewResource(ctx, name, nil, pulumi.URN_(urn))
r = &Resource{}
default:
return nil, fmt.Errorf("unknown resource type: %s", typ)
}

err = ctx.RegisterResource(typ, name, nil, r, pulumi.URN_(urn))
return
}

Expand All @@ -44,7 +45,9 @@ func (p *pkg) ConstructProvider(ctx *pulumi.Context, name, typ, urn string) (pul
return nil, fmt.Errorf("unknown provider type: %s", typ)
}

return NewProvider(ctx, name, nil, pulumi.URN_(urn))
r := &Provider{}
err := ctx.RegisterResource(typ, name, nil, r, pulumi.URN_(urn))
return r, err
}

func init() {
Expand Down

0 comments on commit a9f0aea

Please sign in to comment.