diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/README.md b/sdk/resourcemanager/Azure.ResourceManager.Core/README.md index 1120fc2279b7..1ed1a53c331b 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/README.md +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/README.md @@ -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()); ``` @@ -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; @@ -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; @@ -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 diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample1_HelloWorld.md b/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample1_HelloWorld.md index 4457eb668e9e..c6cd8c20ef52 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample1_HelloWorld.md +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample1_HelloWorld.md @@ -2,9 +2,16 @@ >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); @@ -12,8 +19,8 @@ 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); @@ -21,7 +28,7 @@ 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(); ``` diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample1_HelloWorldAsync.md b/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample1_HelloWorldAsync.md index 224f29a3f3b0..744031acb50b 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample1_HelloWorldAsync.md +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample1_HelloWorldAsync.md @@ -2,9 +2,17 @@ >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); @@ -12,10 +20,10 @@ 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); ``` @@ -23,7 +31,7 @@ With the `Async` suffix on methods that perform API calls, it's possible to diff 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(); ``` diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample2_ManagingResourceGroups.md b/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample2_ManagingResourceGroups.md index 1f5122b1a421..80d7c5d001e2 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample2_ManagingResourceGroups.md +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample2_ManagingResourceGroups.md @@ -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(); @@ -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(); @@ -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(); ``` diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample3_CreatingAVirtualNetwork.md b/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample3_CreatingAVirtualNetwork.md index 6ca85153b819..abcc8f9a9694 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample3_CreatingAVirtualNetwork.md +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample3_CreatingAVirtualNetwork.md @@ -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 @@ -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. @@ -27,4 +43,4 @@ SubnetContainer subnetContainer = virtualNetwork.GetSubnets(); Subnet subnet = await subnetContainer .Construct("10.0.0.0/24") .CreateAsync(subnetName); -``` +``` \ No newline at end of file diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs new file mode 100644 index 000000000000..dc6cd0e01739 --- /dev/null +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs @@ -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 response = rgContainer.ListAsync(); + await foreach (ResourceGroup rg in response) + { + Console.WriteLine(rg.Data.Name); + } + #endregion Snippet:Readme_ListAllRG + } + } +} diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample1_HelloWorld.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample1_HelloWorld.cs new file mode 100644 index 000000000000..7d9ce7431da1 --- /dev/null +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample1_HelloWorld.cs @@ -0,0 +1,46 @@ +#region Snippet:Hello_World_Namespaces +using System; +using Azure.Identity; +using Azure.ResourceManager.Core; +#endregion +using NUnit.Framework; + +namespace Azure.ResourceManager.Core.Tests.Samples +{ + class Sample1_HelloWorld + { + [Test] + [Ignore("Only verifying that the sample builds")] + public void GettingDefaultSubscription() + { + #region Snippet:Hello_World_DefaultSubscription + var armClient = new ArmClient(new DefaultAzureCredential()); + Subscription subscription = armClient.DefaultSubscription; + Console.WriteLine(subscription.Id); + #endregion Snippet:Hello_World_DefaultSubscription + } + + [Test] + [Ignore("Only verifying that the sample builds")] + public void GettingSpecificSubscription() + { + #region 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); + #endregion Snippet:Hello_World_SpecificSubscription + } + + [Test] + [Ignore("Only verifying that the sample builds")] + public void RetrieveResourceGroupContainer() + { + var armClient = new ArmClient(new DefaultAzureCredential()); + Subscription subscription = armClient.DefaultSubscription; + #region Snippet:Hello_World_ResourceGroupContainer + ResourceGroupContainer rgContainer = subscription.GetResourceGroups(); + #endregion Snippet:Hello_World_ResourceGroupContainer + } + } +} diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample1_HelloWorldAsync.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample1_HelloWorldAsync.cs new file mode 100644 index 000000000000..5f427bc95238 --- /dev/null +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample1_HelloWorldAsync.cs @@ -0,0 +1,47 @@ +#region Snippet:Hello_World_Async_Namespaces +using System; +using System.Threading.Tasks; +using Azure.Identity; +using Azure.ResourceManager.Core; +#endregion +using NUnit.Framework; + +namespace Azure.ResourceManager.Core.Tests.Samples +{ + class Sample1_HelloWorldAsync + { + [Test] + [Ignore("Only verifying that the sample builds")] + public void GettingDefaultSubscription() + { + #region Snippet:Hello_World_Async_DefaultSubscription + var armClient = new ArmClient(new DefaultAzureCredential()); + Subscription subscription = armClient.DefaultSubscription; + Console.WriteLine(subscription.Id); + #endregion Snippet:Hello_World_Async_DefaultSubscription + } + + [Test] + [Ignore("Only verifying that the sample builds")] + public async Task GettingSpecificSubscriptionAsync() + { + #region Snippet:Hello_World_Async_SpecificSubscription + string subscriptionId = "your-subscription-id"; + var armClient = new ArmClient(new DefaultAzureCredential()); + Subscription subscription = await armClient.GetSubscriptions().GetAsync(subscriptionId); + Console.WriteLine(subscription.Id); + #endregion Snippet:Hello_World_Async_SpecificSubscription + } + + [Test] + [Ignore("Only verifying that the sample builds")] + public void RetrieveResourceGroupContainer() + { + var armClient = new ArmClient(new DefaultAzureCredential()); + Subscription subscription = armClient.DefaultSubscription; + #region Snippet:Hello_World_Async_ResourceGroupContainer + ResourceGroupContainer rgContainer = subscription.GetResourceGroups(); + #endregion Snippet:Hello_World_Async_ResourceGroupContainer + } + } +} diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample2_ManagingResourceGroups.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample2_ManagingResourceGroups.cs new file mode 100644 index 000000000000..7b4039ffe338 --- /dev/null +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample2_ManagingResourceGroups.cs @@ -0,0 +1,116 @@ +#region Snippet:Managing_Resource_Groups_Namespaces +using System; +using System.Threading.Tasks; +using Azure.Identity; +using Azure.ResourceManager.Core; +#endregion +using NUnit.Framework; + +namespace Azure.ResourceManager.Core.Tests.Samples +{ + class Sample2_ManagingResourceGroups + { + [Test] + [Ignore("Only verifying that the sample builds")] + public void SetUpWithDefaultSubscription() + { + #region Snippet:Managing_Resource_Groups_DefaultSubscription + var armClient = new ArmClient(new DefaultAzureCredential()); + Subscription subscription = armClient.DefaultSubscription; + #endregion Snippet:Managing_Resource_Groups_DefaultSubscription + } + + [Test] + [Ignore("Only verifying that the sample builds")] + public async Task CreateResourceGroup() + { + #region 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).CreateOrUpdateAsync(rgName); + #endregion Snippet:Managing_Resource_Groups_CreateAResourceGroup + } + + [Test] + [Ignore("Only verifying that the sample builds")] + public async Task GettingResourceGroupContainer() + { + #region Snippet:Managing_Resource_Groups_GetResourceGroupContainer + var armClient = new ArmClient(new DefaultAzureCredential()); + Subscription subscription = armClient.DefaultSubscription; + ResourceGroupContainer rgContainer = subscription.GetResourceGroups(); + + // code omitted for brevity + + string rgName = "myRgName"; +#if !SNIPPET + //Check if "myRgName" exists, if not, create it first or run CreateResourceGroup() + var rg = await subscription.GetResourceGroups().TryGetAsync(rgName); + if (rg == null) + { + LocationData location = LocationData.WestUS2; + _ = await rgContainer.Construct(location).CreateOrUpdateAsync(rgName); + } +#endif + ResourceGroup resourceGroup = await rgContainer.GetAsync(rgName); + #endregion Snippet:Managing_Resource_Groups_GetResourceGroupContainer + } + + [Test] + [Ignore("Only verifying that the sample builds")] + public async Task ListAllResourceGroups() + { + #region Snippet:Managing_Resource_Groups_ListAllResourceGroup + var armClient = new ArmClient(new DefaultAzureCredential()); + Subscription subscription = armClient.DefaultSubscription; + ResourceGroupContainer rgContainer = subscription.GetResourceGroups(); + AsyncPageable response = rgContainer.ListAsync(); + await foreach (ResourceGroup rg in response) + { + Console.WriteLine(rg.Data.Name); + } + #endregion Snippet:Managing_Resource_Groups_ListAllResourceGroup + } + + [Test] + [Ignore("Only verifying that the sample builds")] + public async Task UpdateAResourceGroup() + { + #region 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"; +#if !SNIPPET + //Check if 'myRgName' exists, if not, create it first or run CreateResourceGroup() + var rg = await subscription.GetResourceGroups().TryGetAsync(rgName); + if (rg == null) + { + LocationData location = LocationData.WestUS2; + var rgContainer = subscription.GetResourceGroups(); + _ = await rgContainer.Construct(location).CreateOrUpdateAsync(rgName); + } +#endif + ResourceGroup resourceGroup = await subscription.GetResourceGroups().GetAsync(rgName); + resourceGroup = await resourceGroup.AddTagAsync("key", "value"); + #endregion Snippet:Managing_Resource_Groups_UpdateAResourceGroup + } + + [Test] + [Ignore("Only verifying that the sample builds")] + public async Task DeleteResourceGroup() + { + #region 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(); + #endregion Snippet:Managing_Resource_Groups_DeleteResourceGroup + } + } +} diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample3_CreatingAVirtualNetwork.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample3_CreatingAVirtualNetwork.cs new file mode 100644 index 000000000000..bf28b8acd4be --- /dev/null +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample3_CreatingAVirtualNetwork.cs @@ -0,0 +1,22 @@ +using Azure.Identity; +using Azure.ResourceManager.Core; +using System.Threading.Tasks; +using NUnit.Framework; + +namespace Azure.ResourceManager.Core.Tests.Samples +{ + class Sample3_CreatingAVirtualNetwork + { + [Test] + [Ignore("Only verifying that the sample builds")] + public async Task CreateResourceGroupAsync() + { + #region Snippet:Creating_A_Virtual_Network_CreateResourceGroup + var armClient = new ArmClient(new DefaultAzureCredential()); + ResourceGroupContainer rgContainer = armClient.DefaultSubscription.GetResourceGroups(); + string rgName = "myResourceGroup"; + ResourceGroup resourceGroup = await rgContainer.Construct(LocationData.WestUS2).CreateOrUpdateAsync(rgName); + #endregion Snippet:Creating_A_Virtual_Network_CreateResourceGroup + } + } +}