12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
- using System . Management . Automation ;
15
+ using Microsoft . Azure . Commands . Common . Authentication . Models ;
16
16
using Microsoft . Azure . Commands . Profile . Models ;
17
+ using Microsoft . Azure . Commands . Profile . Properties ;
17
18
using Microsoft . Azure . Commands . ResourceManager . Common ;
18
19
using Microsoft . WindowsAzure . Commands . Common ;
19
- using Microsoft . Azure . Commands . Profile . Properties ;
20
20
using System ;
21
- using Microsoft . Azure . Commands . Common . Authentication . Models ;
21
+ using System . Collections . ObjectModel ;
22
+ using System . Management . Automation ;
22
23
23
24
namespace Microsoft . Azure . Commands . Profile
24
25
{
25
26
/// <summary>
26
- /// Cmdlet to change current Azure context.
27
+ /// Cmdlet to change current Azure context.
27
28
/// </summary>
28
29
[ Cmdlet ( VerbsCommon . Set , "AzureRmContext" , DefaultParameterSetName = SubscriptionNameParameterSet ) ]
29
30
[ Alias ( "Select-AzureRmSubscription" ) ]
30
31
[ OutputType ( typeof ( PSAzureContext ) ) ]
31
- public class SetAzureRMContextCommand : AzureRMCmdlet
32
+ public class SetAzureRMContextCommand : AzureRMCmdlet , IDynamicParameters
32
33
{
33
34
private const string SubscriptionNameParameterSet = "SubscriptionName" ;
34
35
private const string SubscriptionIdParameterSet = "SubscriptionId" ;
35
36
private const string ContextParameterSet = "Context" ;
36
-
37
- [ Parameter ( ParameterSetName = SubscriptionNameParameterSet , Mandatory = false , HelpMessage = "TenantId name or ID" , ValueFromPipelineByPropertyName = true ) ]
38
- [ Parameter ( ParameterSetName = SubscriptionIdParameterSet , Mandatory = false , HelpMessage = "TenantId name or ID" , ValueFromPipelineByPropertyName = true ) ]
39
- [ Alias ( "Domain" ) ]
40
- [ ValidateNotNullOrEmpty ]
41
- public string TenantId { get ; set ; }
42
-
43
- [ Parameter ( ParameterSetName = SubscriptionIdParameterSet , Mandatory = false , HelpMessage = "Subscription" , ValueFromPipelineByPropertyName = true ) ]
44
- [ ValidateNotNullOrEmpty ]
45
- public string SubscriptionId { get ; set ; }
37
+ private RuntimeDefinedParameter _tenantId ;
38
+ private RuntimeDefinedParameter _subscriptionId ;
46
39
47
40
[ Parameter ( ParameterSetName = SubscriptionNameParameterSet , Mandatory = false , HelpMessage = "Subscription Name" , ValueFromPipelineByPropertyName = true ) ]
48
41
[ ValidateNotNullOrEmpty ]
@@ -51,6 +44,22 @@ public class SetAzureRMContextCommand : AzureRMCmdlet
51
44
[ Parameter ( ParameterSetName = ContextParameterSet , Mandatory = true , HelpMessage = "Context" , ValueFromPipeline = true ) ]
52
45
public PSAzureContext Context { get ; set ; }
53
46
47
+ private string TenantId
48
+ {
49
+ get
50
+ {
51
+ return _tenantId == null ? null : ( string ) _tenantId . Value ;
52
+ }
53
+ }
54
+
55
+ private string SubscriptionId
56
+ {
57
+ get
58
+ {
59
+ return _subscriptionId == null ? null : ( string ) _subscriptionId . Value ;
60
+ }
61
+ }
62
+
54
63
public override void ExecuteCmdlet ( )
55
64
{
56
65
if ( ParameterSetName == ContextParameterSet )
@@ -60,7 +69,7 @@ public override void ExecuteCmdlet()
60
69
}
61
70
else if ( ParameterSetName == SubscriptionNameParameterSet || ParameterSetName == SubscriptionIdParameterSet )
62
71
{
63
- if ( string . IsNullOrWhiteSpace ( SubscriptionId )
72
+ if ( string . IsNullOrWhiteSpace ( SubscriptionId )
64
73
&& string . IsNullOrWhiteSpace ( SubscriptionName )
65
74
&& string . IsNullOrWhiteSpace ( TenantId ) )
66
75
{
@@ -82,7 +91,7 @@ public override void ExecuteCmdlet()
82
91
AzureRmProfileProvider . Instance . Profile . Context . Subscription != null &&
83
92
AzureRmProfileProvider . Instance . Profile . Context . Subscription . State != null &&
84
93
! AzureRmProfileProvider . Instance . Profile . Context . Subscription . State . Equals (
85
- "Enabled" ,
94
+ "Enabled" ,
86
95
StringComparison . OrdinalIgnoreCase ) )
87
96
{
88
97
WriteWarning ( string . Format (
@@ -91,5 +100,55 @@ public override void ExecuteCmdlet()
91
100
}
92
101
WriteObject ( ( PSAzureContext ) AzureRmProfileProvider . Instance . Profile . Context ) ;
93
102
}
103
+
104
+ public object GetDynamicParameters ( )
105
+ {
106
+ return CreateDynamicParameterDictionary ( ) ;
107
+ }
108
+
109
+ private RuntimeDefinedParameterDictionary CreateDynamicParameterDictionary ( )
110
+ {
111
+ var runtimeDefinedParameterDictionary = new RuntimeDefinedParameterDictionary ( ) ;
112
+
113
+ var subscriptionIdAttributes = new Collection < Attribute >
114
+ {
115
+ new ParameterAttribute
116
+ {
117
+ ParameterSetName = SubscriptionIdParameterSet ,
118
+ Mandatory = false ,
119
+ HelpMessage = "Subscription" ,
120
+ ValueFromPipelineByPropertyName = true
121
+ } ,
122
+ new ValidateSetAttribute ( AzureRmProfileProvider . Instance . Profile . Context . Account . GetPropertyAsArray ( AzureAccount . Property . Subscriptions ) ) ,
123
+ } ;
124
+
125
+ var tenantIdAttributes = new Collection < Attribute >
126
+ {
127
+ new ParameterAttribute
128
+ {
129
+ ParameterSetName = SubscriptionNameParameterSet ,
130
+ Mandatory = false ,
131
+ HelpMessage = "TenantId name or ID" ,
132
+ ValueFromPipelineByPropertyName = true
133
+ } ,
134
+ new ParameterAttribute
135
+ {
136
+ ParameterSetName = SubscriptionIdParameterSet ,
137
+ Mandatory = false ,
138
+ HelpMessage = "TenantId name or ID" ,
139
+ ValueFromPipelineByPropertyName = true
140
+ } ,
141
+ new AliasAttribute ( "Domain" ) ,
142
+ new ValidateSetAttribute ( AzureRmProfileProvider . Instance . Profile . Context . Account . GetPropertyAsArray ( AzureAccount . Property . Tenants ) ) ,
143
+ } ;
144
+
145
+ _tenantId = new RuntimeDefinedParameter ( "TenantId" , typeof ( string ) , tenantIdAttributes ) ;
146
+ _subscriptionId = new RuntimeDefinedParameter ( "SubscriptionId" , typeof ( string ) , subscriptionIdAttributes ) ;
147
+
148
+ runtimeDefinedParameterDictionary . Add ( "SubscriptionId" , _subscriptionId ) ;
149
+ runtimeDefinedParameterDictionary . Add ( "TenantId" , _tenantId ) ;
150
+
151
+ return runtimeDefinedParameterDictionary ;
152
+ }
94
153
}
95
154
}
0 commit comments