private PSVirtualNetworkGatewayNatRule CreateVpnGatewayNatRule() { PSVirtualNetworkGatewayNatRule gatewayNatRule = new PSVirtualNetworkGatewayNatRule { Name = this.Name, Mode = this.Mode, VirtualNetworkGatewayNatRulePropertiesType = this.Type, InternalMappings = new List <PSVpnNatRuleMapping>(), ExternalMappings = new List <PSVpnNatRuleMapping>(), IpConfigurationId = this.IpConfigurationId, }; if (this.InternalMapping != null) { foreach (string internalMappingSubnet in this.InternalMapping) { var internalMapping = new PSVpnNatRuleMapping(); internalMapping.AddressSpace = internalMappingSubnet; gatewayNatRule.InternalMappings.Add(internalMapping); } } if (this.ExternalMapping != null) { foreach (string externalMappingSubnet in this.ExternalMapping) { var externalMapping = new PSVpnNatRuleMapping(); externalMapping.AddressSpace = externalMappingSubnet; gatewayNatRule.ExternalMappings.Add(externalMapping); } } return(gatewayNatRule); }
private PSVirtualNetworkGatewayNatRule CreateVpnGatewayNatRule() { PSVirtualNetworkGatewayNatRule gatewayNatRule = new PSVirtualNetworkGatewayNatRule { Name = this.Name, Mode = this.Mode, VirtualNetworkGatewayNatRulePropertiesType = this.Type, InternalMappings = new List <PSVpnNatRuleMapping>(), ExternalMappings = new List <PSVpnNatRuleMapping>(), IpConfigurationId = this.IpConfigurationId, }; if (this.InternalMapping != null) { foreach (string internalMappingSubnet in this.InternalMapping) { var internalMapping = new PSVpnNatRuleMapping(); internalMapping.AddressSpace = internalMappingSubnet; gatewayNatRule.InternalMappings.Add(internalMapping); } } if (this.ExternalMapping != null) { foreach (string externalMappingSubnet in this.ExternalMapping) { var externalMapping = new PSVpnNatRuleMapping(); externalMapping.AddressSpace = externalMappingSubnet; gatewayNatRule.ExternalMappings.Add(externalMapping); } } if (this.InternalPortRange != null) { if (gatewayNatRule.InternalMappings.Count < this.InternalPortRange.Count()) { throw new PSArgumentException(string.Format(Properties.Resources.VpnNatRuleUnmatchedPortRange, nameof(InternalPortRange), nameof(InternalMapping))); } for (int i = 0; i < this.InternalPortRange.Count(); i++) { gatewayNatRule.InternalMappings[i].PortRange = this.InternalPortRange[i]; } } if (this.ExternalPortRange != null) { if (gatewayNatRule.ExternalMappings.Count < this.ExternalPortRange.Count()) { throw new PSArgumentException(string.Format(Properties.Resources.VpnNatRuleUnmatchedPortRange, nameof(ExternalPortRange), nameof(ExternalMapping))); } for (int i = 0; i < this.ExternalPortRange.Count(); i++) { gatewayNatRule.ExternalMappings[i].PortRange = this.ExternalPortRange[i]; } } return(gatewayNatRule); }
public override void Execute() { base.Execute(); if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnGatewayNatRuleName, StringComparison.OrdinalIgnoreCase)) { this.ResourceGroupName = this.ResourceGroupName; this.ParentResourceName = this.ParentResourceName; this.Name = this.Name; } else if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnGatewayNatRuleObject, StringComparison.OrdinalIgnoreCase)) { this.ResourceId = this.InputObject.Id; if (string.IsNullOrWhiteSpace(this.ResourceId)) { throw new PSArgumentException(Properties.Resources.VpnGatewayNatRuleNotFound); } var parsedResourceId = new ResourceIdentifier(this.ResourceId); this.ResourceGroupName = parsedResourceId.ResourceGroupName; this.ParentResourceName = parsedResourceId.ParentResource.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Last(); this.Name = parsedResourceId.ResourceName; } //// Get the vpngateway object - this will throw not found if the object is not found PSVpnGateway parentGateway = this.GetVpnGateway(this.ResourceGroupName, this.ParentResourceName); if (parentGateway == null || parentGateway.NatRules == null || !parentGateway.NatRules.Any(natRule => natRule.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase))) { throw new PSArgumentException(Properties.Resources.VpnGatewayNatRuleNotFound); } var vpnGatewayNatRuleToModify = parentGateway.NatRules.FirstOrDefault(natRule => natRule.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase)); if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnGatewayNatRuleObject, StringComparison.OrdinalIgnoreCase)) { vpnGatewayNatRuleToModify = this.InputObject; } if (this.IpConfigurationId != null) { vpnGatewayNatRuleToModify.IpConfigurationId = IpConfigurationId; } if (this.Mode != null) { vpnGatewayNatRuleToModify.Mode = Mode; } if (this.Type != null) { vpnGatewayNatRuleToModify.VpnGatewayNatRulePropertiesType = Type; } if (this.InternalMapping != null) { vpnGatewayNatRuleToModify.InternalMappings.Clear(); foreach (string internalMappingSubnet in this.InternalMapping) { var internalMapping = new PSVpnNatRuleMapping(); internalMapping.AddressSpace = internalMappingSubnet; vpnGatewayNatRuleToModify.InternalMappings.Add(internalMapping); } } if (this.ExternalMapping != null) { vpnGatewayNatRuleToModify.ExternalMappings.Clear(); foreach (string externalMappingSubnet in this.ExternalMapping) { var externalMapping = new PSVpnNatRuleMapping(); externalMapping.AddressSpace = externalMappingSubnet; vpnGatewayNatRuleToModify.ExternalMappings.Add(externalMapping); } } if (this.InternalPortRange != null) { if (vpnGatewayNatRuleToModify.InternalMappings.Count < this.InternalPortRange.Count()) { throw new PSArgumentException(string.Format(Properties.Resources.VpnNatRuleUnmatchedPortRange, nameof(InternalPortRange), nameof(InternalMapping))); } for (int i = 0; i < this.InternalPortRange.Count(); i++) { vpnGatewayNatRuleToModify.InternalMappings[i].PortRange = this.InternalPortRange[i]; } } if (this.ExternalPortRange != null) { if (vpnGatewayNatRuleToModify.ExternalMappings.Count < this.ExternalPortRange.Count()) { throw new PSArgumentException(string.Format(Properties.Resources.VpnNatRuleUnmatchedPortRange, nameof(ExternalPortRange), nameof(ExternalMapping))); } for (int i = 0; i < this.ExternalPortRange.Count(); i++) { vpnGatewayNatRuleToModify.ExternalMappings[i].PortRange = this.ExternalPortRange[i]; } } ConfirmAction( Properties.Resources.SettingResourceMessage, this.Name, () => { WriteVerbose(String.Format(Properties.Resources.UpdatingLongRunningOperationMessage, this.ResourceGroupName, this.Name)); this.CreateOrUpdateVpnGateway(this.ResourceGroupName, this.ParentResourceName, parentGateway, parentGateway.Tag); var createdOrUpdatedVpnGateway = this.GetVpnGateway(this.ResourceGroupName, this.ParentResourceName); WriteObject(createdOrUpdatedVpnGateway.NatRules.Where(natRule => natRule.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase)).FirstOrDefault()); }); }
private PSVpnGatewayNatRule CreateVpnGatewayNatRule() { base.Execute(); PSVpnGateway parentVpnGateway = null; //// Resolve the VpnGateway if (ParameterSetName.Contains(CortexParameterSetNames.ByVpnGatewayObject)) { this.ResourceGroupName = this.ParentObject.ResourceGroupName; this.ParentResourceName = this.ParentObject.Name; } else if (ParameterSetName.Contains(CortexParameterSetNames.ByVpnGatewayResourceId)) { var parsedResourceId = new ResourceIdentifier(this.ParentResourceId); this.ResourceGroupName = parsedResourceId.ResourceGroupName; this.ParentResourceName = parsedResourceId.ResourceName; } if (string.IsNullOrWhiteSpace(this.ResourceGroupName) || string.IsNullOrWhiteSpace(this.ParentResourceName)) { throw new PSArgumentException(Properties.Resources.VpnGatewayRequiredToCreateVpnNatRule); } if (this.IsVpnGatewayNatRulePresent(this.ResourceGroupName, this.ParentResourceName, this.Name)) { throw new PSArgumentException(string.Format(Properties.Resources.ChildResourceAlreadyPresentInResourceGroup, this.Name, this.ResourceGroupName, this.ParentResourceName)); } //// At this point, we should have the resource name and the resource group for the parent VpnGateway resolved. //// This will throw not found exception if the VpnGateway does not exist parentVpnGateway = this.GetVpnGateway(this.ResourceGroupName, this.ParentResourceName); if (parentVpnGateway == null) { throw new PSArgumentException(Properties.Resources.ParentVpnGatewayNotFound); } if (parentVpnGateway.NatRules == null) { parentVpnGateway.NatRules = new List <PSVpnGatewayNatRule>(); } PSVpnGatewayNatRule vpnGatewayNatRule = new PSVpnGatewayNatRule { Name = this.Name, IpConfigurationId = this.IpConfigurationId, Mode = this.Mode, VpnGatewayNatRulePropertiesType = this.Type, InternalMappings = new List <PSVpnNatRuleMapping>(), ExternalMappings = new List <PSVpnNatRuleMapping>() }; if (this.InternalMapping != null) { foreach (string internalMappingSubnet in this.InternalMapping) { var internalMapping = new PSVpnNatRuleMapping(); internalMapping.AddressSpace = internalMappingSubnet; vpnGatewayNatRule.InternalMappings.Add(internalMapping); } } if (this.ExternalMapping != null) { foreach (string externalMappingSubnet in this.ExternalMapping) { var externalMapping = new PSVpnNatRuleMapping(); externalMapping.AddressSpace = externalMappingSubnet; vpnGatewayNatRule.ExternalMappings.Add(externalMapping); } } parentVpnGateway.NatRules.Add(vpnGatewayNatRule); WriteVerbose(string.Format(Properties.Resources.CreatingLongRunningOperationMessage, this.ResourceGroupName, this.Name)); PSVpnGatewayNatRule natRuleToReturn = null; ConfirmAction( Properties.Resources.CreatingResourceMessage, this.Name, () => { WriteVerbose(String.Format(Properties.Resources.CreatingLongRunningOperationMessage, this.ResourceGroupName, this.Name)); this.CreateOrUpdateVpnGateway(this.ResourceGroupName, this.ParentResourceName, parentVpnGateway, parentVpnGateway.Tag); var createdOrUpdatedVpnGateway = this.GetVpnGateway(this.ResourceGroupName, this.ParentResourceName); natRuleToReturn = createdOrUpdatedVpnGateway.NatRules.Where(natRule => natRule.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); }); return(natRuleToReturn); }
public override void Execute() { base.Execute(); if (ParameterSetName.Equals(VirtualNetworkGatewayParameterSets.ByVirtualNetworkGatewayNatRuleObject, StringComparison.OrdinalIgnoreCase)) { this.ResourceId = this.InputObject.Id; } if (!string.IsNullOrWhiteSpace(this.ResourceId)) { var parsedResourceId = new ResourceIdentifier(this.ResourceId); this.ResourceGroupName = parsedResourceId.ResourceGroupName; this.ParentResourceName = parsedResourceId.ParentResource.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Last(); this.Name = parsedResourceId.ResourceName; } if (string.IsNullOrWhiteSpace(this.ResourceGroupName) || string.IsNullOrWhiteSpace(this.ParentResourceName) || string.IsNullOrWhiteSpace(this.Name)) { throw new PSArgumentException(Properties.Resources.VirtualNetworkGatewayNatRuleNotFound); } //// Get the virtualNetworkgateway object - this will throw not found if the object is not found PSVirtualNetworkGateway parentGateway = this.GetVirtualNetworkGateway(this.ResourceGroupName, this.ParentResourceName); if (parentGateway == null || parentGateway.NatRules == null || !parentGateway.NatRules.Any(natRule => natRule.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase))) { throw new PSArgumentException(Properties.Resources.VirtualNetworkGatewayNatRuleNotFound); } PSVirtualNetworkGatewayNatRule natRuleToUpdate = null; natRuleToUpdate = this.GetVirtualNetworkGatewayNatRule(this.ResourceGroupName, this.ParentResourceName, this.Name); if (natRuleToUpdate == null) { throw new PSArgumentException(Properties.Resources.VirtualNetworkGatewayNatRuleNotFound); } if (this.IpConfigurationId != null) { natRuleToUpdate.IpConfigurationId = IpConfigurationId; } if (this.InternalMapping != null) { natRuleToUpdate.InternalMappings.Clear(); foreach (string internalMappingSubnet in this.InternalMapping) { var internalMapping = new PSVpnNatRuleMapping(); internalMapping.AddressSpace = internalMappingSubnet; natRuleToUpdate.InternalMappings.Add(internalMapping); } } if (this.ExternalMapping != null) { natRuleToUpdate.ExternalMappings.Clear(); foreach (string externalMappingSubnet in this.ExternalMapping) { var externalMapping = new PSVpnNatRuleMapping(); externalMapping.AddressSpace = externalMappingSubnet; natRuleToUpdate.ExternalMappings.Add(externalMapping); } } ConfirmAction( Properties.Resources.SettingResourceMessage, this.Name, () => { WriteVerbose(String.Format(Properties.Resources.UpdatingLongRunningOperationMessage, this.ResourceGroupName, this.Name)); WriteObject(this.CreateOrUpdateVirtualNetworkGatewayNatRule(this.ResourceGroupName, this.ParentResourceName, this.Name, natRuleToUpdate)); }); }