private PSNetworkInterface CreateNetworkInterface()
        {
            // Get the subnetId and publicIpAddressId from the object if specified
            if (string.Equals(ParameterSetName, Microsoft.Azure.Commands.Network.Properties.Resources.SetByResource))
            {
                this.SubnetId = this.Subnet.Id;

                if (this.PublicIpAddress != null)
                {
                    this.PublicIpAddressId = this.PublicIpAddress.Id;
                }

                if (this.NetworkSecurityGroup != null)
                {
                    this.NetworkSecurityGroupId = this.NetworkSecurityGroup.Id;
                }

                if (this.LoadBalancerBackendAddressPool != null)
                {
                    this.LoadBalancerBackendAddressPoolId = new List <string>();
                    foreach (var bepool in this.LoadBalancerBackendAddressPool)
                    {
                        this.LoadBalancerBackendAddressPoolId.Add(bepool.Id);
                    }
                }

                if (this.LoadBalancerInboundNatRule != null)
                {
                    this.LoadBalancerInboundNatRuleId = new List <string>();
                    foreach (var natRule in this.LoadBalancerInboundNatRule)
                    {
                        this.LoadBalancerInboundNatRuleId.Add(natRule.Id);
                    }
                }

                if (this.ApplicationGatewayBackendAddressPool != null)
                {
                    this.ApplicationGatewayBackendAddressPoolId = new List <string>();
                    foreach (var appgwBepool in this.ApplicationGatewayBackendAddressPool)
                    {
                        this.ApplicationGatewayBackendAddressPoolId.Add(appgwBepool.Id);
                    }
                }
            }

            var networkInterface = new PSNetworkInterface();

            networkInterface.Name               = this.Name;
            networkInterface.Location           = this.Location;
            networkInterface.EnableIPForwarding = this.EnableIPForwarding.IsPresent;
            networkInterface.IpConfigurations   = new List <PSNetworkInterfaceIPConfiguration>();

            var nicIpConfiguration = new PSNetworkInterfaceIPConfiguration();

            nicIpConfiguration.Name = string.IsNullOrEmpty(this.IpConfigurationName) ? "ipconfig1" : this.IpConfigurationName;
            nicIpConfiguration.PrivateIpAllocationMethod = MNM.IPAllocationMethod.Dynamic;

            if (!string.IsNullOrEmpty(this.PrivateIpAddress))
            {
                nicIpConfiguration.PrivateIpAddress          = this.PrivateIpAddress;
                nicIpConfiguration.PrivateIpAllocationMethod = MNM.IPAllocationMethod.Static;
            }

            nicIpConfiguration.Subnet    = new PSSubnet();
            nicIpConfiguration.Subnet.Id = this.SubnetId;

            if (!string.IsNullOrEmpty(this.PublicIpAddressId))
            {
                nicIpConfiguration.PublicIpAddress    = new PSPublicIpAddress();
                nicIpConfiguration.PublicIpAddress.Id = this.PublicIpAddressId;
            }

            if (!string.IsNullOrEmpty(this.NetworkSecurityGroupId))
            {
                networkInterface.NetworkSecurityGroup    = new PSNetworkSecurityGroup();
                networkInterface.NetworkSecurityGroup.Id = this.NetworkSecurityGroupId;
            }

            if (this.LoadBalancerBackendAddressPoolId != null)
            {
                nicIpConfiguration.LoadBalancerBackendAddressPools = new List <PSBackendAddressPool>();
                foreach (var bepoolId in this.LoadBalancerBackendAddressPoolId)
                {
                    nicIpConfiguration.LoadBalancerBackendAddressPools.Add(new PSBackendAddressPool {
                        Id = bepoolId
                    });
                }
            }

            if (this.LoadBalancerInboundNatRuleId != null)
            {
                nicIpConfiguration.LoadBalancerInboundNatRules = new List <PSInboundNatRule>();
                foreach (var natruleId in this.LoadBalancerInboundNatRuleId)
                {
                    nicIpConfiguration.LoadBalancerInboundNatRules.Add(new PSInboundNatRule {
                        Id = natruleId
                    });
                }
            }

            if (this.ApplicationGatewayBackendAddressPoolId != null)
            {
                nicIpConfiguration.ApplicationGatewayBackendAddressPools = new List <PSApplicationGatewayBackendAddressPool>();
                foreach (var appgwBepoolId in this.ApplicationGatewayBackendAddressPoolId)
                {
                    nicIpConfiguration.ApplicationGatewayBackendAddressPools.Add(new PSApplicationGatewayBackendAddressPool {
                        Id = appgwBepoolId
                    });
                }
            }

            if (this.DnsServer != null || this.InternalDnsNameLabel != null)
            {
                networkInterface.DnsSettings = new PSNetworkInterfaceDnsSettings();
                if (this.DnsServer != null)
                {
                    networkInterface.DnsSettings.DnsServers = this.DnsServer;
                }
                if (this.InternalDnsNameLabel != null)
                {
                    networkInterface.DnsSettings.InternalDnsNameLabel = this.InternalDnsNameLabel;
                }
            }

            networkInterface.IpConfigurations.Add(nicIpConfiguration);

            var networkInterfaceModel = Mapper.Map <MNM.NetworkInterface>(networkInterface);

            networkInterfaceModel.Tags = TagsConversionHelper.CreateTagDictionary(this.Tag, validate: true);

            this.NetworkInterfaceClient.CreateOrUpdate(this.ResourceGroupName, this.Name, networkInterfaceModel);

            var getNetworkInterface = this.GetNetworkInterface(this.ResourceGroupName, this.Name);

            return(getNetworkInterface);
        }
Esempio n. 2
0
        private PSNetworkInterface CreateNetworkInterface()
        {
            var networkInterface = new PSNetworkInterface();

            networkInterface.Name = this.Name;

            networkInterface.Location = this.Location;

            networkInterface.EnableIPForwarding          = this.EnableIPForwarding.IsPresent;
            networkInterface.EnableAcceleratedNetworking = this.EnableAcceleratedNetworking.IsPresent;

            // Get the subnetId and publicIpAddressId from the object if specified
            if (ParameterSetName.Contains(Microsoft.Azure.Commands.Network.Properties.Resources.SetByIpConfiguration))
            {
                networkInterface.IpConfigurations = this.IpConfiguration?.ToList();

                if (string.Equals(ParameterSetName, Microsoft.Azure.Commands.Network.Properties.Resources.SetByIpConfigurationResourceId))
                {
                    if (this.NetworkSecurityGroup != null)
                    {
                        this.NetworkSecurityGroupId = this.NetworkSecurityGroup.Id;
                    }
                }
            }
            else
            {
                if (string.Equals(ParameterSetName, Microsoft.Azure.Commands.Network.Properties.Resources.SetByResource))
                {
                    this.SubnetId = this.Subnet.Id;

                    if (this.PublicIpAddress != null)
                    {
                        this.PublicIpAddressId = this.PublicIpAddress.Id;
                    }

                    if (this.NetworkSecurityGroup != null)
                    {
                        this.NetworkSecurityGroupId = this.NetworkSecurityGroup.Id;
                    }

                    if (this.LoadBalancerBackendAddressPool != null)
                    {
                        var poolIds = new List <string>();
                        foreach (var bepool in this.LoadBalancerBackendAddressPool)
                        {
                            poolIds.Add(bepool.Id);
                        }
                        this.LoadBalancerBackendAddressPoolId = poolIds.ToArray();
                    }

                    if (this.LoadBalancerInboundNatRule != null)
                    {
                        var lbNatIds = new List <string>();
                        foreach (var natRule in this.LoadBalancerInboundNatRule)
                        {
                            lbNatIds.Add(natRule.Id);
                        }
                        LoadBalancerInboundNatRuleId = lbNatIds.ToArray();
                    }

                    if (this.ApplicationGatewayBackendAddressPool != null)
                    {
                        var appGwPoolIds = new List <string>();
                        foreach (var appgwBepool in this.ApplicationGatewayBackendAddressPool)
                        {
                            appGwPoolIds.Add(appgwBepool.Id);
                        }
                        ApplicationGatewayBackendAddressPoolId = appGwPoolIds.ToArray();
                    }

                    if (this.ApplicationSecurityGroup != null)
                    {
                        var groupIds = new List <string>();
                        foreach (var asg in this.ApplicationSecurityGroup)
                        {
                            groupIds.Add(asg.Id);
                        }
                        ApplicationSecurityGroupId = groupIds.ToArray();
                    }
                }

                var nicIpConfiguration = new PSNetworkInterfaceIPConfiguration();
                nicIpConfiguration.Name = string.IsNullOrEmpty(this.IpConfigurationName) ? "ipconfig1" : this.IpConfigurationName;
                nicIpConfiguration.PrivateIpAllocationMethod = MNM.IPAllocationMethod.Dynamic;
                nicIpConfiguration.Primary = true;
                // Uncomment when ipv6 is supported as standalone ipconfig in a nic
                // nicIpConfiguration.PrivateIpAddressVersion = this.PrivateIpAddressVersion;

                if (!string.IsNullOrEmpty(this.PrivateIpAddress))
                {
                    nicIpConfiguration.PrivateIpAddress          = this.PrivateIpAddress;
                    nicIpConfiguration.PrivateIpAllocationMethod = MNM.IPAllocationMethod.Static;
                }

                nicIpConfiguration.Subnet    = new PSSubnet();
                nicIpConfiguration.Subnet.Id = this.SubnetId;

                if (!string.IsNullOrEmpty(this.PublicIpAddressId))
                {
                    nicIpConfiguration.PublicIpAddress    = new PSPublicIpAddress();
                    nicIpConfiguration.PublicIpAddress.Id = this.PublicIpAddressId;
                }

                if (this.LoadBalancerBackendAddressPoolId != null)
                {
                    nicIpConfiguration.LoadBalancerBackendAddressPools = new List <PSBackendAddressPool>();
                    foreach (var bepoolId in this.LoadBalancerBackendAddressPoolId)
                    {
                        nicIpConfiguration.LoadBalancerBackendAddressPools.Add(new PSBackendAddressPool {
                            Id = bepoolId
                        });
                    }
                }

                if (this.LoadBalancerInboundNatRuleId != null)
                {
                    nicIpConfiguration.LoadBalancerInboundNatRules = new List <PSInboundNatRule>();
                    foreach (var natruleId in this.LoadBalancerInboundNatRuleId)
                    {
                        nicIpConfiguration.LoadBalancerInboundNatRules.Add(new PSInboundNatRule {
                            Id = natruleId
                        });
                    }
                }

                if (this.ApplicationGatewayBackendAddressPoolId != null)
                {
                    nicIpConfiguration.ApplicationGatewayBackendAddressPools = new List <PSApplicationGatewayBackendAddressPool>();
                    foreach (var appgwBepoolId in this.ApplicationGatewayBackendAddressPoolId)
                    {
                        nicIpConfiguration.ApplicationGatewayBackendAddressPools.Add(new PSApplicationGatewayBackendAddressPool {
                            Id = appgwBepoolId
                        });
                    }
                }

                if (this.ApplicationSecurityGroupId != null)
                {
                    nicIpConfiguration.ApplicationSecurityGroups = new List <PSApplicationSecurityGroup>();
                    foreach (var id in this.ApplicationSecurityGroupId)
                    {
                        nicIpConfiguration.ApplicationSecurityGroups.Add(new PSApplicationSecurityGroup {
                            Id = id
                        });
                    }
                }

                networkInterface.IpConfigurations = new List <PSNetworkInterfaceIPConfiguration>();
                networkInterface.IpConfigurations.Add(nicIpConfiguration);
            }

            if (this.DnsServer != null || this.InternalDnsNameLabel != null)
            {
                networkInterface.DnsSettings = new PSNetworkInterfaceDnsSettings();
                if (this.DnsServer != null)
                {
                    networkInterface.DnsSettings.DnsServers = this.DnsServer?.ToList();
                }
                if (this.InternalDnsNameLabel != null)
                {
                    networkInterface.DnsSettings.InternalDnsNameLabel = this.InternalDnsNameLabel;
                }
            }

            if (!string.IsNullOrEmpty(this.NetworkSecurityGroupId))
            {
                networkInterface.NetworkSecurityGroup    = new PSNetworkSecurityGroup();
                networkInterface.NetworkSecurityGroup.Id = this.NetworkSecurityGroupId;
            }

            var networkInterfaceModel = NetworkResourceManagerProfile.Mapper.Map <MNM.NetworkInterface>(networkInterface);

            this.NullifyApplicationSecurityGroupIfAbsent(networkInterfaceModel);

            networkInterfaceModel.Tags = TagsConversionHelper.CreateTagDictionary(this.Tag, validate: true);

            this.NetworkInterfaceClient.CreateOrUpdate(this.ResourceGroupName, this.Name, networkInterfaceModel);

            var getNetworkInterface = this.GetNetworkInterface(this.ResourceGroupName, this.Name);

            return(getNetworkInterface);
        }