Exemple #1
0
        public override void 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;

                //// At this point, the resource id should not be null. If it is, customer did not specify a valid resource to delete.
                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);
            }

            if (parentGateway.NatRules.Any())
            {
                var vpnGatewayNatRuleToRemove = parentGateway.NatRules.FirstOrDefault(natRule => natRule.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase));
                if (vpnGatewayNatRuleToRemove != null)
                {
                    base.Execute();

                    ConfirmAction(
                        Force.IsPresent,
                        string.Format(Properties.Resources.RemovingResource, this.Name),
                        Properties.Resources.RemoveResourceMessage,
                        this.Name,
                        () =>
                    {
                        parentGateway.NatRules.Remove(vpnGatewayNatRuleToRemove);
                        this.CreateOrUpdateVpnGateway(this.ResourceGroupName, this.ParentResourceName, parentGateway, parentGateway.Tag);
                    });
                }
            }

            if (PassThru)
            {
                WriteObject(true);
            }
        }
Exemple #2
0
        public override void Execute()
        {
            PSVpnGateway existingVpnGateway = null;

            if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnGatewayObject))
            {
                existingVpnGateway     = this.InputObject;
                this.ResourceGroupName = this.InputObject.ResourceGroupName;
                this.Name = this.InputObject.Name;
            }
            else
            {
                if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnGatewayResourceId))
                {
                    var parsedResourceId = new ResourceIdentifier(ResourceId);
                    Name = parsedResourceId.ResourceName;
                    ResourceGroupName = parsedResourceId.ResourceGroupName;
                }

                existingVpnGateway = this.GetVpnGateway(this.ResourceGroupName, this.Name);
            }

            if (existingVpnGateway == null)
            {
                throw new PSArgumentException(Properties.Resources.VpnGatewayNotFound);
            }

            VpnGatewayPacketCaptureStopParameters parameters = new VpnGatewayPacketCaptureStopParameters();

            if (this.SasUrl != null)
            {
                parameters.SasUrl = SasUrl;
            }

            base.Execute();
            if (ShouldProcess(this.Name, String.Format(Properties.Resources.CreatingLongRunningOperationMessage, this.ResourceGroupName, this.Name)))
            {
                WriteVerbose(String.Format(Properties.Resources.CreatingLongRunningOperationMessage, this.ResourceGroupName, this.Name));
                PSVpnGatewayPacketCaptureResult output = new PSVpnGatewayPacketCaptureResult()
                {
                    Name = existingVpnGateway.Name,
                    ResourceGroupName = existingVpnGateway.ResourceGroupName,
                    Tag          = existingVpnGateway.Tag,
                    ResourceGuid = existingVpnGateway.ResourceGuid,
                    Location     = existingVpnGateway.Location,
                };
                output.StartTime = DateTime.UtcNow;
                var result = this.VpnGatewayClient.StopPacketCapture(this.ResourceGroupName, this.Name, parameters);
                output.EndTime = DateTime.UtcNow;
                WriteObject(output);
            }
        }
Exemple #3
0
        public PSVpnGateway CreateOrUpdateVpnGateway(string resourceGroupName, string vpnGatewayName, PSVpnGateway vpnGateway, Hashtable tags)
        {
            var vpnGatewayModel = NetworkResourceManagerProfile.Mapper.Map <MNM.VpnGateway>(vpnGateway);

            vpnGatewayModel.Tags = TagsConversionHelper.CreateTagDictionary(tags, validate: true);

            var          vpnGatewayCreatedOrUpdated = this.VpnGatewayClient.CreateOrUpdate(resourceGroupName, vpnGatewayName, vpnGatewayModel);
            PSVpnGateway gatewayToReturn            = this.ToPsVpnGateway(vpnGatewayCreatedOrUpdated);

            gatewayToReturn.ResourceGroupName = resourceGroupName;

            return(gatewayToReturn);
        }
        public override void Execute()
        {
            PSVpnGateway existingVpnGateway = null;

            if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnGatewayObject))
            {
                existingVpnGateway     = this.InputObject;
                this.ResourceGroupName = this.InputObject.ResourceGroupName;
                this.Name = this.InputObject.Name;
            }
            else
            {
                if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnGatewayResourceId))
                {
                    var parsedResourceId = new ResourceIdentifier(ResourceId);
                    Name = parsedResourceId.ResourceName;
                    ResourceGroupName = parsedResourceId.ResourceGroupName;
                }

                existingVpnGateway = this.GetVpnGateway(this.ResourceGroupName, this.Name);
            }

            if (existingVpnGateway == null)
            {
                throw new PSArgumentException(Properties.Resources.VpnGatewayNotFound);
            }

            //// Modify scale unit if specified
            if (this.VpnGatewayScaleUnit > 0)
            {
                existingVpnGateway.VpnGatewayScaleUnit = Convert.ToInt32(this.VpnGatewayScaleUnit);
            }

            //// Modify the connections
            if (this.VpnConnection != null)
            {
                existingVpnGateway.Connections = new List <PSVpnConnection>();
                existingVpnGateway.Connections.AddRange(this.VpnConnection);
            }

            ConfirmAction(
                Properties.Resources.SettingResourceMessage,
                this.Name,
                () =>
            {
                WriteVerbose(String.Format(Properties.Resources.UpdatingLongRunningOperationMessage, this.ResourceGroupName, this.Name));
                WriteObject(this.CreateOrUpdateVpnGateway(this.ResourceGroupName, this.Name, existingVpnGateway, this.Tag));
            });
        }
