On Google Cloud, resources need to be created in a...
# typescript
l
On Google Cloud, resources need to be created in a Google Cloud Project. I have a custom component around some Google Cloud networking. I would very much like to pass a full object rather than a primitive to my abstraction. What do I mean with this? Well, I do not want to pass the project id as a
string
. I would like to pass a full object. The problem I have is this: if the project is created in the same stack, I have a
gcp.organizations.Project
instance, with
Output
properties. If the project was already created in another stack, I look it up via
getProject
and I am returned a
Promise<GetProjectResult>
. The properties of a
GetProjectResult
instance are the primitive types (not
Output
). So I can’t substitute one for the other just because the resource is created in another stack. Any way around this?
g
Wrapping both in an output should give you the same effect for both, to get the projectId you would do
project.apply(p => p.projectId)
One will unwrap the promise to an output of the object with primitives and the other will traverse the object to have all the outputs as primitives during the apply
l
But in Typescript, what is the common “type” here I can set in my component constructor? I hope not
any
.
constructor(project: <which type here?>)
g
I'm not sure if what those types resolve actually intersect but going for the safe route you could do
Input<Project|GetProjectResult>
and then do
project = output(project)
before using it on your component
👏🏼 1
l
Your
|
character made me think about having
or
-types to indicate one or the other:
constructor(project: gcp.organizations.Project | Promise<gcp.organizations.GetProjectResult>)
My editor is not complaining… will see how far I can get with this.
g
I would still prefer using
Input
wrapping them on the argument list to allow the values to be passed both directly, as promise or as output
l
Why do you prefer it that way? Because it is clearer referring to an
Input
or is there also another technical thing I’m missing here?
g
Input<Project|GetProjectResult>
is the same as
Project | GetProjectResult | Promise<Project | GetProjectResult> | Output<Project | GetProjectResult>
So it will accept resources created inside an
apply
, in a promise chain or directly
💯 1
Copy code
getSomeConfigsExternally().then(config => new Project(projectId: config.projectId))