Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
10f6ad0
Change the accessbility to virtual for Resource.Id
Mar 24, 2021
ccc17c7
merge from usptream
Apr 5, 2021
5060f5c
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Apr 12, 2021
9a9a651
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Apr 21, 2021
7764cb5
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
Apr 23, 2021
832483a
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
Apr 28, 2021
3066cd2
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
May 6, 2021
9597dc7
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
May 6, 2021
86547b0
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash May 10, 2021
4fa650c
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash May 10, 2021
6f858c5
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash May 17, 2021
fb80156
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash May 25, 2021
737171b
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Jun 2, 2021
4f1408d
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
Jun 4, 2021
c5d5790
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Jun 7, 2021
0bcb9e4
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
Jun 7, 2021
2a78d80
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
Jun 11, 2021
23eca40
Merge branch 'mgmt-track2' of https://github.com/AME-Redmond/azure-sd…
m-nash Jun 16, 2021
2bddb0e
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Jun 16, 2021
26e907c
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Jun 23, 2021
3786863
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Jun 23, 2021
a588338
Merge branch 'feature/mgmt-track2' into JonathanCrd-5981-Code-samples…
JonathanCrd Jul 7, 2021
5a619ba
Initial setup
JonathanCrd Jul 6, 2021
0a8286c
Adding Snippets to tests\Samples
JonathanCrd Jul 7, 2021
22279c4
Generate snippets in markdown files
JonathanCrd Jul 7, 2021
9f5bcfc
Sample 3 code snippets
JonathanCrd Jul 7, 2021
ee6a73b
Update Sample2_ManagingResourceGroups.cs
JonathanCrd Jul 7, 2021
2ba2dc7
Update Snippets with generator
JonathanCrd Jul 7, 2021
6926aa4
Fix Sample2 snippets
JonathanCrd Jul 7, 2021
ca9815d
Delete Azure - Backup.ResourceManager.Core.Tests.csproj
JonathanCrd Jul 7, 2021
e40fb45
Ignoring dummy subscription id tests
JonathanCrd Jul 8, 2021
bce156c
Adding namespaces to examples
JonathanCrd Jul 8, 2021
7b3f8aa
Modify code snippets in main readme
JonathanCrd Jul 8, 2021
f2bd769
Add ignore tags
JonathanCrd Jul 8, 2021
126ef16
Adding Azure.ResourceManager.Core namespace
JonathanCrd Jul 9, 2021
cca438d
Merge branch 'feature/mgmt-track2' into JonathanCrd-5981-Code-samples…
JonathanCrd Jul 9, 2021
ecc17e1
Update code snippets
JonathanCrd Jul 9, 2021
f34be39
Adjust indentation
JonathanCrd Jul 12, 2021
705412a
Removing auto-gen snippets for Sample3
JonathanCrd Jul 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 36 additions & 33 deletions sdk/resourcemanager/Azure.ResourceManager.Core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ The default option to create an authenticated client is to use `DefaultAzureCred

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

```csharp
```C# Snippet:Readme_AuthClient
using Azure.Identity;
using Azure.ResourceManager.Core;
using System;

using System.Threading.Tasks;

// code omitted for brevity

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

Expand Down Expand Up @@ -71,36 +72,9 @@ It also has access to all of the operations and like the **[Resource]Operations*
to a specific resource in Azure.

## Examples
### Add a tag to a virtual machine
Imagine that our company requires all virtual machines to be tagged with the owner. We're tasked with writing a program to add the tag to any missing virtual machines in a given resource group.

```csharp
// First we construct our armClient
var armClient = new ArmClient(new DefaultAzureCredential());

// Next we get a resource group object
// ResourceGroup is a [Resource] object from above
ResourceGroup resourceGroup = await armClient.DefaultSubscription.GetResourceGroups().GetAsync("myRgName");

