I’m not sure I understand what ComponentResource’s...
# getting-started
i
I’m not sure I understand what ComponentResource’s offer that
{ parent: xyz }
doesn’t already provide?
In the docs, this is what is described:
Why would you make a component resource instead of just a “simple” logical grouping that you can call and use like a generic library full of classes and functions? A component resource shows up in the Pulumi ecosystem just like any other resource. That means it has a trackable state, appears in diffs, and has a name field you can reference that refers to the entire grouping.
However, all three things don’t seem all that meaningful: 1. trackable state - in this thread on github, a pulumi contributor says “but state for component resources is basically meaningless anyway” 2. appears in diffs - if state on a component resource is meaningless, then so is any diff 3. a name field you can use to reference the entire grouping - you can also do this if you have a root component in the hierarchy you create inside a function via
parent
b
So that github issue is coming up to 4 years old, and lots has changed since then. The idea of a component resource is that yes, you can group these resources together, but then you can share that component as part of an SDK/library with other users (think TF modules) and can even generate the SDKs in other languages should you choose. Parent simply groups the resources in the UI
l
Parent also allows inheritance through opts.providers
i
That’s true, and a great feature for doing multi-regional deployments. Especially since before I started using it, I had forgot to add the
provider
opt to some of the child resources now if only there were also a way to avoid having to put
{ parent: this }
on every component in the constructor
l
That's a problem that can be solved by your preferred programming language 🙂 I have a private property or method in every component resource class that sorts this for me.
i
do you mean there’s a way in typescript to avoid passing a third parameter? I’m all 👂 ’s! I do keep forgetting to add
{ parent: this }
in the constructor 🙁
I’m trying to implement this but struggling with the typescript errors to be used like
this.addResource('role', aws.iam.Role, { … })
Copy code
private addResource<T extends pulumi.Resource>(
    name: string,
    Resource: T,
    args: ConstructorParameters<T>[1]
  ) {
    return new Resource(`${this.name}-${name}`, args, { parent: this })
  }
got it kind of working but
Resource
is very loosely typed, could probably fix it if I new how to use
infer
correctly
Copy code
private addResource<T extends new (...args: any) => any>(
    name: string,
    Resource: T,
    args: ConstructorParameters<T>[1]
  ) {
    return new Resource(`${this.name}-${name}`, args, { parent: this })
  }
l
You might want to add a long time to manage the 3rd parameter? Using a factory method like this is good, though you will want to be able to ignore it occasionally since
this
won't always be the parent. We just have it as a coding convention that's on our code review checklist for Pulumi code.