Skip to content

Commit 1cab99d

Browse files
author
Youri
committed
Add Suggestion and New ParameterSet
1.Add Suggestion to Parameter 'PrivateLinkResourceType' 2.Change the ParameterSet 'ByResource' of the Cmdlet 'PrivateLinkResourceType' 3.Add ParameterSet to Cmdlet 'Get-AzPrivateLinkResource'
1 parent 8dd2243 commit 1cab99d

File tree

4 files changed

+113
-25
lines changed

4 files changed

+113
-25
lines changed

src/Network/Network/PrivateLinkService/PrivateEndpointConnection/GetAzurePrivateEndpointConnection.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@
1717
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
1818
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1919
using System;
20+
using System.Collections.ObjectModel;
2021
using System.Linq;
2122
using System.Management.Automation;
23+
using System.Management.Automation.Language;
2224

2325
namespace Microsoft.Azure.Commands.Network
2426
{
2527
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PrivateEndpointConnection", DefaultParameterSetName = "ByResourceId"), OutputType(typeof(PSPrivateEndpointConnection))]
26-
public class GetAzurePrivateEndpointConnection : PrivateEndpointConnectionBaseCmdlet
28+
public class GetAzurePrivateEndpointConnection : PrivateEndpointConnectionBaseCmdlet, IDynamicParameters
2729
{
2830
[Parameter(
2931
Mandatory = true,
3032
ParameterSetName = "ByPrivateLinkResourceId",
3133
ValueFromPipelineByPropertyName = true)]
3234
[ValidateNotNullOrEmpty]
3335
public string PrivateLinkResourceId { get; set; }
34-
3536
[CmdletParameterBreakingChange("Description", ChangeDescription = "Parameter is being deprecated without being replaced")]
3637
[Parameter(
3738
Mandatory = false,
@@ -60,9 +61,12 @@ public override void Execute()
6061
this.PrivateLinkResourceType = resourceIdentifier.ResourceType;
6162
this.ServiceName = resourceIdentifier.ResourceName;
6263
}
64+
else
65+
{
66+
this.Subscription = DefaultProfile.DefaultContext.Subscription.Id;
67+
this.PrivateLinkResourceType = DynamicParameters["PrivateLinkResourceType"].Value as string;
6368

6469
IPrivateLinkProvider provider = BuildProvider(this.Subscription, this.PrivateLinkResourceType);
65-
6670
if (ShouldGetByName(this.ResourceGroupName, this.Name))
6771
{
6872
var pec = provider.GetPrivateEndpointConnection(this.ResourceGroupName, this.ServiceName, this.Name);

src/Network/Network/PrivateLinkService/PrivateEndpointConnection/PrivateEndpointConnectionBaseCmdlet.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
namespace Microsoft.Azure.Commands.Network
2020
{
21-
public abstract class PrivateEndpointConnectionBaseCmdlet : NetworkBaseCmdlet
21+
public abstract class PrivateEndpointConnectionBaseCmdlet : NetworkBaseCmdlet, IDynamicParameters
2222
{
2323
[Parameter(
2424
Mandatory = true,
@@ -37,14 +37,6 @@ public abstract class PrivateEndpointConnectionBaseCmdlet : NetworkBaseCmdlet
3737
[SupportsWildcards]
3838
public virtual string Name { get; set; }
3939

40-
[Parameter(
41-
Mandatory = true,
42-
ValueFromPipelineByPropertyName = true,
43-
HelpMessage = "The private link service name.",
44-
ParameterSetName = "ByResource")]
45-
[ValidateNotNullOrEmpty]
46-
public string ServiceName { get; set; }
47-
4840
[Parameter(
4941
Mandatory = true,
5042
ValueFromPipelineByPropertyName = true,
@@ -55,10 +47,28 @@ public abstract class PrivateEndpointConnectionBaseCmdlet : NetworkBaseCmdlet
5547
public virtual string ResourceGroupName { get; set; }
5648

5749
[Parameter(
58-
Mandatory = false,
50+
Mandatory = true,
5951
ValueFromPipelineByPropertyName = true,
60-
HelpMessage = "The private link resource type.",
61-
ParameterSetName = "ByResource")]
52+
HelpMessage = "The private link service name.",
53+
ParameterSetName = "ByResource")]
54+
[ValidateNotNullOrEmpty]
55+
public string ServiceName { get; set; }
56+
57+
protected RuntimeDefinedParameterDictionary DynamicParameters;
58+
59+
string NamedContextParameterSet = "ByResource";
60+
public object GetDynamicParameters()
61+
{
62+
var parameters = new RuntimeDefinedParameterDictionary();
63+
RuntimeDefinedParameter namedParameter;
64+
if (ProviderConfiguration.TryGetProvideServiceParameter("PrivateLinkResourceType", NamedContextParameterSet, out namedParameter))
65+
{
66+
parameters.Add("PrivateLinkResourceType", namedParameter);
67+
}
68+
DynamicParameters = parameters;
69+
return parameters;
70+
}
71+
6272
public string PrivateLinkResourceType { get; set; }
6373

6474
public string Subscription { get; set; }

src/Network/Network/PrivateLinkService/PrivateLinkResource/GetAzurePrivateLinkResourceCommand.cs

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
using Microsoft.Azure.Commands.Network.PrivateLinkService.PrivateLinkServiceProvider;
1717
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
1818
using Microsoft.Azure.Management.Network.Models;
19+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1920
using System;
2021
using System.Management.Automation;
2122

2223
namespace Microsoft.Azure.Commands.Network
2324
{
2425
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "PrivateLinkResource", DefaultParameterSetName = "ByPrivateLinkResourceId"), OutputType(typeof(PSPrivateLinkResource))]
25-
public class GetAzurePrivateLinkResourceCommand : NetworkBaseCmdlet
26+
public class GetAzurePrivateLinkResourceCommand : NetworkBaseCmdlet, IDynamicParameters
2627
{
2728
[Parameter(
2829
Mandatory = true,
@@ -31,22 +32,62 @@ public class GetAzurePrivateLinkResourceCommand : NetworkBaseCmdlet
3132
[ValidateNotNullOrEmpty]
3233
public string PrivateLinkResourceId { get; set; }
3334

35+
[Parameter(
36+
Mandatory = true,
37+
ValueFromPipelineByPropertyName = true,
38+
HelpMessage = "The resource group name.",
39+
ParameterSetName = "ByResource")]
40+
[ValidateNotNullOrEmpty]
41+
public virtual string ResourceGroupName { get; set; }
42+
43+
[Parameter(
44+
Mandatory = true,
45+
ValueFromPipelineByPropertyName = true,
46+
HelpMessage = "The private link service name.",
47+
ParameterSetName = "ByResource")]
48+
[ValidateNotNullOrEmpty]
49+
public string ServiceName { get; set; }
50+
51+
public string PrivateLinkResourceType { get; set; }
52+
string NamedContextParameterSet = "ByResource";
53+
private RuntimeDefinedParameterDictionary DynamicParameters;
54+
public string Subscription { get; set; }
55+
56+
public object GetDynamicParameters()
57+
{
58+
var parameters = new RuntimeDefinedParameterDictionary();
59+
RuntimeDefinedParameter namedParameter;
60+
if (ProviderConfiguration.TryGetProvideServiceParameter("PrivateLinkResourceType", NamedContextParameterSet, out namedParameter))
61+
{
62+
parameters.Add("PrivateLinkResourceType", namedParameter);
63+
}
64+
DynamicParameters = parameters;
65+
return parameters;
66+
}
67+
3468
public override void Execute()
3569
{
3670
base.Execute();
37-
38-
var resourceIdentifier = new ResourceIdentifier(this.PrivateLinkResourceId);
39-
string ResourceGroupName = resourceIdentifier.ResourceGroupName;
40-
string Name = resourceIdentifier.ResourceName;
41-
string ResourceType = resourceIdentifier.ResourceType;
42-
43-
IPrivateLinkProvider provider = PrivateLinkProviderFactory.CreatePrivateLinkProvder(this, resourceIdentifier.Subscription, ResourceType);
71+
if (this.IsParameterBound(c => c.PrivateLinkResourceId))
72+
{
73+
var resourceIdentifier = new ResourceIdentifier(this.PrivateLinkResourceId);
74+
this.ResourceGroupName = resourceIdentifier.ResourceGroupName;
75+
this.ServiceName = resourceIdentifier.ResourceName;
76+
this.PrivateLinkResourceType = resourceIdentifier.ResourceType;
77+
this.Subscription = resourceIdentifier.Subscription;
78+
}
79+
else
80+
{
81+
this.Subscription = DefaultProfile.DefaultContext.Subscription.Id;
82+
this.PrivateLinkResourceType = DynamicParameters["PrivateLinkResourceType"].Value as string;
83+
}
84+
IPrivateLinkProvider provider = PrivateLinkProviderFactory.CreatePrivateLinkProvder(this, Subscription, PrivateLinkResourceType);
4485
if (provider == null)
4586
{
4687
throw new ArgumentException(string.Format(Properties.Resources.InvalidResourceId, this.PrivateLinkResourceId));
4788
}
48-
49-
var plrs = provider.ListPrivateLinkResource(ResourceGroupName, Name);
89+
90+
var plrs = provider.ListPrivateLinkResource(ResourceGroupName, ServiceName);
5091
WriteObject(plrs, true);
5192

5293
}

src/Network/Network/PrivateLinkService/PrivateLinkServiceProvider/ProviderConfiguration.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Linq;
5+
using System.Management.Automation;
36
using System.Text;
47

58
namespace Microsoft.Azure.Commands.Network.PrivateLinkService.PrivateLinkServiceProvider
@@ -50,5 +53,35 @@ public static ProviderConfiguration GetProviderConfiguration(string type)
5053
{
5154
return _configurations[type];
5255
}
56+
57+
/// <summary>
58+
/// Generate a runtime parameter with ValidateSet matching the current context
59+
/// </summary>
60+
/// <param name="name">The name of the parameter</param>
61+
/// <param name="runtimeParameter">The returned runtime parameter for context, with appropriate validate set</param>
62+
/// <returns>True if one or more contexts were found, otherwise false</returns>
63+
public static bool TryGetProvideServiceParameter(string name, string parameterSetName, out RuntimeDefinedParameter runtimeParameter)
64+
{
65+
var result = false;
66+
runtimeParameter = null;
67+
if (_configurations != null && _configurations.Values != null)
68+
{
69+
var ObjArray = _configurations.Values.ToArray();
70+
var ProvideTypeList = ObjArray.Select(c => c.Type).ToArray();
71+
runtimeParameter = new RuntimeDefinedParameter(
72+
name, typeof(string),
73+
new Collection<Attribute>()
74+
{
75+
new ParameterAttribute { Mandatory = false,
76+
ValueFromPipelineByPropertyName = true,
77+
HelpMessage = "The private link resource type.",
78+
ParameterSetName = "ByResource" },
79+
new ValidateSetAttribute(ProvideTypeList)
80+
}
81+
);
82+
result = true;
83+
}
84+
return result;
85+
}
5386
}
5487
}

0 commit comments

Comments
 (0)