12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
- using Microsoft . Azure . Management . RemoteApp ;
16
- using Microsoft . Azure . Management . RemoteApp . Models ;
17
- using System . Management . Automation ;
18
- using System . Net ;
19
-
20
15
namespace Microsoft . Azure . Management . RemoteApp . Cmdlets
21
16
{
17
+ using Microsoft . Azure . Management . RemoteApp ;
18
+ using Microsoft . Azure . Management . RemoteApp . Models ;
19
+ using Microsoft . WindowsAzure . Management . Network ;
20
+ using Microsoft . WindowsAzure . Management . Network . Models ;
21
+ using System ;
22
+ using System . Collections . Generic ;
23
+ using System . Management . Automation ;
24
+ using System . Net ;
25
+ using System . Threading . Tasks ;
26
+
22
27
[ Cmdlet ( VerbsCommon . New , "AzureRemoteAppCollection" , DefaultParameterSetName = NoDomain ) , OutputType ( typeof ( TrackingResult ) ) ]
28
+
23
29
public class NewAzureRemoteAppCollection : CmdletWithCollection
24
30
{
25
31
private const string DomainJoined = "DomainJoined" ;
@@ -55,6 +61,22 @@ public class NewAzureRemoteAppCollection : CmdletWithCollection
55
61
) ]
56
62
public string VNetName { get ; set ; }
57
63
64
+ [ Parameter ( Mandatory = false ,
65
+ ValueFromPipelineByPropertyName = true ,
66
+ ParameterSetName = DomainJoined ,
67
+ HelpMessage = "For Azure VNets only, a comma-separated list of DNS servers for the VNet."
68
+ ) ]
69
+ [ ValidateNotNullOrEmpty ]
70
+ public string DnsServers { get ; set ; }
71
+
72
+ [ Parameter ( Mandatory = false ,
73
+ ValueFromPipelineByPropertyName = true ,
74
+ ParameterSetName = DomainJoined ,
75
+ HelpMessage = "For Azure VNets only, the name of the subnet."
76
+ ) ]
77
+ [ ValidateNotNullOrEmpty ]
78
+ public string SubnetName { get ; set ; }
79
+
58
80
[ Parameter ( Mandatory = true ,
59
81
Position = 4 ,
60
82
ValueFromPipelineByPropertyName = true ,
@@ -96,6 +118,10 @@ public class NewAzureRemoteAppCollection : CmdletWithCollection
96
118
97
119
public override void ExecuteCmdlet ( )
98
120
{
121
+ // register the subscription for this service if it has not been before
122
+ // sebsequent call to register is redundent
123
+ RegisterSubscriptionWithRdfeForRemoteApp ( ) ;
124
+
99
125
NetworkCredential creds = null ;
100
126
CollectionCreationDetails details = new CollectionCreationDetails ( )
101
127
{
@@ -112,39 +138,139 @@ public override void ExecuteCmdlet()
112
138
switch ( ParameterSetName )
113
139
{
114
140
case DomainJoined :
141
+ {
142
+ creds = Credential . GetNetworkCredential ( ) ;
143
+ details . VnetName = VNetName ;
144
+
145
+ if ( SubnetName != null && DnsServers != null )
115
146
{
116
- creds = Credential . GetNetworkCredential ( ) ;
117
- details . VnetName = VNetName ;
118
-
119
- details . AdInfo = new ActiveDirectoryConfig ( )
120
- {
121
- DomainName = Domain ,
122
- OrganizationalUnit = OrganizationalUnit ,
123
- UserName = creds . UserName ,
124
- Password = creds . Password ,
125
- } ;
126
- break ;
147
+ details . SubnetName = SubnetName ;
148
+ details . DnsServers = DnsServers . Split ( new char [ ] { ',' } ) ;
149
+
150
+ ValidateCustomerVNetParams ( details . VnetName , details . SubnetName , details . DnsServers ) ;
127
151
}
152
+
153
+ details . AdInfo = new ActiveDirectoryConfig ( )
154
+ {
155
+ DomainName = Domain ,
156
+ OrganizationalUnit = OrganizationalUnit ,
157
+ UserName = creds . UserName ,
158
+ Password = creds . Password ,
159
+ } ;
160
+ break ;
161
+ }
128
162
case NoDomain :
163
+ {
164
+ details . Region = Region ;
165
+ break ;
166
+ }
167
+ }
168
+
169
+ response = CallClient ( ( ) => Client . Collections . Create ( false , details ) , Client . Collections ) ;
170
+
171
+ if ( response != null )
172
+ {
173
+ TrackingResult trackingId = new TrackingResult ( response ) ;
174
+ WriteObject ( trackingId ) ;
175
+ }
176
+
177
+ }
178
+
179
+ private bool ValidateCustomerVNetParams ( string name , string subnet , IEnumerable < string > dns )
180
+ {
181
+ NetworkListResponse . VirtualNetworkSite azureVNet = GetAzureVNet ( name ) ;
182
+ bool isValidSubnetName = false ;
183
+ bool isValidDnsServers = true ;
184
+
185
+ if ( azureVNet == null )
186
+ {
187
+ ErrorRecord er = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
188
+ String . Format ( "Invalid Argument VNetName: {0} not found" , name ) ,
189
+ String . Empty ,
190
+ Client . Collections ,
191
+ ErrorCategory . InvalidArgument
192
+ ) ;
193
+
194
+ ThrowTerminatingError ( er ) ;
195
+ }
196
+
197
+ foreach ( NetworkListResponse . Subnet azureSubnet in azureVNet . Subnets )
198
+ {
199
+ if ( string . Compare ( azureSubnet . Name , subnet , true ) == 0 )
200
+ {
201
+ isValidSubnetName = true ;
202
+ break ;
203
+ }
204
+ }
205
+
206
+ if ( ! isValidSubnetName )
207
+ {
208
+ ErrorRecord er = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
209
+ String . Format ( "Invalid Argument SubnetName: {0} not found" , subnet ) ,
210
+ String . Empty ,
211
+ Client . Collections ,
212
+ ErrorCategory . InvalidArgument
213
+ ) ;
214
+
215
+ ThrowTerminatingError ( er ) ;
216
+ }
217
+
218
+ foreach ( string dnsServerName in dns )
219
+ {
220
+ bool isValidDnsServer = false ;
221
+
222
+ foreach ( var dnsServer in azureVNet . DnsServers )
223
+ {
224
+ if ( string . Compare ( dnsServerName , dnsServer . Name , true ) == 0 )
129
225
{
130
- details . Region = Region ;
226
+ isValidDnsServer = true ;
131
227
break ;
132
228
}
229
+ }
230
+
231
+ if ( ! isValidDnsServer )
232
+ {
233
+ isValidDnsServers = false ;
234
+ break ;
235
+ }
133
236
}
134
237
135
- // register the subscription for this service if it has not been before
136
- // sebsequent call to register is redundent
238
+ if ( ! isValidDnsServers )
239
+ {
240
+ ErrorRecord er = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
241
+ String . Format ( "Invalid Argument DnsServers: {0} not found" , dns ) ,
242
+ String . Empty ,
243
+ Client . Collections ,
244
+ ErrorCategory . InvalidArgument
245
+ ) ;
137
246
138
- RegisterSubscriptionWithRdfeForRemoteApp ( ) ;
247
+ ThrowTerminatingError ( er ) ;
248
+ }
139
249
140
- response = CallClient ( ( ) => Client . Collections . Create ( false , details ) , Client . Collections ) ;
250
+ return true ;
251
+ }
141
252
142
- if ( response != null )
253
+ private NetworkListResponse . VirtualNetworkSite GetAzureVNet ( string name )
254
+ {
255
+ NetworkManagementClient networkClient = new NetworkManagementClient ( this . Client . Credentials , this . Client . BaseUri ) ;
256
+ Task < NetworkListResponse > listNetworkTask = networkClient . Networks . ListAsync ( ) ;
257
+
258
+ listNetworkTask . Wait ( ) ;
259
+
260
+ if ( listNetworkTask . Status == TaskStatus . RanToCompletion )
143
261
{
144
- TrackingResult trackingId = new TrackingResult ( response ) ;
145
- WriteObject ( trackingId ) ;
262
+ NetworkListResponse networkList = listNetworkTask . Result ;
263
+
264
+ foreach ( NetworkListResponse . VirtualNetworkSite network in networkList . VirtualNetworkSites )
265
+ {
266
+ if ( network . Name == name )
267
+ {
268
+ return network ;
269
+ }
270
+ }
146
271
}
147
272
273
+ return null ;
148
274
}
149
275
}
150
276
}
0 commit comments