sparse-intern-71089
09/04/2023, 7:44 AMmelodic-tomato-39005
09/04/2023, 8:48 AMApply is the correct approach, but the Printf needs to be inside the callback function. See the example and explanation here.jolly-activity-81321
09/04/2023, 9:19 AMjolly-activity-81321
09/04/2023, 9:19 AMAll, same thing, basicallyjolly-activity-81321
09/04/2023, 9:20 AMstringValue := pulumi.String("Hello, World!").ToStringOutput()
fmt.Println(pulumi.All(stringValue).ApplyT(func(args []interface{}) (string, error) {
s := args[0]
return fmt.Sprintf("%v", s), nil
}).(pulumi.StringOutput))
//Outputs {0xc00047a460}jolly-activity-81321
09/04/2023, 9:20 AM{0xc00047a460} where I expected "Hello, World!"melodic-tomato-39005
09/04/2023, 9:28 AMApply returns again an Output.melodic-tomato-39005
09/04/2023, 9:29 AMAll is unnecessarily cumbersome if you have only one value, ApplyT is easier.jolly-activity-81321
09/04/2023, 9:29 AMAll 🙂jolly-activity-81321
09/04/2023, 9:29 AMmelodic-tomato-39005
09/04/2023, 9:30 AMHi Martins,?is the correct approachApply
jolly-activity-81321
09/04/2023, 9:35 AMjolly-activity-81321
09/04/2023, 9:35 AMstringValue := pulumi.String("Hello, World!").ToStringOutput()
original := stringValue.ApplyT(func(s string) (string, error) {
return fmt.Sprintf("%v", s), nil
})
fmt.Printf("the parsed string is: %s", original)jolly-activity-81321
09/04/2023, 9:36 AMmelodic-tomato-39005
09/04/2023, 9:46 AMApply, it’s how Pulumi’s whole model of tracking dependencies works.melodic-tomato-39005
09/04/2023, 9:47 AMExport for when you want to use a value outside of your Pulumi program, or in another onemelodic-tomato-39005
09/04/2023, 9:48 AMjolly-activity-81321
09/04/2023, 1:36 PMjolly-activity-81321
09/04/2023, 1:37 PMs := pulumi.Sprintf("%s", stringValue)
would work and give me a meaningful value... it does not...jolly-activity-81321
09/04/2023, 1:43 PMExport is for exporting the value for use in a different stack, isn't it?jolly-activity-81321
09/04/2023, 1:43 PMmelodic-tomato-39005
09/04/2023, 1:46 PMpulumi.Sprintf is a helper wrapping pulumi.Apply . I think your examples above used fmt.Sprintf, which is different. But yes, pulumi.Sprintf with an argument of type pulumi.StringOutput should work.jolly-activity-81321
09/04/2023, 1:47 PMstringValue := pulumi.String("Hello, World!").ToStringOutput()
s := pulumi.Sprintf("%s", stringValue)
fmt.Println(s)jolly-activity-81321
09/04/2023, 1:48 PM{0xc0001a82a0}jolly-activity-81321
09/04/2023, 1:48 PMmelodic-tomato-39005
09/04/2023, 1:50 PMs is again a StringOutput, not a string. Basically, the raw value wrapped by an Output cannot escape Apply and friends. Your logic working with the raw values needs to live in the callbacks to `Apply`/`All`/etc.melodic-tomato-39005
09/04/2023, 1:50 PMIf you need to access an output’s plain value—for example, to compute a derived, new value, or because you want to log it—you have these options:
• Apply: a callback that receives the plain value, and computes a new output
• Lifting: directly read properties off an output value
• Interpolation: concatenate string outputs with other strings directly
jolly-activity-81321
09/04/2023, 2:33 PMprintf.go from Pulumi's PoW?jolly-activity-81321
09/04/2023, 2:34 PMjolly-activity-81321
09/04/2023, 2:34 PMfunc main() {
stringValue := pulumi.String("Hello, World!").ToStringOutput()
s := pulumi.Sprintf("%s", stringValue)
fmt.Println(s)
}melodic-tomato-39005
09/04/2023, 2:37 PMs is a StringOutput, not a string, so you can’t print it. The print needs to happen inside an Apply, i.e., in the `Apply`’s callback. Like in the example in the docs:
myPet.ID().ApplyT(func(id string) error {
fmt.Printf("Hello, %s!", id)
return nil
})melodic-tomato-39005
09/04/2023, 2:38 PMjolly-activity-81321
09/04/2023, 2:42 PMjolly-activity-81321
09/04/2023, 2:42 PMmelodic-tomato-39005
09/04/2023, 2:44 PMjolly-activity-81321
09/04/2023, 3:06 PMjolly-activity-81321
09/04/2023, 3:07 PMctx.Export("publicIp", instance.PublicIp) to export the value. I am provisioning EC2 instances in this case.
2. And then you can access it after running `Up`:
upRes, err := stack.Up(ctx, stdoutStreamer)
if err != nil {
log.Fatal(err)
}
master.publicIp = fmt.Sprintf("%s", upRes.Outputs["publicIp"].Value)jolly-activity-81321
09/04/2023, 3:08 PMmelodic-tomato-39005
09/04/2023, 7:28 PMApply /`All` callback functions.