Exemple #5
0
        public List <PSVpnGateway> ListVpnGateways(string resourceGroupName)
        {
            var vpnGateways = string.IsNullOrWhiteSpace(resourceGroupName) ?
                              this.VpnGatewayClient.List() :                                //// List by sub id
                              this.VpnGatewayClient.ListByResourceGroup(resourceGroupName); //// List by RG name

            List <PSVpnGateway> gatewaysToReturn = new List <PSVpnGateway>();

            if (vpnGateways != null)
            {
                foreach (MNM.VpnGateway gateway in vpnGateways)
                {
                    PSVpnGateway gatewayToReturn = ToPsVpnGateway(gateway);
                    gatewayToReturn.ResourceGroupName = resourceGroupName;
                    gatewaysToReturn.Add(gatewayToReturn);
                }
            }

            return(gatewaysToReturn);
        }
        public List <PSVpnGateway> ListVpnGateways(string resourceGroupName)
        {
            var vpnGateways = ShouldListBySubscription(resourceGroupName, null) ?
                              this.VpnGatewayClient.List() :                                //// List by sub id
                              this.VpnGatewayClient.ListByResourceGroup(resourceGroupName); //// List by RG name

            List <PSVpnGateway> gatewaysToReturn = new List <PSVpnGateway>();

            if (vpnGateways != null)
            {
                foreach (MNM.VpnGateway gateway in vpnGateways)
                {
                    PSVpnGateway gatewayToReturn = ToPsVpnGateway(gateway);
                    gatewayToReturn.ResourceGroupName = NetworkBaseCmdlet.GetResourceGroup(gateway.Id);
                    gatewaysToReturn.Add(gatewayToReturn);
                }
            }

            return(gatewaysToReturn);
        }
        public override void Execute()
        {
            PSVpnGateway existingVpnGateway = null;

            if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnGatewayObject))
            {
                existingVpnGateway     = this.InputObject;
                this.ResourceGroupName = this.InputObject.ResourceGroupName;
                this.Name = this.InputObject.Name;
            }
            else
            {
                if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnGatewayResourceId))
                {
                    var parsedResourceId = new ResourceIdentifier(ResourceId);
                    Name = parsedResourceId.ResourceName;
                    ResourceGroupName = parsedResourceId.ResourceGroupName;
                }

                existingVpnGateway = this.GetVpnGateway(this.ResourceGroupName, this.Name);
            }

            if (existingVpnGateway == null)
            {
                throw new PSArgumentException(Properties.Resources.VpnGatewayNotFound);
            }
            string shouldProcessMessage = string.Format("Execute {0}VpnGateway for ResourceGroupName {1} VpnGateway {2}", ResourceManager.Common.AzureRMConstants.AzureRMPrefix, this.ResourceGroupName, this.Name);

            if (ShouldProcess(shouldProcessMessage, VerbsCommon.Reset))
            {
                this.VpnGatewayClient.Reset(this.ResourceGroupName, this.Name);

                var getVpnGateway = this.GetVpnGateway(this.ResourceGroupName, this.Name);
                WriteObject(getVpnGateway);
            }
        }
