OPS.GoCloud.AzureClient 1.0.9

dotnet add package OPS.GoCloud.AzureClient --version 1.0.9
NuGet\Install-Package OPS.GoCloud.AzureClient -Version 1.0.9
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OPS.GoCloud.AzureClient" Version="1.0.9" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OPS.GoCloud.AzureClient --version 1.0.9
#r "nuget: OPS.GoCloud.AzureClient, 1.0.9"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install OPS.GoCloud.AzureClient as a Cake Addin
#addin nuget:?package=OPS.GoCloud.AzureClient&version=1.0.9

// Install OPS.GoCloud.AzureClient as a Cake Tool
#tool nuget:?package=OPS.GoCloud.AzureClient&version=1.0.9

Installation

dotnet add package OPS.GoCloud.AzureClient --version {version}

Use

Initialize AzureClient

Default credentials

var tenantId = "YOUR_TENANT_ID";

AzureClient azureClient = new AzureClient(tenantId);

Client credentials

var tenantId = "YOUR_TENANT_ID";
var clientId = "YOUR_CLIENT_ID";
var clientsecret = "YOUR_CLIENT_SECRET";

AzureClient azureClient = new AzureClient(tenantId,clientId,clientsecret);

User-managed identity credentials

var tenantId = "YOUR_TENANT_ID";
var umiClientId = "User-Managed Identity_CLIENT_ID";

AzureClient azureClient = new AzureClient(tenantId,umiClientId);

List Azure subscriptions

List all the enabled Azure subscriptions in your tenant

var subs = azureClient.ListSubscriptions();

List all the Azure subscriptions in your tenant

var subs = azureClient.ListSubscriptions(enabledOnly: false);

Query from Azure Resource Graph

  1. Create your custom class and inherit from ArmBase.
public class subscription : ArmBase<subscription>
{
    public string subscriptionId { get; set; }
    public string name { get; set; }
}
  1. Write query and get results. The results will be deserialized to your custom class type.
string query = "resourcecontainers | where type == 'microsoft.resources/subscriptions | project subscriptionId,name";

List<subscription> azureSubs = subscription.Query(azureClient, query);

Send request to Azure REST API

  1. Create your custom class
public class PolicyAssignment
{
    public string id { get; set; }
    public string name { get; set; }
    public string location { get; set; }
    public object properties { get; set; }
}
  1. Send request to Azure REST API by calling SendToAzRestAPI. Successful response returns data in JSON string, and can be deserialized to your custom class type.
string policyAssignmentId = "";
string uri = $"https://management.azure.com/{policyAssignmentId}?api-version=2021-06-01";

// The returned respose content is data in JSON string
var resultsJson = await azureClient.SendToAzRestAPI(uri,HttpMethod.Get);

PolicyAssignment policyAssignment = JsonConvert.DeserializeObject<PolicyAssignment>(resultsJson);

Initialize Microsoft Graph Client

var graphClientId = "YOUR_CLIENT_ID";
var graphClientsecret = "YOUR_CLIENT_SECRET";

azureClient.InitiateGraphClient(graphClientId,graphClientsecret);

List MS Graph objects

To get a full list of MS Graph objects and optinally parse into your custom class type, we can call the method ListFromMsGraph.

  1. Define a container class type. For example,
public class MyUsersContainer
{
    [JsonProperty("@odata.nextLink")]
    public string OdataNextLink { get; set; }

    public List<Microsoft.Graph.Models.User> value { get; set; }
}


// Or use your custom class to parse the User object
public class MyUser
{
    public string id { get; set; }
    public string displayName { get; set; }
}

public class MyUsersContainer
{
    [JsonProperty("@odata.nextLink")]
    public string OdataNextLink { get; set; }
    public List<MyUser> value { get; set; }
}

A container class type is needed for the method ListFromMsGraph to parse the results. All MS Graph LIST APIs return data as in

{
  "@odata.nextLink":"https://...",
  "value":[
    {
     ...
    },
    {
     ...
    }
  ]
}

Notice it is a JSON object with a "@odata.nextLink" field and a "value" field.

  • The "@odata.nextLink" field is an URL. When more than one query request is required to retrieve all the results, Microsoft Graph returns an "@odata.nextLink" property in the response that contains a URL to the next page of results.
  • The "value" field is an array, containg the results in the current page.
  1. Call ListFromMsGraph
var graphClientId = "YOUR_CLIENT_ID";
var graphClientsecret = "YOUR_CLIENT_SECRET";

azureClient.InitiateGraphClient(graphClientId,graphClientsecret);

string uri = "https://graph.microsoft.com/beta/users"; //optionally add MS Graph API-supported query parameters, such as $filter, $top

List<MyUser> users = await azClient.ListFromMsGraph<MyUser, MyUsersContainer>(uri);

Send request to MS Graph API

Another method to get results from MS Graph API is SendToMsGraphAPI

  1. Optionally create your custom class
public class MyUser
{
    public string id { get; set; }
    public string displayName { get; set; }
}
  1. Send request to MS Graph API by calling SendToMsGraphAPI. Successful response returns data in JSON string, and can be deserialized to a class type.
string userId = "";
string uri = $"https://graph.microsoft.com/beta/users/{userId}";

// The returned respose content is data in JSON string
var resultsJson = await azureClient.SendToMsGraphAPI(uri,HttpMethod.Get);

MyUser user = JsonConvert.DeserializeObject<MyUser>(resultsJson);

Use Utils methods

using static GoCloud.AzureClient.Utils;


// EmptyString
string str1 = "";
string str2 = "myTeStStr";
str1.EmptyString(); // => returns True
str2.EmptyString(); // => returns False


// StringContains
string searchStr1 = "est";
string searchStr2 = "ddd";
str2.StringContains(searchStr1); // => returns True
str2.StringContains(searchStr2); // => returns False


// IgnoreSerializeJson and SelectSerializeJson
MyUser user = new MyUser { id="010", displayName = "user1" };

string jsonStr1 = IgnoreSerializeJson(user, ignoreProp:"displayName"); // => returns a JSON string { "id": "010" }
string jsonStr2 = SelectSerializeJson(user, selectProp:"displayName"); // => returns a JSON string { "displayName": "user1" }

For more Utils methods, please see the code repo.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.9 107 3/20/2024
1.0.8 94 2/16/2024
1.0.7 109 2/6/2024
1.0.6 93 1/25/2024
1.0.5 88 1/24/2024
1.0.4 81 1/22/2024
1.0.3 77 1/22/2024
1.0.2 79 1/22/2024
1.0.1 84 1/22/2024
1.0.0 94 1/22/2024