Skip to content

Commit 69aea77

Browse files
authored
Merge pull request #6 from sayedimac/mgmt
Mgmt
2 parents e60dc33 + ca35a0d commit 69aea77

7 files changed

Lines changed: 38 additions & 12 deletions

File tree

Controllers/ResourceGroupsController.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
using Microsoft.AspNetCore.Mvc;
22
using System.Text.Json;
33
using AzureIntegration.Services;
4+
using Microsoft.VisualBasic;
5+
using System.Runtime.CompilerServices;
6+
47

58
namespace AzureIntegration.Controllers
69
{
710

811
public class ResourceGroupsController : Controller
9-
1012
{
1113
private readonly IHttpClientFactory _httpClientFactory;
1214
private readonly IConfiguration _configuration;
15+
1316
public ResourceGroupsController(IHttpClientFactory httpClientFactory, IConfiguration configuration)
1417
{
1518
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
@@ -37,9 +40,9 @@ public async Task<IActionResult> Index()
3740
private async Task<List<ResourceGroup>> GetResourceGroupsAsync()
3841
{
3942
var httpClient = _httpClientFactory.CreateClient("AzureServices");
40-
43+
var subscriptionId = _configuration["SubscriptionId"];
4144
var httpResponseMessage = await httpClient.GetAsync(
42-
$"resourcegroups?api-version=2021-04-01");
45+
$"subscriptions/{subscriptionId}/resourcegroups?api-version=2021-04-01");
4346

4447

4548
var jsonDocument = JsonDocument.Parse(httpResponseMessage.Content.ReadAsStringAsync().Result);
@@ -67,9 +70,10 @@ private async Task<List<AzureResource>> GetResourcesAsync(string name)
6770
{
6871

6972
var httpClient = _httpClientFactory.CreateClient("AzureServices");
73+
var subscriptionId = _configuration["SubscriptionId"];
7074

7175
var httpResponseMessage = await httpClient.GetAsync(
72-
$"resourceGroups/{name}/resources?api-version=2021-04-01");
76+
$"subscriptions/{subscriptionId}/resourceGroups/{name}/resources?api-version=2021-04-01");
7377

7478
var jsonDocument = JsonDocument.Parse(httpResponseMessage.Content.ReadAsStringAsync().Result);
7579

@@ -93,7 +97,7 @@ private async Task<List<AzureManagementGroup>> GetAzureManagementGroups()
9397
var httpClient = _httpClientFactory.CreateClient("AzureServices");
9498

9599
var httpResponseMessage = await httpClient.GetAsync(
96-
$"managementgroups?api-version=2021-04-01");
100+
$"providers/Microsoft.Management/managementGroups?api-version=2020-05-01");
97101

98102
var jsonDocument = JsonDocument.Parse(httpResponseMessage.Content.ReadAsStringAsync().Result);
99103

@@ -103,7 +107,8 @@ private async Task<List<AzureManagementGroup>> GetAzureManagementGroups()
103107
{
104108
foreach (var managementGroup in managementGroupsElement.EnumerateArray())
105109
{
106-
var name = managementGroup.GetProperty("name").GetString();
110+
var name = managementGroup.GetProperty("properties").GetProperty("displayName").GetString();
111+
//var name = managementGroup. .GetProperty("displayName").GetString();
107112
var id = managementGroup.GetProperty("id").GetString();
108113
var type = managementGroup.GetProperty("type").GetString();
109114
azureManagementGroups.Add(new AzureManagementGroup
@@ -117,6 +122,7 @@ private async Task<List<AzureManagementGroup>> GetAzureManagementGroups()
117122
}
118123
return azureManagementGroups;
119124
}
125+
120126
}
121127
}
122128

Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Microsoft.Identity.Client;
22
using System.Net.Http.Headers; // Add this line
3-
3+
using AzureIntegration.Services;
44
var builder = WebApplication.CreateBuilder(args);
55

66
IConfiguration _configuration = new ConfigurationBuilder()
@@ -26,7 +26,7 @@
2626

2727
builder.Services.AddHttpClient("AzureServices", httpClient =>
2828
{
29-
httpClient.BaseAddress = new Uri($"https://management.azure.com/subscriptions/{subscriptionId}/");
29+
httpClient.BaseAddress = new Uri($"https://management.azure.com/");
3030
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.Result.AccessToken);
3131
});
3232

@@ -40,6 +40,7 @@
4040
// Register the HTTP client
4141
builder.Services.AddHttpClient();
4242

43+
builder.Services.AddSingleton(new Subscription(subscriptionId));
4344
// Register the configuration
4445
builder.Services.AddSingleton<IConfiguration>(builder.Configuration);
4546
builder.Services.AddDistributedMemoryCache();

Services/ResourceGroupsClasses.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,19 @@ public string? Url
3030
}
3131
}
3232
}
33+
public class Subscription
34+
{
35+
public string SubscriptionId { get; set; }
36+
public Subscription(string subscriptionId)
37+
{
38+
SubscriptionId = subscriptionId;
39+
}
40+
public string GetSubscriptionId
41+
{
42+
get
43+
{
44+
return SubscriptionId;
45+
}
46+
}
47+
}
3348
}

Views/ResourceGroups/Details.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<ul>
66
@foreach (var resource in Model)
77
{
8-
<li><img src="@resource.Url" /> @resource.Name </li>
8+
<li><img src="@resource.Url" /> @resource.Name - @resource.Type </li>
99
}
1010
</ul>

Views/ResourceGroups/Index.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
foreach (var resourceGroup in Model)
88
{
99
<li><a href="@Url.Action("Details", "ResourceGroups", new { name = resourceGroup.Name })">
10-
@resourceGroup.Name </a> (@resourceGroup.Location)
10+
@resourceGroup.Name </a> - @resourceGroup.Location
1111
</li>
1212
}
1313
}

Views/ResourceGroups/ManagementGroups.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<ul>
66
@foreach (var group in Model)
77
{
8-
<li>@group.Name (@group.Type / @group.Id)</li>
8+
<li>@group.Name - @group.Type</li>
99
}
1010
</ul>

Views/Shared/_Layout.cshtml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
</li>
3232
<li class="nav-item">
3333
<a class="nav-link text-dark" asp-area="" asp-controller="ResourceGroups"
34-
asp-action="">Azure Resource Groups</a>
34+
asp-action="ManagementGroups">Management Groups</a>
35+
</li>
36+
<li class="nav-item">
37+
<a class="nav-link text-dark" asp-area="" asp-controller="ResourceGroups"
38+
asp-action="">Resource Groups</a>
3539
</li>
3640
</ul>
3741
</div>

0 commit comments

Comments
 (0)