public async Task Get() { #region Snippet:Managing_Networks_GetANetworkInterface NetworkInterfaceCollection networkInterfaceCollection = resourceGroup.GetNetworkInterfaces(); NetworkInterfaceResource virtualNetwork = await networkInterfaceCollection.GetAsync("myVnet"); Console.WriteLine(virtualNetwork.Data.Name); #endregion }
private void VerifyVmssNicProperties(NetworkInterfaceResource nic) { Assert.NotNull(nic.NetworkSecurityGroup); Assert.False(string.IsNullOrEmpty(nic.NetworkSecurityGroup.Id)); Assert.NotNull(nic.DnsSettings); Assert.NotNull(nic.DnsSettings.DnsServers); Assert.AreEqual(1, nic.DnsSettings.DnsServers.Count); Assert.NotNull(nic.IpConfigurations[0].PublicIPAddress); Assert.False(string.IsNullOrEmpty(nic.IpConfigurations[0].PublicIPAddress.Id)); }
public async Task DeleteNetworkInterface() { #region Snippet:Managing_Networks_DeleteANetworkInterface NetworkInterfaceCollection networkInterfaceCollection = resourceGroup.GetNetworkInterfaces(); NetworkInterfaceResource virtualNetwork = await networkInterfaceCollection.GetAsync("myVnet"); await virtualNetwork.DeleteAsync(WaitUntil.Completed); #endregion }
public async Task CreateOrUpdate() { VirtualNetworkCollection virtualNetworkCollection = resourceGroup.GetVirtualNetworks(); string vnetName = "myVnet"; // Use the same location as the resource group VirtualNetworkData vnetInput = 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" } } }; VirtualNetworkResource virtualNetwork = await virtualNetworkCollection.CreateOrUpdate(WaitUntil.Completed, vnetName, vnetInput).WaitForCompletionAsync(); #region Snippet:Managing_Networks_CreateANetworkInterface PublicIPAddressCollection publicIPAddressCollection = resourceGroup.GetPublicIPAddresses(); string publicIPAddressName = "myIPAddress"; PublicIPAddressData publicIPInput = new PublicIPAddressData() { Location = resourceGroup.Data.Location, PublicIPAllocationMethod = IPAllocationMethod.Dynamic, DnsSettings = new PublicIPAddressDnsSettings() { DomainNameLabel = "myDomain" } }; PublicIPAddressResource publicIPAddress = await publicIPAddressCollection.CreateOrUpdate(WaitUntil.Completed, publicIPAddressName, publicIPInput).WaitForCompletionAsync(); NetworkInterfaceCollection networkInterfaceCollection = resourceGroup.GetNetworkInterfaces(); string networkInterfaceName = "myNetworkInterface"; NetworkInterfaceData networkInterfaceInput = new NetworkInterfaceData() { Location = resourceGroup.Data.Location, IPConfigurations = { new NetworkInterfaceIPConfigurationData() { Name = "ipConfig", PrivateIPAllocationMethod = IPAllocationMethod.Dynamic, PublicIPAddress = new PublicIPAddressData() { Id = publicIPAddress.Id }, Subnet = new SubnetData() { // use the virtual network just created Id = virtualNetwork.Data.Subnets[0].Id } } } }; NetworkInterfaceResource networkInterface = await networkInterfaceCollection.CreateOrUpdate(WaitUntil.Completed, networkInterfaceName, networkInterfaceInput).WaitForCompletionAsync(); #endregion }
public async Task NewCode() { #endif var armClient = new ArmClient(new DefaultAzureCredential()); var location = AzureLocation.WestUS; // Create ResourceGroupResource SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync(); ArmOperation<ResourceGroupResource> rgOperation = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, "myResourceGroup", new ResourceGroupData(location)); ResourceGroupResource resourceGroup = rgOperation.Value; // Create AvailabilitySet var availabilitySetData = new AvailabilitySetData(location) { PlatformUpdateDomainCount = 5, PlatformFaultDomainCount = 2, Sku = new ComputeSku() { Name = "Aligned" } }; ArmOperation<AvailabilitySetResource> asetOperation = await resourceGroup.GetAvailabilitySets().CreateOrUpdateAsync(WaitUntil.Completed, "myAvailabilitySet", availabilitySetData); AvailabilitySetResource availabilitySet = asetOperation.Value; // Create VNet var vnetData = new VirtualNetworkData() { Location = location, Subnets = { new SubnetData() { Name = "mySubnet", AddressPrefix = "10.0.0.0/24", } }, }; vnetData.AddressPrefixes.Add("10.0.0.0/16"); ArmOperation<VirtualNetworkResource> vnetOperation = await resourceGroup.GetVirtualNetworks().CreateOrUpdateAsync(WaitUntil.Completed, "myVirtualNetwork", vnetData); VirtualNetworkResource 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, } } }; ArmOperation<NetworkInterfaceResource> nicOperation = await resourceGroup.GetNetworkInterfaces().CreateOrUpdateAsync(WaitUntil.Completed, "myNetworkInterface", nicData); NetworkInterfaceResource 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 }, }; ArmOperation<VirtualMachineResource> vmOperation = await resourceGroup.GetVirtualMachines().CreateOrUpdateAsync(WaitUntil.Completed, "myVirtualMachine", vmData); VirtualMachineResource vm = vmOperation.Value; #endregion }
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 <ResourceGroupResource> 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 <AvailabilitySetResource> 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 <VirtualNetworkResource> 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 <NetworkSecurityGroupResource> 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 = IPAllocationMethod.Dynamic, }; NetworkInterfaceData nicData = new NetworkInterfaceData(); nicData.Location = location; nicData.IPConfigurations.Add(networkInterfaceIPConfiguration); NetworkInterfaceCollection networkInterfaces = resourceGroup.GetNetworkInterfaces(); ArmOperation <NetworkInterfaceResource> networkInterfaceOperation = await networkInterfaces.CreateOrUpdateAsync(WaitUntil.Completed, networkInterfaceName, nicData); NetworkInterfaceResource 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 <VirtualMachineResource> virtualMachineOperation = await virtualMachines.CreateOrUpdateAsync(WaitUntil.Completed, virtualMachineName, virutalMachineData); VirtualMachineResource virtualMachine = virtualMachineOperation.Value; Console.WriteLine("VM ID: " + virtualMachine.Id); #endregion }
public async Task ExpandResourceTest() { string resourceGroupName = Recording.GenerateAssetName("csmrg"); string location = TestEnvironment.Location; var resourceGroup = await CreateResourceGroup(resourceGroupName); // Create lbPublicIP string lbPublicIpName = Recording.GenerateAssetName("azsmnet"); string lbDomaingNameLabel = Recording.GenerateAssetName("azsmnet"); PublicIPAddressResource lbPublicIp = await CreateDefaultPublicIpAddress( lbPublicIpName, lbDomaingNameLabel, location, resourceGroup.GetPublicIPAddresses()); // Create Vnet string vnetName = Recording.GenerateAssetName("azsmnet"); string subnetName = Recording.GenerateAssetName("azsmnet"); VirtualNetworkResource vnet = await CreateVirtualNetwork(vnetName, subnetName, location, resourceGroup.GetVirtualNetworks()); // Create Nics string nic1name = Recording.GenerateAssetName("azsmnet"); string nic2name = Recording.GenerateAssetName("azsmnet"); string nic3name = Recording.GenerateAssetName("azsmnet"); NetworkInterfaceResource nic1 = await CreateNetworkInterface( nic1name, null, vnet.Data.Subnets[0].Id, location, "ipconfig", resourceGroup.GetNetworkInterfaces()); NetworkInterfaceResource nic2 = await CreateNetworkInterface( nic2name, null, vnet.Data.Subnets[0].Id, location, "ipconfig", resourceGroup.GetNetworkInterfaces()); NetworkInterfaceResource nic3 = await CreateNetworkInterface( nic3name, null, vnet.Data.Subnets[0].Id, location, "ipconfig", resourceGroup.GetNetworkInterfaces()); // Create the LoadBalancer var lbName = Recording.GenerateAssetName("azsmnet"); var frontendIpConfigName = Recording.GenerateAssetName("azsmnet"); var backEndAddressPoolName = Recording.GenerateAssetName("azsmnet"); var loadBalancingRuleName = Recording.GenerateAssetName("azsmnet"); var probeName = Recording.GenerateAssetName("azsmnet"); var inboundNatRule1Name = Recording.GenerateAssetName("azsmnet"); var inboundNatRule2Name = Recording.GenerateAssetName("azsmnet"); // Populate the loadBalancerCreateOrUpdateParameter var loadBalancerData = new LoadBalancerData() { Location = location, FrontendIPConfigurations = { new FrontendIPConfigurationData() { Name = frontendIpConfigName, PublicIPAddress = new PublicIPAddressData() { Id = lbPublicIp.Id } } }, BackendAddressPools = { new BackendAddressPoolData() { Name = backEndAddressPoolName, } }, LoadBalancingRules = { new LoadBalancingRuleData() { Name = loadBalancingRuleName, FrontendIPConfiguration = new WritableSubResource() { Id = GetChildLbResourceId(TestEnvironment.SubscriptionId, resourceGroupName, lbName, "FrontendIPConfigurations", frontendIpConfigName) }, Protocol = TransportProtocol.Tcp, FrontendPort = 80, BackendPort = 80, EnableFloatingIP = false, IdleTimeoutInMinutes = 15, BackendAddressPool = new WritableSubResource() { Id = GetChildLbResourceId(TestEnvironment.SubscriptionId, resourceGroupName, lbName, "backendAddressPools", backEndAddressPoolName) }, Probe = new WritableSubResource() { Id = GetChildLbResourceId(TestEnvironment.SubscriptionId, resourceGroupName, lbName, "probes", probeName) } } }, Probes = { new ProbeData() { Name = probeName, Protocol = ProbeProtocol.Http, Port = 80, RequestPath = "healthcheck.aspx", IntervalInSeconds = 10, NumberOfProbes = 2 } }, InboundNatRules = { new InboundNatRuleData() { Name = inboundNatRule1Name, FrontendIPConfiguration = new WritableSubResource() { Id = GetChildLbResourceId(TestEnvironment.SubscriptionId, resourceGroupName, lbName, "FrontendIPConfigurations", frontendIpConfigName) }, Protocol = TransportProtocol.Tcp, FrontendPort = 3389, BackendPort = 3389, IdleTimeoutInMinutes = 15, EnableFloatingIP = false }, new InboundNatRuleData() { Name = inboundNatRule2Name, FrontendIPConfiguration = new WritableSubResource() { Id = GetChildLbResourceId(TestEnvironment.SubscriptionId, resourceGroupName, lbName, "FrontendIPConfigurations", frontendIpConfigName) }, Protocol = TransportProtocol.Tcp, FrontendPort = 3390, BackendPort = 3389, IdleTimeoutInMinutes = 15, EnableFloatingIP = false, } } }; // Create the loadBalancer var loadBalancerCollection = resourceGroup.GetLoadBalancers(); LoadBalancerResource loadBalancer = (await loadBalancerCollection.CreateOrUpdateAsync(WaitUntil.Completed, lbName, loadBalancerData)).Value; // Associate the nic with LB //nic1.GetNetworkInterfaceIPConfigurations().List().First().` // TODO: where do we have the following? //nic1.IpConfigurations.First().LoadBalancerBackendAddressPools.Add(getLoadBalancer.Value.BackendAddressPools.First()); //nic1.IpConfigurations.First().LoadBalancerInboundNatRules.Add(getLoadBalancer.Value.InboundNatRules.First()); //nic2.IpConfigurations.First().LoadBalancerBackendAddressPools.Add(getLoadBalancer.Value.BackendAddressPools.First()); //nic3.IpConfigurations.First().LoadBalancerInboundNatRules.Add(getLoadBalancer.Value.InboundNatRules[1]); nic1.Data.IPConfigurations.First().LoadBalancerBackendAddressPools.Add(loadBalancer.Data.BackendAddressPools.First()); nic1.Data.IPConfigurations.First().LoadBalancerInboundNatRules.Add(loadBalancer.Data.InboundNatRules[0]); nic2.Data.IPConfigurations.First().LoadBalancerBackendAddressPools.Add(loadBalancer.Data.BackendAddressPools.First()); nic2.Data.IPConfigurations.First().LoadBalancerInboundNatRules.Add(loadBalancer.Data.InboundNatRules[1]); nic3.Data.IPConfigurations.First().LoadBalancerBackendAddressPools.Add(loadBalancer.Data.BackendAddressPools.First()); // Put Nics var networkInterfaceCollection = resourceGroup.GetNetworkInterfaces(); var createOrUpdateOperation1 = await networkInterfaceCollection.CreateOrUpdateAsync(WaitUntil.Completed, nic1name, nic1.Data); await createOrUpdateOperation1.WaitForCompletionAsync(); var createOrUpdateOperation2 = await networkInterfaceCollection.CreateOrUpdateAsync(WaitUntil.Completed, nic2name, nic2.Data); await createOrUpdateOperation2.WaitForCompletionAsync(); var createOrUpdateOperation3 = await networkInterfaceCollection.CreateOrUpdateAsync(WaitUntil.Completed, nic3name, nic3.Data); await createOrUpdateOperation3.WaitForCompletionAsync(); // Get Nics await networkInterfaceCollection.GetAsync(nic1name); await networkInterfaceCollection.GetAsync(nic2name); await networkInterfaceCollection.GetAsync(nic3name); // Get lb with expanded nics from nat rules loadBalancer = await loadBalancerCollection.GetAsync(lbName, "InboundNatRules/backendIPConfiguration"); await foreach (var natRule in loadBalancer.GetInboundNatRules()) { Assert.NotNull(natRule.Data.BackendIPConfiguration); Assert.NotNull(natRule.Data.BackendIPConfiguration.Id); //Assert.NotNull(natRule.Data.BackendIPConfiguration.Name); //Assert.NotNull(natRule.Data.BackendIPConfiguration.Etag); //Assert.AreEqual(natRule.Id, natRule.Data.BackendIPConfiguration.LoadBalancerInboundNatRules[0].Id); } // Get lb with expanded nics from pools loadBalancer = await loadBalancerCollection.GetAsync(lbName, "BackendAddressPools/backendIPConfigurations"); await foreach (var pool in loadBalancer.GetBackendAddressPools()) { BackendAddressPoolResource firstPool = await GetFirstPoolAsync(loadBalancer); foreach (var ipconfig in firstPool.Data.BackendIPConfigurations) { Assert.NotNull(ipconfig.Id); //Assert.NotNull(ipconfig.Name); //Assert.NotNull(ipconfig.Etag); //Assert.AreEqual(pool.Id, ipconfig.LoadBalancerBackendAddressPools[0].Id); } } // Get lb with expanded publicip loadBalancer = await loadBalancerCollection.GetAsync(lbName, "FrontendIPConfigurations/PublicIPAddress"); foreach (var ipconfig in loadBalancer.Data.FrontendIPConfigurations) { Assert.NotNull(ipconfig.PublicIPAddress); Assert.NotNull(ipconfig.PublicIPAddress.Id); Assert.NotNull(ipconfig.PublicIPAddress.Name); Assert.NotNull(ipconfig.PublicIPAddress.Etag); Assert.AreEqual(ipconfig.Id, ipconfig.PublicIPAddress.IPConfiguration.Id); } // Get NIC with expanded subnet nic1 = await networkInterfaceCollection.GetAsync(nic1name, "IPConfigurations/Subnet"); await foreach (NetworkInterfaceIPConfigurationResource ipconfig in nic1.GetNetworkInterfaceIPConfigurations()) { Assert.NotNull(ipconfig.Data.Subnet); Assert.NotNull(ipconfig.Data.Subnet.Id); //Assert.NotNull(ipconfig.Subnet.Name); //Assert.NotNull(ipconfig.Subnet.Etag); //Assert.IsNotEmpty(ipconfig.Subnet.IpConfigurations); } // Get subnet with expanded ipconfigurations Response <SubnetResource> subnet = await(await resourceGroup.GetVirtualNetworks().GetAsync(vnetName)).Value.GetSubnets().GetAsync( subnetName, "IPConfigurations"); foreach (IPConfiguration ipconfig in subnet.Value.Data.IPConfigurations) { Assert.NotNull(ipconfig.Id); //Assert.NotNull(ipconfig.Name); //Assert.NotNull(ipconfig.Etag); Assert.NotNull(ipconfig.PrivateIPAddress); } // Get publicIPAddress with expanded ipconfigurations Response <PublicIPAddressResource> publicip = await resourceGroup.GetPublicIPAddresses().GetAsync( lbPublicIpName, "IPConfiguration"); Assert.NotNull(publicip.Value.Data.IPConfiguration); Assert.NotNull(publicip.Value.Data.IPConfiguration.Id); //Assert.NotNull(publicip.Value.Data.IpConfiguration.Name); //Assert.NotNull(publicip.Value.Data.IpConfiguration.Etag); // Delete LoadBalancer Operation deleteOperation = await(await loadBalancerCollection.GetAsync(lbName)).Value.DeleteAsync(WaitUntil.Completed); await deleteOperation.WaitForCompletionResponseAsync(); // Verify Delete AsyncPageable <LoadBalancerResource> listLoadBalancerAP = loadBalancerCollection.GetAllAsync(); List <LoadBalancerResource> listLoadBalancer = await listLoadBalancerAP.ToEnumerableAsync(); Assert.IsEmpty(listLoadBalancer); // Delete all NetworkInterfaces await(await networkInterfaceCollection.GetAsync(nic1name)).Value.DeleteAsync(WaitUntil.Completed); await(await networkInterfaceCollection.GetAsync(nic2name)).Value.DeleteAsync(WaitUntil.Completed); await(await networkInterfaceCollection.GetAsync(nic3name)).Value.DeleteAsync(WaitUntil.Completed); // Delete all PublicIPAddresses await(await resourceGroup.GetPublicIPAddresses().GetAsync(lbPublicIpName)).Value.DeleteAsync(WaitUntil.Completed); }