// Next we get the container for the virtual machines
// vmContainer is a [Resource]Container object from above
VirtualMachineContainer vmContainer = resourceGroup.GetVirtualMachines();

// Next we loop over all vms in the container
// Each vm is a [Resource] object from above
await foreach(VirtualMachine vm in vmContainer.ListAsync())
{
// We access the [Resource]Data properties from vm.Data
if(!vm.Data.Tags.ContainsKey("owner"))
{
// We can also access all [Resource]Operations from vm since it is already scoped for us
await vm.StartAddTag("owner", GetOwner()).WaitForCompletionAsync();
}
}
```

### Create a resource group
```csharp
```C# Snippet:Readme_CreateRG
// First, initialize the ArmClient and get the default subscription
var armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;
Expand All @@ -109,11 +83,12 @@ 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.CreateAsync(rgName);
LocationData location = LocationData.WestUS;
ResourceGroup resourceGroup = await rgContainer.Construct(location).CreateOrUpdateAsync(rgName);
```

### List all resource groups
```csharp
```C# Snippet:Readme_ListAllRG
// First, initialize the ArmClient and get the default subscription
var armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;
Expand All @@ -129,6 +104,34 @@ await foreach (ResourceGroup rg in response)
}
```

### Add a tag to a virtual machine
Imagine that our company requires all virtual machines to be tagged with the owner. We're tasked with writing a program to add the tag to any missing virtual machines in a given resource group.

```csharp
// First we construct our armClient
var armClient = new ArmClient(new DefaultAzureCredential());

// Next we get a resource group object
// ResourceGroup is a [Resource] object from above
ResourceGroup resourceGroup = await armClient.DefaultSubscription.GetResourceGroups().GetAsync("myRgName");

// Next we get the container for the virtual machines
// vmContainer is a [Resource]Container object from above
VirtualMachineContainer vmContainer = resourceGroup.GetVirtualMachines();

// Next we loop over all vms in the container
// Each vm is a [Resource] object from above
await foreach(VirtualMachine vm in vmContainer.ListAsync())
{
// We access the [Resource]Data properties from vm.Data
if(!vm.Data.Tags.ContainsKey("owner"))
{
// We can also access all [Resource]Operations from vm since it is already scoped for us
await vm.StartAddTag("owner", GetOwner()).WaitForCompletionAsync();
}
}
```

For more detailed examples, take a look at [samples](https://github.com/Azure/azure-sdk-for-net/tree/feature/mgmt-track2/sdk/resourcemanager/Azure.ResourceManager.Core/samples) we have available.

## Troubleshooting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@

>Note: Before getting started with the samples, make sure to go trough the [prerequisites](https://github.com/Azure/azure-sdk-for-net/tree/feature/mgmt-track2/sdk/resourcemanager/Azure.ResourceManager.Core#prerequisites).

Namespaces for this example:
```C# Snippet:Hello_World_Namespaces
using System;
Comment thread
JonathanCrd marked this conversation as resolved.
using Azure.Identity;
using Azure.ResourceManager.Core;
```

The following code shows how to get the default subscription:

```csharp
```C# Snippet:Hello_World_DefaultSubscription
var armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;
Console.WriteLine(subscription.Id);
```

It's possible to get a specific subscription as follows:

```csharp
string subscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c";
```C# Snippet:Hello_World_SpecificSubscription
string subscriptionId = "your-subscription-id";
var armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.GetSubscriptions().Get(subscriptionId);
Console.WriteLine("Got subscription: " + subscription.Data.DisplayName);
```

From here, it is possible to get the resource groups from the retrieved subscription:

```csharp
```C# Snippet:Hello_World_ResourceGroupContainer
ResourceGroupContainer rgContainer = subscription.GetResourceGroups();
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,36 @@

>Note: Before getting started with the samples, go through the [prerequisites](https://github.com/Azure/azure-sdk-for-net/tree/feature/mgmt-track2/sdk/resourcemanager/Azure.ResourceManager.Core#prerequisites).

Namespaces for this example:
```C# Snippet:Hello_World_Async_Namespaces
using System;
Comment thread
JonathanCrd marked this conversation as resolved.
using System.Threading.Tasks;
using Azure.Identity;
using Azure.ResourceManager.Core;
```

The following code shows how to get the default subscription:

```csharp
```C# Snippet:Hello_World_Async_DefaultSubscription
var armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;
Console.WriteLine(subscription.Id);
```

