hi all, I am pretty sure this is a comon question ...
# getting-started
w
hi all, I am pretty sure this is a comon question but how can I get the value of an output? Essentially the problem here- https://stackoverflow.com/questions/62561660/how-to-convert-pulumi-outputt-to-string
l
If you are not passing it to another Pulumi resources: use apply, and put your code inside the callback that apply generates. Example in Typescript:
Copy code
valueAsOutput.apply(value => console.log(`The value is ${value}`));
If you are passing it to another Pulumi resource: don't worry about it, Pulumi resources can use outputs just fine, even though the docs say "string".
If you are passing it to another Pulumi resource but need to change it a bit: use apply, but pass the returned value to the resource. Example in Typescript:
Copy code
const newResource = new provider.NewResource("example", {
  value: oldResource.apply(old => `Value for new resource is ${old}`)
});
There's a shortcut for strings:
Copy code
const newResource = new provider.NewResource("example", {
  value: pulumi.interpolate`Value for new resource is ${oldResource}`
});
m
calling apply on an Output only returns another Output in python. Is the behavior different in typescript?
l
No. This is correct.
You need to put your code that uses the value inside the apply. Not at the "top" level.
The only things that can access the value in an output are Pulumi resource constructors, and
apply()
e
Note that during preview the value of Outputs might not exist (if for example it's the output from a resource not yet created). In those cases a ".value" on Output would either have to throw an error or return some "undefined" value. There's also the risk that once you've got a plain value the SDK loses all tracking of dependencies and secretness that Output can track. Both of these combined led to the decision that it was a better UX to just not give a method to get the value out and ensure users kept things wrapped in Outputs.
w
ok let me try this. I will let you know if I have any issues.
Ok so picking this up again, this is the code: var clientId = clientApp.ClientId.Apply(x => secret == x); Would this be correct to gt the client id as an output variable?
l
No. That will create a new Output<boolean>.
clientApp.ClientId
is the output you want, you don't need to apply to use it.
You do need to apply to get at the string value inside it, if that's what you need to do. What do you want to do with the ClientId? Do you want to pass it to another Pulumi resource?
w
Hi,
I will be passing this to a console app which will persist it to Octopus Deploy
l
Then you will need to run that console app from inside the apply.
Pseudocode:
clientApp.ClientId.Apply(id => "consoleApp".exec(id))
Note that this will run every time
pulumi up
is run. This might not be what you want.
w
Ah I see! Is there a way I can build a collection of the values I need and call the console app once?
l
Yes. There's a function
All()
that does this. Which language are you using?
w
c#
l
I can't find the C# equivalent of pulumi.all().... I'm sure there's something though 🤔
w
l
That's it. Output.All().
e
I will be passing this to a console app which will persist it to Octopus Deploy
You might want to give the pulumi command package a try for that (https://www.pulumi.com/registry/packages/command/)
w
Ok so I have this code: var clientId = clientApp.ClientId.Apply<string>(x => MyStack.Test("", "")); where Test is a method that can invoke the console app. Using all(), that takes a paramas array and then I can pass that into a method to do whatever I need with the values. I will try the first approach and I can use the command library too. Thanks guys!