Exemple #8
0
        private PSVpnConnection CreateVpnConnection()
        {
            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.VpnGatewayRequiredToCreateVpnConnection);
            }

            if (this.IsVpnConnectionPresent(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.Connections == null)
            {
                parentVpnGateway.Connections = new List <PSVpnConnection>();
            }

            PSVpnConnection vpnConnection = new PSVpnConnection
            {
                Name      = this.Name,
                EnableBgp = this.EnableBgp.IsPresent,
                UseLocalAzureIpAddress = this.UseLocalAzureIpAddress.IsPresent
            };

            //// Resolve the VpnSite reference
            //// And set it in the VpnConnection object.
            string vpnSiteResolvedId = null;

            if (ParameterSetName.Contains(CortexParameterSetNames.ByVpnSiteObject))
            {
                vpnSiteResolvedId = this.VpnSite.Id;
            }
            else if (ParameterSetName.Contains(CortexParameterSetNames.ByVpnSiteResourceId))
            {
                vpnSiteResolvedId = this.VpnSiteId;
            }

            if (string.IsNullOrWhiteSpace(vpnSiteResolvedId))
            {
                throw new PSArgumentException(Properties.Resources.VpnSiteRequiredForVpnConnection);
            }

            //// Let's not resolve the vpnSite here. If this does not exist, NRP/GWM will fail the call.
            vpnConnection.RemoteVpnSite = new PSResourceId()
            {
                Id = vpnSiteResolvedId
            };

            //// Set the shared key, if specified
            if (this.SharedKey != null)
            {
                vpnConnection.SharedKey = SecureStringExtensions.ConvertToString(this.SharedKey);
            }

            if (!String.IsNullOrEmpty(this.VpnConnectionProtocolType))
            {
                vpnConnection.VpnConnectionProtocolType = this.VpnConnectionProtocolType;
            }

            //// Connection bandwidth
            vpnConnection.ConnectionBandwidth = this.ConnectionBandwidthInMbps > 0 ?
                                                Convert.ToInt32(this.ConnectionBandwidthInMbps) :
                                                20;

            if (this.IpSecPolicy != null)
            {
                vpnConnection.IpsecPolicies = new List <PSIpsecPolicy> {
                    this.IpSecPolicy
                };
            }

            parentVpnGateway.Connections.Add(vpnConnection);

            WriteVerbose(string.Format(Properties.Resources.CreatingLongRunningOperationMessage, this.ResourceGroupName, this.Name));

            PSVpnConnection connectionToReturn = 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);
                connectionToReturn             = createdOrUpdatedVpnGateway.Connections.Where(connection => connection.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
            });

            return(connectionToReturn);
        }
        public override void Execute()
        {
            base.Execute();

            if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnConnectionName, StringComparison.OrdinalIgnoreCase))
            {
                this.ResourceGroupName  = this.ResourceGroupName;
                this.ParentResourceName = this.ParentResourceName;
                this.Name = this.Name;
            }
            else if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnConnectionObject, StringComparison.OrdinalIgnoreCase))
            {
                this.ResourceId = this.InputObject.Id;

                if (string.IsNullOrWhiteSpace(this.ResourceId))
                {
                    throw new PSArgumentException(Properties.Resources.VpnConnectionNotFound);
                }

                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.Connections == null ||
                !parentGateway.Connections.Any(connection => connection.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase)))
            {
                throw new PSArgumentException(Properties.Resources.VpnConnectionNotFound);
            }

            var vpnConnectionToModify = parentGateway.Connections.FirstOrDefault(connection => connection.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase));

            if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnConnectionObject, StringComparison.OrdinalIgnoreCase))
            {
                vpnConnectionToModify = this.InputObject;
            }

            if (this.SharedKey != null)
            {
                vpnConnectionToModify.SharedKey = SecureStringExtensions.ConvertToString(this.SharedKey);
            }

            if (this.ConnectionBandwidthInMbps > 0)
            {
                vpnConnectionToModify.ConnectionBandwidth = Convert.ToInt32(this.ConnectionBandwidthInMbps);
            }

            if (this.EnableBgp.HasValue)
            {
                vpnConnectionToModify.EnableBgp = this.EnableBgp.Value;
            }

            if (this.IpSecPolicy != null)
            {
                vpnConnectionToModify.IpsecPolicies = new List <PSIpsecPolicy> {
                    this.IpSecPolicy
                };
            }

            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.Connections.Where(connection => connection.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase)).FirstOrDefault());
            });
        }
        public override void Execute()
        {
            base.Execute();

            if (this.IsVpnGatewayPresent(this.ResourceGroupName, this.Name))
            {
                throw new PSArgumentException(string.Format(Properties.Resources.ResourceAlreadyPresentInResourceGroup, this.Name, this.ResourceGroupName));
            }

            var vpnGateway = new PSVpnGateway();

            vpnGateway.Name = this.Name;
            vpnGateway.ResourceGroupName = this.ResourceGroupName;
            vpnGateway.VirtualHub        = null;

            //// Resolve and Set the virtual hub
            if (ParameterSetName.Equals(CortexParameterSetNames.ByVirtualHubObject, StringComparison.OrdinalIgnoreCase))
            {
                this.VirtualHubName = this.VirtualHub.Name;
            }
            else if (ParameterSetName.Equals(CortexParameterSetNames.ByVirtualHubResourceId, StringComparison.OrdinalIgnoreCase))
            {
                var parsedResourceId = new ResourceIdentifier(this.VirtualHubId);
                this.VirtualHubName = parsedResourceId.ResourceName;
            }

            //// At this point, we should have the virtual hub name resolved. Fail this operation if it is not.
            if (string.IsNullOrWhiteSpace(this.VirtualHubName))
            {
                throw new PSArgumentException(Properties.Resources.VirtualHubRequiredForVpnGateway);
            }

            var resolvedVirtualHub = new VirtualHubBaseCmdlet().GetVirtualHub(this.ResourceGroupName, this.VirtualHubName);

            vpnGateway.Location   = resolvedVirtualHub.Location;
            vpnGateway.VirtualHub = new PSResourceId()
            {
                Id = resolvedVirtualHub.Id
            };

            //// VpnConnections, if specified
            vpnGateway.Connections = new List <PSVpnConnection>();
            if (this.VpnConnection != null && this.VpnConnection.Any())
            {
                vpnGateway.Connections.AddRange(this.VpnConnection);
            }

            //// Scale unit, if specified
            vpnGateway.VpnGatewayScaleUnit = 0;
            if (this.VpnGatewayScaleUnit > 0)
            {
                vpnGateway.VpnGatewayScaleUnit = Convert.ToInt32(this.VpnGatewayScaleUnit);
            }

            vpnGateway.BgpSettings = null;

            ConfirmAction(
                Properties.Resources.CreatingResourceMessage,
                this.Name,
                () =>
            {
                WriteVerbose(String.Format(Properties.Resources.CreatingLongRunningOperationMessage, this.ResourceGroupName, this.Name));
                WriteObject(this.CreateOrUpdateVpnGateway(this.ResourceGroupName, this.Name, vpnGateway, this.Tag));
            });
        }
