Hey! What is the correct way to get a stack refere...
# automation-api
w
Hey! What is the correct way to get a stack reference in async task in C#?
Copy code
class Program
    {
        static async Task<int> Main(string[] args)
        {
            var stack = new Pulumi.StackReference("path to stack");
I get error
Copy code
Unhandled exception. System.InvalidOperationException: Trying to acquire Deployment.Instance before 'Run' was called.
   at Pulumi.Deployment.get_Instance()
   at Pulumi.Deployment.get_InternalInstance()
   at Pulumi.Resource..ctor(String type, String name, Boolean custom, ResourceArgs args, ResourceOptions options, Boolean remote, Boolean dependency)        
   at Pulumi.CustomResource..ctor(String type, String name, ResourceArgs args, CustomResourceOptions options, Boolean dependency)
   at Pulumi.CustomResource..ctor(String type, String name, ResourceArgs args, CustomResourceOptions options)
   at Pulumi.StackReference..ctor(String name, StackReferenceArgs args, CustomResourceOptions options)
   at Program.Main(String[] args) in C:\...\Program.cs:line 34
   at Program.<Main>(String[] args)
b
All your code needs to be in the class that inherits from
Stack
You don't use
StackReference
to get stack information, you use it to access outputs from one stack in another: https://www.pulumi.com/docs/intro/concepts/stack/#stackreferences
w
The use case that I'm trying to implement is to use
getVirtualNetwork
method and get existing subnets to calculate next cidr block and then use that cidr block as input for another Stack. If stack references are not designed to be used like that is there the right way for this case?
b
No, that sounds find. So in the first stack you would have the vnet id as the output and then the other stacks would use that and the
getVirtualNetwork
method and to get the address blocks
w
so, it's not possible to do it as a part of automation? the steps i was envisioning were: 1. automation script will get created subnets from Stack output's virtual network 2. script will figure out next available cidr block and put that as an input for other Stack I wanted to use
getVirtualNetwork
as part of the automation script but couldn't figure this out. I hope it's clearer what I'm trying to achieve here
r
Correct, you cannot use getVirtualNetwork in the automation script. You can only use that function within the pulumi program itself. However you can use the relevant SDK directly to get that information within the script if you’d rather.
w
got it, thanks!