https://pulumi.com logo
Title
a

adventurous-apartment-93389

12/30/2022, 10:49 AM
I'm just following on from Tim Jones post about converting StringOutputs to regular strings.... Use Case: I want to create reusable code in the form of golang packages BUT I need to parameterise the URN and pad it with data such as the project Id e.g. I want to create x3 GCP Projects and each has a VPC all called VPC-1 I get a name clash in the URN, I can
fmt.Sprintf("%s-%s", projectID, args.Name)
but I need to get the projectId from the output of a previous run that creates the project Id (which is a pulumi.StringOutput). I've tried everything with the ApplyT but it doesn't seem to work.... These pulumi types are killing me here, any ideas? my programme:
var project project.Project
         var vpc  vpc.Vpc
 
         pulumi.Run(func(ctx *pulumi.Context) error {
                 project.Args.Name = "b1-services"
                 project.Args.FolderId = "folders/415061719873"
                 project.Args.BillingAccount = "01504C-A2522F-2110FA"
                 project.Args.AutoCreateNetwork = false
 
                 project.Args.Services = []string{"<http://compute.googleapis.com|compute.googleapis.com>", "<http://container.googleapis.com|container.googleapis.com>"}
 
                 prj, err := project.Create(ctx)
                 if err != nil {
                         log.Println(err)
                 }
 
                 vpc.Args.Name = "vpc-1"
                 vpc.Args.ProjectId = prj.ProjectId
                 //vpc.Args.Project = pulumi.Sprintf("%s", prj.ProjectId)
 
 
                 vpcNetwork, err := vpc.Create(ctx)
                 if err != nil {
                         log.Println(err)
                 }
my package:
package vpc 
 
 import (
         "<http://github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/compute|github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/compute>"
         "<http://github.com/pulumi/pulumi/sdk/v3/go/pulumi|github.com/pulumi/pulumi/sdk/v3/go/pulumi>"
         "fmt"
 )
 
 type VpcArgs struct {
         Name                        string
         Description                 string
         ProjectId                   pulumi.StringOutput
         RoutingMode                 string
         AutoCreateSubnetworks       bool
         DeleteDefaultRoutesOnCreate bool
         EnableUIaInternalIpv6       bool
         InternalIpv6Range           bool
 }
 
 type Vpc struct {
         Args VpcArgs
         Name string
 }
 
 func (vpc *Vpc) Create(ctx *pulumi.Context) (vpcNetwork *compute.Network, err error) {
         args := &compute.NetworkArgs{}
         args.Name = pulumi.String(vpc.Args.Name)
         args.Project = vpc.Args.ProjectId
         args.AutoCreateSubnetworks = pulumi.Bool(vpc.Args.AutoCreateSubnetworks)
 
         var projectId string
         vpc.Args.ProjectId.ApplyT(func(p string) error {
                 projectId = fmt.Sprintf("%s", p)
                 return nil
         })  
 
         fmt.Println(projectId)
 
         vpcNetwork, err = compute.NewNetwork(ctx, fmt.Sprintf("%s-%s",projectId,args.Name), args)
         ctx.Export("vpc", vpcNetwork)
         return vpcNetwork, err 
 }
b

billowy-army-68599

12/30/2022, 11:21 AM
It’s hard to read the code as I’m on mobile at the moment, but a few notes. Resource names must have known values when they’re created, so as you noted, it you want to create a resource name from the output of another resource you’d need to do it inside an apply. However Creating resources inside an apply is generally not recommend because it means previews aren’t correct. The best way to move forward for your issue I think is to decide on a name prefix as a string variable and pass that to every resource
So basically, make the project id a strong
a

adventurous-apartment-93389

12/30/2022, 11:30 AM
That makes sense but I am struggling to make project Id a string - I could generate a random string but again that's a StringOuput unless I use a golang native rand function which doesn't get committed to state which means on every run it mutates.
b

billowy-army-68599

12/30/2022, 11:35 AM
What I mean is, set the type on the struct as a string and don’t use an apply at all. You can’t convert an output to a string, ever. This may help: https://leebriggs.co.uk/blog/2021/05/09/pulumi-apply.html
a

adventurous-apartment-93389

12/30/2022, 11:43 AM
oooh interesting ok thanks for that i'll try that
appreciate the quick responses Lee! thanks!