Skip to content

Latest commit

 

History

History
243 lines (185 loc) · 11.1 KB

File metadata and controls

243 lines (185 loc) · 11.1 KB

Azure Compute Management client library for .NET

This package follows the new Azure SDK guidelines,which provide core capabilities that are shared amongst all Azure SDKs, including:

  • The intuitive Azure Identity library.
  • An HTTP pipeline with custom policies.
  • Error handling.
  • Distributed tracing.

Getting started

Install the package

Install the Azure Compute management library for .NET with NuGet:

Install-Package Azure.ResourceManager.Compute -Version 1.0.0-beta.1

Prerequisites

Set up a way to authenticate to Azure with Azure Identity.

Some options are:

More information and different authentication approaches using Azure Identity can be found in this document.

Authenticate the Client

The default option to create an authenticated client is to use DefaultAzureCredential. Since all management APIs go through the same endpoint, in order to interact with resources, only one top-level ArmClient has to be created.

To authenticate to Azure and create an ArmClient, do the following:

using Azure.Identity;
using Azure.ResourceManager;

// Code omitted for brevity

ArmClient armClient = new ArmClient(new DefaultAzureCredential());

Additional documentation for the Azure.Identity.DefaultAzureCredential class can be found in this document.

Key concepts

Key concepts of the Azure .NET SDK can be found here

Examples

Create an availability set

Before creating an availability set, we need to have a resource group.

ArmClient armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;
ResourceGroupContainer rgContainer = subscription.GetResourceGroups();
// With the container, we can create a new resource group with an specific name
string rgName = "myRgName";
Location location = Location.WestUS2;
ResourceGroupCreateOrUpdateOperation lro = await rgContainer.CreateOrUpdateAsync(rgName, new ResourceGroupData(location));
ResourceGroup resourceGroup = lro.Value;
AvailabilitySetContainer availabilitySetContainer = resourceGroup.GetAvailabilitySets();
string availabilitySetName = "myAvailabilitySet";
AvailabilitySetData input = new AvailabilitySetData(location);
AvailabilitySetCreateOrUpdateOperation lro = await availabilitySetContainer.CreateOrUpdateAsync(availabilitySetName, input);
AvailabilitySet availabilitySet = lro.Value;

Get all availability set in a resource group

// First, initialize the ArmClient and get the default subscription
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
// Now we get a ResourceGroup container for that subscription
Subscription subscription = armClient.DefaultSubscription;
ResourceGroupContainer rgContainer = subscription.GetResourceGroups();

string rgName = "myRgName";
ResourceGroup resourceGroup = await rgContainer.GetAsync(rgName);
// First, we get the availability set container from the resource group
AvailabilitySetContainer availabilitySetContainer = resourceGroup.GetAvailabilitySets();
// With GetAllAsync(), we can get a list of the availability sets in the container
AsyncPageable<AvailabilitySet> response = availabilitySetContainer.GetAllAsync();
await foreach (AvailabilitySet availabilitySet in response)
{
    Console.WriteLine(availabilitySet.Data.Name);
}

Update an availability set

// First, initialize the ArmClient and get the default subscription
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
// Now we get a ResourceGroup container for that subscription
Subscription subscription = armClient.DefaultSubscription;
ResourceGroupContainer rgContainer = subscription.GetResourceGroups();

// With the container, we can create a new resource group with an specific name
string rgName = "myRgName";
ResourceGroup resourceGroup = await rgContainer.GetAsync(rgName);
AvailabilitySetContainer availabilitySetContainer = resourceGroup.GetAvailabilitySets();
string availabilitySetName = "myAvailabilitySet";
AvailabilitySet availabilitySet = await availabilitySetContainer.GetAsync(availabilitySetName);
// availabilitySet is an AvailabilitySet instance created above
AvailabilitySetUpdate update = new AvailabilitySetUpdate()
{
    PlatformFaultDomainCount = 3
};
AvailabilitySet updatedAvailabilitySet = await availabilitySet.UpdateAsync(update);

Delete an availability set

// First, initialize the ArmClient and get the default subscription
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
// Now we get a ResourceGroup container for that subscription
Subscription subscription = armClient.DefaultSubscription;
ResourceGroupContainer rgContainer = subscription.GetResourceGroups();

// With the container, we can create a new resource group with an specific name
string rgName = "myRgName";
ResourceGroup resourceGroup = await rgContainer.GetAsync(rgName);
AvailabilitySetContainer availabilitySetContainer = resourceGroup.GetAvailabilitySets();
string availabilitySetName = "myAvailabilitySet";
AvailabilitySet availabilitySet = await availabilitySetContainer.GetAsync(availabilitySetName);
// delete the availability set
await availabilitySet.DeleteAsync();

Check if availability set exists

If you just want to verify if the availability set exists, you can use the function CheckIfExists.

ArmClient armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;
ResourceGroupContainer rgContainer = subscription.GetResourceGroups();

string rgName = "myRgName";
ResourceGroup resourceGroup = await rgContainer.GetAsync(rgName);
string availabilitySetName = "myAvailabilitySet";
bool exists = await resourceGroup.GetAvailabilitySets().CheckIfExistsAsync(availabilitySetName);

if (exists)
{
    Console.WriteLine($"Availability Set {availabilitySetName} exists.");
}
else
{
    Console.WriteLine($"Availability Set {availabilitySetName} does not exist.");
}

If you want to first check if the availability set exists, and if it does, you want to do something else on it, you should use the function GetIfExists():

// First, initialize the ArmClient and get the default subscription
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
// Now we get a ResourceGroup container for that subscription
Subscription subscription = armClient.DefaultSubscription;
ResourceGroupContainer rgContainer = subscription.GetResourceGroups();

string rgName = "myRgName";
ResourceGroup resourceGroup = await rgContainer.GetAsync(rgName);
AvailabilitySetContainer availabilitySetContainer = resourceGroup.GetAvailabilitySets();
string availabilitySetName = "myAvailabilitySet";
AvailabilitySet availabilitySet = await availabilitySetContainer.GetIfExistsAsync(availabilitySetName);

if (availabilitySet == null)
{
    Console.WriteLine($"Availability Set {availabilitySetName} does not exist.");
    return;
}

// At this point, we are sure that availabilitySet is a not null Availability Set, so we can use this object to perform any operations we want.

Add a tag to an availability set

// First, initialize the ArmClient and get the default subscription
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
// Now we get a ResourceGroup container for that subscription
Subscription subscription = armClient.DefaultSubscription;
ResourceGroupContainer rgContainer = subscription.GetResourceGroups();

string rgName = "myRgName";
ResourceGroup resourceGroup = await rgContainer.GetAsync(rgName);
AvailabilitySetContainer availabilitySetContainer = resourceGroup.GetAvailabilitySets();
string availabilitySetName = "myAvailabilitySet";
AvailabilitySet availabilitySet = await availabilitySetContainer.GetAsync(availabilitySetName);
// add a tag on this availabilitySet
AvailabilitySet updatedAvailabilitySet = await availabilitySet.AddTagAsync("key", "value");

For more detailed examples, take a look at samples we have available.

Troubleshooting

Next steps

More sample code

Additional Documentation

For more information on Azure SDK, please refer to this website.

Contributing

For details on contributing to this repository, see the contributing guide.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repositories using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.