Task 1: Get it running.

This commit is contained in:
bob
2024-10-08 20:14:29 -05:00
parent 7cb924ffb9
commit e420afd916
6 changed files with 124 additions and 21 deletions

View File

@@ -3,7 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35312.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cloudless", "Cloudless\Cloudless.csproj", "{F7145F2A-D7D2-44F4-AF02-E2570CB6034B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cloudless", "src\Cloudless.csproj", "{F7145F2A-D7D2-44F4-AF02-E2570CB6034B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Azure", "Azure", "{522CF945-C939-4CB0-8054-BC17BF8DE87C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ResourceGroupResources", "ResourceGroupResources", "{04F7AF6E-2D4B-4087-A687-E02C40D863CD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -19,6 +23,9 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{04F7AF6E-2D4B-4087-A687-E02C40D863CD} = {522CF945-C939-4CB0-8054-BC17BF8DE87C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3AC51662-8311-4D51-8632-F352DCD8AF91}
EndGlobalSection

View File

@@ -1,10 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -1,10 +0,0 @@
namespace Cloudless
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}

16
src/Cloudless.csproj Normal file
View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.12.1" />
<PackageReference Include="Azure.ResourceManager" Version="1.13.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
</ItemGroup>
</Project>

71
src/Conductor.cs Normal file
View File

@@ -0,0 +1,71 @@
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Resources.Models;
using Microsoft.Extensions.Logging;
namespace Cloudless
{
public class Conductor(ILogger<Conductor> _logger)
{
public async Task Run()
{
var armClient = new ArmClient(new DefaultAzureCredential());
SubscriptionResource sub;
try
{
sub = armClient.GetDefaultSubscription();
_logger.LogInformation("Sub name: {subname}", sub.Data.DisplayName);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to retrieve default subscription");
throw;
}
await CreateRG(sub);
}
public async Task CreateRG(SubscriptionResource sub)
{
var rgName = "robby02-rg";
var rgData = new ResourceGroupData(AzureLocation.CentralUS)
{
};
ArmOperation<ResourceGroupResource> operation;
try
{
operation = await sub.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, rgData);
}
catch
{
_logger.LogError("Failed to create or update RG {rgName}", rgName);
throw;
}
var rg = operation.Value;
_logger.LogInformation("RG Name: {name}", rg.Data.Name);
await UpdateResourceGroupTags(rg);
}
public async Task UpdateResourceGroupTags(ResourceGroupResource rg)
{
var x = new KeyValuePair<string, string>("CreatedBy", "Cloudless");
var rgPatch = new ResourceGroupPatch();
rgPatch.Tags.Add(x);
try
{
await rg.UpdateAsync(rgPatch);
_logger.LogInformation("Added tags with key {key}, value {val}", x.Key, x.Value);
}
catch
{
_logger.LogError("Failed to add tags");
}
}
}
}

29
src/Program.cs Normal file
View File

@@ -0,0 +1,29 @@
using Azure.Identity;
using Azure.ResourceManager;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Cloudless
{
public class Program
{
static async Task Main(string[] args)
{
var con = Host
.CreateDefaultBuilder(args)
.ConfigureServices(ConfigureServices)
.ConfigureAppConfiguration(conf => conf.AddEnvironmentVariables())
.Build()
.Services
.GetRequiredService<Conductor>();
await con.Run();
}
private static void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
services.AddSingleton<Conductor>();
}
}
}