Exemple #11
0
        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(CortexParameterSetNames.ByVpnConnectionName, StringComparison.OrdinalIgnoreCase))
            {
                this.ResourceGroupName  = this.ResourceGroupName;
                this.ParentResourceName = this.ParentResourceName;
                this.Name = this.Name;
            }
            else if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnConnectionObject, StringComparison.OrdinalIgnoreCase))
            {
                this.ResourceId = this.InputObject.Id;

                if (string.IsNullOrWhiteSpace(this.ResourceId))
                {
                    throw new PSArgumentException(Properties.Resources.VpnConnectionNotFound);
                }

                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.Connections == null ||
                !parentGateway.Connections.Any(connection => connection.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase)))
            {
                throw new PSArgumentException(Properties.Resources.VpnConnectionNotFound);
            }

            var vpnConnectionToModify = parentGateway.Connections.FirstOrDefault(connection => connection.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase));

            if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnConnectionObject, StringComparison.OrdinalIgnoreCase))
            {
                vpnConnectionToModify = this.InputObject;
            }

            if (vpnConnectionToModify.VpnLinkConnections != null && vpnConnectionToModify.VpnLinkConnections.Any())
            {
                //// Use only link connection properties instead of vpn connection properties.
                if (this.SharedKey != null || this.ConnectionBandwidthInMbps > 0 || this.EnableBgp.HasValue || this.UseLocalAzureIpAddress.HasValue || this.UsePolicyBasedTrafficSelectors.HasValue || this.IpSecPolicy != null)
                {
                    throw new PSArgumentException(Properties.Resources.VpnConnectionPropertyIsDeprecated);
                }
            }

            if (this.SharedKey != null)
            {
                vpnConnectionToModify.SharedKey = SecureStringExtensions.ConvertToString(this.SharedKey);
            }

            if (this.ConnectionBandwidthInMbps > 0)
            {
                vpnConnectionToModify.ConnectionBandwidth = Convert.ToInt32(this.ConnectionBandwidthInMbps);
            }

            if (this.EnableBgp.HasValue)
            {
                vpnConnectionToModify.EnableBgp = this.EnableBgp.Value;
            }

            if (this.UseLocalAzureIpAddress.HasValue)
            {
                vpnConnectionToModify.UseLocalAzureIpAddress = this.UseLocalAzureIpAddress.Value;
            }

            if (this.UsePolicyBasedTrafficSelectors.HasValue)
            {
                vpnConnectionToModify.UsePolicyBasedTrafficSelectors = this.UsePolicyBasedTrafficSelectors.Value;
            }

            if (this.IpSecPolicy != null)
            {
                vpnConnectionToModify.IpsecPolicies = new List <PSIpsecPolicy> {
                    this.IpSecPolicy
                };
            }

            //// Modify the linkconnections
            if (this.VpnSiteLinkConnection != null)
            {
                vpnConnectionToModify.VpnLinkConnections = new List <PSVpnSiteLinkConnection>();
                vpnConnectionToModify.VpnLinkConnections.AddRange(this.VpnSiteLinkConnection);
            }

            if (this.EnableInternetSecurity.HasValue)
            {
                vpnConnectionToModify.EnableInternetSecurity = this.EnableInternetSecurity.Value;
            }

            if (this.RoutingConfiguration != null)
            {
                if (this.RoutingConfiguration.VnetRoutes != null && this.RoutingConfiguration.VnetRoutes.StaticRoutes != null && this.RoutingConfiguration.VnetRoutes.StaticRoutes.Any())
                {
                    throw new PSArgumentException(Properties.Resources.StaticRoutesNotSupportedForThisRoutingConfiguration);
                }

                vpnConnectionToModify.RoutingConfiguration = RoutingConfiguration;
            }

            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.Connections.Where(connection => connection.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase)).FirstOrDefault());
            });
        }
        public override void Execute()
        {
            PSVpnGateway existingVpnGateway = null;

            if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnGatewayObject))
            {
                existingVpnGateway     = this.InputObject;
                this.ResourceGroupName = this.InputObject.ResourceGroupName;
                this.Name = this.InputObject.Name;
            }
            else
            {
                if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnGatewayResourceId))
                {
                    var parsedResourceId = new ResourceIdentifier(ResourceId);
                    Name = parsedResourceId.ResourceName;
                    ResourceGroupName = parsedResourceId.ResourceGroupName;
                }

                existingVpnGateway = this.GetVpnGateway(this.ResourceGroupName, this.Name);
            }

            if (existingVpnGateway == null)
            {
                throw new PSArgumentException(Properties.Resources.VpnGatewayNotFound);
            }

            // Enable the Bgp route translation for NAT on this VpnGateway, if specified by the customer.
            if (this.EnableBgpRouteTranslationForNat.HasValue)
            {
                existingVpnGateway.EnableBgpRouteTranslationForNat = this.EnableBgpRouteTranslationForNat.Value;
            }

            //// Modify scale unit if specified
            if (this.VpnGatewayScaleUnit > 0)
            {
                existingVpnGateway.VpnGatewayScaleUnit = Convert.ToInt32(this.VpnGatewayScaleUnit);
            }

            //// Modify the connections
            if (this.VpnConnection != null)
            {
                existingVpnGateway.Connections = new List <PSVpnConnection>();
                existingVpnGateway.Connections.AddRange(this.VpnConnection);
            }

            //// Modify the natRules
            existingVpnGateway.NatRules = new List <PSVpnGatewayNatRule>();
            if (this.VpnGatewayNatRule != null && this.VpnGatewayNatRule.Any())
            {
                existingVpnGateway.NatRules.AddRange(this.VpnGatewayNatRule);
            }

            //// Modify BgpPeeringAddress
            if (this.BgpPeeringAddress != null)
            {
                if (existingVpnGateway.BgpSettings == null)
                {
                    existingVpnGateway.BgpSettings = new PSBgpSettings();
                }

                if (existingVpnGateway.BgpSettings.BgpPeeringAddresses == null)
                {
                    existingVpnGateway.BgpSettings.BgpPeeringAddresses = new List <PSIpConfigurationBgpPeeringAddress>();

                    foreach (var address in this.BgpPeeringAddress)
                    {
                        existingVpnGateway.BgpSettings.BgpPeeringAddresses.Add(address);
                    }
                }
                else
                {
                    foreach (var address in this.BgpPeeringAddress)
                    {
                        bool isGatewayIpConfigurationExists = existingVpnGateway.BgpSettings.BgpPeeringAddresses.Any(
                            ipconfaddress => ipconfaddress.IpconfigurationId.Equals(address.IpconfigurationId, StringComparison.OrdinalIgnoreCase));

                        if (isGatewayIpConfigurationExists)
                        {
                            var bgpPeeringPropertiesInRequest = existingVpnGateway.BgpSettings.BgpPeeringAddresses.FirstOrDefault(
                                x => x.IpconfigurationId.Equals(address.IpconfigurationId, StringComparison.OrdinalIgnoreCase));

                            bgpPeeringPropertiesInRequest.CustomBgpIpAddresses = address.CustomBgpIpAddresses;
                        }
                        else
                        {
                            existingVpnGateway.BgpSettings.BgpPeeringAddresses.Add(address);
                        }
                    }
                }
            }

            ConfirmAction(
                Properties.Resources.SettingResourceMessage,
                this.Name,
                () =>
            {
                WriteVerbose(String.Format(Properties.Resources.UpdatingLongRunningOperationMessage, this.ResourceGroupName, this.Name));
                WriteObject(this.CreateOrUpdateVpnGateway(this.ResourceGroupName, this.Name, existingVpnGateway, this.Tag));
            });
        }
        public override void Execute()
        {
            if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnConnectionName, StringComparison.OrdinalIgnoreCase))
            {
                this.ResourceGroupName  = this.ResourceGroupName;
                this.ParentResourceName = this.ParentResourceName;
                this.Name = this.Name;
            }
            else if (ParameterSetName.Equals(CortexParameterSetNames.ByVpnConnectionObject, StringComparison.OrdinalIgnoreCase))
            {
                this.ResourceId = this.InputObject.Id;

                if (string.IsNullOrWhiteSpace(this.ResourceId))
                {
                    throw new PSArgumentException(Properties.Resources.VpnConnectionNotFound);
                }

                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.Connections == null ||
                !parentGateway.Connections.Any(connection => connection.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase)))
            {
                throw new PSArgumentException(Properties.Resources.VpnConnectionNotFound);
            }

            var existingConnection = parentGateway.Connections.FirstOrDefault(connection => connection.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase));

            VpnConnectionPacketCaptureStartParameters parameters = new VpnConnectionPacketCaptureStartParameters();

            if (this.FilterData != null)
            {
                parameters.FilterData = FilterData;
            }

            if (this.LinkConnectionName != null)
            {
                parameters.LinkConnectionNames = LinkConnectionName.Split(',').Select(x => x.Trim()).ToList();
            }

            base.Execute();

            if (ShouldProcess(this.Name, String.Format(Properties.Resources.CreatingLongRunningOperationMessage, this.ResourceGroupName, this.Name)))
            {
                WriteVerbose(String.Format(Properties.Resources.CreatingLongRunningOperationMessage, this.ResourceGroupName, this.Name));
                PSVpnConnectionPacketCaptureResult output = new PSVpnConnectionPacketCaptureResult()
                {
                    Name = existingConnection.Name,
                    ResourceGroupName = parentGateway.ResourceGroupName,
                    Tag                = parentGateway.Tag,
                    ResourceGuid       = existingConnection.Id,
                    Location           = parentGateway.Location,
                    LinkConnectionName = this.LinkConnectionName
                };
                output.StartTime = DateTime.UtcNow;
                var result = this.VpnConnectionClient.StartPacketCapture(this.ResourceGroupName, ParentResourceName, this.Name, parameters);
                output.EndTime = DateTime.UtcNow;
                WriteObject(output);
            }
        }