From 7503a2177392db75de38f5798978e5fa7c172610 Mon Sep 17 00:00:00 2001 From: Tejas Shah Date: Tue, 26 May 2020 15:21:48 -0700 Subject: [PATCH 1/2] Add validation to the name field for ip groups --- .../Network/IpGroup/NewIpGroupCommand.cs | 13 ++++ .../Network/Properties/Resources.Designer.cs | 62 ++++++++++--------- src/Network/Network/Properties/Resources.resx | 3 + 3 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/Network/Network/IpGroup/NewIpGroupCommand.cs b/src/Network/Network/IpGroup/NewIpGroupCommand.cs index 206a65ca688a..bb36ce59c2e1 100644 --- a/src/Network/Network/IpGroup/NewIpGroupCommand.cs +++ b/src/Network/Network/IpGroup/NewIpGroupCommand.cs @@ -17,6 +17,7 @@ using System.Collections.Generic; using System.Linq; using System.Management.Automation; +using System.Text.RegularExpressions; using Microsoft.Azure.Commands.Network.Models; using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; using Microsoft.Azure.Commands.ResourceManager.Common.Tags; @@ -88,6 +89,8 @@ public override void Execute() throw new PSArgumentException(string.Format(Properties.Resources.ResourceAlreadyPresentInResourceGroup, this.Name, this.ResourceGroupName)); } + ValidateName(this.Name); + ConfirmAction( Force.IsPresent, string.Format(Properties.Resources.OverwritingResource, Name), @@ -115,5 +118,15 @@ private PSIpGroup CreateIpGroups() this.IpGroupsClient.CreateOrUpdate(this.ResourceGroupName, this.Name, ipGroupSdkObject); return this.GetIpGroup(this.ResourceGroupName, this.Name); } + + private void ValidateName(string name) + { + var IpgroupNameRegex = new Regex("^[^_\\W]([\\w-.]{0,78}[\\w])?$"); + + if (!IpgroupNameRegex.IsMatch(name)) + { + throw new PSArgumentException(string.Format(Properties.Resources.InvalidName, this.Name)); + } + } } } diff --git a/src/Network/Network/Properties/Resources.Designer.cs b/src/Network/Network/Properties/Resources.Designer.cs index 960dc061b631..c009133ec066 100644 --- a/src/Network/Network/Properties/Resources.Designer.cs +++ b/src/Network/Network/Properties/Resources.Designer.cs @@ -528,6 +528,24 @@ internal static string EndpointWilthFilterTypeMustHaveFilterItem { } } + /// + /// Looks up a localized string similar to Circuit Connection with Name '{0}' is already added.. + /// + internal static string ExpressRouteCircuitConnectionAlreadyAdded { + get { + return ResourceManager.GetString("ExpressRouteCircuitConnectionAlreadyAdded", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Circuit Connection with Name '{0}' was not added to the private peering.. + /// + internal static string ExpressRouteCircuitConnectionNotFound { + get { + return ResourceManager.GetString("ExpressRouteCircuitConnectionNotFound", resourceCulture); + } + } + /// /// Looks up a localized string similar to The ExpressRouteConnection {0} could not be found.. /// @@ -554,41 +572,16 @@ internal static string ExpressRouteGatewayRequiredToCreateExpressRouteConnection return ResourceManager.GetString("ExpressRouteGatewayRequiredToCreateExpressRouteConnection", resourceCulture); } } - + /// - /// Looks up a localized string similar to A valid ExpressRouteGateway reference is required to create an ExpressRouteConnection.. + /// Looks up a localized string similar to Private Peering needs to be configured on the Express Route Circuit.. /// - internal static string ExpressRoutePrivatePeeringNotFound - { - get - { + internal static string ExpressRoutePrivatePeeringNotFound { + get { return ResourceManager.GetString("ExpressRoutePrivatePeeringNotFound", resourceCulture); } } - - /// - /// Looks up a localized string similar to A valid ExpressRouteGateway reference is required to create an ExpressRouteConnection.. - /// - internal static string ExpressRouteCircuitConnectionNotFound - { - get - { - return ResourceManager.GetString("ExpressRouteCircuitConnectionNotFound", resourceCulture); - } - } - - - /// - /// Looks up a localized string similar to A valid ExpressRouteGateway reference is required to create an ExpressRouteConnection.. - /// - internal static string ExpressRouteCircuitConnectionAlreadyAdded - { - get - { - return ResourceManager.GetString("ExpressRouteCircuitConnectionAlreadyAdded", resourceCulture); - } - } - + /// /// Looks up a localized string similar to The hub virtual network connection to modify could not be found.. /// @@ -715,6 +708,15 @@ internal static string InvalidIPv6IPPrefixLength { } } + /// + /// Looks up a localized string similar to Field Name {0} contains invalid character.. + /// + internal static string InvalidName { + get { + return ResourceManager.GetString("InvalidName", resourceCulture); + } + } + /// /// Looks up a localized string similar to Port value in protocol configuration is out of range. The value must be between 0 and 65535.. /// diff --git a/src/Network/Network/Properties/Resources.resx b/src/Network/Network/Properties/Resources.resx index 1075e5165964..91d11d773722 100644 --- a/src/Network/Network/Properties/Resources.resx +++ b/src/Network/Network/Properties/Resources.resx @@ -354,6 +354,9 @@ A resource with the same name {0} and same type already exists in ResourceGroup {1}. If you wish to modify this resource please use the Update operation instead. + + Field Name {0} contains invalid character. + The VirtualHub to update could not be found. From 4aab5d981d5c5ce634bc21ebd4368e8a60cb67a6 Mon Sep 17 00:00:00 2001 From: Yabo Hu Date: Wed, 27 May 2020 10:17:53 +0800 Subject: [PATCH 2/2] Update ChangeLog.md --- src/Network/Network/ChangeLog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Network/Network/ChangeLog.md b/src/Network/Network/ChangeLog.md index a55ab2673ccf..d4f95fddf791 100644 --- a/src/Network/Network/ChangeLog.md +++ b/src/Network/Network/ChangeLog.md @@ -19,6 +19,7 @@ ---> ## Upcoming Release +* Added name validation for `New-AzIpGroup` * Added breaking change attribute to notify that Zone default behaviour will be changed - `New-AzPublicIpAddress` - `New-AzPublicIpPrefix`