Skip to content

Commit

Permalink
docs(network): samples for track2 network mgmt sdk (#23360)
Browse files Browse the repository at this point in the history
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
archerzz authored Aug 19, 2021
1 parent 0cc5b62 commit bf2e05c
Show file tree
Hide file tree
Showing 7 changed files with 575 additions and 28 deletions.
35 changes: 17 additions & 18 deletions sdk/network/Azure.ResourceManager.Network/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Release History

## 1.0.0-preview.3 (2021-07-30)

Initial track2 network SDK.
## 1.0.0-preview.3 (Unreleased)

## 1.0.0-preview.2 (2020-09-23)

Expand All @@ -28,13 +26,15 @@ This is a Public Preview version, so expect incompatible changes in subsequent r
### Migration from Previous Version of Azure Management SDK

#### Package Name

The package name has been changed from `Microsoft.Azure.Management.Network` to `Azure.ResourceManager.Network`

#### Management Client Changes

Example: Create a VNet:

Before upgrade:

```csharp
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
Expand Down Expand Up @@ -64,38 +64,36 @@ vnet = await networkClient.VirtualNetworks
```

After upgrade:

```csharp
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager.Network;
using Azure.ResourceManager.Network.Models;

var networkClient = new NetworkManagementClient(subscriptionId, new DefaultAzureCredential());
var virtualNetworksOperations = networkClient.VirtualNetworks;
var armClient = new ArmClient(new DefaultAzureCredential());
var resourceGroup = (await armClient.DefaultSubscription.GetResourceGroups().GetAsync("abc")).Value;
var virtualNetworkContainer = resourceGroup.GetVirtualNetworks();

// Create VNet
var vnet = new VirtualNetwork()
var vnet = new VirtualNetworkData()
{
Location = "westus",
AddressSpace = new AddressSpace() { AddressPrefixes = new List<string>() { "10.0.0.0/16" } },
Subnets = new List<Subnet>()
{
new Subnet()
{
Name = "mySubnet",
AddressPrefix = "10.0.0.0/24",
}
},
};
vnet.AddressSpace.AddressPrefixes.Add("10.0.0.0/16");
vnet.Subnets.Add(new SubnetData {
Name = "mySubnet",
AddressPrefix = "10.0.0.0/24",
});

var response = await virtualNetworksOperations.StartCreateOrUpdateAsync(resourceGroup, vmName + "_vent", vnet);
vnet = await response.WaitForCompletionAsync();
var virtualNetwork = (await virtualNetworkContainer.CreateOrUpdateAsync("_vent", vnet)).Value;
```

#### Object Model Changes

Example: Create a IpsecPolicy Model

Before upgrade:

```csharp
var policy = new IpsecPolicy()
{
Expand All @@ -111,6 +109,7 @@ var policy = new IpsecPolicy()
```

After upgrade:

```csharp
var policy = new IpsecPolicy(
300,
Expand Down
45 changes: 35 additions & 10 deletions sdk/network/Azure.ResourceManager.Network/README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
# Azure Network Management client library for .NET

This package follows the [new Azure SDK guidelines](https://azure.github.io/azure-sdk/general_introduction.html) which provide a number of core capabilities that are shared amongst all Azure SDKs, including the intuitive Azure Identity library, an HTTP Pipeline with custom policies, error-handling, distributed tracing, and much more.
This package follows the [new Azure SDK guidelines](https://azure.github.io/azure-sdk/general_introduction.html) which provide a number of core capabilities that are shared amongst all Azure SDKs, including:

## Getting started
- The intuitive Azure Identity library.
- An HTTP pipeline with custom policies.
- Error handling.
- Distributed tracing.

## Getting started

### Install the package

Install the Azure Network management library for .NET with [NuGet](https://www.nuget.org/):

```PowerShell
Install-Package Azure.ResourceManager.Network -Version 1.0.0-preview.2
Install-Package Azure.ResourceManager.Network -Version 1.0.0-preview.3
```

### Prerequisites

* You must have an [Azure subscription](https://azure.microsoft.com/free/dotnet/)
Set up a way to authenticate to Azure with Azure Identity.

Some options are:

- Through the [Azure CLI Login](https://docs.microsoft.com/cli/azure/authenticate-azure-cli).
- Via [Visual Studio](https://docs.microsoft.com/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#authenticating-via-visual-studio).
- Setting [Environment Variables](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/AuthUsingEnvironmentVariables.md).

More information and different authentication approaches using Azure Identity can be found in [this document](https://docs.microsoft.com/dotnet/api/overview/azure/identity-readme?view=azure-dotnet).

### Authenticate the Client

To create an authenticated client and start interacting with Azure resources, please see the [quickstart guide here](https://github.com/Azure/azure-sdk-for-net/blob/main/doc/mgmt_preview_quickstart.md)
The default option to create an authenticated client is to use `DefaultAzureCredential`. Since all management APIs go through the same endpoint, in order to interact with resources, only one top-level `ArmClient` has to be created.

To authenticate to Azure and create an `ArmClient`, do the following:

```csharp
using Azure.Identity;

// Code omitted for brevity
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
```

Additional documentation for the `Azure.Identity.DefaultAzureCredential` class can be found in [this document](https://docs.microsoft.com/dotnet/api/azure.identity.defaultazurecredential).

## Key concepts

Expand All @@ -35,16 +60,16 @@ Documentation is available to help you learn how to use this package
## Examples

Code samples for using the management library for .NET can be found in the following locations

- [.NET Management Library Code Samples](https://docs.microsoft.com/samples/browse/?branch=master&languages=csharp&term=managing%20using%20Azure%20.NET%20SDK)

## Troubleshooting

- File an issue via [Github
Issues](https://github.com/Azure/azure-sdk-for-net/issues)
- Check [previous
- If you find a bug or have a suggestion, file an issue via [GitHub issues](https://github.com/Azure/azure-sdk-for-net/issues) and make sure you add the "Preview" label to the issue.
- If you need help, check [previous
questions](https://stackoverflow.com/questions/tagged/azure+.net)
or ask new ones on Stack Overflow using azure and .net tags.

or ask new ones on StackOverflow using azure and .NET tags.
- If having trouble with authentication, go to [DefaultAzureCredential documentation](https://docs.microsoft.com/dotnet/api/azure.identity.defaultazurecredential?view=azure-dotnet)

## Next steps

Expand Down
15 changes: 15 additions & 0 deletions sdk/network/Azure.ResourceManager.Network/samples/README.md
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)
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.
Loading

0 comments on commit bf2e05c

Please sign in to comment.