https://pulumi.com logo
#general
Title
# general
e

early-kite-86569

09/15/2021, 7:34 PM
Been looking around in the docs but truly do not see anything about it. Anyone know how to use a created resource in a getter function? i.e. My example / what I am trying to do is get the
default_bucket
(from https://www.pulumi.com/docs/reference/pkg/gcp/appengine/application/) full details with
get_bucket
(https://www.pulumi.com/docs/reference/pkg/google-native/storage/v1/getbucket/)
Copy code
# Get created bucket
self.full_bucket_details = storage.get_bucket(
    self.firestore_app.default_bucket,
    opts=pulumi.InvokeOptions(parent=self.firestore_app),
)
Does not work because it tries to get the bucket before the resource is created:
Exception: invoke of google-nativestorage/v1getBucket failed: invocation of google-nativestorage/v1getBucket returned an error: error sending request: googleapi: Error 400: Invalid bucket name: '{bucket}', invalid
b

bored-oyster-3147

09/15/2021, 7:41 PM
are you trying to get a bucket that is being provisioned by the same stack?
e

early-kite-86569

09/15/2021, 7:42 PM
Yes. Not possible?
b

bored-oyster-3147

09/15/2021, 7:42 PM
keep in mind that pulumi is declarative. It is going to execute all of your pulumi code first before provisioning resources. When you want to get an existing resource it assumes that that resource exists outside of the currently running stack operation. I think what you might instead want is this function: https://www.pulumi.com/docs/reference/pkg/gcp/storage/bucket/#look-up
where the provided
name
is the name of a bucket resource that was provisioned in the same stack
e

early-kite-86569

09/15/2021, 7:43 PM
Ahhhhh thanks will give it a try
b

bored-oyster-3147

09/15/2021, 7:46 PM
That might still not be right though because it looks like
application
is creating a bucket so that bucket isn't being directly managed by pulumi? When you call the
get_bucket
function that you linked you can pass in
InvokeOptions
which I believes takes in a
dependsOn
property? You could pass your
application
resource to the
dependsOn
and that might tell the API call to wait until the application is deployed before fetching the bucket
e

early-kite-86569

09/15/2021, 7:47 PM
unfortunately,
depends_on
isn't an argument to
InvokeOptions
b

bored-oyster-3147

09/15/2021, 8:02 PM
ah yea I might've been thinking of
parent
. Honestly though the output dependency tree should take care of this flow for you? for instance something like:
Copy code
self.full_bucket_details = self.firestone_app.default_bucket.apply(bucketName => {
    return storage.get_bucket(
        bucketName,
        opts=pulumi.InvokeOptions(parent=self.firestore_app));
});
e

early-kite-86569

09/15/2021, 8:16 PM
That worked! Thanks for the help
I guess I should have prefaced this all with "do you think its possible to use that get bucket result to update certain things on the bucket"? what I am ultimately trying to do is update the CORS policy on the bucket and I know that CORS is a param on normal buckets so I thought maybe if I got the bucket then created a new resource with the same params?? https://www.pulumi.com/docs/reference/pkg/google-native/storage/v1/bucket/
b

bored-oyster-3147

09/15/2021, 8:30 PM
That won't work. That would be you declaring to provision a duplicate bucket and you would error. So this pattern would only work with "attachment" type resources, such as policy resources or similar. Can you use
DefaultObjectAccessControl
or
BucketIamPolicy
or
BucketAccessControl
to accomplish this? Those each are resource that attach to an existing bucket.
If this is input that can only be provided to the root
Bucket
resource and not as a resource that can be attached to the root
Bucket
, than you may need to figure out a way to have your stack manage that bucket to begin with. So if that is the case you will want to see if there is a way to provide the
Application
resource an existing bucket
e

early-kite-86569

09/15/2021, 8:32 PM
Yep. I figured as much just wanted to give it a try.
I do need to set ACLs regardless so will see if I can update CORS using an ACL
4 Views