Skip to content

Commit

Permalink
Updating code snippets in Samples and Readme (#22518)
Browse files Browse the repository at this point in the history
* Change the accessbility to virtual for Resource.Id

* Initial setup

* Adding Snippets to tests\Samples

* Generate snippets in markdown files

* Sample 3 code snippets

* Update Sample2_ManagingResourceGroups.cs

* Update Snippets with generator

* Fix Sample2 snippets

* Delete Azure - Backup.ResourceManager.Core.Tests.csproj

* Ignoring dummy subscription id tests

* Adding namespaces to examples

* Modify code snippets in main readme

* Add ignore tags

* Adding Azure.ResourceManager.Core namespace

* Update code snippets

* Adjust indentation

* Removing auto-gen snippets for Sample3

Co-authored-by: YalinLi0312 <[email protected]>
Co-authored-by: m-nash <[email protected]>
Co-authored-by: m-nash <[email protected]>
  • Loading branch information
4 people authored Jul 12, 2021
1 parent a3ffa95 commit 1cd717a
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 56 deletions.
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;
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;
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;
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

0 comments on commit 1cd717a

Please sign in to comment.