It's possible to get a specific subscription as follows:

```csharp
string subscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c";
```C# Snippet:Hello_World_Async_SpecificSubscription
string subscriptionId = "your-subscription-id";
var armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.GetSubscriptions().GetAsync(subscriptionId);
Subscription subscription = await armClient.GetSubscriptions().GetAsync(subscriptionId);
Console.WriteLine(subscription.Id);
```

With the `Async` suffix on methods that perform API calls, it's possible to differentiate the asynchronous and synchronous variants of any method.

From here, it is possible to get the resource groups from the retrieved subscription:

```csharp
```C# Snippet:Hello_World_Async_ResourceGroupContainer
ResourceGroupContainer rgContainer = subscription.GetResourceGroups();
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
Example: Managing Resource Groups
--------------------------------------
For this example, you need the following namespaces:
```C# Snippet:Managing_Resource_Groups_Namespaces
using System;
Comment thread
JonathanCrd marked this conversation as resolved.
using System.Threading.Tasks;
using Azure.Identity;
using Azure.ResourceManager.Core;
```

When you first create your ARM client, choose the subscription you're going to work in. There's a convenient `DefaultSubscription` property that returns the default subscription configured for your user:

```csharp
```C# Snippet:Managing_Resource_Groups_DefaultSubscription
var armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;
```

This is a scoped operations object, and any operations you perform will be done under that subscription. From this object, you have access to all children via container objects. Or you can access individual children by ID.

```csharp
```C# Snippet:Managing_Resource_Groups_GetResourceGroupContainer
var armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;
ResourceGroupContainer rgContainer = subscription.GetResourceGroups();
Expand All @@ -25,19 +32,19 @@ Using the container object, we can perform collection-level operations such as l

***Create a resource group***

```csharp
```C# Snippet:Managing_Resource_Groups_CreateAResourceGroup
var armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;
ResourceGroupContainer rgContainer = subscription.GetResourceGroups();

LocationData location = LocationData.WestUS2;
string rgName = "myRgName";
ResourceGroup resourceGroup = await rgContainer.Construct(location).CreateAsync(rgName);
ResourceGroup resourceGroup = await rgContainer.Construct(location).CreateOrUpdateAsync(rgName);
```

***List all resource groups***

```csharp
```C# Snippet:Managing_Resource_Groups_ListAllResourceGroup
var armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;
ResourceGroupContainer rgContainer = subscription.GetResourceGroups();
Expand All @@ -52,18 +59,21 @@ Using the operation object we can perform entity-level operations, such as updat

***Update a resource group***

```csharp
```C# Snippet:Managing_Resource_Groups_UpdateAResourceGroup
// Note: Resource group named 'myRgName' should exist for this example to work.
var armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;
string rgName = "myRgName";
ResourceGroup resourceGroup = await subscription.GetResourceGroups().GetAsync(rgName);
resourceGroup = await rgOperation.StartAddTag("key", "value").WaitForCompletionAsync();
resourceGroup = await resourceGroup.AddTagAsync("key", "value");
```

***Delete a resource group***

