From 10f6ad0c6416ee7b84427b30a3068fec2005e6aa Mon Sep 17 00:00:00 2001 From: YalinLi0312 Date: Wed, 24 Mar 2021 16:18:02 -0700 Subject: [PATCH 1/5] Change the accessbility to virtual for Resource.Id --- .../Azure.ResourceManager.Core/src/Resources/Resource.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/Resource.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/Resource.cs index 86a35ac05cf7..a8638d1c689b 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/Resource.cs +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/Resource.cs @@ -14,7 +14,7 @@ public abstract class Resource : IEquatable, IEquatable, IComp /// /// Gets or sets the resource identifier. /// - public abstract ResourceIdentifier Id { get; protected set; } + public virtual ResourceIdentifier Id { get; protected set; } /// /// Gets the name. From e1c04fc84cebb5d0911435521901abad92957d3a Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Tue, 13 Jul 2021 11:31:16 -0500 Subject: [PATCH 2/5] Adding missing sections from workshop quickstart --- .../Azure.ResourceManager.Core/README.md | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/README.md b/sdk/resourcemanager/Azure.ResourceManager.Core/README.md index 1120fc2279b7..39a80805fc7b 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/README.md +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/README.md @@ -70,6 +70,74 @@ This represents a full resource object which contains a **Data** property exposi It also has access to all of the operations and like the **[Resource]Operations** object is already scoped to a specific resource in Azure. +### Structured Resource Identifier +Instead of implementing your own parsing logic, you can implicitly cast a resource identifier string into an object which will do the parsing for you. + +There are 3 types of ResourceIdentifiers and they correspond to which level the resource lives at: +- A resource that lives on a tenant will have a `TenantResourceIdentifier`. +- A resource that lives under a subscription will have a `SubscriptionResourceIdentifer`. +- A resource that lives under a resource group will have a `ResourceGroupResourceIdentifier`. + +You can usually tell by the id string itself which type it is, but if you are unsure you can always cast it onto a `ResourceIdentifier` and use the Try methods to retrieve the values. + +#### Casting to a specific type +```csharp + string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"; + //we know the subnet is a resource group level identifier since it has a resource group name in its string + ResourceGroupResourceIdentifier id = resourceId; + Console.WriteLine($"Subscription: {id.SubscriptionId}"); + Console.WriteLine($"ResourceGroup: {id.ResourceGroupName}"); + Console.WriteLine($"Vnet: {id.Parent.Name}"); + Console.WriteLine($"Subnet: {id.Name}"); +``` + +#### Casting to the base resource identifier +```csharp + string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"; + //assume we don't know what type of resource id we have we can cast to the base type + ResourceIdentifier id = resourceId; + string property; + if(id.TryGetSubscriptionId(out property)) + Console.WriteLine($"Subscription: {property}"); + if(id.TryGetResourceGroupName(out property)) + Console.WriteLine($"ResourceGroup: {property}"); + Console.WriteLine($"Vnet: {id.Parent.Name}"); + Console.WriteLine($"Subnet: {id.Name}"); +``` + +### Managing Existing Resources By Id +Performing operations on resources that already exist is a common use case when using the management SDK. In this scenario you usually have the identifier of the resource you want to work on as a string. Although the new object hierarchy is great for provisioning and working within the scope of a given parent, it is a tad awkward when it comes to this specific scenario. + +Here is how you would access an availability set object and manage it with its id: + +```csharp + string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet"; + //we know the availability set is a resource group level identifier since it has a resource group name in its string + ResourceGroupResourceIdentifier id = resourceId; + //we then construct a new armClient to work with + ArmClient armClient = new ArmClient(new DefaultAzureCredential()); + //next we get the specific subscription this resource belongs to + Subscription subscription = armClient.GetSubscriptions().Get(id.SubscriptionId); + //next we get the specific resource group this resource belongs to + ResourceGroup resourceGroup = subscription.GetResourceGroups().Get(id.ResourceGroupName); + //finally we get the resource itself + AvailabilitySet availabilitySet = resourceGroup.GetAvailabilitySets().Get(id.Name); +``` +However, this approach required a lot of code and 3 API calls to Azure. The same can be done with less code and without any API calls by using extension methods that we have provided on the client itself. These extension methods allow you to pass in a resource identifier and retrieve a scoped client. The object returned is a *[Resource]Operations* mentioned above, since it has not reached out to Azure to retrieve the data yet. + +So, the previous example would end up looking like this: + +```csharp + string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet"; + //we construct a new armClient to work with + ArmClient armClient = new ArmClient(new DefaultAzureCredential()); + //next we get the AvailabilitySetOperations object from the client + //the method takes in a ResourceIdentifier but we can use the implicit cast from string + AvailabilitySetOperations availabilitySetOperations = armClient.GetAvailabilitySetOperations(resourceId); + //now if we want to retrieve the objects data we can simply call get + AvailabilitySet availabilitySet = availabilitySetOperations.Get(); +``` + ## 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. From 9b6c4b90c79e57e7eb75bbe22447af9f68dec6d2 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Tue, 13 Jul 2021 13:30:06 -0500 Subject: [PATCH 3/5] Adding missing code snippets --- .../Azure.ResourceManager.Core/README.md | 72 +++++++++------- .../tests/Samples/Readme.cs | 85 +++++++++++++------ 2 files changed, 99 insertions(+), 58 deletions(-) diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/README.md b/sdk/resourcemanager/Azure.ResourceManager.Core/README.md index 3f7bcc204b20..fb1cb0bc23e4 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/README.md +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/README.md @@ -82,28 +82,28 @@ There are 3 types of ResourceIdentifiers and they correspond to which level the You can usually tell by the id string itself which type it is, but if you are unsure you can always cast it onto a `ResourceIdentifier` and use the Try methods to retrieve the values. #### Casting to a specific type -```csharp - string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"; - //we know the subnet is a resource group level identifier since it has a resource group name in its string - ResourceGroupResourceIdentifier id = resourceId; - Console.WriteLine($"Subscription: {id.SubscriptionId}"); - Console.WriteLine($"ResourceGroup: {id.ResourceGroupName}"); - Console.WriteLine($"Vnet: {id.Parent.Name}"); - Console.WriteLine($"Subnet: {id.Name}"); +```C# Snippet:Readme_CastToSpecificType +string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"; +//we know the subnet is a resource group level identifier since it has a resource group name in its string +ResourceGroupResourceIdentifier id = resourceId; +Console.WriteLine($"Subscription: {id.SubscriptionId}"); +Console.WriteLine($"ResourceGroup: {id.ResourceGroupName}"); +Console.WriteLine($"Vnet: {id.Parent.Name}"); +Console.WriteLine($"Subnet: {id.Name}"); ``` #### Casting to the base resource identifier -```csharp - string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"; - //assume we don't know what type of resource id we have we can cast to the base type - ResourceIdentifier id = resourceId; - string property; - if(id.TryGetSubscriptionId(out property)) - Console.WriteLine($"Subscription: {property}"); - if(id.TryGetResourceGroupName(out property)) - Console.WriteLine($"ResourceGroup: {property}"); - Console.WriteLine($"Vnet: {id.Parent.Name}"); - Console.WriteLine($"Subnet: {id.Name}"); +```C# Snippet:Readme_CastToBaseResourceIdentifier +string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"; +//assume we don't know what type of resource id we have we can cast to the base type +ResourceIdentifier id = resourceId; +string property; +if (id.TryGetSubscriptionId(out property)) + Console.WriteLine($"Subscription: {property}"); +if (id.TryGetResourceGroupName(out property)) + Console.WriteLine($"ResourceGroup: {property}"); +Console.WriteLine($"Vnet: {id.Parent.Name}"); +Console.WriteLine($"Subnet: {id.Name}"); ``` ### Managing Existing Resources By Id @@ -142,29 +142,21 @@ So, the previous example would end up looking like this: ## Examples ### Create a resource group -```C# Snippet:Readme_CreateRG -// First, initialize the ArmClient and get the default subscription +```C# Snippet:Managing_Resource_Groups_CreateAResourceGroup 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 +Location location = Location.WestUS2; string rgName = "myRgName"; -Location location = Location.WestUS; ResourceGroup resourceGroup = await rgContainer.Construct(location).CreateOrUpdateAsync(rgName); ``` ### List all resource groups -```C# Snippet:Readme_ListAllRG -// First, initialize the ArmClient and get the default subscription +```C# Snippet:Managing_Resource_Groups_ListAllResourceGroup 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) { @@ -172,6 +164,26 @@ await foreach (ResourceGroup rg in response) } ``` +### Update a resource group + +```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 resourceGroup.AddTagAsync("key", "value"); +``` + +### Delete a resource group + +```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(); +``` ### 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. diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs index 5bc0d06fe1b2..683feee43cd1 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs @@ -24,41 +24,70 @@ public void ClientAuth() [Test] [Ignore("Only verifying that the sample builds")] - public async Task CreateRG() + public void CastingToSpecificType() { - #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"; - Location location = Location.WestUS; - ResourceGroup resourceGroup = await rgContainer.Construct(location).CreateOrUpdateAsync(rgName); - #endregion Snippet:Readme_CreateRG + #region Snippet:Readme_CastToSpecificType + string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"; + //we know the subnet is a resource group level identifier since it has a resource group name in its string + ResourceGroupResourceIdentifier id = resourceId; + Console.WriteLine($"Subscription: {id.SubscriptionId}"); + Console.WriteLine($"ResourceGroup: {id.ResourceGroupName}"); + Console.WriteLine($"Vnet: {id.Parent.Name}"); + Console.WriteLine($"Subnet: {id.Name}"); + #endregion Snippet:Readme_CastToSpecificType } [Test] [Ignore("Only verifying that the sample builds")] - public async Task ListAllRG() + public void CastingToBaseResourceIdentifier() { - #region Snippet:Readme_ListAllRG - // First, initialize the ArmClient and get the default subscription - var armClient = new ArmClient(new DefaultAzureCredential()); - Subscription subscription = armClient.DefaultSubscription; + #region Snippet:Readme_CastToBaseResourceIdentifier + string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"; + //assume we don't know what type of resource id we have we can cast to the base type + ResourceIdentifier id = resourceId; + string property; + if (id.TryGetSubscriptionId(out property)) + Console.WriteLine($"Subscription: {property}"); + if (id.TryGetResourceGroupName(out property)) + Console.WriteLine($"ResourceGroup: {property}"); + Console.WriteLine($"Vnet: {id.Parent.Name}"); + Console.WriteLine($"Subnet: {id.Name}"); + #endregion Snippet:Readme_CastToBaseResourceIdentifier + } - // Now we get a ResourceGroup container for that subscription - ResourceGroupContainer rgContainer = subscription.GetResourceGroups(); + //[Test] + //[Ignore("Only verifying that the sample builds")] + //public void ManagingResourceById() + //{ + // #region Snippet:Readme_ManagingResourceById + // string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet"; + // //we know the availability set is a resource group level identifier since it has a resource group name in its string + // ResourceGroupResourceIdentifier id = resourceId; + // //we then construct a new armClient to work with + // ArmClient armClient = new ArmClient(new DefaultAzureCredential()); + // //next we get the specific subscription this resource belongs to + // Subscription subscription = armClient.GetSubscriptions().Get(id.SubscriptionId); + // //next we get the specific resource group this resource belongs to + // ResourceGroup resourceGroup = subscription.GetResourceGroups().Get(id.ResourceGroupName); + // //finally we get the resource itself + // AvailabilitySet availabilitySet = resourceGroup.GetAvailabilitySets().Get(id.Name); + // #endregion Snippet:Readme_ManagingResourceById + //} - // 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 - } + //[Test] + //[Ignore("Only verifying that the sample builds")] + //public void ManagingResourceByIdWithExtensionMethods() + //{ + // #region Snippet:Readme_ManagingResourceByIdWithExtensionMethods + // string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet"; + // //we construct a new armClient to work with + // ArmClient armClient = new ArmClient(new DefaultAzureCredential()); + // //next we get the AvailabilitySetOperations object from the client + // //the method takes in a ResourceIdentifier but we can use the implicit cast from string + // AvailabilitySetOperations availabilitySetOperations = armClient.GetAvailabilitySetOperations(resourceId); + // //now if we want to retrieve the objects data we can simply call get + // AvailabilitySet availabilitySet = availabilitySetOperations.Get(); + // #endregion Snippet:Readme_ManagingResourceByIdWithExtensionMethods + //} } } From 10c33c57701ca26a0d93e2c7e908de3f85c749cc Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Tue, 13 Jul 2021 16:35:22 -0500 Subject: [PATCH 4/5] Updating code snippets in main readme --- .../Azure.ResourceManager.Core/README.md | 54 +++++++++++-------- .../Sample3_CreatingAVirtualNetwork.md | 2 - .../tests/Samples/Readme.cs | 35 ------------ 3 files changed, 31 insertions(+), 60 deletions(-) diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/README.md b/sdk/resourcemanager/Azure.ResourceManager.Core/README.md index fb1cb0bc23e4..439c2dbf9dd4 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/README.md +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/README.md @@ -84,7 +84,7 @@ You can usually tell by the id string itself which type it is, but if you are un #### Casting to a specific type ```C# Snippet:Readme_CastToSpecificType string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"; -//we know the subnet is a resource group level identifier since it has a resource group name in its string +// We know the subnet is a resource group level identifier since it has a resource group name in its string ResourceGroupResourceIdentifier id = resourceId; Console.WriteLine($"Subscription: {id.SubscriptionId}"); Console.WriteLine($"ResourceGroup: {id.ResourceGroupName}"); @@ -95,7 +95,7 @@ Console.WriteLine($"Subnet: {id.Name}"); #### Casting to the base resource identifier ```C# Snippet:Readme_CastToBaseResourceIdentifier string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"; -//assume we don't know what type of resource id we have we can cast to the base type +// Assume we don't know what type of resource id we have we can cast to the base type ResourceIdentifier id = resourceId; string property; if (id.TryGetSubscriptionId(out property)) @@ -109,34 +109,42 @@ Console.WriteLine($"Subnet: {id.Name}"); ### Managing Existing Resources By Id Performing operations on resources that already exist is a common use case when using the management SDK. In this scenario you usually have the identifier of the resource you want to work on as a string. Although the new object hierarchy is great for provisioning and working within the scope of a given parent, it is a tad awkward when it comes to this specific scenario. -Here is how you would access an availability set object and manage it with its id: - +Here is an example how you to access an `AvailabilitySet` object and manage it directly with its id: ```csharp - string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet"; - //we know the availability set is a resource group level identifier since it has a resource group name in its string - ResourceGroupResourceIdentifier id = resourceId; - //we then construct a new armClient to work with - ArmClient armClient = new ArmClient(new DefaultAzureCredential()); - //next we get the specific subscription this resource belongs to - Subscription subscription = armClient.GetSubscriptions().Get(id.SubscriptionId); - //next we get the specific resource group this resource belongs to - ResourceGroup resourceGroup = subscription.GetResourceGroups().Get(id.ResourceGroupName); - //finally we get the resource itself - AvailabilitySet availabilitySet = resourceGroup.GetAvailabilitySets().Get(id.Name); +using Azure.Identity; +using Azure.ResourceManager.Core; +using Azure.ResourceManager.Compute; +using System; +using System.Threading.Tasks; + +// Code omitted for brevity + +string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet"; +// We know the availability set is a resource group level identifier since it has a resource group name in its string +ResourceGroupResourceIdentifier id = resourceId; +// We then construct a new armClient to work with +ArmClient armClient = new ArmClient(new DefaultAzureCredential()); +// Next we get the specific subscription this resource belongs to +Subscription subscription = await armClient.GetSubscriptions().GetAsync(id.SubscriptionId); +// Next we get the specific resource group this resource belongs to +ResourceGroup resourceGroup = await subscription.GetResourceGroups().GetAsync(id.ResourceGroupName); +// Finally we get the resource itself +// Note: for this last stept in this example, Azure.ResourceManager.Compute is needed +AvailabilitySet availabilitySet = await resourceGroup.GetAvailabilitySets().GetAsync(id.Name); ``` However, this approach required a lot of code and 3 API calls to Azure. The same can be done with less code and without any API calls by using extension methods that we have provided on the client itself. These extension methods allow you to pass in a resource identifier and retrieve a scoped client. The object returned is a *[Resource]Operations* mentioned above, since it has not reached out to Azure to retrieve the data yet. So, the previous example would end up looking like this: ```csharp - string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet"; - //we construct a new armClient to work with - ArmClient armClient = new ArmClient(new DefaultAzureCredential()); - //next we get the AvailabilitySetOperations object from the client - //the method takes in a ResourceIdentifier but we can use the implicit cast from string - AvailabilitySetOperations availabilitySetOperations = armClient.GetAvailabilitySetOperations(resourceId); - //now if we want to retrieve the objects data we can simply call get - AvailabilitySet availabilitySet = availabilitySetOperations.Get(); +string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet"; +// We construct a new armClient to work with +ArmClient armClient = new ArmClient(new DefaultAzureCredential()); +// Next we get the AvailabilitySetOperations object from the client +// The method takes in a ResourceIdentifier but we can use the implicit cast from string +AvailabilitySetOperations availabilitySetOperations = armClient.GetAvailabilitySetOperations(resourceId); +// Now if we want to retrieve the objects data we can simply call get +AvailabilitySet availabilitySet = await availabilitySetOperations.GetAsync(); ``` ## Examples diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample3_CreatingAVirtualNetwork.md b/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample3_CreatingAVirtualNetwork.md index 7bf131718edc..7c51bffab0fd 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample3_CreatingAVirtualNetwork.md +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample3_CreatingAVirtualNetwork.md @@ -12,8 +12,6 @@ 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: diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs index 683feee43cd1..2227a23e7f3f 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs @@ -54,40 +54,5 @@ public void CastingToBaseResourceIdentifier() Console.WriteLine($"Subnet: {id.Name}"); #endregion Snippet:Readme_CastToBaseResourceIdentifier } - - //[Test] - //[Ignore("Only verifying that the sample builds")] - //public void ManagingResourceById() - //{ - // #region Snippet:Readme_ManagingResourceById - // string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet"; - // //we know the availability set is a resource group level identifier since it has a resource group name in its string - // ResourceGroupResourceIdentifier id = resourceId; - // //we then construct a new armClient to work with - // ArmClient armClient = new ArmClient(new DefaultAzureCredential()); - // //next we get the specific subscription this resource belongs to - // Subscription subscription = armClient.GetSubscriptions().Get(id.SubscriptionId); - // //next we get the specific resource group this resource belongs to - // ResourceGroup resourceGroup = subscription.GetResourceGroups().Get(id.ResourceGroupName); - // //finally we get the resource itself - // AvailabilitySet availabilitySet = resourceGroup.GetAvailabilitySets().Get(id.Name); - // #endregion Snippet:Readme_ManagingResourceById - //} - - //[Test] - //[Ignore("Only verifying that the sample builds")] - //public void ManagingResourceByIdWithExtensionMethods() - //{ - // #region Snippet:Readme_ManagingResourceByIdWithExtensionMethods - // string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet"; - // //we construct a new armClient to work with - // ArmClient armClient = new ArmClient(new DefaultAzureCredential()); - // //next we get the AvailabilitySetOperations object from the client - // //the method takes in a ResourceIdentifier but we can use the implicit cast from string - // AvailabilitySetOperations availabilitySetOperations = armClient.GetAvailabilitySetOperations(resourceId); - // //now if we want to retrieve the objects data we can simply call get - // AvailabilitySet availabilitySet = availabilitySetOperations.Get(); - // #endregion Snippet:Readme_ManagingResourceByIdWithExtensionMethods - //} } } From 368f560c0a4f8d19758994103ac902c359f20214 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Tue, 13 Jul 2021 17:08:01 -0500 Subject: [PATCH 5/5] Small fixes in comments in code snippets --- .../Azure.ResourceManager.Core/README.md | 10 ++++++++-- .../samples/Sample2_ManagingResourceGroups.md | 8 +++++++- .../Azure.ResourceManager.Core/tests/Samples/Readme.cs | 6 +++--- .../tests/Samples/Sample2_ManagingResourceGroups.cs | 10 ++++++++-- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/README.md b/sdk/resourcemanager/Azure.ResourceManager.Core/README.md index 439c2dbf9dd4..ff1a7261c070 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/README.md +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/README.md @@ -39,7 +39,7 @@ using Azure.ResourceManager.Core; using System; using System.Threading.Tasks; -// code omitted for brevity +// Code omitted for brevity var armClient = new ArmClient(new DefaultAzureCredential()); ``` @@ -151,20 +151,26 @@ AvailabilitySet availabilitySet = await availabilitySetOperations.GetAsync(); ### Create a resource group ```C# Snippet:Managing_Resource_Groups_CreateAResourceGroup +// First, initialize the ArmClient and get the default subscription var armClient = new ArmClient(new DefaultAzureCredential()); +// Now we get a ResourceGroup container for that subscription Subscription subscription = armClient.DefaultSubscription; ResourceGroupContainer rgContainer = subscription.GetResourceGroups(); -Location location = Location.WestUS2; +// With the container, we can create a new resource group with an specific name string rgName = "myRgName"; +Location location = Location.WestUS2; ResourceGroup resourceGroup = await rgContainer.Construct(location).CreateOrUpdateAsync(rgName); ``` ### List all resource groups ```C# Snippet:Managing_Resource_Groups_ListAllResourceGroup +// 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) { diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample2_ManagingResourceGroups.md b/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample2_ManagingResourceGroups.md index 949a96129e38..b270f4d9f562 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample2_ManagingResourceGroups.md +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/samples/Sample2_ManagingResourceGroups.md @@ -33,21 +33,27 @@ Using the container object, we can perform collection-level operations such as l ***Create a resource group*** ```C# Snippet:Managing_Resource_Groups_CreateAResourceGroup +// First, initialize the ArmClient and get the default subscription var armClient = new ArmClient(new DefaultAzureCredential()); +// Now we get a ResourceGroup container for that subscription Subscription subscription = armClient.DefaultSubscription; ResourceGroupContainer rgContainer = subscription.GetResourceGroups(); -Location location = Location.WestUS2; +// With the container, we can create a new resource group with an specific name string rgName = "myRgName"; +Location location = Location.WestUS2; ResourceGroup resourceGroup = await rgContainer.Construct(location).CreateOrUpdateAsync(rgName); ``` ***List all resource groups*** ```C# Snippet:Managing_Resource_Groups_ListAllResourceGroup +// 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) { diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs index 2227a23e7f3f..d8039bf3c39e 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Readme.cs @@ -16,7 +16,7 @@ public void ClientAuth() { #endif -// code omitted for brevity +// Code omitted for brevity var armClient = new ArmClient(new DefaultAzureCredential()); #endregion Snippet:Readme_AuthClient @@ -28,7 +28,7 @@ public void CastingToSpecificType() { #region Snippet:Readme_CastToSpecificType string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"; - //we know the subnet is a resource group level identifier since it has a resource group name in its string + // We know the subnet is a resource group level identifier since it has a resource group name in its string ResourceGroupResourceIdentifier id = resourceId; Console.WriteLine($"Subscription: {id.SubscriptionId}"); Console.WriteLine($"ResourceGroup: {id.ResourceGroupName}"); @@ -43,7 +43,7 @@ public void CastingToBaseResourceIdentifier() { #region Snippet:Readme_CastToBaseResourceIdentifier string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"; - //assume we don't know what type of resource id we have we can cast to the base type + // Assume we don't know what type of resource id we have we can cast to the base type ResourceIdentifier id = resourceId; string property; if (id.TryGetSubscriptionId(out property)) diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample2_ManagingResourceGroups.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample2_ManagingResourceGroups.cs index 21ea1ecaa838..6512a4ba4a72 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample2_ManagingResourceGroups.cs +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Samples/Sample2_ManagingResourceGroups.cs @@ -25,12 +25,15 @@ public void SetUpWithDefaultSubscription() public async Task CreateResourceGroup() { #region Snippet:Managing_Resource_Groups_CreateAResourceGroup + // First, initialize the ArmClient and get the default subscription var armClient = new ArmClient(new DefaultAzureCredential()); + // Now we get a ResourceGroup container for that subscription Subscription subscription = armClient.DefaultSubscription; ResourceGroupContainer rgContainer = subscription.GetResourceGroups(); - - Location location = Location.WestUS2; + + // With the container, we can create a new resource group with an specific name string rgName = "myRgName"; + Location location = Location.WestUS2; ResourceGroup resourceGroup = await rgContainer.Construct(location).CreateOrUpdateAsync(rgName); #endregion Snippet:Managing_Resource_Groups_CreateAResourceGroup } @@ -65,9 +68,12 @@ public async Task GettingResourceGroupContainer() public async Task ListAllResourceGroups() { #region Snippet:Managing_Resource_Groups_ListAllResourceGroup + // 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) {