This message was deleted.
s
This message was deleted.
b
Template literal {`com.cloudflare.api.account.zone.${zoneId}`: "*",},
Slack is hiding it but the string is wrapped with backticks rather than quotes
s
since your key is not a plain string/number, you'll need square brackets around it too
Copy code
{[`com.cloudflare.api.account.zone.${zoneId}`]: "*"},
e
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
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
Can you post more of code here
e
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
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>
Copy code
variable.id.apply(id => ({[`com.blahblah.${id}`]: "*"}))
b
"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
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.
Copy code
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
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`:
Copy code
policies: variable.id.apply(id => [
        {
            permissionGroups: [dnsWritePermissionId],
            resources: {[`com.cloudflare.api.account.zone.${id}`]: "*",},
        },
    ],
e
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
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
I am using VS Code. I see with the hover the difference. Thank you for your help! Much appreciated!!
p 1
@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
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
Excellent point! There is much to learn. Thank you!
b
Thanks Brian and Mike. I just had to refer to this thread because I was getting get wrong.
👍 1
p 1