pulumi/tests/testdata/codegen/simple-yaml-schema/go/example/resource.go

120 lines
3.1 KiB
Go
Raw Permalink Normal View History

// Code generated by test DO NOT EDIT.
// *** WARNING: Do not edit by hand unless you're certain you know what you are doing! ***
package example
import (
"context"
"reflect"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
2023-06-14 19:59:47 +00:00
"simple-yaml-schema/example/internal"
)
type Resource struct {
pulumi.CustomResourceState
Bar pulumi.StringPtrOutput `pulumi:"bar"`
}
// NewResource registers a new resource with the given unique name, arguments, and options.
func NewResource(ctx *pulumi.Context,
name string, args *ResourceArgs, opts ...pulumi.ResourceOption) (*Resource, error) {
if args == nil {
args = &ResourceArgs{}
}
if args.Bar != nil {
fix(sdkgen/go): illegal cast in resource constructors when secret-wrapping input arguments Codegen for wrapping input properties as secrets performed an incorrect cast, as seen in Pulumi's AWS classic SDK. Using the sample program and the resource constructor described in #11664 as our test case, from `pulumi-aws/sdk/v5/go/aws/secretmanager/secretVersion.go`: ```go func NewSecretVersion(ctx *pulumi.Context, name string, args *SecretVersionArgs, opts ...pulumi.ResourceOption) (*SecretVersion, error) { if args == nil { return nil, errors.New("missing one or more required arguments") } if args.SecretId == nil { return nil, errors.New("invalid value for required argument 'SecretId'") } if args.SecretBinary != nil { 82: args.SecretBinary = pulumi.ToSecret(args.SecretBinary).(pulumi.StringPtrOutput) } if args.SecretString != nil { 85: args.SecretString = pulumi.ToSecret(args.SecretString).(pulumi.StringPtrOutput) } ``` `args.SecretBinary` is of type `pulumi.StringPtrInput` and `pulumi.ToSecret` returns `pulumi.Output` returns its input as an Output-wrapped value marked secret. As `StringPtrInput` is an interface accepting multiple input types, the return value would be either `pulumi.StringOutput` `pulumi.StringPtrOutput`. These are both concrete types, and casting to the incorrect one would panic. Fortunately we can cast back to the arg's type, as verified by building the new codegen and testing the Pulumi program in #11664. This should handle regular inputs and plain inputs. The new codegen below converts an input type `T` to `pulumi.Output`, then casts back to `T`. ```go func NewSecretVersion(ctx *pulumi.Context, // ... if args.SecretBinary != nil { 82: args.SecretBinary = pulumi.ToSecret(args.SecretBinary).(pulumi.StringPtrInput) } if args.SecretString != nil { 85: args.SecretString = pulumi.ToSecret(args.SecretString).(pulumi.StringPtrInput) } ```
2022-12-16 23:39:54 +00:00
args.Bar = pulumi.ToSecret(args.Bar).(pulumi.StringPtrInput)
}
secrets := pulumi.AdditionalSecretOutputs([]string{
"bar",
})
opts = append(opts, secrets)
2023-06-14 16:34:49 +00:00
opts = internal.PkgResourceDefaultOpts(opts)
var resource Resource
err := ctx.RegisterResource("example::Resource", name, args, &resource, opts...)
if err != nil {
return nil, err
}
return &resource, nil
}
// GetResource gets an existing Resource resource's state with the given name, ID, and optional
// state properties that are used to uniquely qualify the lookup (nil if not required).
func GetResource(ctx *pulumi.Context,
name string, id pulumi.IDInput, state *ResourceState, opts ...pulumi.ResourceOption) (*Resource, error) {
var resource Resource
err := ctx.ReadResource("example::Resource", name, id, state, &resource, opts...)
if err != nil {
return nil, err
}
return &resource, nil
}
// Input properties used for looking up and filtering Resource resources.
type resourceState struct {
}
type ResourceState struct {
}
func (ResourceState) ElementType() reflect.Type {
return reflect.TypeOf((*resourceState)(nil)).Elem()
}
type resourceArgs struct {
Bar *string `pulumi:"bar"`
}
// The set of arguments for constructing a Resource resource.
type ResourceArgs struct {
Bar pulumi.StringPtrInput
}
func (ResourceArgs) ElementType() reflect.Type {
return reflect.TypeOf((*resourceArgs)(nil)).Elem()
}
type ResourceInput interface {
pulumi.Input
ToResourceOutput() ResourceOutput
ToResourceOutputWithContext(ctx context.Context) ResourceOutput
}
func (*Resource) ElementType() reflect.Type {
[codegen/go] Remove ResourcePtr input/output types (#8449) These changes remove the `Ptr` variants of input/ouptut types for resources. A `TPtr` input or output is normally generated for `T` if `T` is present in an `optional(input(T))` or `optional(output(T))` and if the Go representation for `T` is not nilable. The generation of `Ptr` variants for resource types breaks the latter rule: the canonical representation of a resource type named `Foo` is a pointer to a struct type named `Foo` (i.e. `*Foo`). `Foo` itself is not a resource, as it does not implement the Go `Resource` interface. Because this representation already accommodates `nil` to indicate the lack of a value, we need not generate `FooPtr{Input,Output}` types. Besides being unnecessary, the implementation of `Ptr` types for resources was incorrect. Rather than using `**Foo` as their element type, these types use `*Foo`--identical to the element type used for the normal input/output types. Furthermore, the generated code for at least `FooOutput.ToFooPtrOutputWithContext` and `FooPtrOutput.Elem` was incorrect, making these types virtually unusable in practice. Finally, these `Ptr` types should never appear on input/output properties in practice, as the logic we use to generate input and output type references never generates them for `optional({input,output}(T)). Instead, it generates references to the standard input/output types. Though this is _technically_ a breaking change--it changes the set of exported types for any package that defines resources--I believe that in practice it will be invisible to users for the reasons stated above. These types are not usable, and were never referenced. This is preparatory work for #7943.
2021-11-23 18:24:56 +00:00
return reflect.TypeOf((**Resource)(nil)).Elem()
}
func (i *Resource) ToResourceOutput() ResourceOutput {
return i.ToResourceOutputWithContext(context.Background())
}
func (i *Resource) ToResourceOutputWithContext(ctx context.Context) ResourceOutput {
return pulumi.ToOutputWithContext(ctx, i).(ResourceOutput)
}
type ResourceOutput struct{ *pulumi.OutputState }
func (ResourceOutput) ElementType() reflect.Type {
[codegen/go] Remove ResourcePtr input/output types (#8449) These changes remove the `Ptr` variants of input/ouptut types for resources. A `TPtr` input or output is normally generated for `T` if `T` is present in an `optional(input(T))` or `optional(output(T))` and if the Go representation for `T` is not nilable. The generation of `Ptr` variants for resource types breaks the latter rule: the canonical representation of a resource type named `Foo` is a pointer to a struct type named `Foo` (i.e. `*Foo`). `Foo` itself is not a resource, as it does not implement the Go `Resource` interface. Because this representation already accommodates `nil` to indicate the lack of a value, we need not generate `FooPtr{Input,Output}` types. Besides being unnecessary, the implementation of `Ptr` types for resources was incorrect. Rather than using `**Foo` as their element type, these types use `*Foo`--identical to the element type used for the normal input/output types. Furthermore, the generated code for at least `FooOutput.ToFooPtrOutputWithContext` and `FooPtrOutput.Elem` was incorrect, making these types virtually unusable in practice. Finally, these `Ptr` types should never appear on input/output properties in practice, as the logic we use to generate input and output type references never generates them for `optional({input,output}(T)). Instead, it generates references to the standard input/output types. Though this is _technically_ a breaking change--it changes the set of exported types for any package that defines resources--I believe that in practice it will be invisible to users for the reasons stated above. These types are not usable, and were never referenced. This is preparatory work for #7943.
2021-11-23 18:24:56 +00:00
return reflect.TypeOf((**Resource)(nil)).Elem()
}
func (o ResourceOutput) ToResourceOutput() ResourceOutput {
return o
}
func (o ResourceOutput) ToResourceOutputWithContext(ctx context.Context) ResourceOutput {
return o
}
func (o ResourceOutput) Bar() pulumi.StringPtrOutput {
return o.ApplyT(func(v *Resource) pulumi.StringPtrOutput { return v.Bar }).(pulumi.StringPtrOutput)
}
func init() {
pulumi.RegisterInputType(reflect.TypeOf((*ResourceInput)(nil)).Elem(), &Resource{})
pulumi.RegisterOutputType(ResourceOutput{})
}