https://pulumi.com logo
#golang
Title
b

bumpy-bear-61087

10/27/2021, 8:48 AM
Hi, any idea how I get a
string
from
Output
? I'd like to get the string from
containerDefaultSA.Email
, but I don't get the docs at https://www.pulumi.com/docs/intro/concepts/inputs-outputs/
Copy code
containerDefaultSA, _ := projects.NewServiceIdentity(p.ctx, "hcSa", &projects.ServiceIdentityArgs{
    Project: pulumi.String(p.projectID),
    Service: pulumi.String("<http://container.googleapis.com|container.googleapis.com>"),
  })

  kmsIAM, err := organizations.LookupIAMPolicy(p.ctx, &organizations.LookupIAMPolicyArgs{
    Bindings: []organizations.GetIAMPolicyBinding{{
        Role: "roles/cloudkms.cryptoKeyEncrypterDecrypter",
        Members: []string{
          pulumi.Sprintf("serviceAccount:%s", containerDefaultSA.Email),
        }},
    },
  }, nil)
b

busy-insurance-52853

10/27/2021, 12:58 PM
ye, docs are written using strong developer language at some places, ops folk with no proper dev training might have hard time understanding
b

bumpy-bear-61087

10/27/2021, 1:15 PM
Hmm, what's the stack reference about? To be fair, I dev in Go often but the documentation section there is a bit confusing.
FYI I need to get a
string
from an
StringOutput
, not the other way around.
🤔 1
b

busy-insurance-52853

10/27/2021, 2:13 PM
sorry, just was expressing my own frustrations, nothing personal
l

limited-rainbow-51650

10/27/2021, 2:23 PM
@bumpy-bear-61087 where exactly do you need a
string
? Out of experience (mainly in Typescript), you have to change how you look at the problem and write your code differently. Have a look at the
Apply
function here: https://www.pulumi.com/docs/intro/concepts/inputs-outputs/#apply In the example, click on the
Go
language selector just above the source code.
b

bumpy-bear-61087

10/27/2021, 2:24 PM
Hey @limited-rainbow-51650! In this case:
Copy code
kmsIAM, err := organizations.LookupIAMPolicy(ctx, &organizations.LookupIAMPolicyArgs{
    Bindings: []organizations.GetIAMPolicyBinding{{
        Role: "roles/cloudkms.cryptoKeyEncrypterDecrypter",
        Members: []string{
          fmt.Sprintf("serviceAccount:%s", containerDefaultSA.Email),
        }},
    },
  }, nil)
So it's basically a text operation I need to do on an attribute of another object.
l

limited-rainbow-51650

10/27/2021, 2:27 PM
Which
organizations
is this? AWS? Do you have a pointer to the docs for me on this?
l

limited-rainbow-51650

10/27/2021, 2:51 PM
@bumpy-bear-61087 not used to coding in Golang for Pulumi, but as mentioned, you should use the
Apply
function. Here is my best attempt on rewriting your snippet using `Apply`:
Copy code
containerDefaultSA.Email.ApplyT(func(email string) (*return type, err) {
  return organizations.LookupIAMPolicy(ctx, &organizations.LookupIAMPolicyArgs{
    Bindings: []organizations.GetIAMPolicyBinding{{
        Role: "roles/cloudkms.cryptoKeyEncrypterDecrypter",
        Members: []string{
          fmt.Sprintf("serviceAccount:%s", email),
        }},
    },
  }, nil)
  return kmsIAM
}).(cast to pulumi.Output of correct type)
First line is your
pulumi.StringOutput
where you apply a function on the raw value (
email
) and the return value is again wrapped in an output. You should cast to the right type on the last line, but that I don't know how to write in Golang.
If you know anything about NodeJS and promises, think of an
Output
that way: a bucket which can contain a value at a certain point in time and where you apply a transformation function on. The transformed value (or struct) is again contained in another bucket (
Output
).
b

bumpy-bear-61087

10/27/2021, 2:59 PM
I was thinking this but you can't return both
error
and something else in a cast. 🤔
Copy code
containerDefaultSA.Email.ApplyT(func(email string) (*organizations.LookupIAMPolicyResult, error){
    kmsIAM, err = organizations.LookupIAMPolicy(p.ctx, &organizations.LookupIAMPolicyArgs{
      Bindings: []organizations.GetIAMPolicyBinding{{
        Role: "roles/cloudkms.cryptoKeyEncrypterDecrypter",
        Members: []string{
          fmt.Sprintf("serviceAccount:%s", email),
        }},
      },
    }, nil); if err != nil {
      return nil, errors.Wrap(err, "could not get IAM policy")
    }
    return kmsIAM, nil
  }).(*organizations.LookupIAMPolicyResult, error)
l

limited-rainbow-51650

10/27/2021, 3:01 PM
b

bumpy-bear-61087

10/27/2021, 3:02 PM
Hmm, they don't seem to catch errors
l

limited-rainbow-51650

10/27/2021, 3:02 PM
While I know a thing or two of Go, I don't have any experience of it with Pulumi. So here ends the help I can offer unfortunately.
b

billowy-army-68599

10/27/2021, 4:07 PM
@bumpy-bear-61087 have a read of this: https://www.leebriggs.co.uk/blog/2021/05/09/pulumi-apply.html hopefully it'll make a little more sense
b

bumpy-bear-61087

10/27/2021, 9:14 PM
That makes sense, thanks @limited-rainbow-51650 & @billowy-army-68599!
6 Views