Skip to content

Commit dae43af

Browse files
authored
refresh snippets based on API changes (Azure#27064)
* refresh snippets based on API changes * remove one var * update snippet for var * update RI ctor consistency * remove section on tryget RI methods * remove getallasync * update comment on getall
1 parent e22d8e9 commit dae43af

File tree

4 files changed

+156
-68
lines changed

4 files changed

+156
-68
lines changed

sdk/resourcemanager/Azure.ResourceManager/README.md

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ The default option to create an authenticated client is to use `DefaultAzureCred
3434
To authenticate to Azure and create an `ArmClient`, do the following:
3535

3636
```C# Snippet:Readme_AuthClient
37-
using Azure.Identity;
38-
using Azure.ResourceManager;
39-
using Azure.ResourceManager.Resources;
4037
using System;
4138
using System.Threading.Tasks;
4239
using Azure.Core;
40+
using Azure.Identity;
41+
using Azure.ResourceManager;
42+
using Azure.ResourceManager.Compute;
43+
using Azure.ResourceManager.Resources;
4344

4445
// Code omitted for brevity
4546
@@ -61,16 +62,16 @@ This represents a full resource client object which contains a **Data** property
6162
It also has access to all of the operations on that resource without needing to pass in scope parameters such as subscription ID or resource name. This makes it very convenient to directly execute operations on the result of list calls
6263
since everything is returned as a full resource client now.
6364

64-
```C#
65+
```C# Snippet:Readme_LoopVms
6566
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
6667
string rgName = "myResourceGroup";
6768
Subscription subscription = await armClient.GetDefaultSubscriptionAsync();
6869
ResourceGroup rg = await subscription.GetResourceGroups().GetAsync(rgName);
69-
await foreach (VirtualMachine vm in rg.GetVirtualMachines().GetAllAsync())
70+
await foreach (VirtualMachine vm in rg.GetVirtualMachines())
7071
{
7172
//previously we would have to take the resourceGroupName and the vmName from the vm object
7273
//and pass those into the powerOff method as well as we would need to execute that on a separate compute client
73-
await vm.StartPowerOff().WaitForCompletionAsync();
74+
await vm.PowerOffAsync(true);
7475
}
7576
```
7677

@@ -96,9 +97,9 @@ For most things, the parent will be a **ResourceGroup**. However, each parent /
9697
## Putting it all together
9798
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.
9899

99-
```C#
100+
```C# Snippet:Readme_PuttingItAllTogether
100101
// First we construct our armClient
101-
var armClient = new ArmClient(new DefaultAzureCredential());
102+
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
102103

103104
// Next we get a resource group object
104105
// ResourceGroup is a [Resource] object from above
@@ -111,59 +112,35 @@ VirtualMachineCollection vmCollection = resourceGroup.GetVirtualMachines();
111112

112113
// Next we loop over all vms in the collection
113114
// Each vm is a [Resource] object from above
114-
await foreach(VirtualMachine vm in vmCollection)
115+
await foreach (VirtualMachine vm in vmCollection)
115116
{
116117
// We access the [Resource]Data properties from vm.Data
117-
if(!vm.Data.Tags.ContainsKey("owner"))
118+
if (!vm.Data.Tags.ContainsKey("owner"))
118119
{
119120
// We can also access all operations from vm since it is already scoped for us
120-
await vm.StartAddTag("owner", GetOwner()).WaitForCompletionAsync();
121+
await vm.AddTagAsync("owner", "tagValue");
121122
}
122123
}
123-
```
124+
```
124125

125126
## Structured Resource Identifier
126127
Resource IDs contain useful information about the resource itself, but they are plain strings that have to be parsed. Instead of implementing your own parsing logic, you can use a `ResourceIdentifier` object which will do the parsing for you: `new ResourceIdentifer("myid");`.
127128

128129
### Example: Parsing an ID using a ResourceIdentifier object
129130
```C# Snippet:Readme_CastToSpecificType
130-
string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet";
131-
ResourceIdentifier id = new ResourceIdentifier(resourceId);
131+
ResourceIdentifier id = new ResourceIdentifier("/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet");
132132
Console.WriteLine($"Subscription: {id.SubscriptionId}");
133133
Console.WriteLine($"ResourceGroup: {id.ResourceGroupName}");
134134
Console.WriteLine($"Vnet: {id.Parent.Name}");
135135
Console.WriteLine($"Subnet: {id.Name}");
136136
```
137-
However, keep in mind that some of those properties could be null. You can usually tell by the id string itself which type a resource ID is, but if you are unsure, check if the properties are null or use the Try methods to retrieve the values as it's shown below:
138-
139-
### Example: ResourceIdentifier TryGet methods
140-
```C# Snippet:Readme_CastToBaseResourceIdentifier
141-
string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet";
142-
ResourceIdentifier id = new ResourceIdentifier(resourceId);
143-
Console.WriteLine($"Subscription: {id.SubscriptionId}");
144-
Console.WriteLine($"ResourceGroup: {id.ResourceGroupName}");
145-
// Parent is only null when we reach the top of the chain which is a Tenant
146-
Console.WriteLine($"Vnet: {id.Parent.Name}");
147-
// Name will never be null
148-
Console.WriteLine($"Subnet: {id.Name}");
149-
```
150137

151138
## Managing Existing Resources By Id
152139
Performing operations on resources that already exist is a common use case when using the management client libraries. 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 not the most efficient when it comes to this specific scenario.
153140

154141
Here is an example how you to access an `AvailabilitySet` object and manage it directly with its id:
155-
```C#
156-
using Azure.Identity;
157-
using Azure.ResourceManager;
158-
using Azure.ResourceManager.Resources;
159-
using Azure.ResourceManager.Compute;
160-
using System;
161-
using System.Threading.Tasks;
162-
163-
// Code omitted for brevity
164-
165-
string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet";
166-
ResourceIdentifier id = new ResourceIdentifier(resourceId);
142+
```C# Snippet:Readme_ManageAvailabilitySetOld
143+
ResourceIdentifier id = new ResourceIdentifier("/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet");
167144
// We construct a new armClient to work with
168145
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
169146
// Next we get the specific subscription this resource belongs to
@@ -179,8 +156,27 @@ This approach required a lot of code and 3 API calls to Azure. The same can be d
179156

180157
So, the previous example would end up looking like this:
181158

182-
```C#
183-
string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet";
159+
```C# Snippet:Readme_ManageAvailabilitySetNow
160+
ResourceIdentifier resourceId = new ResourceIdentifier("/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet");
161+
// We construct a new armClient to work with
162+
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
163+
// Next we get the AvailabilitySet resource client from the armClient
164+
// The method takes in a ResourceIdentifier but we can use the implicit cast from string
165+
AvailabilitySet availabilitySet = armClient.GetAvailabilitySet(resourceId);
166+
// At this point availabilitySet.Data will be null and trying to access it will throw
167+
// If we want to retrieve the objects data we can simply call get
168+
availabilitySet = await availabilitySet.GetAsync();
169+
// we now have the data representing the availabilitySet
170+
Console.WriteLine(availabilitySet.Data.Name);
171+
```
172+
173+
We also provide an option that if you only know the pieces that make up the `ResourceIdentifier` each resource provides a static method to construct the full string from those pieces.
174+
The above example would then look like this.
175+
```C# Snippet:Readme_ManageAvailabilitySetPieces
176+
string subscriptionId = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
177+
string resourceGroupName = "workshop2021-rg";
178+
string availabilitySetName = "ws2021availSet";
179+
ResourceIdentifier resourceId = AvailabilitySet.CreateResourceIdentifier(subscriptionId, resourceGroupName, availabilitySetName);
184180
// We construct a new armClient to work with
185181
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
186182
// Next we get the AvailabilitySet resource client from the armClient
@@ -284,8 +280,8 @@ ArmClient armClient = new ArmClient(new DefaultAzureCredential());
284280
Subscription subscription = await armClient.GetDefaultSubscriptionAsync();
285281
// Now we get a ResourceGroup collection for that subscription
286282
ResourceGroupCollection rgCollection = subscription.GetResourceGroups();
287-
// With GetAllAsync(), we can get a list of the resources in the collection
288-
await foreach (ResourceGroup rg in rgCollection.GetAllAsync())
283+
// We can then iterate over this collection to get the resources in the collection
284+
await foreach (ResourceGroup rg in rgCollection)
289285
{
290286
Console.WriteLine(rg.Data.Name);
291287
}

sdk/resourcemanager/Azure.ResourceManager/samples/Sample2_ManagingResourceGroups.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ ArmClient armClient = new ArmClient(new DefaultAzureCredential());
5757
Subscription subscription = await armClient.GetDefaultSubscriptionAsync();
5858
// Now we get a ResourceGroup collection for that subscription
5959
ResourceGroupCollection rgCollection = subscription.GetResourceGroups();
60-
// With GetAllAsync(), we can get a list of the resources in the collection
61-
await foreach (ResourceGroup rg in rgCollection.GetAllAsync())
60+
// We can then iterate over this collection to get the resources in the collection
61+
await foreach (ResourceGroup rg in rgCollection)
6262
{
6363
Console.WriteLine(rg.Data.Name);
6464
}

sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Readme.cs

Lines changed: 113 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#region Snippet:Readme_AuthClient
2-
using Azure.Identity;
3-
using Azure.ResourceManager;
4-
using Azure.ResourceManager.Resources;
52
using System;
63
using System.Threading.Tasks;
74
using Azure.Core;
5+
using Azure.Identity;
6+
using Azure.ResourceManager;
7+
using Azure.ResourceManager.Compute;
8+
using Azure.ResourceManager.Resources;
89
#if !SNIPPET
910
using NUnit.Framework;
1011

@@ -29,31 +30,14 @@ public void ClientAuth()
2930
public void CastingToSpecificType()
3031
{
3132
#region Snippet:Readme_CastToSpecificType
32-
string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet";
33-
ResourceIdentifier id = new ResourceIdentifier(resourceId);
33+
ResourceIdentifier id = new ResourceIdentifier("/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet");
3434
Console.WriteLine($"Subscription: {id.SubscriptionId}");
3535
Console.WriteLine($"ResourceGroup: {id.ResourceGroupName}");
3636
Console.WriteLine($"Vnet: {id.Parent.Name}");
3737
Console.WriteLine($"Subnet: {id.Name}");
3838
#endregion Snippet:Readme_CastToSpecificType
3939
}
4040

41-
[Test]
42-
[Ignore("Only verifying that the sample builds")]
43-
public void CastingToBaseResourceIdentifier()
44-
{
45-
#region Snippet:Readme_CastToBaseResourceIdentifier
46-
string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet";
47-
ResourceIdentifier id = new ResourceIdentifier(resourceId);
48-
Console.WriteLine($"Subscription: {id.SubscriptionId}");
49-
Console.WriteLine($"ResourceGroup: {id.ResourceGroupName}");
50-
// Parent is only null when we reach the top of the chain which is a Tenant
51-
Console.WriteLine($"Vnet: {id.Parent.Name}");
52-
// Name will never be null
53-
Console.WriteLine($"Subnet: {id.Name}");
54-
#endregion Snippet:Readme_CastToBaseResourceIdentifier
55-
}
56-
5741
[Test]
5842
[Ignore("Only verifying that the sample builds")]
5943
public async Task CheckIfResourceGroupExists()
@@ -122,5 +106,113 @@ public async Task TryGetResourceGroupOld()
122106
}
123107
#endregion Snippet:Readme_OldExistsRG
124108
}
109+
110+
[Test]
111+
[Ignore("Only verifying that the sample builds")]
112+
public async Task LoopVms()
113+
{
114+
#region Snippet:Readme_LoopVms
115+
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
116+
string rgName = "myResourceGroup";
117+
Subscription subscription = await armClient.GetDefaultSubscriptionAsync();
118+
ResourceGroup rg = await subscription.GetResourceGroups().GetAsync(rgName);
119+
await foreach (VirtualMachine vm in rg.GetVirtualMachines())
120+
{
121+
//previously we would have to take the resourceGroupName and the vmName from the vm object
122+
//and pass those into the powerOff method as well as we would need to execute that on a separate compute client
123+
await vm.PowerOffAsync(true);
124+
}
125+
#endregion Snippet:Readme_LoopVms
126+
}
127+
128+
[Test]
129+
[Ignore("Only verifying that the sample builds")]
130+
public async Task PuttingItAllTogether()
131+
{
132+
#region Snippet:Readme_PuttingItAllTogether
133+
// First we construct our armClient
134+
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
135+
136+
// Next we get a resource group object
137+
// ResourceGroup is a [Resource] object from above
138+
Subscription subscription = await armClient.GetDefaultSubscriptionAsync();
139+
ResourceGroup resourceGroup = await subscription.GetResourceGroups().GetAsync("myRgName");
140+
141+
// Next we get the collection for the virtual machines
142+
// vmCollection is a [Resource]Collection object from above
143+
VirtualMachineCollection vmCollection = resourceGroup.GetVirtualMachines();
144+
145+
// Next we loop over all vms in the collection
146+
// Each vm is a [Resource] object from above
147+
await foreach (VirtualMachine vm in vmCollection)
148+
{
149+
// We access the [Resource]Data properties from vm.Data
150+
if (!vm.Data.Tags.ContainsKey("owner"))
151+
{
152+
// We can also access all operations from vm since it is already scoped for us
153+
await vm.AddTagAsync("owner", "tagValue");
154+
}
155+
}
156+
#endregion Snippet:Readme_PuttingItAllTogether
157+
}
158+
159+
[Test]
160+
[Ignore("Only verifying that the sample builds")]
161+
public async Task ManageAvailabilitySetOld()
162+
{
163+
#region Snippet:Readme_ManageAvailabilitySetOld
164+
ResourceIdentifier id = new ResourceIdentifier("/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet");
165+
// We construct a new armClient to work with
166+
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
167+
// Next we get the specific subscription this resource belongs to
168+
Subscription subscription = await armClient.GetSubscriptions().GetAsync(id.SubscriptionId);
169+
// Next we get the specific resource group this resource belongs to
170+
ResourceGroup resourceGroup = await subscription.GetResourceGroups().GetAsync(id.ResourceGroupName);
171+
// Finally we get the resource itself
172+
// Note: for this last step in this example, Azure.ResourceManager.Compute is needed
173+
AvailabilitySet availabilitySet = await resourceGroup.GetAvailabilitySets().GetAsync(id.Name);
174+
#endregion Snippet:Readme_ManageAvailabilitySetOld
175+
}
176+
177+
[Test]
178+
[Ignore("Only verifying that the sample builds")]
179+
public async Task ManageAvailabilitySetNow()
180+
{
181+
#region Snippet:Readme_ManageAvailabilitySetNow
182+
ResourceIdentifier resourceId = new ResourceIdentifier("/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Compute/availabilitySets/ws2021availSet");
183+
// We construct a new armClient to work with
184+
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
185+
// Next we get the AvailabilitySet resource client from the armClient
186+
// The method takes in a ResourceIdentifier but we can use the implicit cast from string
187+
AvailabilitySet availabilitySet = armClient.GetAvailabilitySet(resourceId);
188+
// At this point availabilitySet.Data will be null and trying to access it will throw
189+
// If we want to retrieve the objects data we can simply call get
190+
availabilitySet = await availabilitySet.GetAsync();
191+
// we now have the data representing the availabilitySet
192+
Console.WriteLine(availabilitySet.Data.Name);
193+
#endregion Snippet:Readme_ManageAvailabilitySetNow
194+
}
195+
196+
[Test]
197+
[Ignore("Only verifying that the sample builds")]
198+
public async Task ManageAvailabilitySetPieces()
199+
{
200+
#region Snippet:Readme_ManageAvailabilitySetPieces
201+
string subscriptionId = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
202+
string resourceGroupName = "workshop2021-rg";
203+
string availabilitySetName = "ws2021availSet";
204+
ResourceIdentifier resourceId = AvailabilitySet.CreateResourceIdentifier(subscriptionId, resourceGroupName, availabilitySetName);
205+
// We construct a new armClient to work with
206+
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
207+
// Next we get the AvailabilitySet resource client from the armClient
208+
// The method takes in a ResourceIdentifier but we can use the implicit cast from string
209+
AvailabilitySet availabilitySet = armClient.GetAvailabilitySet(resourceId);
210+
// At this point availabilitySet.Data will be null and trying to access it will throw
211+
// If we want to retrieve the objects data we can simply call get
212+
availabilitySet = await availabilitySet.GetAsync();
213+
// we now have the data representing the availabilitySet
214+
Console.WriteLine(availabilitySet.Data.Name);
215+
#endregion Snippet:Readme_ManageAvailabilitySetPieces
216+
}
125217
}
126218
}

sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample2_ManagingResourceGroups.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public async Task ListAllResourceGroups()
7979
Subscription subscription = await armClient.GetDefaultSubscriptionAsync();
8080
// Now we get a ResourceGroup collection for that subscription
8181
ResourceGroupCollection rgCollection = subscription.GetResourceGroups();
82-
// With GetAllAsync(), we can get a list of the resources in the collection
83-
await foreach (ResourceGroup rg in rgCollection.GetAllAsync())
82+
// We can then iterate over this collection to get the resources in the collection
83+
await foreach (ResourceGroup rg in rgCollection)
8484
{
8585
Console.WriteLine(rg.Data.Name);
8686
}

0 commit comments

Comments
 (0)