-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(network): samples for track2 network mgmt sdk (#23360)
1. change version to `1.0.0-preview.3` 2. update `README.md` and `CHANGELOG.md` 3. add sample test cases and docs with code snippets AB#5578
- Loading branch information
Showing
7 changed files
with
575 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
sdk/network/Azure.ResourceManager.Network/samples/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
page_type: sample | ||
languages: | ||
- csharp | ||
products: | ||
- azure | ||
- azure-resource-manager | ||
name: Azure.ResourceManager.Network samples for .NET | ||
description: Samples for the Azure.ResourceManager.Network client library | ||
--- | ||
|
||
# Azure.ResourceManager.Network Samples | ||
|
||
- [Managing Virtual Networks](https://github.com/Azure/azure-sdk-for-net/blob/feature/mgmt-track2-network/sdk/network/Azure.ResourceManager.Network/samples/Sample1_ManagingVirtualNetworks.md) | ||
- [Managing Network Interfaces](https://github.com/Azure/azure-sdk-for-net/blob/feature/mgmt-track2-network/sdk/network/Azure.ResourceManager.Network/samples/Sample2_ManagingNetworkInterfaces.md) |
110 changes: 110 additions & 0 deletions
110
...etwork/Azure.ResourceManager.Network/samples/Sample1_ManagingVirtualNetworks.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Example: Managing the virtual networks | ||
|
||
>Note: Before getting started with the samples, go through the [prerequisites](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/resourcemanager/Azure.ResourceManager#prerequisites). | ||
Namespaces for this example: | ||
|
||
```C# Snippet:Manage_Networks_Namespaces | ||
using System; | ||
using System.Threading.Tasks; | ||
using Azure.Identity; | ||
using Azure.ResourceManager.Network.Models; | ||
using Azure.ResourceManager.Resources; | ||
using Azure.ResourceManager.Resources.Models; | ||
using NUnit.Framework; | ||
``` | ||
|
||
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: | ||
|
||
```C# Snippet:Readme_DefaultSubscription | ||
ArmClient 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. | ||
|
||
```C# Snippet:Readme_GetResourceGroupContainer | ||
ResourceGroupContainer rgContainer = subscription.GetResourceGroups(); | ||
// 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.CreateOrUpdateAsync(rgName, new ResourceGroupData(location)); | ||
``` | ||
|
||
Now that we have the resource group created, we can manage the virtual networks inside this resource group. | ||
|
||
***Create a virtual network*** | ||
|
||
```C# Snippet:Managing_Networks_CreateAVirtualNetwork | ||
VirtualNetworkContainer virtualNetworkContainer = resourceGroup.GetVirtualNetworks(); | ||
|
||
string vnetName = "myVnet"; | ||
|
||
// Use the same location as the resource group | ||
var input = new VirtualNetworkData() | ||
{ | ||
Location = resourceGroup.Data.Location, | ||
AddressSpace = new AddressSpace() | ||
{ | ||
AddressPrefixes = { "10.0.0.0/16", } | ||
}, | ||
DhcpOptions = new DhcpOptions() | ||
{ | ||
DnsServers = { "10.1.1.1", "10.1.2.4" } | ||
}, | ||
Subnets = { new SubnetData() { Name = "mySubnet", AddressPrefix = "10.0.1.0/24", } } | ||
}; | ||
|
||
VirtualNetwork vnet = await virtualNetworkContainer.CreateOrUpdateAsync(vnetName, input); | ||
``` | ||
|
||
***List all virtual networks*** | ||
|
||
```C# Snippet:Managing_Networks_ListAllVirtualNetworks | ||
VirtualNetworkContainer virtualNetworkContainer = resourceGroup.GetVirtualNetworks(); | ||
|
||
AsyncPageable<VirtualNetwork> response = virtualNetworkContainer.GetAllAsync(); | ||
await foreach (VirtualNetwork virtualNetwork in response) | ||
{ | ||
Console.WriteLine(virtualNetwork.Data.Name); | ||
} | ||
``` | ||
|
||
***Get a virtual network*** | ||
|
||
```C# Snippet:Managing_Networks_GetAVirtualNetwork | ||
VirtualNetworkContainer virtualNetworkContainer = resourceGroup.GetVirtualNetworks(); | ||
|
||
VirtualNetwork virtualNetwork = await virtualNetworkContainer.GetAsync("myVnet"); | ||
Console.WriteLine(virtualNetwork.Data.Name); | ||
``` | ||
|
||
***Try to get a virtual network if it exists*** | ||
|
||
```C# Snippet:Managing_Networks_GetAVirtualNetworkIfExists | ||
VirtualNetworkContainer virtualNetworkContainer = resourceGroup.GetVirtualNetworks(); | ||
|
||
VirtualNetwork virtualNetwork = await virtualNetworkContainer.GetIfExistsAsync("foo"); | ||
if (virtualNetwork != null) | ||
{ | ||
Console.WriteLine(virtualNetwork.Data.Name); | ||
} | ||
|
||
if (await virtualNetworkContainer.CheckIfExistsAsync("bar")) | ||
{ | ||
Console.WriteLine("Virtual network 'bar' exists."); | ||
} | ||
``` | ||
|
||
***Delete a virtual network*** | ||
|
||
```C# Snippet:Managing_Networks_DeleteAVirtualNetwork | ||
VirtualNetworkContainer virtualNetworkContainer = resourceGroup.GetVirtualNetworks(); | ||
|
||
VirtualNetwork virtualNetwork = await virtualNetworkContainer.GetAsync("myVnet"); | ||
await virtualNetwork.DeleteAsync(); | ||
``` | ||
|
||
## Next steps | ||
|
||
Take a look at the [Managing Network Interfaces](https://github.com/Azure/azure-sdk-for-net/blob/feature/mgmt-track2-network/sdk/network/Azure.ResourceManager.Network/samples/Sample2_ManagingNetworkInterfaces.md) samples. |
Oops, something went wrong.