This message was deleted.
s
This message was deleted.
l
This is the classic async/await problem. You could mark a method as async, but then you can't use it in a constructor. Normally, the correct solution is to not resolve the async value until
up
-time.
In this case, it might be something like
Copy code
constructor(name, args, opts) {
    super('pkg:CustomComponent', name, {}, opts)

    const computeDefaultServiceAccount = gcp.compute.getDefaultServiceAccount({project: projectId});
    gcp.projects.IAMBinding('blah', { role: '', project: projectId, members: [pulumi.interpolate`serviceAcccount:${computeDefaultServiceAccount.email}`]}
  }
(Code untested)
Note that this requires that the
members
property takes a
pulumi.Input<string>[]
rather than just a
string[]
. Not all properties do.. it can be tricky to get it working in cases where a
string
is required...
b
awesome answer @little-cartoon-10569
l
Really? Thanks. I can't tell. As my socks say, I cannot tell a lie, I just make $h!t up.
n
@little-cartoon-10569 Since members is an array of Input <string> it can also accept a promise. So I am currently going to try setting members to a promise:
Copy code
members: [gcp.compute
          .getDefaultServiceAccount({ project: args.projectId })
          .then(sa => `serviceAccount:${sa.email}`)]
@little-cartoon-10569 do you happen have an example where it can be resolved at up time? As I think I may run into that in the future.
l
Can you explain what you're looking for? Sorry, I'm not sure what "it" is in that sentence?
Ah I think I see, you're referring back to the first response. The example I gave above, using
pulumi.interpolate
, is the example. I'll see if I can find one in the github repo, shouldn't be too hard.
Wow, it really is close.. my example could have been lifted from there!
FYI, you can tell that something is "resolved at
up
-time" just by checking that it's some type of
Output
.
pulumi.interpotate
returns an Output, and you can't view its value until you run
pulumi up
.
n
@little-cartoon-10569 sorry for the confusion I mean’t the example where it expects a string and you are struck trying to resolve a promise inside the constructor
l
Fortunately it doesn't expect a string 🙂 As the example I linked to in github shows (see line 22), the
members
property is an array of
Input<string>
and thus can deal with stuff that is resolved at
up
-time. Which is what
pulumi.interpolate
does.
n
@little-cartoon-10569 I mean for other situations where it expects a string and not input type.
Here is an example: Create a project Pass project (Output type) as a parameter to your component resource Now try to access projectId as string in the constructor as it is needed for gcp.compute.getDefaultserviceAccount How does one resolve an Output to string? I keep running into this Input/Output/Interpolate/All/Apply conundrum. It makes using Pulumi very frustrating.
l
Yes, the getXYZ functions make it hard, since they're wrappers around the SDK, and not aware of Inputs or outputs. In general, I've moved towards importing the resources first, and not bothering with the getXYZ functions.
One way to work around it is to pass the value in via Config instead of StackResource, so you can get an actual string instead of an Output<string>.
n
Since I am creating the project let me see if I can generate the service account name so I don’t have to use get.
l
If it's a constant, you could also hardcode it or pass it in via Config.