public async Task <NetworkInterface> CreateNetworkInterface(string name, string publicIpAddressId, string subnetId,
                                                                    string location, string ipConfigName, NetworkInterfaceCollection networkInterfaceCollection)
        {
            var nicParameters = new NetworkInterfaceData()
            {
                Location         = location,
                Tags             = { { "key", "value" } },
                IpConfigurations =
                {
                    new NetworkInterfaceIPConfigurationData()
                    {
                        Name = ipConfigName,
                        PrivateIPAllocationMethod = IPAllocationMethod.Dynamic,
                        Subnet = new SubnetData()
                        {
                            Id = subnetId
                        }
                    }
                }
            };

            if (!string.IsNullOrEmpty(publicIpAddressId))
            {
                nicParameters.IpConfigurations[0].PublicIPAddress = new PublicIPAddressData() /*Id = publicIpAddressId*/ }
                {
                    ;
            }

            // Test NIC apis
            var operation = InstrumentOperation(await networkInterfaceCollection.CreateOrUpdateAsync(true, name, nicParameters));
            await operation.WaitForCompletionAsync();

            Response <NetworkInterface> getNicResponse = await networkInterfaceCollection.GetAsync(name);

            Assert.AreEqual(getNicResponse.Value.Data.Name, name);

            // because its a single CA nic, primaryOnCA is always true
            Assert.True(getNicResponse.Value.Data.IpConfigurations[0].Primary);

            Assert.AreEqual("Succeeded", getNicResponse.Value.Data.ProvisioningState.ToString());

            return(getNicResponse);
        }
コード例 #2
0
        public async Task MigrationExample()
        {
            #region Snippet:Construct_Client
            ArmClient client = new ArmClient(new DefaultAzureCredential());
            #endregion

            #region Snippet:Create_ResourceGroup
            Subscription subscription = await client.GetDefaultSubscriptionAsync();

            ResourceGroupCollection resourceGroups = subscription.GetResourceGroups();

            AzureLocation location          = AzureLocation.WestUS2;
            string        resourceGroupName = "QuickStartRG";

            ResourceGroupData            resourceGroupData      = new ResourceGroupData(location);
            ArmOperation <ResourceGroup> resourceGroupOperation = await resourceGroups.CreateOrUpdateAsync(WaitUntil.Completed, resourceGroupName, resourceGroupData);

            ResourceGroup resourceGroup = resourceGroupOperation.Value;
            #endregion

            #region Snippet:Create_AvailabilitySet
            string virtualMachineName = "quickstartvm";
            AvailabilitySetData            availabilitySetData      = new AvailabilitySetData(location);
            AvailabilitySetCollection      availabilitySets         = resourceGroup.GetAvailabilitySets();
            ArmOperation <AvailabilitySet> availabilitySetOperation = await availabilitySets.CreateOrUpdateAsync(WaitUntil.Completed, virtualMachineName + "_aSet", availabilitySetData);

            AvailabilitySet 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 <VirtualNetwork> virtualNetworkOperation = await virtualNetworks.CreateOrUpdateAsync(WaitUntil.Completed, virtualNetworkName, virtualNetworkData);

            VirtualNetwork virtualNetwork = virtualNetworkOperation.Value;
            #endregion

            #region Snippet:Create_NetworkSecurityGroup
            string networkSecurityGroupName = virtualMachineName + "_nsg";
            NetworkSecurityGroupData networkSecurityGroupData = new NetworkSecurityGroupData()
            {
                Location = location
            };
            NetworkSecurityGroupCollection      networkSecurityGroups         = resourceGroup.GetNetworkSecurityGroups();
            ArmOperation <NetworkSecurityGroup> networkSecurityGroupOperation = await networkSecurityGroups.CreateOrUpdateAsync(WaitUntil.Completed, networkSecurityGroupName, networkSecurityGroupData);

            NetworkSecurityGroup 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 = IPAllocationMethod.Dynamic,
            };

            NetworkInterfaceData nicData = new NetworkInterfaceData();
            nicData.Location = location;
            nicData.IPConfigurations.Add(networkInterfaceIPConfiguration);
            NetworkInterfaceCollection      networkInterfaces         = resourceGroup.GetNetworkInterfaces();
            ArmOperation <NetworkInterface> networkInterfaceOperation = await networkInterfaces.CreateOrUpdateAsync(WaitUntil.Completed, networkInterfaceName, nicData);

            NetworkInterface networkInterface = networkInterfaceOperation.Value;
            #endregion

            #region Snippet:Create_VirtualMachine
            VirtualMachineData virutalMachineData = new VirtualMachineData(location);
            virutalMachineData.OSProfile.AdminUsername = "******";
            virutalMachineData.OSProfile.AdminPassword = "******";
            virutalMachineData.OSProfile.ComputerName  = "computer-name";
            virutalMachineData.AvailabilitySetId       = availabilitySet.Id;
            NetworkInterfaceReference nicReference = new NetworkInterfaceReference();
            nicReference.Id = networkInterface.Id;
            virutalMachineData.NetworkProfile.NetworkInterfaces.Add(nicReference);

            VirtualMachineCollection      virtualMachines         = resourceGroup.GetVirtualMachines();
            ArmOperation <VirtualMachine> virtualMachineOperation = await virtualMachines.CreateOrUpdateAsync(WaitUntil.Completed, virtualMachineName, virutalMachineData);

            VirtualMachine virtualMachine = virtualMachineOperation.Value;
            Console.WriteLine("VM ID: " + virtualMachine.Id);
            #endregion
        }