Hi all, I've got a `pulumi.StringInput` that I nee...
# golang
f
Hi all, I've got a
pulumi.StringInput
that I need to convert into a regular golang
string
so I can
fmt.Printf
it, can anyone help me out?
g
Copy code
StringOutput.ApplyT(func(myString string) error {
// use Output as string here
return nil
})
Sounds like you might want
Copy code
ctx.Export() though.
f
sorry, I meant
StringInput
, edited my original message.
g
Copy code
StringInput.ToStringOutput.ApplyT
f
I think that's what I tried, following the docs (https://www.pulumi.com/docs/intro/concepts/inputs-outputs/#apply) but it still outputs as a
StringOutput
g
But I think
Copy code
string(StringInput) should work fine.
f
I'm really struggling with the pulumi type system :/
I tried that too, but get
Copy code
cannot convert nodeId (variable of type pulumi.StringInput) to type string
g
Curious, what's your imports look like?
f
it's the ID of a created resource
g
Curious why that's an Input, but which libs are you using?
f
Copy code
nodeId := instance.ID().ToStringOutput().ApplyT(func(id string) string {
    return id
}).(pulumi.StringOutput)
this is the GCP provider
g
So there you are casting it to a pulumi.StringOutput.
f
right, as it is needed by some other functions as a string output, then another function takes it as a
StringInput
and inside there I need to convert it to a regular string
f
@flaky-school-70251 Do you want fmt.Printf to print it to stdout when the program runs or fmt.Sprintf to add something to the variable and use somewhere else?
g
@flaky-school-70251 Sounds weird as an Input should be able to take an Output just fine.
f
right, but if I use
fmt.Printf
or
fmt.Sprintf
with a
pulumi.Output
I get output like
Copy code
{%!s(*pulumi.OutputState=&{{0 0} 0xc000513a40 0xc0005ce258 0 <nil> <nil> false false 0x11e1ee0 [0xc0005ce500]})}
g
There are ways to print not using the fmt lib that take pulumi outputs.
f
There's
pulumi.Sprintf
! It takes inputs and you get an output.
g
ctx.Export, ctx.Log as examples.
Also what @fierce-ability-58936 if you need to add to the Output.
f
pulumi.Sprintf
also creates a
pulumi.StringOutput
not a
string
Copy code
cannot use pulumi.Sprintf("%s", NodeId) (value of type pulumi.StringOutput) as type string in argument
f
And where do you use that output, sorry? As an input for another resource?
g
Copy code
subnet.CidrBlock.ApplyT(func(cidr string){
       fmt.Printf("%s", cidr)
		})
That works for me.
CidrBlock here is a pulumi.StringPtrOutput.
f
No, in a regular string concatenation, to then provide some output & labelling
right, but that only supports printing direct to a console, not converting to a string
this eventually gets printed to the console, but it passed down a few functions first
g
var myString string subnet.CidrBlock.ApplyT(func(cidr string){ myString = fmt.Sprintf("%s", cidr) }) fmt.Printf("%s", myString)
f
at the top of the stack, it's a
pulumi.StringOutput
, somewhere in the middle it is passed as a
pulumi.StringInput
, then it needs to become a
string
for another step to then be printed out to the console in the last steap
it's a horrid mess
Oh, I'll try that
yey!! a panic!!
Copy code
panic: applier must return exactly one or two values
    goroutine 1 [running]:
I guess the func passed to
ApplyT
should return something??
g
Send it to an anonoymous var
Copy code
var myString string
 _ := subnet.CidrBlock.ApplyT(func(cidr string){
       myString = fmt.Sprintf("%s", cidr)
    })
fmt.Printf("%s", myString)
Or yeah, return error then return nil.
f
aaarrrgggghhh
OK, no failures, but because it's an
ApplyT
I can't test it with a
pulumi preview
but I think this is way closer to what I need, so a million thanks for that!!
g
Correct, you can however, test it with a unit test.
f
OK, so I'm just playing now with
pulumi up
and my code is
Copy code
var strNodeId string
	nodeId.ToStringOutput().ApplyT(func (nodeId string) error {
		fmt.Printf("Node ID is: %s\n", nodeId)
		strNodeId = fmt.Sprintf("%s", nodeId)
		return nil
	})
	fmt.Printf("and strNodeId is: %s\n", strNodeId)
but the output is
Copy code
and strNodeId is:
    and strNodeId is:
    and strNodeId is:
    Node ID is: 062653f3-4369-466a-83a3-c48537c81449
    Node ID is: 88f93e13-75ba-470a-b51e-00ba8ebe1bd8
    Node ID is: 6e390b51-d7ba-447b-a0c3-7c6ed22d0993
so
strNodeId
is not being populated properly when passed down the stack
g
I still think that ctx.Export is what you really want.
f
Because it can't be. When the time the program runs, the value hasn't been resolved yet (possibly). That's why you get strNodeId prints first - it runs before
fmt.Printf("Node ID is: %s\n", nodeId)
You can use ctx.Export("var", strNodeId - when previewing, it'll have unresolved value, which will resolve after
up
is finished.
f
then how do I retrieve that later on in the stack?
f
You just use it as is if it'll be passed to another resource as an input.
g
You don't what is the purpose of grabbing the String? Just to append it to some log output for your end user?
f
pretty much
g
Ctx.Export does all that for you.
Without any extra code.
f
it's also used as a partial key for a resource
g
For the partial key part just wrap that with the applier.
f
that's what I'm doing now, but it is giving duplicate URN errors, since the
strNodeId
is empty when creating the other resource
g
For logging to the end user. Do this
Copy code
output := pulumi.Sprintf("%s: %s", nodeId, <some other details I want to display>)
ctx.Export("My NodeID and Other Stuff:", output)
f
Ah, you can't use an Output as a resource name!
f
😭
I need something unique from each instance resource for a later config resource
g
pulumi does the unique-ness for you right?
f
If you're creating resources in a loop, you can use the index key - name it 1,2,3.
f
???
f
Better to show the full code.
So long as you don't set the Name property on resources, pulumi will append a random suffix hash for you.
Otherwise, do what @fierce-ability-58936 said.
f
I get the problem now @flaky-school-70251 . It depends on your exact use case what you want to do. There must be a loop there somewhere in your code - what do you iterate over?
f
yup
I tried it with just passing the index of the loop down
looks like it's the quickest solution
thanks so much for all your help @fierce-ability-58936 and @gifted-fall-44000
930 Views