diff --git a/Controllers/ResourceGroupsController.cs b/Controllers/ResourceGroupsController.cs index 54fabe6..2fb87a0 100644 --- a/Controllers/ResourceGroupsController.cs +++ b/Controllers/ResourceGroupsController.cs @@ -1,15 +1,18 @@ using Microsoft.AspNetCore.Mvc; using System.Text.Json; using AzureIntegration.Services; +using Microsoft.VisualBasic; +using System.Runtime.CompilerServices; + namespace AzureIntegration.Controllers { public class ResourceGroupsController : Controller - { private readonly IHttpClientFactory _httpClientFactory; private readonly IConfiguration _configuration; + public ResourceGroupsController(IHttpClientFactory httpClientFactory, IConfiguration configuration) { _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); @@ -37,9 +40,9 @@ public async Task Index() private async Task> GetResourceGroupsAsync() { var httpClient = _httpClientFactory.CreateClient("AzureServices"); - + var subscriptionId = _configuration["SubscriptionId"]; var httpResponseMessage = await httpClient.GetAsync( - $"resourcegroups?api-version=2021-04-01"); + $"subscriptions/{subscriptionId}/resourcegroups?api-version=2021-04-01"); var jsonDocument = JsonDocument.Parse(httpResponseMessage.Content.ReadAsStringAsync().Result); @@ -67,9 +70,10 @@ private async Task> GetResourcesAsync(string name) { var httpClient = _httpClientFactory.CreateClient("AzureServices"); + var subscriptionId = _configuration["SubscriptionId"]; var httpResponseMessage = await httpClient.GetAsync( - $"resourceGroups/{name}/resources?api-version=2021-04-01"); + $"subscriptions/{subscriptionId}/resourceGroups/{name}/resources?api-version=2021-04-01"); var jsonDocument = JsonDocument.Parse(httpResponseMessage.Content.ReadAsStringAsync().Result); @@ -93,7 +97,7 @@ private async Task> GetAzureManagementGroups() var httpClient = _httpClientFactory.CreateClient("AzureServices"); var httpResponseMessage = await httpClient.GetAsync( - $"managementgroups?api-version=2021-04-01"); + $"providers/Microsoft.Management/managementGroups?api-version=2020-05-01"); var jsonDocument = JsonDocument.Parse(httpResponseMessage.Content.ReadAsStringAsync().Result); @@ -103,7 +107,8 @@ private async Task> GetAzureManagementGroups() { foreach (var managementGroup in managementGroupsElement.EnumerateArray()) { - var name = managementGroup.GetProperty("name").GetString(); + var name = managementGroup.GetProperty("properties").GetProperty("displayName").GetString(); + //var name = managementGroup. .GetProperty("displayName").GetString(); var id = managementGroup.GetProperty("id").GetString(); var type = managementGroup.GetProperty("type").GetString(); azureManagementGroups.Add(new AzureManagementGroup @@ -117,6 +122,7 @@ private async Task> GetAzureManagementGroups() } return azureManagementGroups; } + } } diff --git a/Program.cs b/Program.cs index cb541eb..d0a0eba 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,6 @@ using Microsoft.Identity.Client; using System.Net.Http.Headers; // Add this line - +using AzureIntegration.Services; var builder = WebApplication.CreateBuilder(args); IConfiguration _configuration = new ConfigurationBuilder() @@ -26,7 +26,7 @@ builder.Services.AddHttpClient("AzureServices", httpClient => { - httpClient.BaseAddress = new Uri($"https://management.azure.com/subscriptions/{subscriptionId}/"); + httpClient.BaseAddress = new Uri($"https://management.azure.com/"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.Result.AccessToken); }); @@ -40,6 +40,7 @@ // Register the HTTP client builder.Services.AddHttpClient(); +builder.Services.AddSingleton(new Subscription(subscriptionId)); // Register the configuration builder.Services.AddSingleton(builder.Configuration); builder.Services.AddDistributedMemoryCache(); diff --git a/Services/ResourceGroupsClasses.cs b/Services/ResourceGroupsClasses.cs index f37f322..4276e58 100644 --- a/Services/ResourceGroupsClasses.cs +++ b/Services/ResourceGroupsClasses.cs @@ -30,4 +30,19 @@ public string? Url } } } + public class Subscription + { + public string SubscriptionId { get; set; } + public Subscription(string subscriptionId) + { + SubscriptionId = subscriptionId; + } + public string GetSubscriptionId + { + get + { + return SubscriptionId; + } + } + } } diff --git a/Views/ResourceGroups/Details.cshtml b/Views/ResourceGroups/Details.cshtml index 9245a45..08cece6 100644 --- a/Views/ResourceGroups/Details.cshtml +++ b/Views/ResourceGroups/Details.cshtml @@ -5,6 +5,6 @@
    @foreach (var resource in Model) { -
  • @resource.Name
  • +
  • @resource.Name - @resource.Type
  • }
\ No newline at end of file diff --git a/Views/ResourceGroups/Index.cshtml b/Views/ResourceGroups/Index.cshtml index 958b610..00e9a08 100644 --- a/Views/ResourceGroups/Index.cshtml +++ b/Views/ResourceGroups/Index.cshtml @@ -7,7 +7,7 @@ foreach (var resourceGroup in Model) {
  • - @resourceGroup.Name (@resourceGroup.Location) + @resourceGroup.Name - @resourceGroup.Location
  • } } diff --git a/Views/ResourceGroups/ManagementGroups.cshtml b/Views/ResourceGroups/ManagementGroups.cshtml index fb93df6..64c542e 100644 --- a/Views/ResourceGroups/ManagementGroups.cshtml +++ b/Views/ResourceGroups/ManagementGroups.cshtml @@ -5,6 +5,6 @@
      @foreach (var group in Model) { -
    • @group.Name (@group.Type / @group.Id)
    • +
    • @group.Name - @group.Type
    • }
    \ No newline at end of file diff --git a/Views/Shared/_Layout.cshtml b/Views/Shared/_Layout.cshtml index f90bf88..2c1de97 100644 --- a/Views/Shared/_Layout.cshtml +++ b/Views/Shared/_Layout.cshtml @@ -31,7 +31,11 @@ +