Hi, any idea how I get a `string` from `Output`? I...
# golang
b
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
ye, docs are written using strong developer language at some places, ops folk with no proper dev training might have hard time understanding
b
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
sorry, just was expressing my own frustrations, nothing personal âš¡
l
@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
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
Which
organizations
is this? AWS? Do you have a pointer to the docs for me on this?
l
@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
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
b
Hmm, they don't seem to catch errors
l
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
@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
That makes sense, thanks @limited-rainbow-51650 & @billowy-army-68599!