/// <summary>
 /// The Put NetworkInterface operation creates/updates a
 /// networkInterface
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Network.INetworkInterfaceOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='networkInterfaceName'>
 /// Required. The name of the network interface.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the create/update NetworkInterface
 /// operation
 /// </param>
 /// <returns>
 /// Response for PutNetworkInterface Api servive call
 /// </returns>
 public static NetworkInterfacePutResponse BeginCreateOrUpdating(this INetworkInterfaceOperations operations, string resourceGroupName, string networkInterfaceName, NetworkInterface parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((INetworkInterfaceOperations)s).BeginCreateOrUpdatingAsync(resourceGroupName, networkInterfaceName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        public PSNetworkInterface ToPsNetworkInterface(NetworkInterface nic)
        {
            var psNic = Mapper.Map<PSNetworkInterface>(nic);

            psNic.Tag = TagsConversionHelper.CreateTagHashtable(nic.Tags);

            foreach (var ipconfig in psNic.IpConfigurations)
            {
                ipconfig.LoadBalancerBackendAddressPools = ipconfig.LoadBalancerBackendAddressPools ?? new List<PSResourceId>();
                ipconfig.LoadBalancerInboundNatRules = ipconfig.LoadBalancerInboundNatRules ?? new List<PSResourceId>();
            }
            
            return psNic;
        }
Exemplo n.º 3
0
        public static NetworkInterface CreateNetworkInterface(
            string name,
            string resourceGroupName,
            string publicIpAddressId,
            string subnetId,
            string location,
            string ipConfigName,
            NetworkResourceProviderClient client)
        {
            var nicParameters = new NetworkInterface()
            {
                Location = location,
                Name = name,
                Tags = new Dictionary<string, string>()
                        {
                           {"key","value"}
                        },
                IpConfigurations = new List<NetworkInterfaceIpConfiguration>()
                {
                    new NetworkInterfaceIpConfiguration()
                    {
                         Name = ipConfigName,
                         PrivateIpAllocationMethod = IpAllocationMethod.Dynamic,
                         
                         Subnet = new ResourceId()
                         {
                             Id = subnetId
                         }
                    }
                }
            };

            if (!string.IsNullOrEmpty(publicIpAddressId))
            {
                nicParameters.IpConfigurations[0].PublicIpAddress = new ResourceId() { Id = publicIpAddressId };
            }

            // Test NIC apis
            var putNicResponse = client.NetworkInterfaces.CreateOrUpdate(resourceGroupName, name, nicParameters);
            Assert.Equal(HttpStatusCode.OK, putNicResponse.StatusCode);

            var getNicResponse = client.NetworkInterfaces.Get(resourceGroupName, name);
            Assert.Equal(getNicResponse.NetworkInterface.Name, name);
            Assert.Equal(getNicResponse.NetworkInterface.ProvisioningState, Microsoft.Azure.Management.Resources.Models.ProvisioningState.Succeeded);

            return getNicResponse.NetworkInterface;
        }
        public void NetworkInterfaceEnableIPForwardingTest()
        {
            var handler = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK };

            using (var context = UndoContext.Current)
            {
                context.Start();
                var resourcesClient = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(handler);
                var networkResourceProviderClient = NetworkManagementTestUtilities.GetNetworkResourceProviderClient(handler);

                // IDNS is supported only in centralus currently
                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/networkInterfaces");
                
                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                    new ResourceGroup
                    {
                        Location = location
                    });

                // Create Vnet
                // Populate parameter for Put Vnet
                string vnetName = TestUtilities.GenerateName();
                string subnetName = TestUtilities.GenerateName();

                var vnet = new VirtualNetwork()
                {
                    Location = location,

                    AddressSpace = new AddressSpace()
                    {
                        AddressPrefixes = new List<string>()
                    {
                        "10.0.0.0/16",
                    }
                    },
                    DhcpOptions = new DhcpOptions()
                    {
                        DnsServers = new List<string>()
                    {
                        "10.1.1.1",
                        "10.1.2.4"
                    }
                    },
                    Subnets = new List<Subnet>()
                    {
                        new Subnet()
                        {
                            Name = subnetName,
                            AddressPrefix = "10.0.0.0/24",
                        }
                    }
                };

                var putVnetResponse = networkResourceProviderClient.VirtualNetworks.CreateOrUpdate(resourceGroupName, vnetName, vnet);
                Assert.Equal(HttpStatusCode.OK, putVnetResponse.StatusCode);

                var getSubnetResponse = networkResourceProviderClient.Subnets.Get(resourceGroupName, vnetName, subnetName);

                // Create Nic
                string nicName = TestUtilities.GenerateName();
                string ipConfigName = TestUtilities.GenerateName();

                var nicParameters = new NetworkInterface()
                {
                    Location = location,
                    Name = nicName,
                    Tags = new Dictionary<string, string>()
                        {
                           {"key","value"}
                        },
                    IpConfigurations = new List<NetworkInterfaceIpConfiguration>()
                    {
                        new NetworkInterfaceIpConfiguration()
                        {
                             Name = ipConfigName,
                             PrivateIpAllocationMethod = IpAllocationMethod.Dynamic,
                             Subnet = new ResourceId()
                             {
                                 Id = getSubnetResponse.Subnet.Id
                             }
                        }
                    },
                    EnableIPForwarding = false,
                };

                // Test NIC apis
                var putNicResponse = networkResourceProviderClient.NetworkInterfaces.CreateOrUpdate(resourceGroupName, nicName, nicParameters);
                Assert.Equal(HttpStatusCode.OK, putNicResponse.StatusCode);

                var getNicResponse = networkResourceProviderClient.NetworkInterfaces.Get(resourceGroupName, nicName);
                Assert.Equal(getNicResponse.NetworkInterface.Name, nicName);
                Assert.Equal(getNicResponse.NetworkInterface.ProvisioningState, Microsoft.Azure.Management.Resources.Models.ProvisioningState.Succeeded);
                Assert.Null(getNicResponse.NetworkInterface.VirtualMachine);
                Assert.Null(getNicResponse.NetworkInterface.MacAddress);
                Assert.Equal(1, getNicResponse.NetworkInterface.IpConfigurations.Count);
                Assert.Equal(ipConfigName, getNicResponse.NetworkInterface.IpConfigurations[0].Name);
                Assert.False(getNicResponse.NetworkInterface.EnableIPForwarding);

                getNicResponse.NetworkInterface.EnableIPForwarding = true;
                networkResourceProviderClient.NetworkInterfaces.CreateOrUpdate(resourceGroupName, nicName, getNicResponse.NetworkInterface);
                getNicResponse = networkResourceProviderClient.NetworkInterfaces.Get(resourceGroupName, nicName);
                Assert.Equal(getNicResponse.NetworkInterface.Name, nicName);
                Assert.True(getNicResponse.NetworkInterface.EnableIPForwarding);

                // Delete Nic
                var deleteNicResponse = networkResourceProviderClient.NetworkInterfaces.Delete(resourceGroupName, nicName);
                Assert.Equal(HttpStatusCode.OK, deleteNicResponse.StatusCode);

                var getListNicResponse = networkResourceProviderClient.NetworkInterfaces.List(resourceGroupName);
                Assert.Equal(0, getListNicResponse.NetworkInterfaces.Count);

                // Delete VirtualNetwork
                var deleteVnetResponse = networkResourceProviderClient.VirtualNetworks.Delete(resourceGroupName, vnetName);
                Assert.Equal(HttpStatusCode.OK, deleteVnetResponse.StatusCode);
            }
        }
        public void NetworkInterfaceApiTest()
        {
            var handler = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK };

            using (var context = UndoContext.Current)
            {
                context.Start();
                var resourcesClient = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(handler);
                var networkResourceProviderClient = NetworkManagementTestUtilities.GetNetworkResourceProviderClient(handler);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/networkInterfaces");
                
                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                    new ResourceGroup
                    {
                        Location = location
                    });

                // Create publicIP
                string publicIpName = TestUtilities.GenerateName();
                string domainNameLabel = TestUtilities.GenerateName();
                
                var publicIp = new PublicIpAddress()
                {
                    Location = location,
                    Tags = new Dictionary<string, string>()
                    {
                       {"key","value"}
                    },
                    PublicIpAllocationMethod = IpAllocationMethod.Dynamic,
                    DnsSettings = new PublicIpAddressDnsSettings()
                    {
                       DomainNameLabel = domainNameLabel
                    }
                };

                // Put PublicIpAddress
                var putPublicIpAddressResponse = networkResourceProviderClient.PublicIpAddresses.CreateOrUpdate(resourceGroupName, publicIpName, publicIp);
                Assert.Equal(HttpStatusCode.OK, putPublicIpAddressResponse.StatusCode);
                Assert.Equal("Succeeded", putPublicIpAddressResponse.Status);
                var getPublicIpAddressResponse = networkResourceProviderClient.PublicIpAddresses.Get(resourceGroupName, publicIpName);

                // Create Vnet
                // Populate parameter for Put Vnet
                string vnetName = TestUtilities.GenerateName();
                string subnetName = TestUtilities.GenerateName();

                var vnet = new VirtualNetwork()
                {
                    Location = location,

                    AddressSpace = new AddressSpace()
                    {
                        AddressPrefixes = new List<string>()
                    {
                        "10.0.0.0/16",
                    }
                    },
                    DhcpOptions = new DhcpOptions()
                    {
                        DnsServers = new List<string>()
                    {
                        "10.1.1.1",
                        "10.1.2.4"
                    }
                    },
                    Subnets = new List<Subnet>()
                    {
                        new Subnet()
                        {
                            Name = subnetName,
                            AddressPrefix = "10.0.0.0/24",
                        }
                    }
                };

                var putVnetResponse = networkResourceProviderClient.VirtualNetworks.CreateOrUpdate(resourceGroupName, vnetName, vnet);
                Assert.Equal(HttpStatusCode.OK, putVnetResponse.StatusCode);

                var getSubnetResponse = networkResourceProviderClient.Subnets.Get(resourceGroupName, vnetName, subnetName);

                // Create Nic
                string nicName = TestUtilities.GenerateName();
                string ipConfigName = TestUtilities.GenerateName();

                var nicParameters = new NetworkInterface()
                {
                    Location = location,
                    Name = nicName,
                    Tags = new Dictionary<string, string>()
                        {
                           {"key","value"}
                        },
                    IpConfigurations = new List<NetworkInterfaceIpConfiguration>()
                    {
                        new NetworkInterfaceIpConfiguration()
                        {
                             Name = ipConfigName,
                             PrivateIpAllocationMethod = IpAllocationMethod.Dynamic,
                             PublicIpAddress = new ResourceId()
                             {
                                 Id = getPublicIpAddressResponse.PublicIpAddress.Id
                             },
                             Subnet = new ResourceId()
                             {
                                 Id = getSubnetResponse.Subnet.Id
                             }
                        }
                    }
                };

                // Test NIC apis
                var putNicResponse = networkResourceProviderClient.NetworkInterfaces.CreateOrUpdate(resourceGroupName, nicName, nicParameters);
                Assert.Equal(HttpStatusCode.OK, putNicResponse.StatusCode);

                var getNicResponse = networkResourceProviderClient.NetworkInterfaces.Get(resourceGroupName, nicName);
                Assert.Equal(getNicResponse.NetworkInterface.Name, nicName);
                Assert.Equal(getNicResponse.NetworkInterface.ProvisioningState, Microsoft.Azure.Management.Resources.Models.ProvisioningState.Succeeded);
                Assert.Null(getNicResponse.NetworkInterface.VirtualMachine);
                Assert.Null(getNicResponse.NetworkInterface.MacAddress);
                Assert.Equal(1, getNicResponse.NetworkInterface.IpConfigurations.Count);
                Assert.Equal(ipConfigName, getNicResponse.NetworkInterface.IpConfigurations[0].Name);
                Assert.Equal(getPublicIpAddressResponse.PublicIpAddress.Id, getNicResponse.NetworkInterface.IpConfigurations[0].PublicIpAddress.Id);
                Assert.Equal(getSubnetResponse.Subnet.Id, getNicResponse.NetworkInterface.IpConfigurations[0].Subnet.Id);

                // Get all Nics
                var getListNicResponse = networkResourceProviderClient.NetworkInterfaces.List(resourceGroupName);
                Assert.Equal(1, getListNicResponse.NetworkInterfaces.Count);
                Assert.Equal(getNicResponse.NetworkInterface.Name, getListNicResponse.NetworkInterfaces[0].Name);
                Assert.Equal(getNicResponse.NetworkInterface.Etag, getListNicResponse.NetworkInterfaces[0].Etag);
                Assert.Equal(getNicResponse.NetworkInterface.IpConfigurations[0].Etag, getListNicResponse.NetworkInterfaces[0].IpConfigurations[0].Etag);

                // Get all Nics in subscription
                var listNicSubscription = networkResourceProviderClient.NetworkInterfaces.ListAll();
                Assert.Equal(1, getListNicResponse.NetworkInterfaces.Count);
                Assert.Equal(getNicResponse.NetworkInterface.Name, listNicSubscription.NetworkInterfaces[0].Name);
                Assert.Equal(getNicResponse.NetworkInterface.Etag, listNicSubscription.NetworkInterfaces[0].Etag);
                Assert.Equal(listNicSubscription.NetworkInterfaces[0].IpConfigurations[0].Etag, getListNicResponse.NetworkInterfaces[0].IpConfigurations[0].Etag);

                // Delete Nic
                var deleteNicResponse = networkResourceProviderClient.NetworkInterfaces.Delete(resourceGroupName, nicName);
                Assert.Equal(HttpStatusCode.OK, deleteNicResponse.StatusCode);

                getListNicResponse = networkResourceProviderClient.NetworkInterfaces.List(resourceGroupName);
                Assert.Equal(0, getListNicResponse.NetworkInterfaces.Count);

                // Delete PublicIpAddress
                var deletePublicIpAddressResponse = networkResourceProviderClient.PublicIpAddresses.Delete(resourceGroupName, publicIpName);
                Assert.Equal(HttpStatusCode.OK, deletePublicIpAddressResponse.StatusCode);

                // Delete VirtualNetwork
                var deleteVnetResponse = networkResourceProviderClient.VirtualNetworks.Delete(resourceGroupName, vnetName);
                Assert.Equal(HttpStatusCode.OK, deleteVnetResponse.StatusCode);
            }
        }
        protected NetworkInterfaceGetResponse CreateNIC(string rgName, Subnet subnet, PublicIpAddress publicIPaddress, string nicname = null)
        {
            // Create Nic
            nicname = nicname ?? TestUtilities.GenerateName();
            string ipConfigName = TestUtilities.GenerateName();

            var nicParameters = new NetworkInterface()
            {
                Location = m_location,
                Name = nicname,
                Tags = new Dictionary<string, string>()
                {
                    { "key" ,"value" }
                },
                IpConfigurations = new List<NetworkInterfaceIpConfiguration>()
                {
                    new NetworkInterfaceIpConfiguration()
                    {
                        Name = ipConfigName,
                        PrivateIpAllocationMethod = IpAllocationMethod.Dynamic,
                        Subnet = subnet,
                    }
                }
            };

            if (publicIPaddress != null)
            {
                nicParameters.IpConfigurations[0].PublicIpAddress = new ResourceId { Id = publicIPaddress.Id };
            }

            var putNicResponse = m_NrpClient.NetworkInterfaces.CreateOrUpdate(rgName, nicname, nicParameters);
            var getNicResponse = m_NrpClient.NetworkInterfaces.Get(rgName, nicname);
            return getNicResponse;
        }
 /// <summary>
 /// The Put NetworkInterface operation creates/updates a
 /// networkInterface
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Network.INetworkInterfaceOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='networkInterfaceName'>
 /// Required. The name of the network interface.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the create/update NetworkInterface
 /// operation
 /// </param>
 /// <returns>
 /// Response for PutNetworkInterface Api servive call
 /// </returns>
 public static Task<NetworkInterfacePutResponse> BeginCreateOrUpdatingAsync(this INetworkInterfaceOperations operations, string resourceGroupName, string networkInterfaceName, NetworkInterface parameters)
 {
     return operations.BeginCreateOrUpdatingAsync(resourceGroupName, networkInterfaceName, parameters, CancellationToken.None);
 }
        private static LoadBalancerRequest GetUpdateRequest(VirtualMachine vm, NetworkInterface nic)
        {
            var parametersNewMaster = new LoadBalancerRequest();

            parametersNewMaster.Id = nic.Id;
            parametersNewMaster.Name = nic.Name;
            parametersNewMaster.Location = vm.Location;
            parametersNewMaster.Type = "Microsoft.Network/networkInterfaces";
            parametersNewMaster.Properties.VirtualMachine.Id = vm.Id;

            var ipconfiguration = new IpConfigurationRequest
            {
                Id = nic.IpConfigurations[0].Id,
                Name = nic.IpConfigurations[0].Name,
                Properties = new IpConfigurationProperties
                {
                    LoadBalancerBackendAddressPools = new List<LoadBalancerBackendProperties>(),
                    Subnet = new SubnetRequest
                    {
                        Id = nic.IpConfigurations[0].Subnet.Id
                    }
                }
            };

            parametersNewMaster.Properties.IpConfigurations = new List<IpConfigurationRequest>();
            parametersNewMaster.Properties.IpConfigurations.Add(ipconfiguration);
            return parametersNewMaster;
        }
Exemplo n.º 9
0
        public string AddPublicIp(string publicIpName)
        {
            azureLogin.Authenticate();
            using (var networkClient = new NetworkResourceProviderClient(azureLogin.Credentials))
            {
                var nicName = publicIpName + "_nic";

                var pia = new PublicIpAddress()
                {
                    Location = demo.Location,
                    PublicIpAllocationMethod = "Dynamic",
                };
                HandleResult("Create Public IP", () => networkClient.PublicIpAddresses.CreateOrUpdate(demo.Group, publicIpName, pia));

                var ni = new NetworkInterface()
                {
                    Name = nicName,
                    Location = demo.Location,
                    IpConfigurations = new List<NetworkInterfaceIpConfiguration>()
                    {
                        new NetworkInterfaceIpConfiguration()
                        {
                            Name = nicName + "config",
                            PublicIpAddress = GetPublicIpId(publicIpName),
                            Subnet = GetSubnetId(demo.Network),
                        }
                    }
                };
                HandleResult("Create Network Interface", () => networkClient.NetworkInterfaces.CreateOrUpdate(demo.Group, nicName, ni));
            }
            return publicIpName;
        }