https://pulumi.com logo
#golang
Title
# golang
i

icy-london-58403

10/20/2020, 11:23 PM
can’t find any information on how to access a component’s outputs in golang. I have the following component:
Copy code
// AZ args for the vpc
type AZ struct {
	pulumi.ResourceState
}
// NewAZ makes a new AZ for the VPC to test NAT
func NewAZ(ctx *pulumi.Context, name string, args *AZArgs, opts ...pulumi.ResourceOption) (*AZ, error) {
...
...
...
	ctx.RegisterResourceOutputs(AZ, pulumi.Map{
		"PublicSubnetID":  publicSubnet.ID(),
		"PrivateSubnetID": privateSubnet.ID(),
	})
	return AZ, nil
}
I need the IDs of the subnets being created.
l

lemon-agent-27707

10/20/2020, 11:27 PM
Have you taken a look at this go component resource example? https://github.com/pulumi/examples/tree/master/azure-go-webserver-component
Webserver for instance makes some aggregate outputs available via a public method: https://github.com/pulumi/examples/blob/master/azure-go-webserver-component/webserver.go#L110-L126
You could do something similar, adding a helper method that returns the appropriate subnet IDs for your use case.
(they will be outputs)
i

icy-london-58403

10/20/2020, 11:34 PM
@lemon-agent-27707: thank you so much. This gives me some great info to look over and try.
it’s interesting that this example doesn’t show the
ctx.RegisterResourceOutputs
that the documentation emphasizes is important. This example you sent makes sense. But what then is the point of registering resource outputs besides telling pulumi that your component is completed?
The call to 
registerOutputs
 also tells Pulumi that the resource is done registering children and should be considered fully constructed, so—although it’s not enforced—the best practice is to call it in all components even if no outputs need to be registered.
l

lemon-agent-27707

10/20/2020, 11:52 PM
This is how you make sure a composite output ends up in your state file.
👍 1
i

icy-london-58403

10/20/2020, 11:53 PM
oh ok. that makes sense.
l

lemon-agent-27707

10/20/2020, 11:53 PM
This may or may not be important for your use case, but it will be important when we release multi-language components.
👍 1
i

icy-london-58403

10/20/2020, 11:55 PM
It seems like a best-practice, so I’ll just make sure I do it.
👍 1
@lemon-agent-27707 Thanks! This worked great. I learned a bit more on reflection too in the process. Especially thank you for getting to me so quickly.
partypus 8bit 1
3 Views