public async virtual Task <NetworkInterfaceCreateOrUpdateOperation> CreateOrUpdateAsync(string networkInterfaceName, NetworkInterfaceData parameters, bool waitForCompletion = true, CancellationToken cancellationToken = default)
        {
            if (networkInterfaceName == null)
            {
                throw new ArgumentNullException(nameof(networkInterfaceName));
            }
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            using var scope = _clientDiagnostics.CreateScope("NetworkInterfaceContainer.CreateOrUpdate");
            scope.Start();
            try
            {
                var response = await _restClient.CreateOrUpdateAsync(Id.ResourceGroupName, networkInterfaceName, parameters, cancellationToken).ConfigureAwait(false);

                var operation = new NetworkInterfaceCreateOrUpdateOperation(Parent, _clientDiagnostics, Pipeline, _restClient.CreateCreateOrUpdateRequest(Id.ResourceGroupName, networkInterfaceName, parameters).Request, response);
                if (waitForCompletion)
                {
                    await operation.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false);
                }
                return(operation);
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
Ejemplo n.º 2
0
        public virtual NetworkInterfaceCreateOrUpdateOperation CreateOrUpdate(bool waitForCompletion, string networkInterfaceName, NetworkInterfaceData parameters, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNullOrEmpty(networkInterfaceName, nameof(networkInterfaceName));
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            using var scope = _networkInterfaceClientDiagnostics.CreateScope("NetworkInterfaceCollection.CreateOrUpdate");
            scope.Start();
            try
            {
                var response  = _networkInterfaceRestClient.CreateOrUpdate(Id.SubscriptionId, Id.ResourceGroupName, networkInterfaceName, parameters, cancellationToken);
                var operation = new NetworkInterfaceCreateOrUpdateOperation(ArmClient, _networkInterfaceClientDiagnostics, Pipeline, _networkInterfaceRestClient.CreateCreateOrUpdateRequest(Id.SubscriptionId, Id.ResourceGroupName, networkInterfaceName, parameters).Request, response);
                if (waitForCompletion)
                {
                    operation.WaitForCompletion(cancellationToken);
                }
                return(operation);
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
Ejemplo n.º 3
0
        public async Task NewCode()
        {
#endif
            var armClient = new ArmClient(new DefaultAzureCredential());

            var location = AzureLocation.WestUS;
            // Create ResourceGroup
            Subscription subscription = await armClient.GetDefaultSubscriptionAsync();
            ResourceGroupCreateOrUpdateOperation rgOperation = await subscription.GetResourceGroups().CreateOrUpdateAsync(true, "myResourceGroup", new ResourceGroupData(location));
            ResourceGroup resourceGroup = rgOperation.Value;

            // Create AvailabilitySet
            var availabilitySetData = new AvailabilitySetData(location)
            {
                PlatformUpdateDomainCount = 5,
                PlatformFaultDomainCount = 2,
                Sku = new Compute.Models.Sku() { Name = "Aligned" }
            };
            AvailabilitySetCreateOrUpdateOperation asetOperation = await resourceGroup.GetAvailabilitySets().CreateOrUpdateAsync(true, "myAvailabilitySet", availabilitySetData);
            AvailabilitySet availabilitySet = asetOperation.Value;

            // Create VNet
            var vnetData = new VirtualNetworkData()
            {
                Location = location,
                AddressSpace = new AddressSpace() { AddressPrefixes = { "10.0.0.0/16" } },
                Subnets =
                {
                    new SubnetData()
                    {
                        Name = "mySubnet",
                        AddressPrefix = "10.0.0.0/24",
                    }
                },
            };
            VirtualNetworkCreateOrUpdateOperation vnetOperation = await resourceGroup.GetVirtualNetworks().CreateOrUpdateAsync(true, "myVirtualNetwork", vnetData);
            VirtualNetwork vnet = vnetOperation.Value;

            // Create Network interface
            var nicData = new NetworkInterfaceData()
            {
                Location = location,
                IpConfigurations =
                {
                    new NetworkInterfaceIPConfigurationData()
                    {
                        Name = "Primary",
                        Primary = true,
                        Subnet = new SubnetData() { Id = vnet.Data.Subnets.First().Id },
                        PrivateIPAllocationMethod = IPAllocationMethod.Dynamic,
                    }
                }
            };
            NetworkInterfaceCreateOrUpdateOperation nicOperation = await resourceGroup.GetNetworkInterfaces().CreateOrUpdateAsync(true, "myNetworkInterface", nicData);
            NetworkInterface nic = nicOperation.Value;

            var vmData = new VirtualMachineData(location)
            {
                AvailabilitySet = new WritableSubResource() { Id = availabilitySet.Id },
                NetworkProfile = new Compute.Models.NetworkProfile { NetworkInterfaces = { new NetworkInterfaceReference() { Id = nic.Id } } },
                OsProfile = new OSProfile
                {
                    ComputerName = "testVM",
                    AdminUsername = "******",
                    AdminPassword = "******",
                    LinuxConfiguration = new LinuxConfiguration { DisablePasswordAuthentication = false, ProvisionVMAgent = true }
                },
                StorageProfile = new StorageProfile()
                {
                    ImageReference = new ImageReference()
                    {
                        Offer = "UbuntuServer",
                        Publisher = "Canonical",
                        Sku = "18.04-LTS",
                        Version = "latest"
                    }
                },
                HardwareProfile = new HardwareProfile() { VmSize = VirtualMachineSizeTypes.StandardB1Ms },
            };
            VirtualMachineCreateOrUpdateOperation vmOperation = await resourceGroup.GetVirtualMachines().CreateOrUpdateAsync(true, "myVirtualMachine", vmData);
            VirtualMachine vm = vmOperation.Value;
            #endregion
        }
Ejemplo n.º 4
0
        public async Task MigrationExample()
        {
            #region Snippet:Construct_Client
            ArmClient armClient = new ArmClient(new DefaultAzureCredential());
            #endregion

            #region Snippet:Create_ResourceGroup
            Subscription           subscription = armClient.DefaultSubscription;
            ResourceGroupContainer rgContainer  = subscription.GetResourceGroups();

            Location location = Location.WestUS2;
            string   rgName   = "QuickStartRG";

            ResourceGroupData rgData = new ResourceGroupData(location);
            ResourceGroupCreateOrUpdateOperation rgCreateLro = await rgContainer.CreateOrUpdateAsync(rgName, rgData);

            ResourceGroup resourceGroup = rgCreateLro.Value;
            #endregion

            #region Snippet:Create_AvailabilitySet
            string vmName = "quickstartvm";
            AvailabilitySetData aSetData = new AvailabilitySetData(location);
            AvailabilitySetCreateOrUpdateOperation asetCreateLro = await resourceGroup.GetAvailabilitySets().CreateOrUpdateAsync(vmName + "_aSet", aSetData);

            AvailabilitySet aset   = asetCreateLro.Value;
            string          asetId = aset.Id;
            #endregion

            #region Snippet:Create_Vnet_and_Subnet
            string       vnetName     = "MYVM" + "_vnet";
            string       subnetName   = "mySubnet";
            AddressSpace addressSpace = new AddressSpace();
            addressSpace.AddressPrefixes.Add("10.0.0.0/16");

            VirtualNetworkData vnetData = new VirtualNetworkData()
            {
                AddressSpace = addressSpace,
                Subnets      =
                {
                    new SubnetData()
                    {
                        Name          = subnetName,
                        AddressPrefix = "10.0.0.0/24"
                    }
                }
            };
            VirtualNetworkCreateOrUpdateOperation vnetCreateLro = await resourceGroup.GetVirtualNetworks().CreateOrUpdateAsync(vnetName, vnetData);

            VirtualNetwork vnet = vnetCreateLro.Value;
            #endregion

            #region Snippet:Create_NetworkSecurityGroup
            string nsgName = vmName + "_nsg";
            NetworkSecurityGroupData nsgData = new NetworkSecurityGroupData()
            {
                Location = location
            };
            NetworkSecurityGroupCreateOrUpdateOperation nsgCreateLro = await resourceGroup.GetNetworkSecurityGroups().CreateOrUpdateAsync(nsgName, nsgData);

            NetworkSecurityGroup nsg = nsgCreateLro.Value;
            #endregion

            #region Snippet:Create_NetworkInterface
            string nicName = vmName + "_nic";
            NetworkInterfaceIPConfiguration nicIPConfig = new NetworkInterfaceIPConfiguration()
            {
                Name    = "Primary",
                Primary = true,
                Subnet  = new SubnetData()
                {
                    Id = vnet.Data.Subnets.First().Id
                },
                PrivateIPAllocationMethod = IPAllocationMethod.Dynamic,
            };

            NetworkInterfaceData nicData = new NetworkInterfaceData();
            nicData.Location = location;
            nicData.IpConfigurations.Add(nicIPConfig);
            NetworkInterfaceCreateOrUpdateOperation nicCreateLro = await resourceGroup.GetNetworkInterfaces().CreateOrUpdateAsync(nicName, nicData);

            NetworkInterface nic = nicCreateLro.Value;
            #endregion

            #region Snippet:Create_VirtualMachine
            VirtualMachineData vmData = new VirtualMachineData(location);
            vmData.OsProfile.AdminUsername = "******";
            vmData.OsProfile.AdminPassword = "******";
            vmData.OsProfile.ComputerName  = "computer-name";
            vmData.AvailabilitySet         = new Compute.Models.SubResource();
            vmData.AvailabilitySet.Id      = aset.Id;
            NetworkInterfaceReference nicReference = new NetworkInterfaceReference();
            nicReference.Id = nic.Id;
            vmData.NetworkProfile.NetworkInterfaces.Add(nicReference);

            VirtualMachine vm = (await resourceGroup.GetVirtualMachines().CreateOrUpdateAsync(vmName, vmData)).Value;
            Console.WriteLine("VM ID: " + vm.Id);
            #endregion
        }