-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- use file scoped namespace - use `{}` for single line - use custom Exception thrown - use local variables when they are only in one method -
- Loading branch information
Showing
8 changed files
with
50 additions
and
47 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/stargripcorp.dataplatform.infra.azure/Exceptions/ApiResponseExceptions.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace stargripcorp.dataplatform.infra.azure.Exceptions | ||
{ | ||
internal class ApiResponseExceptions(string message) : Exception(message) | ||
Check warning on line 9 in src/stargripcorp.dataplatform.infra.azure/Exceptions/ApiResponseExceptions.cs GitHub Actions / Build and analyze
Check warning on line 9 in src/stargripcorp.dataplatform.infra.azure/Exceptions/ApiResponseExceptions.cs GitHub Actions / Build and analyze
Check warning on line 9 in src/stargripcorp.dataplatform.infra.azure/Exceptions/ApiResponseExceptions.cs GitHub Actions / Build and analyze
|
||
{ | ||
} | ||
} |
43 changes: 22 additions & 21 deletions
43
src/stargripcorp.dataplatform.infra.azure/Helpers/Utils.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,33 @@ | ||
using Pulumi.AzureNative.Authorization; | ||
using stargripcorp.dataplatform.infra.azure.Exceptions; | ||
using System.Net.Http.Headers; | ||
using System.Text.Json; | ||
|
||
namespace stargripcorp.dataplatform.infra.azure.Helpers | ||
namespace stargripcorp.dataplatform.infra.azure.Helpers; | ||
|
||
internal static class Utils | ||
{ | ||
internal class Utils | ||
public static async Task<string> GetRoleIdByNameAsync(string roleName) | ||
{ | ||
public static async Task<string> GetRoleIdByNameAsync(string roleName) | ||
{ | ||
var config = await GetClientConfig.InvokeAsync(); | ||
var token = await GetClientToken.InvokeAsync(); | ||
var config = await GetClientConfig.InvokeAsync(); | ||
var token = await GetClientToken.InvokeAsync(); | ||
|
||
// Unfortunately, Microsoft hasn't shipped an .NET5-compatible SDK at the time of writing this. | ||
// So, we have to hand-craft an HTTP request to retrieve a role definition. | ||
var httpClient = new HttpClient(); | ||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Token); | ||
var response = await httpClient.GetAsync($"https://management.azure.com/subscriptions/{config.SubscriptionId}/providers/Microsoft.Authorization/roleDefinitions?api-version=2018-01-01-preview&$filter=roleName%20eq%20'{roleName}'"); | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
throw new Exception($"Request failed with {response.StatusCode}"); | ||
} | ||
var body = await response.Content.ReadAsStringAsync(); | ||
var definition = JsonSerializer.Deserialize<RoleDefinition>(body); | ||
return definition!.value[0].id; | ||
// Unfortunately, Microsoft hasn't shipped an .NET5-compatible SDK at the time of writing this. | ||
// So, we have to hand-craft an HTTP request to retrieve a role definition. | ||
var httpClient = new HttpClient(); | ||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Token); | ||
var response = await httpClient.GetAsync($"https://management.azure.com/subscriptions/{config.SubscriptionId}/providers/Microsoft.Authorization/roleDefinitions?api-version=2018-01-01-preview&$filter=roleName%20eq%20'{roleName}'"); | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
throw new ApiResponseExceptions($"Request failed with {response.StatusCode}"); | ||
} | ||
var body = await response.Content.ReadAsStringAsync(); | ||
var definition = JsonSerializer.Deserialize<RoleDefinition>(body); | ||
return definition!.value[0].id; | ||
} | ||
} | ||
#pragma warning disable IDE1006 // Naming Styles | ||
public record RoleDefinition(List<RoleDefinitionValue> value); | ||
public record RoleDefinitionValue(string id, string type, string name); | ||
public record RoleDefinition(List<RoleDefinitionValue> value); | ||
public record RoleDefinitionValue(string id, string type, string name); | ||
#pragma warning restore IDE1006 // Naming Styles | ||
} | ||
|
||
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
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
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
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
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
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