https://pulumi.com logo
#dotnet
Title
# dotnet
v

victorious-ability-58329

07/05/2023, 4:50 PM
Hello, I am building a program that creates an Azure virtual machine. I have separated all the components into the following files. program.cs rg.cs vm.cs vmext.cs vmstorage.cs vmnetwork.cs program.cs constants.cs I am using the config file for all of the resource arguments such as resource group name, vm name, tags, region, admin username and password. So far I have been able to create the resource group with the program. Below is the current code in my program.cs
Copy code
using Pulumi;
using AzureNative = Pulumi.AzureNative;
using Random = Pulumi.Random;
using System.Collections.Generic;
using System;
using System.Text;
using System.Threading.Tasks;
using Pulumi.AzureNative.Resources;
using System.Text.Json;

namespace iac.AzureStack
{
    public class Program
    {
        // Import the program's configuration settings.
        static Pulumi.Config config = new Pulumi.Config();

        static async Task<int> Main(string[] args)
        {
            return await Pulumi.Deployment.RunAsync(() =>
            {
                var tags = config.RequireObject<JsonElement>("resourceGroupTags");
                var rgArgs = new AzureResourceGroupArgs
                {
                    ResourceGroupName = config.Require(StackConstants.ResourceGroupConstants.Name),
                    ResourceGroupTags = config.RequireObject<Dictionary<string, string>>(
                        "resourceGroupTags"
                    ),
                };

                // Create a resource group for all resources.
                var newResourceGroup = new AzGrusResourceGroup(
                    rgArgs.ResourceGroupName.ToString(),
                    rgArgs
                );

                return Task.FromResult(0);
            });
        }
    }
}
Is it possible not to have everything wrapped in a return await? Like creating different functions in the program.cs for each component? Here is the code in my resourcegroup.cs file
Copy code
using System;
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Pulumi.AzureNative.Resources;
using Pulumi.AzureNative.Resources.Inputs;

namespace IaC.AzureStack;
class AzureResourceGroupArgs
{
    public Input<string> ResourceGroupName { get; set; } = null!;
    public Dictionary<string, string> ResourceGroupTags { get; set; } = null!;
}

class AzResourceGroup : ComponentResource
{
    public AzResourceGroup(
        string name,
        AzureResourceGroupArgs args,
        ComponentResourceOptions? options = null
    )
        : base("custom:azure:ResourceGroup", name, options)
    {
        var newResourceGroup = new ResourceGroup(
            name,
            new ResourceGroupArgs
            {
                ResourceGroupName = args.ResourceGroupName,
                Tags = args.ResourceGroupTags,
            },
            new CustomResourceOptions { Parent = this }
        );
        this.ResourceGroupName = newResourceGroup.Name;
    }

    [Output]
    public Output<string> ResourceGroupName { get; set; }
}
2 Views