https://pulumi.com logo
Title
g

glamorous-helmet-50600

03/29/2021, 2:15 PM
Hi guys, what's the best way to have dependencies referenced within a ComponentResource? It's not clear from the documentation. Can I just pass Output parameters to my component's constructor? Basically I have a component that can either re-use a cosmosDB account or create a new one if none is provided.
b

brave-planet-10645

03/29/2021, 2:25 PM
So like built in resources, ComponentResource has a third constructor parameter which you can pass in dependencies
Give me a sec and I'll show you what I mean
g

glamorous-helmet-50600

03/29/2021, 2:27 PM
I'm a bit confused, thank you 🙂
I'm using C# btw, shouldn't make a difference but
The idea is that I want my component to either use existing resources if they are passed in or create them. The best example is probably passing the resource group name, as a potential input and if it's not there then the component creates once for itself
b

brave-planet-10645

03/29/2021, 2:33 PM
Oh THAT kind of dependency.. I thought you meant using
dependsOn
I mean, you can pass in the component, then use the SDK to check whether they exist or not and if not create them?
For example, if you wanted to do this on a VPC you could run GetVpc() and if it doesn't exist then create the resource
g

glamorous-helmet-50600

03/29/2021, 2:40 PM
I'm sorry I think the scenario I put up is confusing even to myself. I guess what I need is the dependsOn if I can access that resource. Imagine my platform has 10 component resources and I want all of them to be on the same resource group, then I'd use the dependsOn and access whatever is in there from within each component, is that fair? sorry is basic question I'm quite new to pulumi
The same way, if I pass a cosmosDB into the dependsOn then I know I don't need to create a new instance for example, I just need the ability to make the dependency optional
b

brave-planet-10645

03/29/2021, 2:46 PM
So dependsOn is there because we need a way of building the dependency graph when you're not passing outputs. Pulumi is async by default. For example if you need to ensure that a particular resource exists before we start deploying another one.
Does that make sense?
g

glamorous-helmet-50600

03/29/2021, 2:47 PM
Yes
b

brave-planet-10645

03/29/2021, 2:47 PM
And is it what you're looking for?
g

glamorous-helmet-50600

03/29/2021, 2:50 PM
Possibly, if I have a way of accessing that within my component if that makes sense.
var resourceGroup = new AzureNextGen.Resources.Latest.ResourceGroup("rg-test-dev", new AzureNextGen.Resources.Latest.ResourceGroupArgs {
            Location = location,
            ResourceGroupName = "rg-test-dev"
        });

        var myComponent = new MyComponent("my-component", new ComponentResourceOptions {
            DependsOn = new InputList<Resource> {
                resourceGroup
            }
        });
So now within MyComponent class, would it be a matter of iterating the inputList and checking for a ResourceGroup type of entry so that I can apply some logic and decide if I need to create one (if none was passed)?
This wouldn't happen within the same stack, but different stacks may have a different infrastructure layout if that makes sense
b

brave-planet-10645

03/29/2021, 2:54 PM
So the ComponentResourceOptions are mainly for Pulumi resources. What you could do is have another parameter called
ResourceGroupName
(for example) and then in myComponent you could run the GetResourceGroup method on the list of resourcegroup names... and you could just do this as a
List<string>
rather than using our
InputList<T>
class
g

glamorous-helmet-50600

03/29/2021, 2:57 PM
Right I see, thanks for the that! I think I need to review what I'm trying to do as it feels I'm over complicating a bit xD