Hello, stupid question time. We have abstracted a...
# dotnet
r
Hello, stupid question time. We have abstracted away the creation of resources into modules which can be re-used in other projects. How can I get access to the stack outputs within these modules without passing them into the method. I wanted to keep this as clean as possible. I tried using stack references to the current instance stack name but the Pulumi threw a duplicate resource urn error
b
when you say “module” are you using Component?
r
No when I say modules I just mean a bunch of classes that abstract away common things for each resource e.g. correct naming standard and security settings etc.
b
You extend the pulumi Component resource class you can just make the outputs a part of it.
It’s not super clear how you’ve structured things, if you have any code we can review it might help
A duplicate resource urn error is going to always happen if you create two resources with the same name
r
but I haven't that was what confused me. I was just creating a StackReference to the current stack. I was surprised I needed to do that as I thought I could just get a static instance off the Pulumi.Deployment.Instance
Is there a c# example anywhere?
yes that’s what I’m referring to
r
I did look into that before but it seemed more like where you want to group the creation of multiple resources into a single operation.
b
yeah it may not be what you need based on what you’re saying, I assumed with the usage of modules that might be it
r
They are literally just there to apply consistency to naming and settings across projects and also make the Stack code more readable i.e. not 500 lines long
so my stack code is more or less like this:
var asp = appServicePlanModule.CreateResource();
Which is fine for a simple resource like an App Service Plan. But then for a function app it needs the ASP, AppInsights, StorageAccount etc.
b
so you have an example of what one looks like?
r
Copy code
public Component CreateResource(CustomResource? parent = null, CustomResource? associatedResource = null, CustomResource? dependsOn = null, string index = "01")
        {
            var ai = new Component($"ai-{index}", new ComponentArgs
            {
                ApplicationType = ApplicationType.Web,
                ResourceGroupName = _configOptions.ResourceGroupName,
                Location = _configOptions.Location,
                ResourceName = $"{_configOptions.ResourcePrefix}-ai-{_configOptions.EnvironmentIdentifier.ToLower()}-{_configOptions.ProjectIdentifier.ToLower()}-{index}",
                Kind = "web"
            });

            return ai;
        }
That's the simplest one
I'm going to re-factor it anyway as I had everything working off the same interface to try and ensure consistency and for mocking but I don't think its really helping
b
Copy code
Component($"ai-{index}"
This is going to create the same resource name every time it’s invokved, you need to have something in there that invites randomness like a
name
property you can pass
r
that's the point of the index
b
I’m not sure familiar with csharp, but if you’re setting
index = "01"
and then invoking like so
appServicePlanModule.CreateResource()
isn’t that setting the index as 1 every time?
r
no that's just a default value being set
so if I wanted to create two app service plans I would explicitly set the index to something else
I might refactor this so it has to be explicit rather than defaulted
b
I do think a Component is a better way to handle these defaults, you can create a coomponent with a single resource in it that just sets some sane defaults
r
yeah I think I'll give it another look through
thanks for your time