Last active
July 27, 2023 15:30
-
-
Save Kittoes0124/251488db4f29aba0caefda5430c195af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Azure.Core; | |
using Azure.Identity; | |
using System.Net.Http.Headers; | |
using System.Net.Http.Json; | |
using System.Text.Json; | |
using System.Text.Json.Serialization; | |
var apiMap = new Dictionary<string, (string ApiVersion, string ResourceIdFormatString)> { | |
{ "subnets", ("2022-11-01", "Microsoft.Network/virtualNetworks/{0}/subnets/{1}") }, | |
{ "virtualMachines", ("2023-03-01", "Microsoft.Compute/virtualMachines/{0}") }, | |
{ "virtualMachineScaleSets", ("2023-03-01", "Microsoft.Compute/virtualMachineScaleSets/{0}") }, | |
}; | |
var api = apiMap["virtualMachineScaleSets"]; | |
var baseUri = "https://management.azure.com/"; | |
var scope = "https://management.azure.com/.default"; | |
var resourceGroupName = "byteterrace"; | |
var subscriptionId = "f29ec19d-daae-4062-81c3-b80630a3dc2a"; | |
var exportUri = string.Format($"subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/{api.ResourceIdFormatString}?api-version={api.ApiVersion}", "byteterrace"); | |
var requestContext = new TokenRequestContext(scopes: new[] { scope, }); | |
var tokenCredential = new DefaultAzureCredential(); | |
var accessToken = tokenCredential.GetToken(requestContext: requestContext).Token; | |
var jsonSerializerOptions = new JsonSerializerOptions { | |
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | |
WriteIndented = true, | |
}; | |
using var httpClient = new HttpClient(); | |
httpClient.BaseAddress = new Uri(uriString: baseUri); | |
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(parameter: accessToken, scheme: "Bearer"); | |
var result = httpClient | |
.GetAsync(requestUri: exportUri) | |
.GetAwaiter() | |
.GetResult() | |
.Content | |
.ReadFromJsonAsync<GenericResource>(options: jsonSerializerOptions) | |
.GetAwaiter() | |
.GetResult()! | |
.As<object>(converter: resource => { | |
var properties = resource.Properties!; | |
var virtualMachineProfile = properties[key: "virtualMachineProfile"].Deserialize<IDictionary<string, JsonElement>>()!; | |
var operatingSystemProfile = virtualMachineProfile[key: "osProfile"].Deserialize<IDictionary<string, JsonElement>>()!; | |
var operatingSystemConfiguration = (operatingSystemProfile.ContainsKey(key: "linuxConfiguration") ? | |
operatingSystemProfile[key: "linuxConfiguration"] : | |
operatingSystemProfile[key: "windowsConfiguration"] | |
) | |
.Deserialize<IDictionary<string, JsonElement>>()!; | |
return new { | |
Identity = resource.Identity?.ToShim(), | |
IsGuestAgentEnabled = operatingSystemConfiguration[key: "provisionVMAgent"].GetBoolean(), | |
LicenseType = virtualMachineProfile[key: "licenseType"].GetString(), | |
Location = resource.Location, | |
Name = resource.Name, | |
}; | |
}); | |
var resultJson = JsonSerializer.Serialize(options: jsonSerializerOptions, value: result); | |
Console.WriteLine("Done."); | |
class GenericResource | |
{ | |
public string? ApiVersion { get; init; } | |
public string? Etag { get; init; } | |
public string? Id { get; init; } | |
public ResourceIdentity? Identity { get; init; } | |
public string? Location { get; init; } | |
public string? Name { get; init; } | |
public IDictionary<string, JsonElement>? Properties { get; init; } | |
public ResourceSku? Sku { get; init; } | |
public IDictionary<string, string>? Tags { get; init; } | |
public string? Type { get; init; } | |
public U As<U>(Func<GenericResource, U> converter) => converter(arg: this); | |
} | |
class ResourceId | |
{ | |
private string? m_id; | |
[JsonIgnore] | |
public string? Id { | |
get => m_id; | |
init { | |
m_id = value; | |
if (value is not null) { | |
var idParts = value.Split("/"); | |
Name = idParts[^1]; | |
ProviderName = idParts[6]; | |
ResourceGroupName = idParts[4]; | |
SubscriptionId = idParts[2]; | |
TypeName = idParts[7]; | |
} | |
} | |
} | |
public string? Name { get; init; } | |
public string? ResourceGroupName { get; init; } | |
[JsonIgnore] | |
public string? ProviderName { get; init; } | |
public string? SubscriptionId { get; init; } | |
[JsonIgnore] | |
public string? TypeName { get; init; } | |
} | |
class ResourceIdentity | |
{ | |
public string? Type { get; init; } | |
public IDictionary<string, JsonElement>? UserAssignedIdentities { get; init; } | |
public ResourceIdentityShim ToShim() => new() { | |
Type = Type, | |
UserAssignedIdentities = (UserAssignedIdentities?.Select(kvp => new ResourceId { Id = kvp.Key, }) ?? default) | |
}; | |
} | |
class ResourceIdentityShim { | |
public string? Type { get; init; } | |
public IEnumerable<ResourceId>? UserAssignedIdentities { get; init; } | |
} | |
class ResourceSku | |
{ | |
public int? Capacity { get; init; } | |
public string? Family { get; init; } | |
public string? Name { get; init; } | |
public string? Tier { get; init; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment