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;
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)
{
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 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 });
}
}
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;
}
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 = 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;
}
}