From c5f8873d8d8d18653ed490fd5fbacba116091bc8 Mon Sep 17 00:00:00 2001 From: Lucas Yao Date: Wed, 27 Apr 2022 11:19:40 +0800 Subject: [PATCH 01/16] Add Microsoft.Network/privateLinkServices configuration to support PEC and add hasSupportResourceURI parameter. --- .../GenericProvider.cs | 10 +++++++++- .../ProviderConfiguration.cs | 18 ++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs index 8bae2ce58a53..8746a77b950c 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs @@ -133,7 +133,11 @@ public void DeletePrivateEndpointConnection(string resourceGroupName, string ser public PSPrivateLinkResource GetPrivateLinkResource(string resourceGroupName, string serviceName, string name) { - if (_configuration.HasResourceURI) + if (!_configuration.HasSupportResourceURI) + { + throw new System.Exception($"The {_configuration.Type} api {_configuration.ApiVersion} doesn't provide private link resource related API"); + } + if (_configuration.HasResourceURIById) { string url = BuildPrivateLinkResourceURL(resourceGroupName, serviceName, name); PrivateLinkResource resource = ServiceClient.Operations.GetResource(url, _configuration.ApiVersion); @@ -147,6 +151,10 @@ public PSPrivateLinkResource GetPrivateLinkResource(string resourceGroupName, st public List ListPrivateLinkResource(string resourceGroupName, string serviceName) { + if (!_configuration.HasSupportResourceURI) + { + throw new System.Exception($"The {_configuration.Type} api {_configuration.ApiVersion} doesn't provide private link resource related API"); + } var psPLRs = new List(); string url = BuildPrivateLinkResourcesURL(resourceGroupName, serviceName); IPage list = ServiceClient.Operations.GetResourcePage, PrivateLinkResource>(url, _configuration.ApiVersion); diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs index d5025e39d3b1..7715edc2e809 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs @@ -52,6 +52,7 @@ static ProviderConfiguration() RegisterConfiguration("Microsoft.Migrate/assessmentProjects", "2020-05-01-preview", false, false); RegisterConfiguration("Microsoft.Migrate/migrateProjects", "2020-06-01-preview", false, false); RegisterConfiguration("Microsoft.Network/applicationgateways", "2020-05-01", true, false); + RegisterConfiguration("Microsoft.Network/privateLinkServices", "2020-05-01", true, false, false); RegisterConfiguration("Microsoft.OffAzure/masterSites", "2020-07-07", false, false); RegisterConfiguration("Microsoft.PowerBI/privateLinkServicesForPowerBI", "2020-06-01", false, true); RegisterConfiguration("Microsoft.Purview/accounts", "2020-12-01-preview", true, true); @@ -71,15 +72,23 @@ static ProviderConfiguration() RegisterConfiguration("Microsoft.Web/hostingEnvironments", "2020-10-01", true, false); RegisterConfiguration("Microsoft.BotService/botServices", "2021-05-01-preview", true, true); } - - private static void RegisterConfiguration(string type, string apiVersion, bool hasConnectionsURI = false, bool hasResourceURI = false) + /// + /// Register priavte endopoint connection and private link resource configuration + /// + /// Resource type + /// Resource api version + /// True if the private endpoint connection can be list by URL , otherwise it can be list by URL + /// True if the private link resource can be get by Id, otherwise it can be list + /// True if the private link resource be supported, otherwise false + private static void RegisterConfiguration(string type, string apiVersion, bool hasConnectionsURI = false, bool hasResourceURIById = false, bool hasSupportResourceURI = true) { ProviderConfiguration configuration = new ProviderConfiguration { Type = type, ApiVersion = apiVersion, HasConnectionsURI = hasConnectionsURI, - HasResourceURI = hasResourceURI + HasResourceURIById = hasResourceURIById, + HasSupportResourceURI = hasSupportResourceURI, }; _configurations.Add(type, configuration); } @@ -87,7 +96,8 @@ private static void RegisterConfiguration(string type, string apiVersion, bool h public string Type { get; set; } public string ApiVersion { get; set; } public bool HasConnectionsURI { get; set; } - public bool HasResourceURI { get; set; } + public bool HasResourceURIById { get; set; } + public bool HasSupportResourceURI { get; set; } public static ProviderConfiguration GetProviderConfiguration(string type) { From 9e40299273c74640d0b7978fb232d49091c799f0 Mon Sep 17 00:00:00 2001 From: Lucas Yao Date: Thu, 28 Apr 2022 16:16:48 +0800 Subject: [PATCH 02/16] [Network] modified privatelinkresourcetype dynamic parameter. --- .../PrivateEndpointConnectionBaseCmdlet.cs | 3 ++- .../GetAzurePrivateLinkResourceCommand.cs | 3 ++- .../GenericProvider.cs | 10 +++++----- .../ProviderConfiguration.cs | 19 ++++++++++--------- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs index ca6c004bd307..c51061bb2d4d 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs @@ -60,9 +60,10 @@ public abstract class PrivateEndpointConnectionBaseCmdlet : NetworkBaseCmdlet, I string NamedContextParameterSet = "ByResource"; public new object GetDynamicParameters() { + InvocationInfo invocationInfo = MyInvocation; var parameters = new RuntimeDefinedParameterDictionary(); RuntimeDefinedParameter namedParameter; - if (ProviderConfiguration.TryGetProvideServiceParameter(privateEndpointTypeName, NamedContextParameterSet, out namedParameter)) + if (ProviderConfiguration.TryGetProvideServiceParameter("PEC", privateEndpointTypeName, NamedContextParameterSet, out namedParameter)) { parameters.Add(privateEndpointTypeName, namedParameter); } diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs b/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs index 5d6d5bee49c9..157f6b13efa5 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs @@ -63,9 +63,10 @@ public class GetAzurePrivateLinkResourceCommand : NetworkBaseCmdlet, IDynamicPar public new object GetDynamicParameters() { + InvocationInfo invocationInfo = MyInvocation; var parameters = new RuntimeDefinedParameterDictionary(); RuntimeDefinedParameter namedParameter; - if (ProviderConfiguration.TryGetProvideServiceParameter(privateEndpointTypeName, NamedContextParameterSet, out namedParameter)) + if (ProviderConfiguration.TryGetProvideServiceParameter("PLR", privateEndpointTypeName, NamedContextParameterSet, out namedParameter)) { parameters.Add(privateEndpointTypeName, namedParameter); } diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs index 8746a77b950c..97a162126eb7 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs @@ -133,11 +133,11 @@ public void DeletePrivateEndpointConnection(string resourceGroupName, string ser public PSPrivateLinkResource GetPrivateLinkResource(string resourceGroupName, string serviceName, string name) { - if (!_configuration.HasSupportResourceURI) + if (!_configuration.SupportPrivateLinkResource) { - throw new System.Exception($"The {_configuration.Type} api {_configuration.ApiVersion} doesn't provide private link resource related API"); + throw new System.Exception($"The {_configuration.Type} api {_configuration.ApiVersion} doesn't support private link resource"); } - if (_configuration.HasResourceURIById) + if (_configuration.SupportGetPrivateLinkResource) { string url = BuildPrivateLinkResourceURL(resourceGroupName, serviceName, name); PrivateLinkResource resource = ServiceClient.Operations.GetResource(url, _configuration.ApiVersion); @@ -151,9 +151,9 @@ public PSPrivateLinkResource GetPrivateLinkResource(string resourceGroupName, st public List ListPrivateLinkResource(string resourceGroupName, string serviceName) { - if (!_configuration.HasSupportResourceURI) + if (!_configuration.SupportPrivateLinkResource) { - throw new System.Exception($"The {_configuration.Type} api {_configuration.ApiVersion} doesn't provide private link resource related API"); + throw new System.Exception($"The {_configuration.Type} api {_configuration.ApiVersion} doesn't support private link resource"); } var psPLRs = new List(); string url = BuildPrivateLinkResourcesURL(resourceGroupName, serviceName); diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs index 7715edc2e809..8f97f98e8dce 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs @@ -78,17 +78,17 @@ static ProviderConfiguration() /// Resource type /// Resource api version /// True if the private endpoint connection can be list by URL , otherwise it can be list by URL - /// True if the private link resource can be get by Id, otherwise it can be list - /// True if the private link resource be supported, otherwise false - private static void RegisterConfiguration(string type, string apiVersion, bool hasConnectionsURI = false, bool hasResourceURIById = false, bool hasSupportResourceURI = true) + /// True if the private link resource can be get by Id, otherwise it can be list + /// True if the private link resource be supported, otherwise false + private static void RegisterConfiguration(string type, string apiVersion, bool hasConnectionsURI = false, bool supportGetPrivateLinkResource = false, bool supportPrivateLinkResource = true) { ProviderConfiguration configuration = new ProviderConfiguration { Type = type, ApiVersion = apiVersion, HasConnectionsURI = hasConnectionsURI, - HasResourceURIById = hasResourceURIById, - HasSupportResourceURI = hasSupportResourceURI, + SupportGetPrivateLinkResource = supportGetPrivateLinkResource, + SupportPrivateLinkResource = supportPrivateLinkResource, }; _configurations.Add(type, configuration); } @@ -96,8 +96,8 @@ private static void RegisterConfiguration(string type, string apiVersion, bool h public string Type { get; set; } public string ApiVersion { get; set; } public bool HasConnectionsURI { get; set; } - public bool HasResourceURIById { get; set; } - public bool HasSupportResourceURI { get; set; } + public bool SupportGetPrivateLinkResource { get; set; } + public bool SupportPrivateLinkResource { get; set; } public static ProviderConfiguration GetProviderConfiguration(string type) { @@ -107,17 +107,18 @@ public static ProviderConfiguration GetProviderConfiguration(string type) /// /// Generate a runtime parameter with ValidateSet matching the current context /// + /// Has two value, PLR => private link resource, PEC => private endpoint connection. /// The name of the parameter /// The returned runtime parameter for context, with appropriate validate set /// True if one or more contexts were found, otherwise false - public static bool TryGetProvideServiceParameter(string name, string parameterSetName, out RuntimeDefinedParameter runtimeParameter) + public static bool TryGetProvideServiceParameter(string serviceType, string name, string parameterSetName, out RuntimeDefinedParameter runtimeParameter) { var result = false; runtimeParameter = null; if (_configurations != null && _configurations.Values != null) { var ObjArray = _configurations.Values.ToArray(); - var ProvideTypeList = ObjArray.Select(c => c.Type).ToArray(); + var ProvideTypeList = serviceType.ToUpper() == "PLR" ? ObjArray.Where(c => c.SupportPrivateLinkResource).Select(c => c.Type).ToArray() : ObjArray.Select(c => c.Type).ToArray(); runtimeParameter = new RuntimeDefinedParameter( name, typeof(string), new Collection() From f1adfcdf1c457185c1dd426d69b142f0efd1c477 Mon Sep 17 00:00:00 2001 From: Lucas Yao Date: Fri, 29 Apr 2022 09:59:46 +0800 Subject: [PATCH 03/16] [Network]throw message when RP not support private endpoint connection or private link resource. --- .../PrivateEndpointConnectionBaseCmdlet.cs | 2 ++ .../GetAzurePrivateLinkResourceCommand.cs | 6 ++++++ .../PrivateLinkServiceProvider/ProviderConfiguration.cs | 4 +++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs index c51061bb2d4d..2a7c737d8195 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs @@ -77,6 +77,8 @@ public abstract class PrivateEndpointConnectionBaseCmdlet : NetworkBaseCmdlet, I protected IPrivateLinkProvider BuildProvider(string subscription, string privateLinkResourceType) { + if (!GenericProvider.SupportsPrivateLinkResourceType(privateLinkResourceType)) + throw new System.Exception($"The {privateLinkResourceType} doesn't support private endpoint connection"); return PrivateLinkProviderFactory.CreatePrivateLinkProvder(this, subscription, privateLinkResourceType); } } diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs b/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs index 157f6b13efa5..5b976cf77d31 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs @@ -90,6 +90,12 @@ public override void Execute() this.Subscription = DefaultProfile.DefaultContext.Subscription.Id; this.PrivateLinkResourceType = DynamicParameters[privateEndpointTypeName].Value as string; } + + if (!GenericProvider.SupportsPrivateLinkResourceType(this.PrivateLinkResourceType)) + { + throw new Exception($"The {this.PrivateLinkResourceType} doesn't support private link resource"); + } + IPrivateLinkProvider provider = PrivateLinkProviderFactory.CreatePrivateLinkProvder(this, Subscription, PrivateLinkResourceType); if (provider == null) { diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs index 8f97f98e8dce..2910dc0eec86 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs @@ -101,7 +101,9 @@ private static void RegisterConfiguration(string type, string apiVersion, bool h public static ProviderConfiguration GetProviderConfiguration(string type) { - return _configurations[type]; + ProviderConfiguration outProviderConfiguration = null; + _configurations.TryGetValue(type, out outProviderConfiguration); + return outProviderConfiguration; } /// From 12de965b82cd833e68e791563426602f776caffd Mon Sep 17 00:00:00 2001 From: dingmeng-xue Date: Mon, 9 May 2022 10:29:42 +0800 Subject: [PATCH 04/16] update example --- .../examples/private-link-resource-example.md | 52 +++++++++++-------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/documentation/development-docs/examples/private-link-resource-example.md b/documentation/development-docs/examples/private-link-resource-example.md index 3fce37bd1087..60d18d5810f9 100644 --- a/documentation/development-docs/examples/private-link-resource-example.md +++ b/documentation/development-docs/examples/private-link-resource-example.md @@ -1,63 +1,69 @@ ## Applicability -Az.Network supports the retrieval of private link resource in `Get-AzPrivateLinkResource` as well as the management of private endpoint connection in `Approve-AzPrivateEndpointConnect`, `Deny-AzPrivateEndpointConnect`, `Remove-AzPrivateEndpointConnect` and `Set-AzPrivateEndpointConnect`. +Az.Network supports the retrieval of private link resource in `Get-AzPrivateLinkResource` as well as the management of private endpoint connection by `Get-AzPrivateEndpointConnect`, `Approve-AzPrivateEndpointConnect`, `Deny-AzPrivateEndpointConnect`, `Remove-AzPrivateEndpointConnect` and `Set-AzPrivateEndpointConnect`. -For providers who -- supports the features of private linke resource and private endpoint connection already -- and want to onboard these features in Azure PowerShell, +This example is for provider who +- supports the features of private link resource and private endpoint connection already +- and wants to onboard these features in Azure PowerShell, -they need register provider configuration in [ProviderConfiguration.cs](https://github.com/Azure/azure-powershell/blob/main/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs#L12). +they need to register provider configuration in [ProviderConfiguration.cs](https://github.com/Azure/azure-powershell/blob/main/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs#L12). -Notes: No additional commands for the features of private linke resource and private endpoint connection need to be added. +Notes: No additional commands for the features of PrivateLinkResource and PrivateEndpointConnection need to be added. ## Prerequisite We assume the API for `List` private link resource and `Get` private endpoint connection is available in the provider that claims to support private endpoint connection features. That means it supports following APIs: ``` # List Private Link Resource API -"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Level-Resource}/{Top-Level-Resource-Name}/privateLinkResources" +"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Resource}/{Top-Resource-Name}/privateLinkResources" ``` ``` # Get Private Endpoint Connection API -"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Level-Resource}/{Top-Level-Resource-Name}/privateEndpointConnections/{PrivateEndpointConnection-Name}" +"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Resource}/{Top-Resource-Name}/privateEndpointConnections/{Resource-Name}" ``` -if "List Private Endpoint Connection API" is not available, `privateEndpointConnections` must be included in the properties of top resource returned by -"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Level-Resource}/{Top-Level-Resource-Name}". So that `Private Endpoint Connections` will be retrieved from the top resource. +if "List Private Endpoint Connection API" below is not available, `privateEndpointConnections` must be included in the properties of top resource returned by +"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Resource}/{Top-Resource-Name}". So that `Get-AzPrivateEndpointConnect` will retrieve connections from the top resource. ``` # List Private Endpoint Connection API -"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Level-Resource}/{Top-Level-Resource-Name}/privateEndpointConnections" +"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Resource}/{Top-Resource-Name}/privateEndpointConnections" ``` ## Code Changes Needed -To add corresponding {Provider}, {Top-Level-Resource} and {API-Version} into [ProviderConfiguration.cs](https://github.com/Azure/azure-powershell/blob/main/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs#L12), we need to follow -in following pattern: +To add corresponding {Provider}, {Top-Resource} and {API-Version} into [ProviderConfiguration.cs](https://github.com/Azure/azure-powershell/blob/main/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs#L12), we need to follow in following pattern: ``` -RegisterConfiguration("{Provider}/{Top-Level-Resource}", "{API-Version}", bool hasPrivateEndppointConnectionsURI, bool hasPrivateLinkResourceURI) +RegisterConfiguration(string type, string apiVersion, bool hasConnectionsURI = false, bool supportGetPrivateLinkResource = false, bool supportPrivateLinkResource = true) ``` -- "{Provider}/{Top-Level-Resource}" describes the type of provider. For example, "Microsoft.Sql/servers". -- "{API-Version}" specifies the API version to be used. For example, "2018-06-01-preview". -- `hasPrivateEndppointConnectionsURI` marks the provider whether provides "List Private Endpoint Connection API". +- `type` includes resource provider and resource type which supports PrivateLink feature. For example, "Microsoft.Sql/servers". +- `apiVersion` specifies the API version to be used. For example, "2018-06-01-preview". +- `hasConnectionsURI` marks whether the provider exposes "List Private Endpoint Connection API". Default value is false. ``` # Get Private Link Resource API -"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Level-Resource}/{Top-Level-Resource-Name}/privateLinkResources/{PrivateLinkResource-Name}" +"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Resource}/{Top-Resource-Name}/privateLinkResources/{PrivateLinkResource-Name}" ``` -- `hasPrivateLinkResourceURI` marks the provider whether providers "Get Private Endpoint Connection API". +- `supportGetPrivateLinkResource` marks whether the provider supports "Get Private Link Resource API". Default value is false. -For instance, for provider "Microsoft.Sql/servers" with API version "2018-06-01-preview", it supports "List Private Endpoint Connection API" and "Get Private Endpoint Connection API". So it's registration configuration should be +For instance, for provider "Microsoft.Sql/servers" with API version "2018-06-01-preview", it supports "List Private Endpoint Connection API" and "Get Private Link Resource API". So its registration configuration should be: ``` RegisterConfiguration("Microsoft.Sql/servers", "2018-06-01-preview", true, true); ``` +- `supportPrivateLinkResource` marks whether the provider supports either Get or List API of sPrivateLinkResource. Default value is true. + +For instance, `Microsoft.Network/privateLinkServices` supports PrivateEndpointConnections but doesn't support resource type PrivateLinkResource. Its configuration should be: +``` +RegisterConfiguration("Microsoft.Network/privateLinkServices", "2020-05-01", true, false, false); +``` + ## End-To-End Test ### Item Needed + Top level resource ``` -New-Az{Top-Level-Resource} -ResourceGroupName {rg_name} -Name {top_level_resource_name} +New-Az{Top-Resource} -ResourceGroupName {rg_name} -Name {top_level_resource_name} -$TopLevelResource = Get-Az{Top-Level-Resource} -ResourceGroupName {rg_name} -Name {top_level_resource_name} +$TopLevelResource = Get-Az{Top-Resource} -ResourceGroupName {rg_name} -Name {top_level_resource_name} ``` + private link resource @@ -99,7 +105,7 @@ $connection = Get-AzPrivateEndpointConnection -PrivateLinkResourceId $TopLevelRe * To get the connection, if `list` for private endpoint connection was not supported, ``` -$TopLevelResource = Get-Az{Top-Level-Resource} -ResourceGroupName {rg_name} -Name {top_level_resource_name} +$TopLevelResource = Get-Az{Top-Resource} -ResourceGroupName {rg_name} -Name {top_level_resource_name} $ConnectionId = $TopLevelResource.PrivateEndpointConnection[0].Id From 2f832fcc4c349c6f55e7b9d0cff7004b8b5da918 Mon Sep 17 00:00:00 2001 From: dingmeng-xue Date: Mon, 9 May 2022 12:15:01 +0800 Subject: [PATCH 05/16] update code --- .../examples/private-link-resource-example.md | 43 +++++++++---------- .../ProviderConfiguration.cs | 4 +- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/documentation/development-docs/examples/private-link-resource-example.md b/documentation/development-docs/examples/private-link-resource-example.md index 60d18d5810f9..20235f9a7666 100644 --- a/documentation/development-docs/examples/private-link-resource-example.md +++ b/documentation/development-docs/examples/private-link-resource-example.md @@ -1,11 +1,10 @@ ## Applicability -Az.Network supports the retrieval of private link resource in `Get-AzPrivateLinkResource` as well as the management of private endpoint connection by `Get-AzPrivateEndpointConnect`, `Approve-AzPrivateEndpointConnect`, `Deny-AzPrivateEndpointConnect`, `Remove-AzPrivateEndpointConnect` and `Set-AzPrivateEndpointConnect`. +Az.Network supports the retrieval of private link resource in `Get-AzPrivateLinkResource` as well as the management of private endpoint connection by `Get-AzPrivateEndpointConnection`, `Approve-AzPrivateEndpointConnection`, `Deny-AzPrivateEndpointConnection`, `Remove-AzPrivateEndpointConnection` and `Set-AzPrivateEndpointConnection`. -This example is for provider who -- supports the features of private link resource and private endpoint connection already +For provider who +- supports the features of private link resource or private endpoint connection already - and wants to onboard these features in Azure PowerShell, - -they need to register provider configuration in [ProviderConfiguration.cs](https://github.com/Azure/azure-powershell/blob/main/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs#L12). +You need to register provider configuration in [ProviderConfiguration.cs](https://github.com/Azure/azure-powershell/blob/main/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs#L12). Notes: No additional commands for the features of PrivateLinkResource and PrivateEndpointConnection need to be added. @@ -14,23 +13,23 @@ We assume the API for `List` private link resource and `Get` private endpoint co ``` # List Private Link Resource API -"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Resource}/{Top-Resource-Name}/privateLinkResources" +"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{topResourceType}/{topResourceName}/privateLinkResources" ``` ``` # Get Private Endpoint Connection API -"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Resource}/{Top-Resource-Name}/privateEndpointConnections/{Resource-Name}" +"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{topResourceType}/{topResourceName}/privateEndpointConnections/{privateEndpointConnectionName}" ``` if "List Private Endpoint Connection API" below is not available, `privateEndpointConnections` must be included in the properties of top resource returned by -"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Resource}/{Top-Resource-Name}". So that `Get-AzPrivateEndpointConnect` will retrieve connections from the top resource. +"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{topResourceType}/{topResourceName}". So that `Get-AzPrivateEndpointConnect` will retrieve connections from the top resource. ``` # List Private Endpoint Connection API -"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Resource}/{Top-Resource-Name}/privateEndpointConnections" +"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{topResourceType}/{topResourceName}/privateEndpointConnections" ``` ## Code Changes Needed -To add corresponding {Provider}, {Top-Resource} and {API-Version} into [ProviderConfiguration.cs](https://github.com/Azure/azure-powershell/blob/main/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs#L12), we need to follow in following pattern: +To add corresponding {Provider}, {topResourceType} and {API-Version} into [ProviderConfiguration.cs](https://github.com/Azure/azure-powershell/blob/main/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs#L12), we need to follow in following pattern: ``` RegisterConfiguration(string type, string apiVersion, bool hasConnectionsURI = false, bool supportGetPrivateLinkResource = false, bool supportPrivateLinkResource = true) ``` @@ -39,18 +38,18 @@ RegisterConfiguration(string type, string apiVersion, bool hasConnectionsURI = f - `hasConnectionsURI` marks whether the provider exposes "List Private Endpoint Connection API". Default value is false. ``` # Get Private Link Resource API -"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{Top-Resource}/{Top-Resource-Name}/privateLinkResources/{PrivateLinkResource-Name}" +"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{provider}/{topResourceType}/{topResourceName}/privateLinkResources/{privateLinkResourceName}" ``` -- `supportGetPrivateLinkResource` marks whether the provider supports "Get Private Link Resource API". Default value is false. +- `supportGetPrivateLinkResource` marks whether the provider supports Get API of PrivateLinkResource. Default value is false. For instance, for provider "Microsoft.Sql/servers" with API version "2018-06-01-preview", it supports "List Private Endpoint Connection API" and "Get Private Link Resource API". So its registration configuration should be: ``` RegisterConfiguration("Microsoft.Sql/servers", "2018-06-01-preview", true, true); ``` -- `supportPrivateLinkResource` marks whether the provider supports either Get or List API of sPrivateLinkResource. Default value is true. +- `supportListPrivateLinkResource` marks whether the provider supports List API of PrivateLinkResource. Default value is true. -For instance, `Microsoft.Network/privateLinkServices` supports PrivateEndpointConnections but doesn't support resource type PrivateLinkResource. Its configuration should be: +For instance, `Microsoft.Network/privateLinkServices` supports PrivateEndpointConnections but doesn't support resource type PrivateLinkResource (We assume List API is mandatory to resource support). Its configuration should be: ``` RegisterConfiguration("Microsoft.Network/privateLinkServices", "2020-05-01", true, false, false); ``` @@ -61,9 +60,9 @@ RegisterConfiguration("Microsoft.Network/privateLinkServices", "2020-05-01", tru + Top level resource ``` -New-Az{Top-Resource} -ResourceGroupName {rg_name} -Name {top_level_resource_name} +New-Az{topResourceType} -ResourceGroupName {rgName} -Name {topResourceName} -$TopLevelResource = Get-Az{Top-Resource} -ResourceGroupName {rg_name} -Name {top_level_resource_name} +$TopLevelResource = Get-Az{topResourceType} -ResourceGroupName {rgName} -Name {topResourceName} ``` + private link resource @@ -73,24 +72,24 @@ $PrivateLinkResource = Get-AzPrivateLinkResource -PrivateLinkResourceId $TopLeve + subnet config (object in memory) ``` -$SubnetConfig = New-AzVirtualNetworkSubnetConfig -Name {config_name} -AddressPrefix "11.0.1.0/24" -PrivateEndpointNetworkPolicies "Disabled" +$SubnetConfig = New-AzVirtualNetworkSubnetConfig -Name {configName} -AddressPrefix "11.0.1.0/24" -PrivateEndpointNetworkPolicies "Disabled" ``` + virtual network ``` -New-AzVirtualNetwork -ResourceGroupName {rg_name} -Name {vnet_name} -Location {location} -AddressPrefix "11.0.0.0/16" -Subnet $SubnetConfig +New-AzVirtualNetwork -ResourceGroupName {rgName} -Name {vnetName} -Location {location} -AddressPrefix "11.0.0.0/16" -Subnet $SubnetConfig -$VNet=Get-AzVirtualNetwork -ResourceGroupName {rg_name} -Name {vnet_name} +$VNet=Get-AzVirtualNetwork -ResourceGroupName {rgName} -Name {vnetName} ``` + private link service connection (object in memory) ``` -$PLSConnection = New-AzPrivateLinkServiceConnection -Name {pls_connection_name} -PrivateLinkServiceId $TopLevelResource.Id -GroupId $TopLevelResource.GroupId +$PLSConnection = New-AzPrivateLinkServiceConnection -Name {plsConnectionName} -PrivateLinkServiceId $TopLevelResource.Id -GroupId $TopLevelResource.GroupId ``` + endpoint ``` -New-AzPrivateEndpoint -ResourceGroupName {rg_name} -Name {endpoint_name} -Location {location} -Subnet $VNet.subnets[0] -PrivateLinkServiceConnection $PLSConnection -ByManualRequest +New-AzPrivateEndpoint -ResourceGroupName {rgName} -Name {endpointName} -Location {location} -Subnet $VNet.subnets[0] -PrivateLinkServiceConnection $PLSConnection -ByManualRequest ``` ### step-by-step @@ -105,7 +104,7 @@ $connection = Get-AzPrivateEndpointConnection -PrivateLinkResourceId $TopLevelRe * To get the connection, if `list` for private endpoint connection was not supported, ``` -$TopLevelResource = Get-Az{Top-Resource} -ResourceGroupName {rg_name} -Name {top_level_resource_name} +$TopLevelResource = Get-Az{topResourceType} -ResourceGroupName {rgName} -Name {topResourceName} $ConnectionId = $TopLevelResource.PrivateEndpointConnection[0].Id diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs index 2910dc0eec86..2a1164ba2ecd 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs @@ -73,7 +73,7 @@ static ProviderConfiguration() RegisterConfiguration("Microsoft.BotService/botServices", "2021-05-01-preview", true, true); } /// - /// Register priavte endopoint connection and private link resource configuration + /// Register private endpoint connection and private link resource configuration /// /// Resource type /// Resource api version @@ -127,7 +127,7 @@ public static bool TryGetProvideServiceParameter(string serviceType, string name { new ParameterAttribute { Mandatory = false, ValueFromPipeline = true, - HelpMessage = "The private link resource type.", + HelpMessage = "The resource provider and resource type which supports private link resource.", ParameterSetName = parameterSetName }, new ValidateSetAttribute(ProvideTypeList) } From 0637d53d6a0406e30aef493573f8e5c7cdcf5820 Mon Sep 17 00:00:00 2001 From: Lucas Yao Date: Mon, 9 May 2022 18:49:35 +0800 Subject: [PATCH 06/16] [Network] adjust private endpoint connection and link resource logic. --- .../PrivateEndpointConnectionBaseCmdlet.cs | 5 ++- .../GetAzurePrivateLinkResourceCommand.cs | 5 ++- .../GenericProvider.cs | 9 ++-- .../ProviderConfiguration.cs | 45 +++++++++++++++---- .../Network/Properties/Resources.Designer.cs | 22 ++++++++- src/Network/Network/Properties/Resources.resx | 6 +++ 6 files changed, 74 insertions(+), 18 deletions(-) diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs index 2a7c737d8195..5ee5bc436c4d 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs @@ -12,6 +12,7 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using Microsoft.Azure.Commands.Common.Exceptions; using Microsoft.Azure.Commands.Network.PrivateLinkService.PrivateLinkServiceProvider; using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; using System.Management.Automation; @@ -63,7 +64,7 @@ public abstract class PrivateEndpointConnectionBaseCmdlet : NetworkBaseCmdlet, I InvocationInfo invocationInfo = MyInvocation; var parameters = new RuntimeDefinedParameterDictionary(); RuntimeDefinedParameter namedParameter; - if (ProviderConfiguration.TryGetProvideServiceParameter("PEC", privateEndpointTypeName, NamedContextParameterSet, out namedParameter)) + if (ProviderConfiguration.TryGetEndpointConnectionServiceParameter(privateEndpointTypeName, NamedContextParameterSet, out namedParameter)) { parameters.Add(privateEndpointTypeName, namedParameter); } @@ -78,7 +79,7 @@ public abstract class PrivateEndpointConnectionBaseCmdlet : NetworkBaseCmdlet, I protected IPrivateLinkProvider BuildProvider(string subscription, string privateLinkResourceType) { if (!GenericProvider.SupportsPrivateLinkResourceType(privateLinkResourceType)) - throw new System.Exception($"The {privateLinkResourceType} doesn't support private endpoint connection"); + throw new AzPSApplicationException(string.Format(Properties.Resources.UnsupportPrivateEndpointConnectionType, privateLinkResourceType)); return PrivateLinkProviderFactory.CreatePrivateLinkProvder(this, subscription, privateLinkResourceType); } } diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs b/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs index 5b976cf77d31..16c3399bf3d5 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs @@ -12,6 +12,7 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using Microsoft.Azure.Commands.Common.Exceptions; using Microsoft.Azure.Commands.Network.Models; using Microsoft.Azure.Commands.Network.PrivateLinkService.PrivateLinkServiceProvider; using Microsoft.Azure.Management.Internal.Resources.Utilities.Models; @@ -66,7 +67,7 @@ public class GetAzurePrivateLinkResourceCommand : NetworkBaseCmdlet, IDynamicPar InvocationInfo invocationInfo = MyInvocation; var parameters = new RuntimeDefinedParameterDictionary(); RuntimeDefinedParameter namedParameter; - if (ProviderConfiguration.TryGetProvideServiceParameter("PLR", privateEndpointTypeName, NamedContextParameterSet, out namedParameter)) + if (ProviderConfiguration.TryGetLinkResourceServiceParameter(privateEndpointTypeName, NamedContextParameterSet, out namedParameter)) { parameters.Add(privateEndpointTypeName, namedParameter); } @@ -93,7 +94,7 @@ public override void Execute() if (!GenericProvider.SupportsPrivateLinkResourceType(this.PrivateLinkResourceType)) { - throw new Exception($"The {this.PrivateLinkResourceType} doesn't support private link resource"); + throw new ArgumentException(string.Format(Properties.Resources.UnsupportPrivateLinkResourceType, this.PrivateLinkResourceType)); } IPrivateLinkProvider provider = PrivateLinkProviderFactory.CreatePrivateLinkProvder(this, Subscription, PrivateLinkResourceType); diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs index 97a162126eb7..b78446bd697f 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs @@ -20,6 +20,7 @@ using Microsoft.Azure.Internal.Common; using System.Collections.Generic; using System.Linq; +using Microsoft.Azure.Commands.Common.Exceptions; namespace Microsoft.Azure.Commands.Network.PrivateLinkService.PrivateLinkServiceProvider { @@ -133,9 +134,9 @@ public void DeletePrivateEndpointConnection(string resourceGroupName, string ser public PSPrivateLinkResource GetPrivateLinkResource(string resourceGroupName, string serviceName, string name) { - if (!_configuration.SupportPrivateLinkResource) + if (!_configuration.SupportListPrivateLinkResource) { - throw new System.Exception($"The {_configuration.Type} api {_configuration.ApiVersion} doesn't support private link resource"); + throw new AzPSApplicationException(string.Format(Properties.Resources.UnsupportPrivateLinkResourceType, $"{_configuration.Type} api {_configuration.ApiVersion}")); } if (_configuration.SupportGetPrivateLinkResource) { @@ -151,9 +152,9 @@ public PSPrivateLinkResource GetPrivateLinkResource(string resourceGroupName, st public List ListPrivateLinkResource(string resourceGroupName, string serviceName) { - if (!_configuration.SupportPrivateLinkResource) + if (!_configuration.SupportListPrivateLinkResource) { - throw new System.Exception($"The {_configuration.Type} api {_configuration.ApiVersion} doesn't support private link resource"); + throw new AzPSApplicationException(string.Format(Properties.Resources.UnsupportPrivateLinkResourceType, $"{_configuration.Type} api {_configuration.ApiVersion}")); } var psPLRs = new List(); string url = BuildPrivateLinkResourcesURL(resourceGroupName, serviceName); diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs index 2910dc0eec86..315d55ffb17d 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs @@ -79,8 +79,8 @@ static ProviderConfiguration() /// Resource api version /// True if the private endpoint connection can be list by URL , otherwise it can be list by URL /// True if the private link resource can be get by Id, otherwise it can be list - /// True if the private link resource be supported, otherwise false - private static void RegisterConfiguration(string type, string apiVersion, bool hasConnectionsURI = false, bool supportGetPrivateLinkResource = false, bool supportPrivateLinkResource = true) + /// True if the private link resource can be list, otherwise false + private static void RegisterConfiguration(string type, string apiVersion, bool hasConnectionsURI = false, bool supportGetPrivateLinkResource = false, bool supportListPrivateLinkResource = true) { ProviderConfiguration configuration = new ProviderConfiguration { @@ -88,7 +88,7 @@ private static void RegisterConfiguration(string type, string apiVersion, bool h ApiVersion = apiVersion, HasConnectionsURI = hasConnectionsURI, SupportGetPrivateLinkResource = supportGetPrivateLinkResource, - SupportPrivateLinkResource = supportPrivateLinkResource, + SupportListPrivateLinkResource = supportListPrivateLinkResource, }; _configurations.Add(type, configuration); } @@ -97,7 +97,7 @@ private static void RegisterConfiguration(string type, string apiVersion, bool h public string ApiVersion { get; set; } public bool HasConnectionsURI { get; set; } public bool SupportGetPrivateLinkResource { get; set; } - public bool SupportPrivateLinkResource { get; set; } + public bool SupportListPrivateLinkResource { get; set; } public static ProviderConfiguration GetProviderConfiguration(string type) { @@ -109,25 +109,54 @@ public static ProviderConfiguration GetProviderConfiguration(string type) /// /// Generate a runtime parameter with ValidateSet matching the current context /// - /// Has two value, PLR => private link resource, PEC => private endpoint connection. /// The name of the parameter /// The returned runtime parameter for context, with appropriate validate set /// True if one or more contexts were found, otherwise false - public static bool TryGetProvideServiceParameter(string serviceType, string name, string parameterSetName, out RuntimeDefinedParameter runtimeParameter) + public static bool TryGetEndpointConnectionServiceParameter(string name, string parameterSetName, out RuntimeDefinedParameter runtimeParameter) { var result = false; runtimeParameter = null; if (_configurations != null && _configurations.Values != null) { var ObjArray = _configurations.Values.ToArray(); - var ProvideTypeList = serviceType.ToUpper() == "PLR" ? ObjArray.Where(c => c.SupportPrivateLinkResource).Select(c => c.Type).ToArray() : ObjArray.Select(c => c.Type).ToArray(); + var ProvideTypeList = ObjArray.Select(c => c.Type).ToArray(); runtimeParameter = new RuntimeDefinedParameter( name, typeof(string), new Collection() { new ParameterAttribute { Mandatory = false, ValueFromPipeline = true, - HelpMessage = "The private link resource type.", + HelpMessage = "The resource type that supported private endpoint connection.", + ParameterSetName = parameterSetName }, + new ValidateSetAttribute(ProvideTypeList) + } + ); + result = true; + } + return result; + } + + /// + /// Generate a runtime parameter with ValidateSet matching the current context + /// + /// The name of the parameter + /// The returned runtime parameter for context, with appropriate validate set + /// True if one or more contexts were found, otherwise false + public static bool TryGetLinkResourceServiceParameter(string name, string parameterSetName, out RuntimeDefinedParameter runtimeParameter) + { + var result = false; + runtimeParameter = null; + if (_configurations != null && _configurations.Values != null) + { + var ObjArray = _configurations.Values.ToArray(); + var ProvideTypeList = ObjArray.Where(c => (c.SupportListPrivateLinkResource || c.SupportGetPrivateLinkResource)).Select(c => c.Type).ToArray(); + runtimeParameter = new RuntimeDefinedParameter( + name, typeof(string), + new Collection() + { + new ParameterAttribute { Mandatory = false, + ValueFromPipeline = true, + HelpMessage = "The resource type that supported private link resource.", ParameterSetName = parameterSetName }, new ValidateSetAttribute(ProvideTypeList) } diff --git a/src/Network/Network/Properties/Resources.Designer.cs b/src/Network/Network/Properties/Resources.Designer.cs index c6ddaf69b667..c3cf03d6a769 100644 --- a/src/Network/Network/Properties/Resources.Designer.cs +++ b/src/Network/Network/Properties/Resources.Designer.cs @@ -19,7 +19,7 @@ namespace Microsoft.Azure.Commands.Network.Properties { // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Resources { @@ -853,7 +853,7 @@ internal static string InvalidStorageId { } /// - /// Looks up a localized string similar to TargetResourceId specified in flow log is not a valid resource ID of Network security group.. + /// Looks up a localized string similar to TargetResourceId specified in flow log is not a valid resource ID of Network security group, Virtual Network, Subnet or Network Interface.. /// internal static string InvalidTargetResourceId { get { @@ -1554,6 +1554,24 @@ internal static string UnsupportedProtocolConfigurationType { } } + /// + /// Looks up a localized string similar to The {0} doesn't support private endpoint connection.. + /// + internal static string UnsupportPrivateEndpointConnectionType { + get { + return ResourceManager.GetString("UnsupportPrivateEndpointConnectionType", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The {0} doesn't support private link resource.. + /// + internal static string UnsupportPrivateLinkResourceType { + get { + return ResourceManager.GetString("UnsupportPrivateLinkResourceType", resourceCulture); + } + } + /// /// Looks up a localized string similar to Updating resource with ResourceGroupName {0}, ResourceName {1}.. /// diff --git a/src/Network/Network/Properties/Resources.resx b/src/Network/Network/Properties/Resources.resx index 014f3eefc3cc..347d331c6dfa 100644 --- a/src/Network/Network/Properties/Resources.resx +++ b/src/Network/Network/Properties/Resources.resx @@ -723,4 +723,10 @@ The VirtualNetworkGatewayNatRule could not be found + + The {0} doesn't support private endpoint connection. + + + The {0} doesn't support private link resource. + \ No newline at end of file From 99c59f240f55a695b3aa517124d730698579d830 Mon Sep 17 00:00:00 2001 From: Lucas Yao Date: Mon, 9 May 2022 18:58:04 +0800 Subject: [PATCH 07/16] [Network] modified help message. --- .../PrivateLinkServiceProvider/ProviderConfiguration.cs | 8 ++++---- src/Network/Network/Properties/Resources.Designer.cs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs index 315d55ffb17d..1f2be740f954 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs @@ -78,8 +78,8 @@ static ProviderConfiguration() /// Resource type /// Resource api version /// True if the private endpoint connection can be list by URL , otherwise it can be list by URL - /// True if the private link resource can be get by Id, otherwise it can be list - /// True if the private link resource can be list, otherwise false + /// True if the private link resource can be obtained by Id, otherwise false + /// True if the private link resource can be listed, otherwise false private static void RegisterConfiguration(string type, string apiVersion, bool hasConnectionsURI = false, bool supportGetPrivateLinkResource = false, bool supportListPrivateLinkResource = true) { ProviderConfiguration configuration = new ProviderConfiguration @@ -126,7 +126,7 @@ public static bool TryGetEndpointConnectionServiceParameter(string name, string { new ParameterAttribute { Mandatory = false, ValueFromPipeline = true, - HelpMessage = "The resource type that supported private endpoint connection.", + HelpMessage = "The resource provider and resource type which supports private link resource.", ParameterSetName = parameterSetName }, new ValidateSetAttribute(ProvideTypeList) } @@ -156,7 +156,7 @@ public static bool TryGetLinkResourceServiceParameter(string name, string parame { new ParameterAttribute { Mandatory = false, ValueFromPipeline = true, - HelpMessage = "The resource type that supported private link resource.", + HelpMessage = "The resource provider and resource type which supports private link resource.", ParameterSetName = parameterSetName }, new ValidateSetAttribute(ProvideTypeList) } diff --git a/src/Network/Network/Properties/Resources.Designer.cs b/src/Network/Network/Properties/Resources.Designer.cs index c3cf03d6a769..157e3d15fc07 100644 --- a/src/Network/Network/Properties/Resources.Designer.cs +++ b/src/Network/Network/Properties/Resources.Designer.cs @@ -19,7 +19,7 @@ namespace Microsoft.Azure.Commands.Network.Properties { // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Resources { @@ -853,7 +853,7 @@ internal static string InvalidStorageId { } /// - /// Looks up a localized string similar to TargetResourceId specified in flow log is not a valid resource ID of Network security group, Virtual Network, Subnet or Network Interface.. + /// Looks up a localized string similar to TargetResourceId specified in flow log is not a valid resource ID of Network security group.. /// internal static string InvalidTargetResourceId { get { From b7db4df489db71f2a4809b05ea9a270ac8a7c483 Mon Sep 17 00:00:00 2001 From: Lucas Yao Date: Tue, 10 May 2022 10:14:37 +0800 Subject: [PATCH 08/16] [Network] delete unnecessary. --- .../PrivateEndpointConnectionBaseCmdlet.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs index 5ee5bc436c4d..956c560f9f71 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs @@ -61,7 +61,6 @@ public abstract class PrivateEndpointConnectionBaseCmdlet : NetworkBaseCmdlet, I string NamedContextParameterSet = "ByResource"; public new object GetDynamicParameters() { - InvocationInfo invocationInfo = MyInvocation; var parameters = new RuntimeDefinedParameterDictionary(); RuntimeDefinedParameter namedParameter; if (ProviderConfiguration.TryGetEndpointConnectionServiceParameter(privateEndpointTypeName, NamedContextParameterSet, out namedParameter)) From 09f1d17083384dbe2dded3ddc11651008b9b8e4d Mon Sep 17 00:00:00 2001 From: Lucas Yao Date: Tue, 10 May 2022 11:14:16 +0800 Subject: [PATCH 09/16] [Network]adjust prinvate link resource logic. --- .../PrivateLinkServiceProvider/GenericProvider.cs | 10 +++++----- src/Network/Network/Properties/Resources.Designer.cs | 4 ++-- src/Network/Network/Properties/Resources.resx | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs index b78446bd697f..1157750ec135 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs @@ -134,20 +134,20 @@ public void DeletePrivateEndpointConnection(string resourceGroupName, string ser public PSPrivateLinkResource GetPrivateLinkResource(string resourceGroupName, string serviceName, string name) { - if (!_configuration.SupportListPrivateLinkResource) - { - throw new AzPSApplicationException(string.Format(Properties.Resources.UnsupportPrivateLinkResourceType, $"{_configuration.Type} api {_configuration.ApiVersion}")); - } if (_configuration.SupportGetPrivateLinkResource) { string url = BuildPrivateLinkResourceURL(resourceGroupName, serviceName, name); PrivateLinkResource resource = ServiceClient.Operations.GetResource(url, _configuration.ApiVersion); return ToPsPrivateLinkResource(resource); } - else + else if (_configuration.SupportListPrivateLinkResource) { return ListPrivateLinkResource(resourceGroupName, serviceName).Single(plr => plr.Name.Equals(name)); } + else + { + throw new AzPSApplicationException(string.Format(Properties.Resources.UnsupportPrivateLinkResourceType, $"{_configuration.Type} api {_configuration.ApiVersion}")); + } } public List ListPrivateLinkResource(string resourceGroupName, string serviceName) diff --git a/src/Network/Network/Properties/Resources.Designer.cs b/src/Network/Network/Properties/Resources.Designer.cs index 157e3d15fc07..ac07fddbb5c0 100644 --- a/src/Network/Network/Properties/Resources.Designer.cs +++ b/src/Network/Network/Properties/Resources.Designer.cs @@ -1555,7 +1555,7 @@ internal static string UnsupportedProtocolConfigurationType { } /// - /// Looks up a localized string similar to The {0} doesn't support private endpoint connection.. + /// Looks up a localized string similar to The {0} doesn't register private endpoint connection.. /// internal static string UnsupportPrivateEndpointConnectionType { get { @@ -1564,7 +1564,7 @@ internal static string UnsupportPrivateEndpointConnectionType { } /// - /// Looks up a localized string similar to The {0} doesn't support private link resource.. + /// Looks up a localized string similar to The {0} doesn't register private link resource.. /// internal static string UnsupportPrivateLinkResourceType { get { diff --git a/src/Network/Network/Properties/Resources.resx b/src/Network/Network/Properties/Resources.resx index 347d331c6dfa..891e9f5dc02c 100644 --- a/src/Network/Network/Properties/Resources.resx +++ b/src/Network/Network/Properties/Resources.resx @@ -724,9 +724,9 @@ The VirtualNetworkGatewayNatRule could not be found - The {0} doesn't support private endpoint connection. + The {0} doesn't register private endpoint connection. - The {0} doesn't support private link resource. + The {0} doesn't register private link resource. \ No newline at end of file From 822ca1e83d20ae594a7369bfbe0435afe4af0b25 Mon Sep 17 00:00:00 2001 From: Lucas Yao Date: Wed, 11 May 2022 10:02:13 +0800 Subject: [PATCH 10/16] [Network] add changelog. --- src/Network/Network/ChangeLog.md | 5 +++++ .../GetAzurePrivateLinkResourceCommand.cs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Network/Network/ChangeLog.md b/src/Network/Network/ChangeLog.md index 962467952126..dd0f4d9e71b0 100644 --- a/src/Network/Network/ChangeLog.md +++ b/src/Network/Network/ChangeLog.md @@ -19,6 +19,11 @@ ---> ## Upcoming Release +* Fixed unsupports Microsoft.Network/privateLinkServices [#16984]. + - `Get-AzPrivateEndpointConnection` +* Fixed shows friendly message if rp and resource is not supported [#17091]. + - `Get-AzPrivateEndpointConnection` + - `Get-AzPrivateLinkResource` ## Version 4.16.1 * Fixed `ArgumentNullException` in `Add-AzureRmRouteConfig` when `RouteTable.Routes` is null. diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs b/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs index 16c3399bf3d5..3a77469c368e 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs @@ -94,7 +94,7 @@ public override void Execute() if (!GenericProvider.SupportsPrivateLinkResourceType(this.PrivateLinkResourceType)) { - throw new ArgumentException(string.Format(Properties.Resources.UnsupportPrivateLinkResourceType, this.PrivateLinkResourceType)); + throw new AzPSApplicationException(string.Format(Properties.Resources.UnsupportPrivateLinkResourceType, this.PrivateLinkResourceType)); } IPrivateLinkProvider provider = PrivateLinkProviderFactory.CreatePrivateLinkProvder(this, Subscription, PrivateLinkResourceType); From 7bd8a93e7eef19a1ac47a88d17cecabbf24c8118 Mon Sep 17 00:00:00 2001 From: Lucas Yao Date: Wed, 11 May 2022 10:13:56 +0800 Subject: [PATCH 11/16] [Network] update changelog. --- src/Network/Network/ChangeLog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Network/Network/ChangeLog.md b/src/Network/Network/ChangeLog.md index c4d2c4a5bb78..1c04462e1e1e 100644 --- a/src/Network/Network/ChangeLog.md +++ b/src/Network/Network/ChangeLog.md @@ -46,7 +46,7 @@ - `New-AzVpnServerConfigurationPolicyGroup` - `Update-AzVpnServerConfigurationPolicyGroup` - `Remove-AzVpnServerConfigurationPolicyGroup` - - + ## Version 4.16.1 * Fixed `ArgumentNullException` in `Add-AzureRmRouteConfig` when `RouteTable.Routes` is null. From 6e4633b30c67ee1546dc3d94a742363d62cd8e32 Mon Sep 17 00:00:00 2001 From: Beisi Zhou Date: Wed, 11 May 2022 10:35:43 +0800 Subject: [PATCH 12/16] Update src/Network/Network/ChangeLog.md --- src/Network/Network/ChangeLog.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Network/Network/ChangeLog.md b/src/Network/Network/ChangeLog.md index 1c04462e1e1e..da06fec4c48f 100644 --- a/src/Network/Network/ChangeLog.md +++ b/src/Network/Network/ChangeLog.md @@ -19,9 +19,8 @@ ---> ## Upcoming Release -* Fixed unsupports Microsoft.Network/privateLinkServices [#16984]. - - `Get-AzPrivateEndpointConnection` -* Fixed shows friendly message if rp and resource is not supported [#17091]. +* Supported `Microsoft.Network/privateLinkServices` in `Get-AzPrivateEndpointConnection` [#16984]. +* Provided friendly message if resource type is not supported for private endpoint connection features [#17091]. - `Get-AzPrivateEndpointConnection` - `Get-AzPrivateLinkResource` * Added `DisableIPsecProtection` to `Virtual Network Gateway`. From be3623f7d560f333b8456dbc215cd2cbf10dcabf Mon Sep 17 00:00:00 2001 From: Beisi Zhou Date: Wed, 11 May 2022 10:36:59 +0800 Subject: [PATCH 13/16] Update src/Network/Network/ChangeLog.md --- src/Network/Network/ChangeLog.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Network/Network/ChangeLog.md b/src/Network/Network/ChangeLog.md index da06fec4c48f..9973601ac24c 100644 --- a/src/Network/Network/ChangeLog.md +++ b/src/Network/Network/ChangeLog.md @@ -21,8 +21,6 @@ ## Upcoming Release * Supported `Microsoft.Network/privateLinkServices` in `Get-AzPrivateEndpointConnection` [#16984]. * Provided friendly message if resource type is not supported for private endpoint connection features [#17091]. - - `Get-AzPrivateEndpointConnection` - - `Get-AzPrivateLinkResource` * Added `DisableIPsecProtection` to `Virtual Network Gateway`. * Added new cmdlets to create/manage authorization objects for ExpressRoutePort: - `Add-AzExpressRoutePortAuthorization` From 387414470713d6549e642fece0c1e53113d7cc34 Mon Sep 17 00:00:00 2001 From: Lucas Yao Date: Wed, 11 May 2022 16:18:16 +0800 Subject: [PATCH 14/16] [Network] adjust private link feature logic. --- .../ApproveAzurePrivateEndpointConnection.cs | 1 + .../DenyAzurePrivateEndpointConnection.cs | 1 + .../GetAzurePrivateEndpointConnection.cs | 1 + .../PrivateEndpointConnectionBaseCmdlet.cs | 16 +++++++++++++++- .../RemoveAzurePrivateEndpointConnection.cs | 1 + .../SetAzurePrivateEndpointConnection.cs | 1 + .../GetAzurePrivateLinkResourceCommand.cs | 4 ++-- .../GenericProvider.cs | 16 +++------------- .../PrivateLinkProviderFactory.cs | 2 +- src/Network/Network/Properties/Resources.resx | 4 ++-- 10 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/ApproveAzurePrivateEndpointConnection.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/ApproveAzurePrivateEndpointConnection.cs index 61716e538499..940240063d41 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/ApproveAzurePrivateEndpointConnection.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/ApproveAzurePrivateEndpointConnection.cs @@ -45,6 +45,7 @@ public override void Execute() if (this.IsParameterBound(c => c.ResourceId)) { + CheckResourceId(); var resourceIdentifier = new ResourceIdentifier(this.ResourceId); this.ResourceGroupName = resourceIdentifier.ResourceGroupName; this.Name = resourceIdentifier.ResourceName; diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/DenyAzurePrivateEndpointConnection.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/DenyAzurePrivateEndpointConnection.cs index 13f591c4e4a1..c0ddea9a4ef2 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/DenyAzurePrivateEndpointConnection.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/DenyAzurePrivateEndpointConnection.cs @@ -45,6 +45,7 @@ public override void Execute() if (this.IsParameterBound(c => c.ResourceId)) { + CheckResourceId(); var resourceIdentifier = new ResourceIdentifier(this.ResourceId); this.ResourceGroupName = resourceIdentifier.ResourceGroupName; this.Name = resourceIdentifier.ResourceName; diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/GetAzurePrivateEndpointConnection.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/GetAzurePrivateEndpointConnection.cs index 3aaddb3ad1b6..36b5d39483fc 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/GetAzurePrivateEndpointConnection.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/GetAzurePrivateEndpointConnection.cs @@ -45,6 +45,7 @@ public override void Execute() if (this.IsParameterBound(c => c.ResourceId)) { + CheckResourceId(); var resourceIdentifier = new ResourceIdentifier(this.ResourceId); this.ResourceGroupName = resourceIdentifier.ResourceGroupName; this.Name = resourceIdentifier.ResourceName; diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs index 956c560f9f71..95861b699cc7 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs @@ -16,6 +16,7 @@ using Microsoft.Azure.Commands.Network.PrivateLinkService.PrivateLinkServiceProvider; using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; using System.Management.Automation; +using Microsoft.Azure.Management.Internal.Resources.Utilities.Models; namespace Microsoft.Azure.Commands.Network { @@ -75,9 +76,22 @@ public abstract class PrivateEndpointConnectionBaseCmdlet : NetworkBaseCmdlet, I public string Subscription { get; set; } + /// + /// Check if the resource id format is valid. + /// + /// unvaild throw exception + public void CheckResourceId () + { + var resourceIdentifier = new ResourceIdentifier(this.ResourceId); + if (string.IsNullOrEmpty(resourceIdentifier.ParentResource)) + { + throw new AzPSApplicationException(string.Format(Properties.Resources.InvalidResourceId, this.ResourceId)); + } + + } protected IPrivateLinkProvider BuildProvider(string subscription, string privateLinkResourceType) { - if (!GenericProvider.SupportsPrivateLinkResourceType(privateLinkResourceType)) + if (!GenericProvider.SupportsPrivateLinkFeature(privateLinkResourceType)) throw new AzPSApplicationException(string.Format(Properties.Resources.UnsupportPrivateEndpointConnectionType, privateLinkResourceType)); return PrivateLinkProviderFactory.CreatePrivateLinkProvder(this, subscription, privateLinkResourceType); } diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/RemoveAzurePrivateEndpointConnection.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/RemoveAzurePrivateEndpointConnection.cs index 49dd99b848fd..21bf89483f9b 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/RemoveAzurePrivateEndpointConnection.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/RemoveAzurePrivateEndpointConnection.cs @@ -58,6 +58,7 @@ public override void Execute() if (this.IsParameterBound(c => c.ResourceId)) { + CheckResourceId(); var resourceIdentifier = new ResourceIdentifier(this.ResourceId); this.ResourceGroupName = resourceIdentifier.ResourceGroupName; this.Name = resourceIdentifier.ResourceName; diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/SetAzurePrivateEndpointConnection.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/SetAzurePrivateEndpointConnection.cs index 4a64de6a28fe..2034f65ae0a2 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/SetAzurePrivateEndpointConnection.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/SetAzurePrivateEndpointConnection.cs @@ -53,6 +53,7 @@ public override void Execute() if (this.IsParameterBound(c => c.ResourceId)) { + CheckResourceId(); var resourceIdentifier = new ResourceIdentifier(this.ResourceId); this.ResourceGroupName = resourceIdentifier.ResourceGroupName; this.Name = resourceIdentifier.ResourceName; diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs b/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs index 3a77469c368e..5c3b2965c684 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs @@ -91,8 +91,8 @@ public override void Execute() this.Subscription = DefaultProfile.DefaultContext.Subscription.Id; this.PrivateLinkResourceType = DynamicParameters[privateEndpointTypeName].Value as string; } - - if (!GenericProvider.SupportsPrivateLinkResourceType(this.PrivateLinkResourceType)) + // First check resource type whether support private link feature, if support then check whether support private link resource feature. + if (!GenericProvider.SupportsPrivateLinkFeature(this.PrivateLinkResourceType) || !ProviderConfiguration.GetProviderConfiguration(this.PrivateLinkResourceType).SupportListPrivateLinkResource) { throw new AzPSApplicationException(string.Format(Properties.Resources.UnsupportPrivateLinkResourceType, this.PrivateLinkResourceType)); } diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs index 1157750ec135..3b6cdcdc4f16 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/GenericProvider.cs @@ -37,7 +37,7 @@ public GenericProvider(NetworkBaseCmdlet baseCmdlet, string subscription, string #region Interface Implementation - public static bool SupportsPrivateLinkResourceType(string privateLinkResourceType) + public static bool SupportsPrivateLinkFeature(string privateLinkResourceType) { ProviderConfiguration configuration = ProviderConfiguration.GetProviderConfiguration(privateLinkResourceType); return (configuration != null); @@ -140,22 +140,12 @@ public PSPrivateLinkResource GetPrivateLinkResource(string resourceGroupName, st PrivateLinkResource resource = ServiceClient.Operations.GetResource(url, _configuration.ApiVersion); return ToPsPrivateLinkResource(resource); } - else if (_configuration.SupportListPrivateLinkResource) - { - return ListPrivateLinkResource(resourceGroupName, serviceName).Single(plr => plr.Name.Equals(name)); - } - else - { - throw new AzPSApplicationException(string.Format(Properties.Resources.UnsupportPrivateLinkResourceType, $"{_configuration.Type} api {_configuration.ApiVersion}")); - } + + return ListPrivateLinkResource(resourceGroupName, serviceName).Single(plr => plr.Name.Equals(name)); } public List ListPrivateLinkResource(string resourceGroupName, string serviceName) { - if (!_configuration.SupportListPrivateLinkResource) - { - throw new AzPSApplicationException(string.Format(Properties.Resources.UnsupportPrivateLinkResourceType, $"{_configuration.Type} api {_configuration.ApiVersion}")); - } var psPLRs = new List(); string url = BuildPrivateLinkResourcesURL(resourceGroupName, serviceName); IPage list = ServiceClient.Operations.GetResourcePage, PrivateLinkResource>(url, _configuration.ApiVersion); diff --git a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/PrivateLinkProviderFactory.cs b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/PrivateLinkProviderFactory.cs index bd840346ff1b..d59108babd01 100644 --- a/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/PrivateLinkProviderFactory.cs +++ b/src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/PrivateLinkProviderFactory.cs @@ -27,7 +27,7 @@ public static IPrivateLinkProvider CreatePrivateLinkProvder(NetworkBaseCmdlet cm return new NetworkingProvider(cmdlet); } - if(GenericProvider.SupportsPrivateLinkResourceType(privateLinkResourceType)) + if(GenericProvider.SupportsPrivateLinkFeature(privateLinkResourceType)) { return new GenericProvider(cmdlet, subscription, privateLinkResourceType); } diff --git a/src/Network/Network/Properties/Resources.resx b/src/Network/Network/Properties/Resources.resx index 59178d98ac65..7d3e579c0316 100644 --- a/src/Network/Network/Properties/Resources.resx +++ b/src/Network/Network/Properties/Resources.resx @@ -736,9 +736,9 @@ The VirtualNetworkGatewayNatRule could not be found - The {0} doesn't register private endpoint connection. + {0} doesn't support private endpoint connection. - The {0} doesn't register private link resource. + {0} doesn't support private link resource. \ No newline at end of file From 0053ceaf3b9619a362d78f3cf881ce86f4a8f383 Mon Sep 17 00:00:00 2001 From: Lucas Yao Date: Wed, 11 May 2022 17:16:02 +0800 Subject: [PATCH 15/16] [Network] add VaildateParentResourceAttribute for resource id field." --- .../ApproveAzurePrivateEndpointConnection.cs | 1 - .../DenyAzurePrivateEndpointConnection.cs | 1 - .../GetAzurePrivateEndpointConnection.cs | 1 - .../PrivateEndpointConnectionBaseCmdlet.cs | 32 +++++++++++-------- .../RemoveAzurePrivateEndpointConnection.cs | 1 - .../SetAzurePrivateEndpointConnection.cs | 1 - 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/ApproveAzurePrivateEndpointConnection.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/ApproveAzurePrivateEndpointConnection.cs index 940240063d41..61716e538499 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/ApproveAzurePrivateEndpointConnection.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/ApproveAzurePrivateEndpointConnection.cs @@ -45,7 +45,6 @@ public override void Execute() if (this.IsParameterBound(c => c.ResourceId)) { - CheckResourceId(); var resourceIdentifier = new ResourceIdentifier(this.ResourceId); this.ResourceGroupName = resourceIdentifier.ResourceGroupName; this.Name = resourceIdentifier.ResourceName; diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/DenyAzurePrivateEndpointConnection.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/DenyAzurePrivateEndpointConnection.cs index c0ddea9a4ef2..13f591c4e4a1 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/DenyAzurePrivateEndpointConnection.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/DenyAzurePrivateEndpointConnection.cs @@ -45,7 +45,6 @@ public override void Execute() if (this.IsParameterBound(c => c.ResourceId)) { - CheckResourceId(); var resourceIdentifier = new ResourceIdentifier(this.ResourceId); this.ResourceGroupName = resourceIdentifier.ResourceGroupName; this.Name = resourceIdentifier.ResourceName; diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/GetAzurePrivateEndpointConnection.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/GetAzurePrivateEndpointConnection.cs index 36b5d39483fc..3aaddb3ad1b6 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/GetAzurePrivateEndpointConnection.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/GetAzurePrivateEndpointConnection.cs @@ -45,7 +45,6 @@ public override void Execute() if (this.IsParameterBound(c => c.ResourceId)) { - CheckResourceId(); var resourceIdentifier = new ResourceIdentifier(this.ResourceId); this.ResourceGroupName = resourceIdentifier.ResourceGroupName; this.Name = resourceIdentifier.ResourceName; diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs index 95861b699cc7..e5770a376071 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs @@ -17,6 +17,7 @@ using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; using System.Management.Automation; using Microsoft.Azure.Management.Internal.Resources.Utilities.Models; +using System; namespace Microsoft.Azure.Commands.Network { @@ -27,6 +28,7 @@ public abstract class PrivateEndpointConnectionBaseCmdlet : NetworkBaseCmdlet, I ParameterSetName = "ByResourceId", ValueFromPipelineByPropertyName = true)] [ValidateNotNullOrEmpty] + [ValidateParentResourceNotNullOrEmpty] public string ResourceId { get; set; } [Alias("ResourceName")] @@ -76,24 +78,28 @@ public abstract class PrivateEndpointConnectionBaseCmdlet : NetworkBaseCmdlet, I public string Subscription { get; set; } - /// - /// Check if the resource id format is valid. - /// - /// unvaild throw exception - public void CheckResourceId () - { - var resourceIdentifier = new ResourceIdentifier(this.ResourceId); - if (string.IsNullOrEmpty(resourceIdentifier.ParentResource)) - { - throw new AzPSApplicationException(string.Format(Properties.Resources.InvalidResourceId, this.ResourceId)); - } - - } protected IPrivateLinkProvider BuildProvider(string subscription, string privateLinkResourceType) { if (!GenericProvider.SupportsPrivateLinkFeature(privateLinkResourceType)) throw new AzPSApplicationException(string.Format(Properties.Resources.UnsupportPrivateEndpointConnectionType, privateLinkResourceType)); return PrivateLinkProviderFactory.CreatePrivateLinkProvder(this, subscription, privateLinkResourceType); } + + /// + /// Validate parent resource of the resource id not null or empty. + /// + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] + public sealed class ValidateParentResourceNotNullOrEmptyAttribute : ValidateArgumentsAttribute + { + protected override void Validate(object arguments, EngineIntrinsics engineIntrinsics) + { + string resourceId = (string)arguments; + var resourceIdentifier = new ResourceIdentifier(resourceId); + if (string.IsNullOrEmpty(resourceIdentifier.ParentResource)) + { + throw new AzPSApplicationException(string.Format(Properties.Resources.InvalidResourceId, resourceId)); + } + } + } } } diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/RemoveAzurePrivateEndpointConnection.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/RemoveAzurePrivateEndpointConnection.cs index 21bf89483f9b..49dd99b848fd 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/RemoveAzurePrivateEndpointConnection.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/RemoveAzurePrivateEndpointConnection.cs @@ -58,7 +58,6 @@ public override void Execute() if (this.IsParameterBound(c => c.ResourceId)) { - CheckResourceId(); var resourceIdentifier = new ResourceIdentifier(this.ResourceId); this.ResourceGroupName = resourceIdentifier.ResourceGroupName; this.Name = resourceIdentifier.ResourceName; diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/SetAzurePrivateEndpointConnection.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/SetAzurePrivateEndpointConnection.cs index 2034f65ae0a2..4a64de6a28fe 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/SetAzurePrivateEndpointConnection.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/SetAzurePrivateEndpointConnection.cs @@ -53,7 +53,6 @@ public override void Execute() if (this.IsParameterBound(c => c.ResourceId)) { - CheckResourceId(); var resourceIdentifier = new ResourceIdentifier(this.ResourceId); this.ResourceGroupName = resourceIdentifier.ResourceGroupName; this.Name = resourceIdentifier.ResourceName; From 08dce7f992f342306665c91da593f3291f91b023 Mon Sep 17 00:00:00 2001 From: Lucas Yao Date: Wed, 11 May 2022 19:43:55 +0800 Subject: [PATCH 16/16] [Network] changed public to internal. --- .../PrivateEndpointConnectionBaseCmdlet.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs index e5770a376071..aee9df129ac1 100644 --- a/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs +++ b/src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs @@ -89,7 +89,7 @@ protected IPrivateLinkProvider BuildProvider(string subscription, string private /// Validate parent resource of the resource id not null or empty. /// [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] - public sealed class ValidateParentResourceNotNullOrEmptyAttribute : ValidateArgumentsAttribute + internal sealed class ValidateParentResourceNotNullOrEmptyAttribute : ValidateArgumentsAttribute { protected override void Validate(object arguments, EngineIntrinsics engineIntrinsics) {