```csharp
```C# Snippet:Managing_Resource_Groups_DeleteResourceGroup
var armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;
string rgName = "myRgName";
ResourceGroup resourceGroup = await subscription.GetResourceGroups().GetAsync(rgName);
await resourceGroup.DeleteAsync();
```
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
Example: Creating a Virtual Network
--------------------------------------

In this example, we'll create a virtual network. Since the SDK follows the resource hierarchy in Azure, we'll need to do this inside of a resource group. Start by creating a new resource group, like we did above:
In this example, we'll create a virtual network. Since the SDK follows the resource hierarchy in Azure, we'll need to do this inside of a resource group.

```csharp
## Import the namespaces
These are the namespaces needed for this project:
```C#
using Azure.Identity;
using Azure.ResourceManager.Core;
using Azure.ResourceManager.Network;
using System.Threading.Tasks;
```

In addition, you need to install the `Azure.ResourceManager.Compute` library in your project and import it.

## Create a Resource Group
Start by creating a new resource group, like we did above:

```C# Snippet:Creating_A_Virtual_Network_CreateResourceGroup
var armClient = new ArmClient(new DefaultAzureCredential());
ResourceGroupContainer rgContainer = armClient.DefaultSubscription.GetResourceGroups();
ResourceGroup resourceGroup = await rgContainer.Construct(LocationData.WestUS2).CreateAsync(rg);
string rgName = "myResourceGroup";
ResourceGroup resourceGroup = await rgContainer.Construct(LocationData.WestUS2).CreateOrUpdateAsync(rgName);
```

## Create a Virtual Network
Now that we have a resource group, we'll create our virtual network. To do this, we will use a helper method on the container object called `Construct`. The helper method allows us to create the request object and then send that to the `Create` method.

```csharp
Expand All @@ -18,6 +33,7 @@ VirtualNetwork virtualNetwork = await vnetContainer
.CreateAsync("myVnetName");
```

## Create a Subnet
Now that we have a virtual network, we must create at least one subnet in order to add any virtual machines.
Following the hierarchy in Azure, subnets belong to a virtual network, so that's where we'll get our `SubnetContainer` instance. After that, we'll again use the `Construct` helper method to create our subnet.

Expand All @@ -27,4 +43,4 @@ SubnetContainer subnetContainer = virtualNetwork.GetSubnets();
Subnet subnet = await subnetContainer
.Construct("10.0.0.0/24")
.CreateAsync(subnetName);
```
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#region Snippet:Readme_AuthClient
using Azure.Identity;
using Azure.ResourceManager.Core;
using System;
using System.Threading.Tasks;
#if !SNIPPET
using NUnit.Framework;

namespace Azure.ResourceManager.Core.Tests.Samples
{
class Readme
{
[Test]
[Ignore("Only verifying that the sample builds")]
public void ClientAuth()
{
#endif

// code omitted for brevity

var armClient = new ArmClient(new DefaultAzureCredential());
#endregion Snippet:Readme_AuthClient
}

[Test]
[Ignore("Only verifying that the sample builds")]
public async Task CreateRG()
{
#region Snippet:Readme_CreateRG
// First, initialize the ArmClient and get the default subscription
var armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;
// Now we get a ResourceGroup container for that subscription
ResourceGroupContainer rgContainer = subscription.GetResourceGroups();

// With the container, we can create a new resource group with an specific name
string rgName = "myRgName";
LocationData location = LocationData.WestUS;
ResourceGroup resourceGroup = await rgContainer.Construct(location).CreateOrUpdateAsync(rgName);
#endregion Snippet:Readme_CreateRG
}

[Test]
[Ignore("Only verifying that the sample builds")]
public async Task ListAllRG()
{
#region Snippet:Readme_ListAllRG
// First, initialize the ArmClient and get the default subscription
var armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;

// Now we get a ResourceGroup container for that subscription
ResourceGroupContainer rgContainer = subscription.GetResourceGroups();

// With ListAsync(), we can get a list of the resources in the container
AsyncPageable<ResourceGroup> response = rgContainer.ListAsync();
await foreach (ResourceGroup rg in response)
{
Console.WriteLine(rg.Data.Name);
}
#endregion Snippet:Readme_ListAllRG
}
}
}
Loading