From 6c063f6b76f2ef0c568c296b92be8fff5e879108 Mon Sep 17 00:00:00 2001 From: Arthur Ma Date: Fri, 29 Sep 2023 19:07:42 +0800 Subject: [PATCH] Update the migration guide. (#38961) --- .../Azure.ResourceManager.sln | 14 +- .../Azure.ResourceManager/CHANGELOG.md | 2 +- .../Azure.ResourceManager/README.md | 4 +- .../docs/MigrationGuide.md | 432 +++++++++++++++--- .../{samples => docs}/Sample1_HelloWorld.md | 2 +- .../Sample1_HelloWorldAsync.md | 2 +- .../Sample2_ManagingResourceGroups.md | 2 + .../Sample3_CreatingAVirtualNetwork.md | 3 + .../{samples => docs}/Sample4_MultiTenant.md | 2 +- .../{samples/README.md => docs/Samples.md} | 10 +- .../Azure.ResourceManager.Samples.csproj | 2 + .../DocSamples}/AuxiliaryPoilcy.cs | 7 +- .../samples/DocSamples/MigrationGuide.cs | 347 ++++++++++++++ .../Samples => samples/DocSamples}/Readme.cs | 7 +- .../DocSamples}/Sample1_HelloWorld.cs | 7 +- .../DocSamples}/Sample1_HelloWorldAsync.cs | 7 +- .../Sample2_ManagingResourceGroups.cs | 7 +- .../Sample3_CreatingAVirtualNetwork.cs | 7 +- .../DocSamples}/Sample4_MultiTenant.cs | 5 +- .../DocSamples}/Sample5_GenericResource.cs | 7 +- .../tests/Samples/MigrationGuide.cs | 125 ----- .../tests/Samples/README.md | 3 - 22 files changed, 782 insertions(+), 222 deletions(-) rename sdk/resourcemanager/Azure.ResourceManager/{samples => docs}/Sample1_HelloWorld.md (96%) rename sdk/resourcemanager/Azure.ResourceManager/{samples => docs}/Sample1_HelloWorldAsync.md (96%) rename sdk/resourcemanager/Azure.ResourceManager/{samples => docs}/Sample2_ManagingResourceGroups.md (95%) rename sdk/resourcemanager/Azure.ResourceManager/{samples => docs}/Sample3_CreatingAVirtualNetwork.md (95%) rename sdk/resourcemanager/Azure.ResourceManager/{samples => docs}/Sample4_MultiTenant.md (98%) rename sdk/resourcemanager/Azure.ResourceManager/{samples/README.md => docs/Samples.md} (68%) rename sdk/resourcemanager/Azure.ResourceManager/{tests/Samples => samples/DocSamples}/AuxiliaryPoilcy.cs (83%) create mode 100644 sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/MigrationGuide.cs rename sdk/resourcemanager/Azure.ResourceManager/{tests/Samples => samples/DocSamples}/Readme.cs (98%) rename sdk/resourcemanager/Azure.ResourceManager/{tests/Samples => samples/DocSamples}/Sample1_HelloWorld.cs (93%) rename sdk/resourcemanager/Azure.ResourceManager/{tests/Samples => samples/DocSamples}/Sample1_HelloWorldAsync.cs (92%) rename sdk/resourcemanager/Azure.ResourceManager/{tests/Samples => samples/DocSamples}/Sample2_ManagingResourceGroups.cs (96%) rename sdk/resourcemanager/Azure.ResourceManager/{tests/Samples => samples/DocSamples}/Sample3_CreatingAVirtualNetwork.cs (86%) rename sdk/resourcemanager/Azure.ResourceManager/{tests/Samples => samples/DocSamples}/Sample4_MultiTenant.cs (92%) rename sdk/resourcemanager/Azure.ResourceManager/{tests/Samples => samples/DocSamples}/Sample5_GenericResource.cs (98%) delete mode 100644 sdk/resourcemanager/Azure.ResourceManager/tests/Samples/MigrationGuide.cs delete mode 100644 sdk/resourcemanager/Azure.ResourceManager/tests/Samples/README.md diff --git a/sdk/resourcemanager/Azure.ResourceManager/Azure.ResourceManager.sln b/sdk/resourcemanager/Azure.ResourceManager/Azure.ResourceManager.sln index dc181b5f7efe1..9cf6e1762a845 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/Azure.ResourceManager.sln +++ b/sdk/resourcemanager/Azure.ResourceManager/Azure.ResourceManager.sln @@ -9,20 +9,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.ResourceManager", "sr EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.ResourceManager.Tests", "tests\Azure.ResourceManager.Tests.csproj", "{83E7651C-7FBE-45AA-B740-31FE9A3E44C7}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{217C9506-A0E0-4486-96FF-DAC0DF92CD1F}" - ProjectSection(SolutionItems) = preProject - samples\README.md = samples\README.md - samples\Sample1_HelloWorld.md = samples\Sample1_HelloWorld.md - samples\Sample1_HelloWorldAsync.md = samples\Sample1_HelloWorldAsync.md - samples\Sample2_ManagingResourceGroups.md = samples\Sample2_ManagingResourceGroups.md - samples\Sample3_CreatingAVirtualNetwork.md = samples\Sample3_CreatingAVirtualNetwork.md - EndProjectSection -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{4E159B41-7E02-4039-983A-F94EBF41D3C1}" ProjectSection(SolutionItems) = preProject docs\AuthUsingEnvironmentVariables.md = docs\AuthUsingEnvironmentVariables.md docs\CONTRIBUTING.md = docs\CONTRIBUTING.md docs\MigrationGuide.md = docs\MigrationGuide.md + docs\README.md = docs\README.md + docs\Sample1_HelloWorld.md = docs\Sample1_HelloWorld.md + docs\Sample1_HelloWorldAsync.md = docs\Sample1_HelloWorldAsync.md + docs\Sample2_ManagingResourceGroups.md = docs\Sample2_ManagingResourceGroups.md + docs\Sample3_CreatingAVirtualNetwork.md = docs\Sample3_CreatingAVirtualNetwork.md EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Core", "..\..\core\Azure.Core\src\Azure.Core.csproj", "{E0F3952A-D1C7-4CE4-B2A8-5C34945EBED8}" diff --git a/sdk/resourcemanager/Azure.ResourceManager/CHANGELOG.md b/sdk/resourcemanager/Azure.ResourceManager/CHANGELOG.md index 9f3eb4856447f..273157f0234e8 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/CHANGELOG.md +++ b/sdk/resourcemanager/Azure.ResourceManager/CHANGELOG.md @@ -202,7 +202,7 @@ Minor changes since the public beta release: ### Breaking Changes -- Removed DefaultSubscription property from ArmClient and added GetDefaultSubscription()/GetDefaultSubscriptionAsync() methods. See the [Hello World examples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/samples/README.md) of how to use the new methods to get the default subscription. +- Removed DefaultSubscription property from ArmClient and added GetDefaultSubscription()/GetDefaultSubscriptionAsync() methods. See the [Hello World examples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/Samples.md) of how to use the new methods to get the default subscription. - Renamed [Resource]Container to [Resource]Collection and added the IEnumerable and IAsyncEnumerable interfaces to them making it easier to iterate over the list in the simple case. ## 1.0.0-beta.4 (2021-09-28) diff --git a/sdk/resourcemanager/Azure.ResourceManager/README.md b/sdk/resourcemanager/Azure.ResourceManager/README.md index ac3acef733743..87ffd68f09508 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/README.md +++ b/sdk/resourcemanager/Azure.ResourceManager/README.md @@ -457,8 +457,8 @@ To run test with code coverage and auto generate an html report with just a sing ## Next steps ### More sample code -- [Managing Resource Groups](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/samples/Sample2_ManagingResourceGroups.md) -- [Creating a Virtual Network](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/samples/Sample3_CreatingAVirtualNetwork.md) +- [Managing Resource Groups](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/Sample2_ManagingResourceGroups.md) +- [Creating a Virtual Network](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/Sample3_CreatingAVirtualNetwork.md) - [.NET Management Library Code Samples](https://docs.microsoft.com/samples/browse/?branch=master&languages=csharp&term=managing%20using%20Azure%20.NET%20SDK) ### Other Documentation diff --git a/sdk/resourcemanager/Azure.ResourceManager/docs/MigrationGuide.md b/sdk/resourcemanager/Azure.ResourceManager/docs/MigrationGuide.md index 82a1d0ce8e34a..126c6e2adc90c 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/docs/MigrationGuide.md +++ b/sdk/resourcemanager/Azure.ResourceManager/docs/MigrationGuide.md @@ -1,27 +1,45 @@ # Migrating from old to new management SDK -There are several differences between the old sdk and this new sdk. Here's an example of how to create a Virtual Machine with both SDKs: +If you are using the old Azure management SDK for .Net, you may need to make some changes to your code to take advantage of the new features and improvements in the new Track 2 Azure management SDK. Here are some examples that show you how to migrate your code to the new Azure management SDK for .Net. -## Create a Virtual Machine example +* [Migrating from Track 1 SDK to Track 2 SDK](#migrating-from-track-1-sdk-to-track-2-sdk) + * [Import the namespaces](#import-the-namespaces) + * [Setting up the clients](#setting-up-the-clients) + * [Create a Resource Group](#create-a-resource-group) + * [Create an Availability Set](#create-an-availability-set) + * [Create a Security Group](#create-a-security-group) + * [Create a Virtual Network and Subnet](#create-a-virtual-network-and-subnet) + * [Create a Network Interface](#create-a-network-interface) + * [Create a Virtual Machine](#create-a-virtual-machine) +* [Migrating from Track 1 Fluent SDK to Track 2 SDK](#migrating-from-track-1-fluent-sdk-to-track-2-sdk) + * [Import the namespaces](#import-the-namespaces-1) + * [Setting up the clients](#setting-up-the-clients-1) + * [Create a Security Group](#create-a-security-group-1) + * [Create a Virtual Network and Subnet](#create-a-virtual-network-and-subnet-1) + * [Create a Virtual Machine](#create-a-virtual-machine-1) + * [List all Virtual Networks](#list-all-virtual-networks) + * [Delete a Virtual Network](#delete-a-virtual-network) + +## Migrating from Track 1 SDK to Track 2 SDK + +The old Track 1 SDK uses package names that start with `Microsoft.Azure.Management` and without `Fluent` suffix. +To assist you with the migration process, we have prepared some examples for you. ### Import the namespaces -#### Old (Microsoft.Azure.Management._) + +**Old (Microsoft.Azure.Management._)** ```C# using Microsoft.Azure.Management.Compute; using Microsoft.Azure.Management.Compute.Models; using Microsoft.Azure.Management.Network; using Microsoft.Azure.Management.Network.Models; using Microsoft.Azure.Management.ResourceManager; -using Microsoft.Rest; -using System; -using System.Threading.Tasks; +... ``` -#### New (Azure.ResourceManager._) +**New (Azure.ResourceManager._)** ```C# Snippet:Using_Statements -using System; -using System.Linq; -using Azure.Identity; using Azure.Core; +using Azure.Identity; using Azure.ResourceManager; using Azure.ResourceManager.Compute; using Azure.ResourceManager.Compute.Models; @@ -32,21 +50,23 @@ using Azure.ResourceManager.Resources.Models; ``` ### Setting up the clients -#### Old + +**Old** ```C# ServiceClientCredentials credentials = getMyCredentials(); ComputeManagementClient computeClient = new ComputeManagementClient(credentials); NetworkManagementClient networkClient = new NetworkManagementClient(credentials); ManagedServiceIdentityClient managedServiceIdentityClient = new ManagedServiceIdentityClient(credentials); ``` -#### New +**New** ```C# Snippet:Construct_Client ArmClient client = new ArmClient(new DefaultAzureCredential()); ``` -As you can see, authentication is now handled by Azure.Identity, and now just a single client is needed, from which you can get the default subscription and start managing your resources. +As you can see, now authentication is handled by Azure.Identity, and just a single client is needed, from which you can get the default subscription and start managing your resources. ### Create a Resource Group -#### Old + +**Old** ```C# ServiceClientCredentials credentials = getMyCredentials(); ResourceManagementClient resourcesClient = new ResourceManagementClient(credentials); @@ -61,15 +81,14 @@ resourcesClient.ResourceGroups.CreateOrUpdate( Tags = new Dictionary() { { rgName, DateTime.UtcNow.ToString("u") } } }); ``` -#### New +**New** ```C# Snippet:Create_ResourceGroup SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync(); ResourceGroupCollection resourceGroups = subscription.GetResourceGroups(); -AzureLocation location = AzureLocation.WestUS2; string resourceGroupName = "QuickStartRG"; -ResourceGroupData resourceGroupData = new ResourceGroupData(location); +ResourceGroupData resourceGroupData = new ResourceGroupData(AzureLocation.WestUS2); ArmOperation resourceGroupOperation = await resourceGroups.CreateOrUpdateAsync(WaitUntil.Completed, resourceGroupName, resourceGroupData); ResourceGroupResource resourceGroup = resourceGroupOperation.Value; ``` @@ -78,7 +97,8 @@ The main difference is that the previous libraries represent all operations as f The new SDK also provides some common classes to represent commonly-used constructs, like `Location`, and allows you to use them directly throughout the APIs, making it easier to discover how to properly configure resources. ### Create an Availability Set -#### Old + +**Old** ```C# AvailabilitySet inputAvailabilitySet = new AvailabilitySet { @@ -100,19 +120,44 @@ string aSetName = "quickstartvm_aSet"; AvailabilitySet asCreateOrUpdateResponse = computeClient.AvailabilitySets.CreateOrUpdate(rgName,aSetName,inputAvailabilitySet); string aSetID = $"/subscriptions/{computeClient.SubscriptionId}/resourceGroups/{rgName}/providers/Microsoft.Compute/availabilitySets/{aSetName}"; ``` -#### New +**New** ```C# Snippet:Create_AvailabilitySet -string virtualMachineName = "quickstartvm"; -AvailabilitySetData availabilitySetData = new AvailabilitySetData(location); +string availabilitySetName = "QuickstartAvailabilitySet"; + +AvailabilitySetData availabilitySetData = new AvailabilitySetData(resourceGroup.Data.Location); AvailabilitySetCollection availabilitySets = resourceGroup.GetAvailabilitySets(); -ArmOperation availabilitySetOperation = await availabilitySets.CreateOrUpdateAsync(WaitUntil.Completed, virtualMachineName + "_aSet", availabilitySetData); +ArmOperation availabilitySetOperation = await availabilitySets.CreateOrUpdateAsync(WaitUntil.Completed, availabilitySetName + "_aSet", availabilitySetData); AvailabilitySetResource availabilitySet = availabilitySetOperation.Value; ``` Parameters can be specified via the `AvailabilitySetData` object, in here, the basic default only requires the location. The availability set is created using the AvailabilitySetsCollection returned from the `GetAvailabilitySets()` extension method instead of using another client. +### Create a Security Group + +**Old** +```C# +string nsgName = vmName + "_nsg"; +NetworkSecurityGroup nsgParameters = new NetworkSecurityGroup() +{ + Location = location +}; + +NetworkSecurityGroup putNSgResponse = networkClient.NetworkSecurityGroups.CreateOrUpdate(rgName, nsgName, nsgParameters); +NetworkSecurityGroup nsg = networkClient.NetworkSecurityGroups.Get(rgName, nsgName); +``` +**New** +```C# Snippet:Create_NetworkSecurityGroup +string networkSecurityGroupName = "QuickstartNsg"; + +NetworkSecurityGroupData networkSecurityGroupData = new NetworkSecurityGroupData() { Location = resourceGroup.Data.Location }; +NetworkSecurityGroupCollection networkSecurityGroups = resourceGroup.GetNetworkSecurityGroups(); +ArmOperation networkSecurityGroupOperation = await networkSecurityGroups.CreateOrUpdateAsync(WaitUntil.Completed, networkSecurityGroupName, networkSecurityGroupData); +NetworkSecurityGroupResource networkSecurityGroup = networkSecurityGroupOperation.Value; +``` + ### Create a Virtual Network and Subnet -#### Old + +**Old** ```C# string vnetName = vmName + "_vnet"; string subnetName = "mySubnet"; @@ -139,10 +184,10 @@ VirtualNetwork vnet = new VirtualNetwork() VirtualNetwork putVnetResponse = networkClient.VirtualNetworks.CreateOrUpdate(rgName, vnetName, vnet); VirtualNetwork subnetResponse = networkClient.Subnets.Get(rgName, vnetName, subnetName); ``` -#### New +**New** ```C# Snippet:Create_Vnet_and_Subnet -string virtualNetworkName = "MYVM" + "_vnet"; -string subnetName = "mySubnet"; +string virtualNetworkName = "QuickstartVnet"; +string subnetName = "QuickstartSubnet"; VirtualNetworkData virtualNetworkData = new VirtualNetworkData() { @@ -155,37 +200,17 @@ VirtualNetworkData virtualNetworkData = new VirtualNetworkData() } } }; -VirtualNetworkCollection virtualNetworks = resourceGroup.GetVirtualNetworks(); virtualNetworkData.AddressPrefixes.Add("10.0.0.0/16"); +VirtualNetworkCollection virtualNetworks = resourceGroup.GetVirtualNetworks(); ArmOperation virtualNetworkOperation = await virtualNetworks.CreateOrUpdateAsync(WaitUntil.Completed, virtualNetworkName, virtualNetworkData); VirtualNetworkResource virtualNetwork = virtualNetworkOperation.Value; ``` -In both libraries, subnets are defined inside virtual networks, however, with the new SDK you can get a subnets collection using `.GetSubnets()`, and from there create any subnet in the virtual network from which the method is being called. - -### Create a Security Group -#### Old -```C# -string nsgName = vmName + "_nsg"; -NetworkSecurityGroup nsgParameters = new NetworkSecurityGroup() -{ - Location = location -}; - -NetworkSecurityGroup putNSgResponse = networkClient.NetworkSecurityGroups.CreateOrUpdate(rgName, nsgName, nsgParameters); -NetworkSecurityGroup nsg = networkClient.NetworkSecurityGroups.Get(rgName, nsgName); -``` -#### New -```C# Snippet:Create_NetworkSecurityGroup -string networkSecurityGroupName = virtualMachineName + "_nsg"; -NetworkSecurityGroupData networkSecurityGroupData = new NetworkSecurityGroupData() { Location = location }; -NetworkSecurityGroupCollection networkSecurityGroups = resourceGroup.GetNetworkSecurityGroups(); -ArmOperation networkSecurityGroupOperation = await networkSecurityGroups.CreateOrUpdateAsync(WaitUntil.Completed, networkSecurityGroupName, networkSecurityGroupData); -NetworkSecurityGroupResource networkSecurityGroup = networkSecurityGroupOperation.Value; -``` +In both libraries, subnets are defined inside virtual networks, however, with the new SDK you can get a subnet collection using `.GetSubnets()`, and from there create any subnet in the virtual network from which the method is being called. ### Create a Network Interface -#### Old + +**Old** ```C# string nicname = vmName + "_nic"; string ipConfigName = vmName + "_IP"; @@ -212,9 +237,10 @@ NetworkInterface nicParameters = new NetworkInterface() NetworkInterface putNicResponse = networkClient.NetworkInterfaces.CreateOrUpdate(rgName, nicname, nicParameters); NetworkInterface nicResponse = networkClient.NetworkInterfaces.Get(rgName, nicname); ``` -#### New +**New** ```C# Snippet:Create_NetworkInterface -string networkInterfaceName = virtualMachineName + "_nic"; +string networkInterfaceName = "QuickstartNic"; + NetworkInterfaceIPConfigurationData networkInterfaceIPConfiguration = new NetworkInterfaceIPConfigurationData() { Name = "Primary", @@ -223,8 +249,7 @@ NetworkInterfaceIPConfigurationData networkInterfaceIPConfiguration = new Networ PrivateIPAllocationMethod = NetworkIPAllocationMethod.Dynamic, }; -NetworkInterfaceData nicData = new NetworkInterfaceData(); -nicData.Location = location; +NetworkInterfaceData nicData = new NetworkInterfaceData() { Location = resourceGroup.Data.Location }; nicData.IPConfigurations.Add(networkInterfaceIPConfiguration); NetworkInterfaceCollection networkInterfaces = resourceGroup.GetNetworkInterfaces(); ArmOperation networkInterfaceOperation = await networkInterfaces.CreateOrUpdateAsync(WaitUntil.Completed, networkInterfaceName, nicData); @@ -234,7 +259,8 @@ NetworkInterfaceResource networkInterface = networkInterfaceOperation.Value; This step is similar to the old SDK, however, notice that the `CreateOrUpdateAsync()` method returns the network interface that has been created. ### Create a Virtual Machine -#### Old + +**Old** ```C# string vmSize = VirtualMachineSizeTypes.StandardA1V2 VirtualMachine inputVM = new VirtualMachine @@ -285,9 +311,11 @@ inputVM.NetworkProfile = vmNetworkProfile; VirtualMachine vm = VMcomputeClient.VirtualMachines.CreateOrUpdate(rgName, inputVM.Name, inputVM); ``` -#### New +**New** ```C# Snippet:Create_VirtualMachine -VirtualMachineData virutalMachineData = new VirtualMachineData(location) +string virtualMachineName = "QuickstartVm"; + +VirtualMachineData virutalMachineData = new VirtualMachineData(resourceGroup.Data.Location) { OSProfile = new VirtualMachineOSProfile() { @@ -311,10 +339,296 @@ VirtualMachineData virutalMachineData = new VirtualMachineData(location) VirtualMachineCollection virtualMachines = resourceGroup.GetVirtualMachines(); ArmOperation virtualMachineOperation = await virtualMachines.CreateOrUpdateAsync(WaitUntil.Completed, virtualMachineName, virutalMachineData); VirtualMachineResource virtualMachine = virtualMachineOperation.Value; -Console.WriteLine("VirtualMachine ID: " + virtualMachine.Id); ``` Finally, as it can be seen here, from the resource group you can get the Virtual Machine collection and create a new one using the `VirtualMachineData` for the parameters. +## Migrating from Track 1 Fluent SDK to Track 2 SDK + +The old Track 1 Fluent SDK uses package names that start with `Microsoft.Azure.Management` and end with `Fluent` suffix. +To assist you with the migration process, we have prepared some examples for you. + +### Import the namespaces + +**Old (Microsoft.Azure.Management._.Fluent)** +```C# +using Microsoft.Azure.Management.Compute.Fluent; +using Microsoft.Azure.Management.Compute.Fluent.Models; +using Microsoft.Azure.Management.Fluent; +using Microsoft.Azure.Management.Network.Fluent.Models; +using Microsoft.Azure.Management.ResourceManager.Fluent; +using Microsoft.Azure.Management.ResourceManager.Fluent.Core; +... +``` +**New (Azure.ResourceManager._)** +```C# Snippet:Using_Statements +using Azure.Core; +using Azure.Identity; +using Azure.ResourceManager; +using Azure.ResourceManager.Compute; +using Azure.ResourceManager.Compute.Models; +using Azure.ResourceManager.Network; +using Azure.ResourceManager.Network.Models; +using Azure.ResourceManager.Resources; +using Azure.ResourceManager.Resources.Models; +``` + +### Setting up the clients + +**Old** +```C# +var credentials = SdkContext.AzureCredentialsFactory.FromFile("Filename"); +var azure = Azure.Configure() + .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) + .Authenticate(credentials) + .WithDefaultSubscription(); +``` +**New** +```C# Snippet:Construct_CreateClient +string clientId = "CLIENT_ID"; +string clientSecret = "CLIENT_SECRET"; +string tenantId = "TENANT_ID"; +string subscription = "SUBSCRIPTION_ID"; +ClientSecretCredential credential = new ClientSecretCredential(tenantId, clientId, clientSecret); +ArmClient client = new ArmClient(credential, subscription); +``` +As you can see, now authentication is handled by Azure.Identity, and just a single client is needed, from which you can get the default subscription and start managing your resources. + +### Create a Security Group + +**Old** +```C# +var networkNsg = azure.NetworkSecurityGroups + .Define(VNet1BackEndSubnetNsgName) + .WithRegion(Region.USEast) + .WithNewResourceGroup(ResourceGroupName) + .DefineRule("DenyInternetInComing") + .DenyInbound() + .FromAddress("INTERNET") + .FromAnyPort() + .ToAnyAddress() + .ToAnyPort() + .WithAnyProtocol() + .Attach() + .DefineRule("DenyInternetOutGoing") + .DenyOutbound() + .FromAnyAddress() + .FromAnyPort() + .ToAddress("INTERNET") + .ToAnyPort() + .WithAnyProtocol() + .Attach() + .Create(); +``` +**New** +```C# Snippet:Create_Fluent_Nsg +string networkNsgName = "QuickstartNsg"; + +NetworkSecurityGroupData networkNsgData = new NetworkSecurityGroupData() +{ + Location = resourceGroup.Data.Location, + SecurityRules = + { + new SecurityRuleData() + { + Name = "DenyInternetInComing", + Protocol = SecurityRuleProtocol.Asterisk, + SourcePortRange = "*", + DestinationPortRange = "*", + SourceAddressPrefix = "INTERNET", + DestinationAddressPrefix = "*", + Access = SecurityRuleAccess.Deny, + Priority = 100, + Direction = SecurityRuleDirection.Inbound, + }, + new SecurityRuleData() + { + Name = "DenyInternetOutGoing", + Protocol = SecurityRuleProtocol.Asterisk, + SourcePortRange = "*", + DestinationPortRange = "*", + SourceAddressPrefix = "*", + DestinationAddressPrefix = "internet", + Access = SecurityRuleAccess.Deny, + Priority = 200, + Direction = SecurityRuleDirection.Outbound, + } + } +}; +NetworkSecurityGroupCollection networkSecurityGroups = resourceGroup.GetNetworkSecurityGroups(); +ArmOperation networkSecurityGroupOperation = await networkSecurityGroups.CreateOrUpdateAsync(WaitUntil.Completed, networkNsgName, networkNsgData); +NetworkSecurityGroupResource networkSecurityGroup = networkSecurityGroupOperation.Value; +``` + +### Create a Virtual Network and Subnet + +**Old** +```C# +var virtualNetwork = azure.Networks.Define(virtualNetworkName) + .WithRegion(Region.USEast) + .WithExistingResourceGroup(ResourceGroupName) + .WithAddressSpace("192.168.0.0/16") + .WithSubnet("subnet1", "192.168.1.0/24") + .DefineSubnet(subnetName) + .WithAddressPrefix("192.168.2.0/24") + .WithExistingNetworkSecurityGroup(backEndSubnetNsg) + .Attach() + .Create(); +``` +**New** +```C# Snippet:Create_Fluent_Vnet_and_Subnet +string virtualNetworkName = "QuickstartVnet"; +string subnetName = "QuickstartSubnet"; + +VirtualNetworkData virtualNetworkData = new VirtualNetworkData() + { + Location = AzureLocation.EastUS, + AddressPrefixes = { "192.168.0.0/16" }, + Subnets = + { + new SubnetData() + { + AddressPrefix = "192.168.2.0/24", + Name = subnetName, + NetworkSecurityGroup = networkSecurityGroup.Data + }, + new SubnetData() + { + AddressPrefix = "192.168.1.0/24", + Name = "subnet1" + } + }, + }; +VirtualNetworkCollection virtualNetworks = resourceGroup.GetVirtualNetworks(); +ArmOperation virtualNetworkOperation = await virtualNetworks.CreateOrUpdateAsync(WaitUntil.Completed, virtualNetworkName, virtualNetworkData); +VirtualNetworkResource virtualNetwork = virtualNetworkOperation.Value; +``` + +In both libraries, subnets are defined inside virtual networks, however, with the new SDK you can get a subnets collection using `.GetSubnets()`, and from there create any subnet in the virtual network from which the method is being called. + +### Create a Virtual Machine + +**Old** +```C# +var virutalMachine = azure.VirtualMachines.Define(virtualMachineName) + .WithRegion(Region.USEast) + .WithExistingResourceGroup(ResourceGroupName) + .WithExistingPrimaryNetwork(virtualNetwork) + .WithSubnet(subnetName) + .WithPrimaryPrivateIPAddressDynamic() + .WithNewPrimaryPublicIPAddress(publicIpAddress) + .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts) + .WithRootUsername(UserName) + .WithSsh(SshKey) + .WithSize(VirtualMachineSizeTypes.Parse("Standard_D2a_v4")) + .Create(); +``` +**New** +```C# Snippet:Create_Fluent_VirtualMachine +// Create Nic +string networkInterfaceName = "QuickstartNic"; + +NetworkInterfaceData nicData = new NetworkInterfaceData() +{ + Location = AzureLocation.EastUS, + IPConfigurations = + { + new NetworkInterfaceIPConfigurationData() + { + Name = "default-config", + PrivateIPAllocationMethod = NetworkIPAllocationMethod.Dynamic, + Subnet = new SubnetData() { Id = virtualNetwork.Data.Subnets.First().Id }, + } + } +}; +NetworkInterfaceCollection networkInterfaces = resourceGroup.GetNetworkInterfaces(); +ArmOperation networkInterfaceOperation = await networkInterfaces.CreateOrUpdateAsync(WaitUntil.Completed, networkInterfaceName, nicData); +NetworkInterfaceResource networkInterface = networkInterfaceOperation.Value; + +// Create VM +string virtualMachineName = "QuickstartVm"; + +VirtualMachineData virutalMachineData = new VirtualMachineData(AzureLocation.EastUS) +{ + HardwareProfile = new VirtualMachineHardwareProfile() + { + VmSize = VirtualMachineSizeType.StandardD2V3 + }, + StorageProfile = new VirtualMachineStorageProfile() + { + ImageReference = new ImageReference() + { + Publisher = "Canonical", + Offer = "0001-com-ubuntu-server-jammy", + Sku = "22_04-lts-gen2", + Version = "latest", + }, + OSDisk = new VirtualMachineOSDisk(DiskCreateOptionType.FromImage) + { + OSType = SupportedOperatingSystemType.Windows, + Name = "QuickstartVmOSDisk", + Caching = CachingType.ReadOnly, + ManagedDisk = new VirtualMachineManagedDisk() + { + StorageAccountType = StorageAccountType.StandardLrs, + }, + }, + }, + OSProfile = new VirtualMachineOSProfile() + { + AdminUsername = "admin-username", + AdminPassword = "admin-p4$$w0rd", + ComputerName = "computer-name" + }, + NetworkProfile = new VirtualMachineNetworkProfile() + { + NetworkInterfaces = + { + new VirtualMachineNetworkInterfaceReference() + { + Id = networkInterface.Id, + Primary = true, + } + } + }, +}; +VirtualMachineCollection virtualMachines = resourceGroup.GetVirtualMachines(); +ArmOperation virtualMachineOperation = await virtualMachines.CreateOrUpdateAsync(WaitUntil.Completed, virtualMachineName, virutalMachineData); +VirtualMachineResource virtualMachine = virtualMachineOperation.Value; +``` + +Finally, as it can be seen here, from the resource group you can get the Virtual Machine collection and create a new one using the `VirtualMachineData` for the parameters. + +### List all Virtual Networks + +**Old** +```C# +foreach (var virtualNetwork in azure.Networks.ListByResourceGroup(ResourceGroupName)) +{ + // Do something + Console.WriteLine(virtualNetwork.Data.Name); +} +``` +**New** +```C# Snippet:Create_Fluent_ListNetworks +await foreach (VirtualNetworkResource virtualNetwork in resourceGroup.GetVirtualNetworks().GetAllAsync()) +{ + // Do something + Console.WriteLine(virtualNetwork.Data.Name); +} +``` + +### Delete a Virtual Network + +**Old** +```C# +azure.Networks.DeleteById(virtualNetwork.Id); +``` +**New** +```C# Snippet:Create_Fluent_DeleteNetwork +await virtualNetwork.DeleteAsync(WaitUntil.Completed); +``` + ## Next steps -Check out [more examples](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/resourcemanager/Azure.ResourceManager/samples) we have available. + +Check out [more .NET Management Library Code Samples](https://docs.microsoft.com/samples/browse/?branch=master&languages=csharp&term=managing%20using%20Azure%20.NET%20SDK) we have available. diff --git a/sdk/resourcemanager/Azure.ResourceManager/samples/Sample1_HelloWorld.md b/sdk/resourcemanager/Azure.ResourceManager/docs/Sample1_HelloWorld.md similarity index 96% rename from sdk/resourcemanager/Azure.ResourceManager/samples/Sample1_HelloWorld.md rename to sdk/resourcemanager/Azure.ResourceManager/docs/Sample1_HelloWorld.md index c535bbc4a1294..8f23d0d98632f 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/samples/Sample1_HelloWorld.md +++ b/sdk/resourcemanager/Azure.ResourceManager/docs/Sample1_HelloWorld.md @@ -44,4 +44,4 @@ ResourceGroupCollection resourceGroups = subscription.GetResourceGroups(); ``` ## Next stepts -Take a look at the [Managing Resource Groups](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/samples/Sample2_ManagingResourceGroups.md) samples. +Take a look at the [Managing Resource Groups](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/Sample2_ManagingResourceGroups.md) samples. diff --git a/sdk/resourcemanager/Azure.ResourceManager/samples/Sample1_HelloWorldAsync.md b/sdk/resourcemanager/Azure.ResourceManager/docs/Sample1_HelloWorldAsync.md similarity index 96% rename from sdk/resourcemanager/Azure.ResourceManager/samples/Sample1_HelloWorldAsync.md rename to sdk/resourcemanager/Azure.ResourceManager/docs/Sample1_HelloWorldAsync.md index bc84f0a1fa93b..344834b9a3eca 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/samples/Sample1_HelloWorldAsync.md +++ b/sdk/resourcemanager/Azure.ResourceManager/docs/Sample1_HelloWorldAsync.md @@ -46,4 +46,4 @@ ResourceGroupCollection resourceGroups = subscription.GetResourceGroups(); ``` ## Next stepts -Take a look at the [Managing Resource Groups](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/samples/Sample2_ManagingResourceGroups.md) samples. +Take a look at the [Managing Resource Groups](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/Sample2_ManagingResourceGroups.md) samples. diff --git a/sdk/resourcemanager/Azure.ResourceManager/samples/Sample2_ManagingResourceGroups.md b/sdk/resourcemanager/Azure.ResourceManager/docs/Sample2_ManagingResourceGroups.md similarity index 95% rename from sdk/resourcemanager/Azure.ResourceManager/samples/Sample2_ManagingResourceGroups.md rename to sdk/resourcemanager/Azure.ResourceManager/docs/Sample2_ManagingResourceGroups.md index 93c95a8170ad2..f04b13d96e83f 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/samples/Sample2_ManagingResourceGroups.md +++ b/sdk/resourcemanager/Azure.ResourceManager/docs/Sample2_ManagingResourceGroups.md @@ -88,3 +88,5 @@ string resourceGroupName = "myRgName"; ResourceGroupResource resourceGroup = await resourceGroups.GetAsync(resourceGroupName); await resourceGroup.DeleteAsync(WaitUntil.Completed); ``` +## Next stepts +Take a look at the [Creating a Virtual Network](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/Sample3_CreatingAVirtualNetwork.md) samples. \ No newline at end of file diff --git a/sdk/resourcemanager/Azure.ResourceManager/samples/Sample3_CreatingAVirtualNetwork.md b/sdk/resourcemanager/Azure.ResourceManager/docs/Sample3_CreatingAVirtualNetwork.md similarity index 95% rename from sdk/resourcemanager/Azure.ResourceManager/samples/Sample3_CreatingAVirtualNetwork.md rename to sdk/resourcemanager/Azure.ResourceManager/docs/Sample3_CreatingAVirtualNetwork.md index 375383724aa5c..2cafe3244b902 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/samples/Sample3_CreatingAVirtualNetwork.md +++ b/sdk/resourcemanager/Azure.ResourceManager/docs/Sample3_CreatingAVirtualNetwork.md @@ -98,3 +98,6 @@ You can verify that your virtual network now has 2 subnets by doing the followin VirtualNetwork myVNet = await resourceGroup.GetVirtualNetworks().GetAsync(vnetName); Console.WriteLine(myVNet.Data.Subnets.Count); ``` + +## Next stepts +Take a look at the [Authenticate across tenants](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/Sample4_MultiTenant.md) samples. \ No newline at end of file diff --git a/sdk/resourcemanager/Azure.ResourceManager/samples/Sample4_MultiTenant.md b/sdk/resourcemanager/Azure.ResourceManager/docs/Sample4_MultiTenant.md similarity index 98% rename from sdk/resourcemanager/Azure.ResourceManager/samples/Sample4_MultiTenant.md rename to sdk/resourcemanager/Azure.ResourceManager/docs/Sample4_MultiTenant.md index 885965efa0f5a..4bce3b8af4f4f 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/samples/Sample4_MultiTenant.md +++ b/sdk/resourcemanager/Azure.ResourceManager/docs/Sample4_MultiTenant.md @@ -23,7 +23,7 @@ In order to test for multi-tenant, you will need to setup a service principal fo internal class AuxiliaryPoilcy : HttpPipelineSynchronousPolicy { private static string AUTHORIZATION_AUXILIARY_HEADER = "x-ms-authorization-auxiliary"; - string _token; + private string _token; internal AuxiliaryPoilcy(string token) { diff --git a/sdk/resourcemanager/Azure.ResourceManager/samples/README.md b/sdk/resourcemanager/Azure.ResourceManager/docs/Samples.md similarity index 68% rename from sdk/resourcemanager/Azure.ResourceManager/samples/README.md rename to sdk/resourcemanager/Azure.ResourceManager/docs/Samples.md index 267b66490740e..5cc3f9c41ac78 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/samples/README.md +++ b/sdk/resourcemanager/Azure.ResourceManager/docs/Samples.md @@ -11,8 +11,8 @@ description: Samples for the Azure.ResourceManager client library # Azure.ResourceManager Samples -- [Hello World - Getting a subscription](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/samples/Sample1_HelloWorld.md) -- [Hello World - Getting a subscription async](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/samples/Sample1_HelloWorldAsync.md) -- [Managing Resource Groups](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/samples/Sample2_ManagingResourceGroups.md) -- [Creating a virtual network](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/samples/Sample3_CreatingAVirtualNetwork.md) -- [Enable Cross-Tenant Authentication](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/samples/Sample4_MultiTenant.md) +- [Hello World - Getting a subscription](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/Sample1_HelloWorld.md) +- [Hello World - Getting a subscription async](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/Sample1_HelloWorldAsync.md) +- [Managing Resource Groups](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/Sample2_ManagingResourceGroups.md) +- [Creating a virtual network](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/Sample3_CreatingAVirtualNetwork.md) +- [Enable Cross-Tenant Authentication](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/Sample4_MultiTenant.md) diff --git a/sdk/resourcemanager/Azure.ResourceManager/samples/Azure.ResourceManager.Samples.csproj b/sdk/resourcemanager/Azure.ResourceManager/samples/Azure.ResourceManager.Samples.csproj index 153d0d172d6eb..0c958d45472ee 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/samples/Azure.ResourceManager.Samples.csproj +++ b/sdk/resourcemanager/Azure.ResourceManager/samples/Azure.ResourceManager.Samples.csproj @@ -6,5 +6,7 @@ + + diff --git a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/AuxiliaryPoilcy.cs b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/AuxiliaryPoilcy.cs similarity index 83% rename from sdk/resourcemanager/Azure.ResourceManager/tests/Samples/AuxiliaryPoilcy.cs rename to sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/AuxiliaryPoilcy.cs index aefd744ed3e3f..65a037a73d0b7 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/AuxiliaryPoilcy.cs +++ b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/AuxiliaryPoilcy.cs @@ -1,4 +1,7 @@ -using Azure.Core; +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Azure.Core; using Azure.Core.Pipeline; namespace Azure.ResourceManager.Tests.Samples @@ -7,7 +10,7 @@ namespace Azure.ResourceManager.Tests.Samples internal class AuxiliaryPoilcy : HttpPipelineSynchronousPolicy { private static string AUTHORIZATION_AUXILIARY_HEADER = "x-ms-authorization-auxiliary"; - string _token; + private string _token; internal AuxiliaryPoilcy(string token) { diff --git a/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/MigrationGuide.cs b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/MigrationGuide.cs new file mode 100644 index 0000000000000..70c788c8935e0 --- /dev/null +++ b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/MigrationGuide.cs @@ -0,0 +1,347 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Linq; +#region Snippet:Using_Statements +using Azure.Core; +using Azure.Identity; +using Azure.ResourceManager; +using Azure.ResourceManager.Compute; +using Azure.ResourceManager.Compute.Models; +using Azure.ResourceManager.Network; +using Azure.ResourceManager.Network.Models; +using Azure.ResourceManager.Resources; +using Azure.ResourceManager.Resources.Models; +#endregion +using System.Threading.Tasks; +using NUnit.Framework; + +namespace Azure.ResourceManager.Tests.Samples +{ + internal class MigrationGuide + { + public ArmClient MigrationExample_CreateDefaultClient() + { + #region Snippet:Construct_Client + ArmClient client = new ArmClient(new DefaultAzureCredential()); + #endregion + + return client; + } + + public ArmClient MigrationExample_CreateClient() + { + #region Snippet:Construct_CreateClient + string clientId = "CLIENT_ID"; + string clientSecret = "CLIENT_SECRET"; + string tenantId = "TENANT_ID"; + string subscription = "SUBSCRIPTION_ID"; + ClientSecretCredential credential = new ClientSecretCredential(tenantId, clientId, clientSecret); + ArmClient client = new ArmClient(credential, subscription); + #endregion + + return client; + } + + public async Task MigrationExample_CreateRg(ArmClient client) + { + #region Snippet:Create_ResourceGroup + SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync(); + ResourceGroupCollection resourceGroups = subscription.GetResourceGroups(); + + string resourceGroupName = "QuickStartRG"; + + ResourceGroupData resourceGroupData = new ResourceGroupData(AzureLocation.WestUS2); + ArmOperation resourceGroupOperation = await resourceGroups.CreateOrUpdateAsync(WaitUntil.Completed, resourceGroupName, resourceGroupData); + ResourceGroupResource resourceGroup = resourceGroupOperation.Value; + #endregion + + return resourceGroup; + } + + public async Task MigrationExample_CreateAvailabilitySet(ResourceGroupResource resourceGroup) + { + #region Snippet:Create_AvailabilitySet + string availabilitySetName = "QuickstartAvailabilitySet"; + + AvailabilitySetData availabilitySetData = new AvailabilitySetData(resourceGroup.Data.Location); + AvailabilitySetCollection availabilitySets = resourceGroup.GetAvailabilitySets(); + ArmOperation availabilitySetOperation = await availabilitySets.CreateOrUpdateAsync(WaitUntil.Completed, availabilitySetName + "_aSet", availabilitySetData); + AvailabilitySetResource availabilitySet = availabilitySetOperation.Value; + #endregion + + return availabilitySet; + } + + public async Task MigrationExample_CreateVnetSubnet(ResourceGroupResource resourceGroup) + { + #region Snippet:Create_Vnet_and_Subnet + string virtualNetworkName = "QuickstartVnet"; + string subnetName = "QuickstartSubnet"; + + VirtualNetworkData virtualNetworkData = new VirtualNetworkData() + { + Subnets = + { + new SubnetData() + { + Name = subnetName, + AddressPrefix = "10.0.0.0/24" + } + } + }; + virtualNetworkData.AddressPrefixes.Add("10.0.0.0/16"); + VirtualNetworkCollection virtualNetworks = resourceGroup.GetVirtualNetworks(); + ArmOperation virtualNetworkOperation = await virtualNetworks.CreateOrUpdateAsync(WaitUntil.Completed, virtualNetworkName, virtualNetworkData); + VirtualNetworkResource virtualNetwork = virtualNetworkOperation.Value; + #endregion + + return virtualNetwork; + } + + public async Task MigrationExample_Nsg(ResourceGroupResource resourceGroup) + { + #region Snippet:Create_NetworkSecurityGroup + string networkSecurityGroupName = "QuickstartNsg"; + + NetworkSecurityGroupData networkSecurityGroupData = new NetworkSecurityGroupData() { Location = resourceGroup.Data.Location }; + NetworkSecurityGroupCollection networkSecurityGroups = resourceGroup.GetNetworkSecurityGroups(); + ArmOperation networkSecurityGroupOperation = await networkSecurityGroups.CreateOrUpdateAsync(WaitUntil.Completed, networkSecurityGroupName, networkSecurityGroupData); + NetworkSecurityGroupResource networkSecurityGroup = networkSecurityGroupOperation.Value; + #endregion + + return networkSecurityGroup; + } + + public async Task MigrationExample_Nic(ResourceGroupResource resourceGroup, VirtualNetworkResource virtualNetwork) + { + #region Snippet:Create_NetworkInterface + string networkInterfaceName = "QuickstartNic"; + + NetworkInterfaceIPConfigurationData networkInterfaceIPConfiguration = new NetworkInterfaceIPConfigurationData() + { + Name = "Primary", + Primary = true, + Subnet = new SubnetData() { Id = virtualNetwork.Data.Subnets.First().Id }, + PrivateIPAllocationMethod = NetworkIPAllocationMethod.Dynamic, + }; + + NetworkInterfaceData nicData = new NetworkInterfaceData() { Location = resourceGroup.Data.Location }; + nicData.IPConfigurations.Add(networkInterfaceIPConfiguration); + NetworkInterfaceCollection networkInterfaces = resourceGroup.GetNetworkInterfaces(); + ArmOperation networkInterfaceOperation = await networkInterfaces.CreateOrUpdateAsync(WaitUntil.Completed, networkInterfaceName, nicData); + NetworkInterfaceResource networkInterface = networkInterfaceOperation.Value; + #endregion + + return networkInterface; + } + + public async Task MigrationExample_CreateVm(ResourceGroupResource resourceGroup, AvailabilitySetResource availabilitySet, NetworkInterfaceResource networkInterface) + { + #region Snippet:Create_VirtualMachine + string virtualMachineName = "QuickstartVm"; + + VirtualMachineData virutalMachineData = new VirtualMachineData(resourceGroup.Data.Location) + { + OSProfile = new VirtualMachineOSProfile() + { + AdminUsername = "admin-username", + AdminPassword = "admin-p4$$w0rd", + ComputerName = "computer-name" + }, + AvailabilitySetId = availabilitySet.Id, + NetworkProfile = new VirtualMachineNetworkProfile() + { + NetworkInterfaces = + { + new VirtualMachineNetworkInterfaceReference() + { + Id = networkInterface.Id + } + } + } + }; + + VirtualMachineCollection virtualMachines = resourceGroup.GetVirtualMachines(); + ArmOperation virtualMachineOperation = await virtualMachines.CreateOrUpdateAsync(WaitUntil.Completed, virtualMachineName, virutalMachineData); + VirtualMachineResource virtualMachine = virtualMachineOperation.Value; + #endregion + + return virtualMachine; + } + + public async Task MigrationExample_Fluent_CreateNsg(ArmClient client, ResourceGroupResource resourceGroup) + { + #region Snippet:Create_Fluent_Nsg + string networkNsgName = "QuickstartNsg"; + + NetworkSecurityGroupData networkNsgData = new NetworkSecurityGroupData() + { + Location = resourceGroup.Data.Location, + SecurityRules = + { + new SecurityRuleData() + { + Name = "DenyInternetInComing", + Protocol = SecurityRuleProtocol.Asterisk, + SourcePortRange = "*", + DestinationPortRange = "*", + SourceAddressPrefix = "INTERNET", + DestinationAddressPrefix = "*", + Access = SecurityRuleAccess.Deny, + Priority = 100, + Direction = SecurityRuleDirection.Inbound, + }, + new SecurityRuleData() + { + Name = "DenyInternetOutGoing", + Protocol = SecurityRuleProtocol.Asterisk, + SourcePortRange = "*", + DestinationPortRange = "*", + SourceAddressPrefix = "*", + DestinationAddressPrefix = "internet", + Access = SecurityRuleAccess.Deny, + Priority = 200, + Direction = SecurityRuleDirection.Outbound, + } + } + }; + NetworkSecurityGroupCollection networkSecurityGroups = resourceGroup.GetNetworkSecurityGroups(); + ArmOperation networkSecurityGroupOperation = await networkSecurityGroups.CreateOrUpdateAsync(WaitUntil.Completed, networkNsgName, networkNsgData); + NetworkSecurityGroupResource networkSecurityGroup = networkSecurityGroupOperation.Value; + #endregion + + return networkSecurityGroup; + } + + public async Task MigrationExample_Fluent_CreateVnetSubnet(ResourceGroupResource resourceGroup, NetworkSecurityGroupResource networkSecurityGroup) + { + #region Snippet:Create_Fluent_Vnet_and_Subnet + string virtualNetworkName = "QuickstartVnet"; + string subnetName = "QuickstartSubnet"; + + VirtualNetworkData virtualNetworkData = new VirtualNetworkData() + { + Location = AzureLocation.EastUS, + AddressPrefixes = { "192.168.0.0/16" }, + Subnets = + { + new SubnetData() + { + AddressPrefix = "192.168.2.0/24", + Name = subnetName, + NetworkSecurityGroup = networkSecurityGroup.Data + }, + new SubnetData() + { + AddressPrefix = "192.168.1.0/24", + Name = "subnet1" + } + }, + }; + VirtualNetworkCollection virtualNetworks = resourceGroup.GetVirtualNetworks(); + ArmOperation virtualNetworkOperation = await virtualNetworks.CreateOrUpdateAsync(WaitUntil.Completed, virtualNetworkName, virtualNetworkData); + VirtualNetworkResource virtualNetwork = virtualNetworkOperation.Value; + #endregion + + return virtualNetwork; + } + + public async Task MigrationExample_Fluent_CreateVm(ResourceGroupResource resourceGroup, VirtualNetworkResource virtualNetwork) + { + #region Snippet:Create_Fluent_VirtualMachine + // Create Nic + string networkInterfaceName = "QuickstartNic"; + + NetworkInterfaceData nicData = new NetworkInterfaceData() + { + Location = AzureLocation.EastUS, + IPConfigurations = + { + new NetworkInterfaceIPConfigurationData() + { + Name = "default-config", + PrivateIPAllocationMethod = NetworkIPAllocationMethod.Dynamic, + Subnet = new SubnetData() { Id = virtualNetwork.Data.Subnets.First().Id }, + } + } + }; + NetworkInterfaceCollection networkInterfaces = resourceGroup.GetNetworkInterfaces(); + ArmOperation networkInterfaceOperation = await networkInterfaces.CreateOrUpdateAsync(WaitUntil.Completed, networkInterfaceName, nicData); + NetworkInterfaceResource networkInterface = networkInterfaceOperation.Value; + + // Create VM + string virtualMachineName = "QuickstartVm"; + + VirtualMachineData virutalMachineData = new VirtualMachineData(AzureLocation.EastUS) + { + HardwareProfile = new VirtualMachineHardwareProfile() + { + VmSize = VirtualMachineSizeType.StandardD2V3 + }, + StorageProfile = new VirtualMachineStorageProfile() + { + ImageReference = new ImageReference() + { + Publisher = "Canonical", + Offer = "0001-com-ubuntu-server-jammy", + Sku = "22_04-lts-gen2", + Version = "latest", + }, + OSDisk = new VirtualMachineOSDisk(DiskCreateOptionType.FromImage) + { + OSType = SupportedOperatingSystemType.Windows, + Name = "QuickstartVmOSDisk", + Caching = CachingType.ReadOnly, + ManagedDisk = new VirtualMachineManagedDisk() + { + StorageAccountType = StorageAccountType.StandardLrs, + }, + }, + }, + OSProfile = new VirtualMachineOSProfile() + { + AdminUsername = "admin-username", + AdminPassword = "admin-p4$$w0rd", + ComputerName = "computer-name" + }, + NetworkProfile = new VirtualMachineNetworkProfile() + { + NetworkInterfaces = + { + new VirtualMachineNetworkInterfaceReference() + { + Id = networkInterface.Id, + Primary = true, + } + } + }, + }; + VirtualMachineCollection virtualMachines = resourceGroup.GetVirtualMachines(); + ArmOperation virtualMachineOperation = await virtualMachines.CreateOrUpdateAsync(WaitUntil.Completed, virtualMachineName, virutalMachineData); + VirtualMachineResource virtualMachine = virtualMachineOperation.Value; + #endregion + + return virtualMachine; + } + + public async Task MigrationExample_Fluent_ListNetworks(ResourceGroupResource resourceGroup) + { + #region Snippet:Create_Fluent_ListNetworks + await foreach (VirtualNetworkResource virtualNetwork in resourceGroup.GetVirtualNetworks().GetAllAsync()) + { + // Do something + Console.WriteLine(virtualNetwork.Data.Name); + } + #endregion + } + + public async Task MigrationExample_Fluent_DeleteNetwork(VirtualNetworkResource virtualNetwork) + { + #region Snippet:Create_Fluent_DeleteNetwork + await virtualNetwork.DeleteAsync(WaitUntil.Completed); + #endregion + } + } +} diff --git a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Readme.cs b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Readme.cs similarity index 98% rename from sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Readme.cs rename to sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Readme.cs index df57220c7f424..2fb30082bd926 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Readme.cs +++ b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Readme.cs @@ -1,4 +1,7 @@ -#region Snippet:Readme_AuthClient_Namespaces +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#region Snippet:Readme_AuthClient_Namespaces using System; using System.Threading.Tasks; using Azure.Core; @@ -11,7 +14,7 @@ namespace Azure.ResourceManager.Tests.Samples { - class Readme + public class Readme { [Test] [Ignore("Only verifying that the sample builds")] diff --git a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample1_HelloWorld.cs b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample1_HelloWorld.cs similarity index 93% rename from sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample1_HelloWorld.cs rename to sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample1_HelloWorld.cs index ab9bee54ca99b..5670ba042888c 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample1_HelloWorld.cs +++ b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample1_HelloWorld.cs @@ -1,4 +1,7 @@ -#region Snippet:Hello_World_Namespaces +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#region Snippet:Hello_World_Namespaces using System; using Azure.Identity; using Azure.ResourceManager; @@ -8,7 +11,7 @@ namespace Azure.ResourceManager.Tests.Samples { - class Sample1_HelloWorld + public class Sample1_HelloWorld { [Test] [Ignore("Only verifying that the sample builds")] diff --git a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample1_HelloWorldAsync.cs b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample1_HelloWorldAsync.cs similarity index 92% rename from sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample1_HelloWorldAsync.cs rename to sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample1_HelloWorldAsync.cs index 029e23d504193..550b1bef65c31 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample1_HelloWorldAsync.cs +++ b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample1_HelloWorldAsync.cs @@ -1,4 +1,7 @@ -#region Snippet:Hello_World_Async_Namespaces +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#region Snippet:Hello_World_Async_Namespaces using System; using System.Threading.Tasks; using Azure.Identity; @@ -8,7 +11,7 @@ namespace Azure.ResourceManager.Tests.Samples { - class Sample1_HelloWorldAsync + public class Sample1_HelloWorldAsync { [Test] [Ignore("Only verifying that the sample builds")] diff --git a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample2_ManagingResourceGroups.cs b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample2_ManagingResourceGroups.cs similarity index 96% rename from sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample2_ManagingResourceGroups.cs rename to sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample2_ManagingResourceGroups.cs index a058b5b235fbc..8623cdc4b3391 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample2_ManagingResourceGroups.cs +++ b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample2_ManagingResourceGroups.cs @@ -1,4 +1,7 @@ -#region Snippet:Managing_Resource_Groups_Namespaces +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#region Snippet:Managing_Resource_Groups_Namespaces using System; using System.Threading.Tasks; using Azure.Core; @@ -10,7 +13,7 @@ namespace Azure.ResourceManager.Tests.Samples { - class Sample2_ManagingResourceGroups + public class Sample2_ManagingResourceGroups { [Test] [Ignore("Only verifying that the sample builds")] diff --git a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample3_CreatingAVirtualNetwork.cs b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample3_CreatingAVirtualNetwork.cs similarity index 86% rename from sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample3_CreatingAVirtualNetwork.cs rename to sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample3_CreatingAVirtualNetwork.cs index 5155d2ebaa696..c32040699f7e6 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample3_CreatingAVirtualNetwork.cs +++ b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample3_CreatingAVirtualNetwork.cs @@ -1,4 +1,7 @@ -using System.Threading.Tasks; +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.Threading.Tasks; using Azure.Core; using Azure.Identity; using Azure.ResourceManager.Resources; @@ -7,7 +10,7 @@ namespace Azure.ResourceManager.Tests.Samples { - class Sample3_CreatingAVirtualNetwork + public class Sample3_CreatingAVirtualNetwork { [Test] [Ignore("Only verifying that the sample builds")] diff --git a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample4_MultiTenant.cs b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample4_MultiTenant.cs similarity index 92% rename from sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample4_MultiTenant.cs rename to sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample4_MultiTenant.cs index a4ddb5c92973f..9235f310c697d 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample4_MultiTenant.cs +++ b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample4_MultiTenant.cs @@ -1,4 +1,7 @@ -#region Snippet:MultiTenant_Namespaces +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#region Snippet:MultiTenant_Namespaces using System; using System.Threading; using System.Threading.Tasks; diff --git a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample5_GenericResource.cs b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample5_GenericResource.cs similarity index 98% rename from sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample5_GenericResource.cs rename to sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample5_GenericResource.cs index 6c3a52b42f974..e6c61924521c3 100644 --- a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/Sample5_GenericResource.cs +++ b/sdk/resourcemanager/Azure.ResourceManager/samples/DocSamples/Sample5_GenericResource.cs @@ -1,4 +1,7 @@ -using System; +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; using System.Collections.Generic; using System.Threading.Tasks; using Azure.Core; @@ -77,7 +80,7 @@ public static async Task UpdateGenericResource() { #region Snippet:Update_GenericResource ArmClient client = new ArmClient(new DefaultAzureCredential()); - + var subnetName = "samplesubnet"; var addressSpaces = new Dictionary() { diff --git a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/MigrationGuide.cs b/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/MigrationGuide.cs deleted file mode 100644 index b3c1015360760..0000000000000 --- a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/MigrationGuide.cs +++ /dev/null @@ -1,125 +0,0 @@ -#region Snippet:Using_Statements -using System; -using System.Linq; -using Azure.Identity; -using Azure.Core; -using Azure.ResourceManager; -using Azure.ResourceManager.Compute; -using Azure.ResourceManager.Compute.Models; -using Azure.ResourceManager.Network; -using Azure.ResourceManager.Network.Models; -using Azure.ResourceManager.Resources; -using Azure.ResourceManager.Resources.Models; -#endregion -using System.Threading.Tasks; -using NUnit.Framework; - -namespace Azure.ResourceManager.Tests.Samples -{ - internal class MigrationGuide - { - [Test] - [Ignore("Only verifying that the sample builds")] - public async Task MigrationExample() - { - #region Snippet:Construct_Client - ArmClient client = new ArmClient(new DefaultAzureCredential()); - #endregion - - #region Snippet:Create_ResourceGroup - SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync(); - ResourceGroupCollection resourceGroups = subscription.GetResourceGroups(); - - AzureLocation location = AzureLocation.WestUS2; - string resourceGroupName = "QuickStartRG"; - - ResourceGroupData resourceGroupData = new ResourceGroupData(location); - ArmOperation resourceGroupOperation = await resourceGroups.CreateOrUpdateAsync(WaitUntil.Completed, resourceGroupName, resourceGroupData); - ResourceGroupResource resourceGroup = resourceGroupOperation.Value; - #endregion - - #region Snippet:Create_AvailabilitySet - string virtualMachineName = "quickstartvm"; - AvailabilitySetData availabilitySetData = new AvailabilitySetData(location); - AvailabilitySetCollection availabilitySets = resourceGroup.GetAvailabilitySets(); - ArmOperation availabilitySetOperation = await availabilitySets.CreateOrUpdateAsync(WaitUntil.Completed, virtualMachineName + "_aSet", availabilitySetData); - AvailabilitySetResource availabilitySet = availabilitySetOperation.Value; - #endregion - - #region Snippet:Create_Vnet_and_Subnet - string virtualNetworkName = "MYVM" + "_vnet"; - string subnetName = "mySubnet"; - - VirtualNetworkData virtualNetworkData = new VirtualNetworkData() - { - Subnets = - { - new SubnetData() - { - Name = subnetName, - AddressPrefix = "10.0.0.0/24" - } - } - }; - VirtualNetworkCollection virtualNetworks = resourceGroup.GetVirtualNetworks(); - virtualNetworkData.AddressPrefixes.Add("10.0.0.0/16"); - ArmOperation virtualNetworkOperation = await virtualNetworks.CreateOrUpdateAsync(WaitUntil.Completed, virtualNetworkName, virtualNetworkData); - VirtualNetworkResource virtualNetwork = virtualNetworkOperation.Value; - #endregion - - #region Snippet:Create_NetworkSecurityGroup - string networkSecurityGroupName = virtualMachineName + "_nsg"; - NetworkSecurityGroupData networkSecurityGroupData = new NetworkSecurityGroupData() { Location = location }; - NetworkSecurityGroupCollection networkSecurityGroups = resourceGroup.GetNetworkSecurityGroups(); - ArmOperation networkSecurityGroupOperation = await networkSecurityGroups.CreateOrUpdateAsync(WaitUntil.Completed, networkSecurityGroupName, networkSecurityGroupData); - NetworkSecurityGroupResource networkSecurityGroup = networkSecurityGroupOperation.Value; - #endregion - - #region Snippet:Create_NetworkInterface - string networkInterfaceName = virtualMachineName + "_nic"; - NetworkInterfaceIPConfigurationData networkInterfaceIPConfiguration = new NetworkInterfaceIPConfigurationData() - { - Name = "Primary", - Primary = true, - Subnet = new SubnetData() { Id = virtualNetwork.Data.Subnets.First().Id }, - PrivateIPAllocationMethod = NetworkIPAllocationMethod.Dynamic, - }; - - NetworkInterfaceData nicData = new NetworkInterfaceData(); - nicData.Location = location; - nicData.IPConfigurations.Add(networkInterfaceIPConfiguration); - NetworkInterfaceCollection networkInterfaces = resourceGroup.GetNetworkInterfaces(); - ArmOperation networkInterfaceOperation = await networkInterfaces.CreateOrUpdateAsync(WaitUntil.Completed, networkInterfaceName, nicData); - NetworkInterfaceResource networkInterface = networkInterfaceOperation.Value; - #endregion - - #region Snippet:Create_VirtualMachine - VirtualMachineData virutalMachineData = new VirtualMachineData(location) - { - OSProfile = new VirtualMachineOSProfile() - { - AdminUsername = "admin-username", - AdminPassword = "admin-p4$$w0rd", - ComputerName = "computer-name" - }, - AvailabilitySetId = availabilitySet.Id, - NetworkProfile = new VirtualMachineNetworkProfile() - { - NetworkInterfaces = - { - new VirtualMachineNetworkInterfaceReference() - { - Id = networkInterface.Id - } - } - } - }; - - VirtualMachineCollection virtualMachines = resourceGroup.GetVirtualMachines(); - ArmOperation virtualMachineOperation = await virtualMachines.CreateOrUpdateAsync(WaitUntil.Completed, virtualMachineName, virutalMachineData); - VirtualMachineResource virtualMachine = virtualMachineOperation.Value; - Console.WriteLine("VirtualMachine ID: " + virtualMachine.Id); - #endregion - } - } -} diff --git a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/README.md b/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/README.md deleted file mode 100644 index c36f1ad1d7d51..0000000000000 --- a/sdk/resourcemanager/Azure.ResourceManager/tests/Samples/README.md +++ /dev/null @@ -1,3 +0,0 @@ -Source files in this directory are written as tests from which samples are extracted. -They are not intended to be viewed directly and help ensure our samples compile and work correctly. -See our [list of samples](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/resourcemanager/Azure.ResourceManager/samples) for more explanation about how to use this client library.