Skip to content

Instantly share code, notes, and snippets.

@clientbala
Created May 8, 2023 18:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clientbala/93f9b12c076e3b90b86636013b9c5bd0 to your computer and use it in GitHub Desktop.
Save clientbala/93f9b12c076e3b90b86636013b9c5bd0 to your computer and use it in GitHub Desktop.
PulumiOverHttpContainer
using Microsoft.AspNetCore.Mvc;
using Pulumi.Automation;
using System.Reflection;
using Resources = Pulumi.AzureNative.Resources;
using Storage = Pulumi.AzureNative.Storage;
namespace PulumiOverHttpContainer.Controllers
{
public class CInlineProgramArgs : InlineProgramArgs
{
internal static ProjectSettings Default(string name) =>
new ProjectSettings(name, new ProjectRuntime(ProjectRuntimeName.Dotnet))
{
Main = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.Parent.FullName
};
public CInlineProgramArgs(
IWebHostEnvironment env,
string projectName,
string stackName,
PulumiFn program) : base(projectName, stackName, program)
{
this.WorkDir = env.WebRootPath;
this.ProjectSettings = CInlineProgramArgs.Default(projectName);
}
}
[ApiController]
[Route("[controller]")]
public class CreateStorageController : ControllerBase
{
private readonly ILogger<CreateStorageController> _logger;
[Obsolete]
private readonly IWebHostEnvironment _env;
public CreateStorageController(ILogger<CreateStorageController> logger, IWebHostEnvironment env)
{
_logger = logger;
_env = env;
}
[HttpGet]
public async Task<IActionResult> Get(string stackName, string resourceGroupName, string storageAccountName)
{
var program = PulumiFn.Create(() =>
{
var resourceGroup = new Resources.ResourceGroup(resourceGroupName, new Resources.ResourceGroupArgs
{
Tags = { { "Environment", "production" } }
});
var storageAccount = new Storage.StorageAccount(storageAccountName, new Storage.StorageAccountArgs
{
Kind = Storage.Kind.StorageV2,
ResourceGroupName = resourceGroup.Name,
Sku = new Storage.Inputs.SkuArgs
{
Name = Storage.SkuName.Standard_LRS,
},
});
});
var stackArgs = new CInlineProgramArgs(_env, "PulumiOverHttpContainer", stackName, program);
var stack = await LocalWorkspace.CreateOrSelectStackAsync(stackArgs);
Console.WriteLine("successfully initialized stack");
// for inline programs, we must manage plugins ourselves
Console.WriteLine("installing plugins...");
await stack.Workspace.InstallPluginAsync("azure-native", "v1.102.0");
Console.WriteLine("plugins installed");
Console.WriteLine("setting up config...");
await stack.SetConfigAsync("azure-native:clientId", new ConfigValue("<<client_id>>"));
await stack.SetConfigAsync("azure-native:clientSecret", new ConfigValue("client_scret", true));
await stack.SetConfigAsync("azure-native:tenantId", new ConfigValue("<<tenant_id>>"));
await stack.SetConfigAsync("azure-native:subscriptionId", new ConfigValue("<<subscription_id>>"));
await stack.SetConfigAsync("azure-native:location", new ConfigValue("<<region>>"));
Console.WriteLine("config set");
Console.WriteLine("refreshing stack...");
await stack.RefreshAsync(new RefreshOptions { OnStandardOutput = Console.WriteLine });
Console.WriteLine("refresh complete");
Console.WriteLine("updating stack...");
var result = await stack.UpAsync(new UpOptions { OnStandardOutput = Console.WriteLine });
if (result.Summary.ResourceChanges != null)
{
Console.WriteLine("update summary:");
foreach (var change in result.Summary.ResourceChanges)
Console.WriteLine($" {change.Key}: {change.Value}");
}
return new OkObjectResult("Success");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment