Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -76,6 +77,8 @@ public abstract class PrivateEndpointConnectionBaseCmdlet : NetworkBaseCmdlet, I

protected IPrivateLinkProvider BuildProvider(string subscription, string privateLinkResourceType)
{
if (!GenericProvider.SupportsPrivateLinkResourceType(privateLinkResourceType))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please help to rename method to SupportsPrivateLinkFeature because it includes resource type and connection endpoint both.

throw new System.Exception($"The {privateLinkResourceType} doesn't support private endpoint connection");
return PrivateLinkProviderFactory.CreatePrivateLinkProvder(this, subscription, privateLinkResourceType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -89,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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.SupportPrivateLinkResource)
{
throw new System.Exception($"The {_configuration.Type} api {_configuration.ApiVersion} doesn't support private link resource");
}
if (_configuration.SupportGetPrivateLinkResource)
{
string url = BuildPrivateLinkResourceURL(resourceGroupName, serviceName, name);
PrivateLinkResource resource = ServiceClient.Operations.GetResource<PrivateLinkResource>(url, _configuration.ApiVersion);
Expand All @@ -147,6 +151,10 @@ public PSPrivateLinkResource GetPrivateLinkResource(string resourceGroupName, st

public List<PSPrivateLinkResource> ListPrivateLinkResource(string resourceGroupName, string serviceName)
{
if (!_configuration.SupportPrivateLinkResource)
{
throw new System.Exception($"The {_configuration.Type} api {_configuration.ApiVersion} doesn't support private link resource");
}
var psPLRs = new List<PSPrivateLinkResource>();
string url = BuildPrivateLinkResourcesURL(resourceGroupName, serviceName);
IPage<PrivateLinkResource> list = ServiceClient.Operations.GetResourcePage<Page<PrivateLinkResource>, PrivateLinkResource>(url, _configuration.ApiVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -71,43 +72,55 @@ 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)
/// <summary>
/// Register priavte endopoint connection and private link resource configuration
/// </summary>
/// <param name="type">Resource type</param>
/// <param name="apiVersion">Resource api version</param>
/// <param name="hasConnectionsURI">True if the private endpoint connection can be list by URL <see cref="GenericProvider.BuildPrivateEndpointConnectionsURL(string, string)"/>, otherwise it can be list by URL <see cref="GenericProvider.BuildPrivateEndpointConnectionsOwnerURL(string, string)"/></param>
/// <param name="supportGetPrivateLinkResource">True if the private link resource can be get by Id, otherwise it can be list</param>
/// <param name="supportPrivateLinkResource">True if the private link resource be supported, otherwise false</param>
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,
HasResourceURI = hasResourceURI
SupportGetPrivateLinkResource = supportGetPrivateLinkResource,
SupportPrivateLinkResource = supportPrivateLinkResource,
};
_configurations.Add(type, configuration);
}

public string Type { get; set; }
public string ApiVersion { get; set; }
public bool HasConnectionsURI { get; set; }
public bool HasResourceURI { get; set; }
public bool SupportGetPrivateLinkResource { get; set; }
public bool SupportPrivateLinkResource { get; set; }

public static ProviderConfiguration GetProviderConfiguration(string type)
{
return _configurations[type];
ProviderConfiguration outProviderConfiguration = null;
_configurations.TryGetValue(type, out outProviderConfiguration);
return outProviderConfiguration;
}

/// <summary>
/// Generate a runtime parameter with ValidateSet matching the current context
/// </summary>
/// <param name="serviceType">Has two value, PLR => private link resource, PEC => private endpoint connection.</param>
/// <param name="name">The name of the parameter</param>
/// <param name="runtimeParameter">The returned runtime parameter for context, with appropriate validate set</param>
/// <returns>True if one or more contexts were found, otherwise false</returns>
public static bool TryGetProvideServiceParameter(string name, string parameterSetName, out RuntimeDefinedParameter runtimeParameter)
public static bool TryGetProvideServiceParameter(string serviceType, string name, string parameterSetName, out RuntimeDefinedParameter runtimeParameter)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is It is not required. Let's discuss further.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, the method should be moved to each cmdlet implementation. So, one should go to privateendpointconnnection, another goes to GetAzurePrivateLinkResource.

{
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<Attribute>()
Expand Down