https://pulumi.com logo
Title
e

enough-caravan-98871

02/14/2023, 12:54 AM
Noob question.... my property requires an object of key: string like
resources: {"com.cloudflare.api.account.zone.<zoneId>": "*",},
. How can I replace <zoneId> with a variable?
b

best-summer-38252

02/14/2023, 12:57 AM
Template literal {`com.cloudflare.api.account.zone.${zoneId}`: "*",},
Slack is hiding it but the string is wrapped with backticks rather than quotes
s

steep-toddler-94095

02/14/2023, 1:00 AM
since your key is not a plain string/number, you'll need square brackets around it too
{[`com.cloudflare.api.account.zone.${zoneId}`]: "*"},
e

enough-caravan-98871

02/14/2023, 1:05 AM
I had tried the general backticks method. I got happy for a minute with the brackets around all of that because it made the code clean. However pulumi up produces the following:
I find it hit and miss with what I can do in ts/node and what I have to use pulumi for. If the zoneId is not a string but an output then you need to do it the pulumi way
e

enough-caravan-98871

02/14/2023, 1:08 AM
Been through that article so many times. Seems like it is a different prescription every time. I will keep plugging through it until I figure it out I guess.
s

steep-toddler-94095

02/14/2023, 1:09 AM
Can you post more of code here
e

enough-caravan-98871

02/14/2023, 1:11 AM
Thats really it. I already have the zone.id from the zone resource. I just need to plugin that in as a variable as part of the key for this key: string object.
I have used
pulumi.interpolate
for many cases. Because in this instance, it is an object, it seems more difficult. It wont let me use
pulumi
functions inside of the `{}' object and it wont let me wrap the entire object.
Here is what I have now and it shows as clean code.
I imagine the lifted property method is what is going to be the trick here. More power equals more complexity I guess. 😀
s

steep-toddler-94095

02/14/2023, 1:31 AM
the reason you don't get a compile error is because `Output`s have a
toString
method that's called when you attempt to interpolate it, but that method gives you some human readable garbage string. So from the compiler's standpoint, everything's fine. You need the Type of the variable you are interpolating to be a
string
, not an
Output<string>
. To do that, you can use the
apply
method on the value you want to interpolate so that you can interact with its string value as a string: assuming
resources
takes an
Input<T>
variable.id.apply(id => ({[`com.blahblah.${id}`]: "*"}))
b

best-summer-38252

02/14/2023, 1:32 AM
"keep plugging through it until I figure it out I guess." I feel a trial and error dev experience too and even started messing with cdktf last week over it. Do you need to apply the resources that is giving you the resolved zone and then access the id property?
Ya, what Mike said 😄
e

enough-caravan-98871

02/14/2023, 2:37 AM
None of that is working here and it appears to me that the Cloudflare API does not like it. Here is a more complete snip of my code currently.
export const zoneId = pulumi.concat("com.cloudflare.api.account.zone.", zone.id,);

const testApiToken = new cloudflare.ApiToken("testApiToken", {
    
    expiresOn: "2023-02-19T00:00:00Z",
    name: "testApi_token",
    notBefore: "2023-02-13T00:00:00Z",
    policies: [
        {
            permissionGroups: [dnsWritePermissionId],
            resources: {[`${zoneId}`]: "*",},
            //resources: {"com.cloudflare.api.account.zone.13328431d71a7bca194bdf56aea26b35": "*",},
        },
    ],
},{provider: providerApiTokens});
The commented line line works perfectly. The output of zoneId matches the key in the commented line identically. Its is just a simple text string.
s

steep-toddler-94095

02/14/2023, 3:03 AM
it doesn't work because
resources
does not take an
Input
. Pulumi `Output`s only make sense as inputs to props that accept an
Input
. just move up where the
variable.id.apply
id done. for example, if
policies
takes an `Input`:
policies: variable.id.apply(id => [
        {
            permissionGroups: [dnsWritePermissionId],
            resources: {[`com.cloudflare.api.account.zone.${id}`]: "*",},
        },
    ],
e

enough-caravan-98871

02/14/2023, 3:23 AM
That works! It makes sense and it is cool that you can just move up the tree. I am not sure how I would know or clearly see that resources does not take an input. Perhaps that is a stupid question but I am just trying to learn from this one.
s

steep-toddler-94095

02/14/2023, 3:26 AM
If you are using an IDE that supports TypeScript (like VS Code) you can just hold down the
cmd
button and left click on a property, e.g.
policies
(or just hover your mouse over the param for a handy popup). You can traverse the type downward from there too.
e

enough-caravan-98871

02/14/2023, 3:31 AM
I am using VS Code. I see with the hover the difference. Thank you for your help! Much appreciated!!
@best-summer-38252 Thank you too! I think the more seasoned I get with Typescript, the more I will like Pulumi. There are several other things I like too but I still struggle in places. It is forcing me to reference the source provider's API documentation more which is probably a good thing. With TF, I mostly rely on their documentation as it is good enough. I get frustrated sometimes but the bottom line is that I have only been using Pulumi for a little over a month. I have used TF for years.
s

steep-toddler-94095

02/14/2023, 5:14 PM
It is forcing me to reference the source provider's API documentation more
you shouldn't need to do this outside very rare situations. Let VS Code help you navigate the codebase with auto-import, auto-hinting, cmd+click discovery, etc. once you get in tune with your IDE, you should find you need to reference documentation less than you did with Terraform, since much of the documentation is in the TypeScript Types.
e

enough-caravan-98871

02/14/2023, 8:38 PM
Excellent point! There is much to learn. Thank you!
b

best-summer-38252

02/22/2023, 10:48 PM
Thanks Brian and Mike. I just had to refer to this thread because I was getting get wrong.