19
19
// Changes to this file may cause incorrect behavior and will be lost if the
20
20
// code is regenerated.
21
21
22
+ using Microsoft . Azure . Commands . Common . Strategies . Compute ;
22
23
using Microsoft . Azure . Commands . Compute . Automation . Models ;
23
24
using Microsoft . Azure . Management . Compute ;
24
25
using Microsoft . Azure . Management . Compute . Models ;
27
28
using System . Collections . Generic ;
28
29
using System . Linq ;
29
30
using System . Management . Automation ;
31
+ using Microsoft . Azure . Commands . Common . Strategies ;
32
+ using Microsoft . Azure . Commands . Common . Strategies . ResourceManager ;
33
+ using System . Threading ;
34
+ using Microsoft . Azure . Commands . ResourceManager . Common . ArgumentCompleters ;
35
+ using Microsoft . Azure . Commands . Common . Strategies . Network ;
36
+ using Microsoft . Azure . Commands . Compute . Common ;
37
+ using System . Net ;
30
38
31
39
namespace Microsoft . Azure . Commands . Compute . Automation
32
40
{
@@ -115,23 +123,119 @@ protected PSArgument[] CreateVirtualMachineScaleSetCreateOrUpdateParameters()
115
123
[ OutputType ( typeof ( PSVirtualMachineScaleSet ) ) ]
116
124
public partial class NewAzureRmVmss : ComputeAutomationBaseCmdlet
117
125
{
126
+ public const string SimpleParameterSet = "SimpleParameterSet" ;
127
+
118
128
protected override void ProcessRecord ( )
119
129
{
120
- ExecuteClientAction ( ( ) =>
130
+ switch ( ParameterSetName )
121
131
{
122
- if ( ShouldProcess ( this . VMScaleSetName , VerbsCommon . New ) )
132
+ case SimpleParameterSet :
133
+ SimpleParameterSetExecuteCmdlet ( ) ;
134
+ break ;
135
+ default :
136
+ ExecuteClientAction ( ( ) =>
137
+ {
138
+ if ( ShouldProcess ( this . VMScaleSetName , VerbsCommon . New ) )
139
+ {
140
+ string resourceGroupName = this . ResourceGroupName ;
141
+ string vmScaleSetName = this . VMScaleSetName ;
142
+ VirtualMachineScaleSet parameters = new VirtualMachineScaleSet ( ) ;
143
+ ComputeAutomationAutoMapperProfile . Mapper . Map < PSVirtualMachineScaleSet , VirtualMachineScaleSet > ( this . VirtualMachineScaleSet , parameters ) ;
144
+
145
+ var result = VirtualMachineScaleSetsClient . CreateOrUpdate ( resourceGroupName , vmScaleSetName , parameters ) ;
146
+ var psObject = new PSVirtualMachineScaleSet ( ) ;
147
+ ComputeAutomationAutoMapperProfile . Mapper . Map < VirtualMachineScaleSet , PSVirtualMachineScaleSet > ( result , psObject ) ;
148
+ WriteObject ( psObject ) ;
149
+ }
150
+ } ) ;
151
+ break ;
152
+ }
153
+ }
154
+
155
+ public void SimpleParameterSetExecuteCmdlet ( )
156
+ {
157
+ ResourceGroupName = ResourceGroupName ?? VMScaleSetName ;
158
+ InstanceCount = InstanceCount ?? 2 ;
159
+ VmSku = VmSku ?? "Standard_DS2" ;
160
+ UpgradePolicyMode = UpgradePolicyMode ?? UpgradeMode . Automatic ;
161
+
162
+ VirtualNetworkName = VirtualNetworkName ?? VMScaleSetName ;
163
+ SubnetName = SubnetName ?? VMScaleSetName ;
164
+ PublicIpAddressName = PublicIpAddressName ?? VMScaleSetName ;
165
+ DomainNameLabel = DomainNameLabel ?? ( VMScaleSetName + ResourceGroupName ) . ToLower ( ) ;
166
+ SecurityGroupName = SecurityGroupName ?? VMScaleSetName ;
167
+ LoadBalancerName = LoadBalancerName ?? VMScaleSetName ;
168
+
169
+ // get image
170
+ var image = Images
171
+ . Instance
172
+ . Select ( osAndMap =>
173
+ new { OsType = osAndMap . Key , Image = osAndMap . Value . GetOrNull ( ImageName ) } )
174
+ . First ( osAndImage => osAndImage . Image != null ) ;
175
+
176
+ BackendPorts = BackendPorts
177
+ ?? ( image . OsType == "Windows" ? new [ ] { 3389 , 5985 } : new [ ] { 22 } ) ;
178
+
179
+ var resourceGroup = ResourceGroupStrategy . CreateResourceGroupConfig ( ResourceGroupName ) ;
180
+
181
+ var publicIpAddress = resourceGroup . CreatePublicIPAddressConfig (
182
+ name : PublicIpAddressName ,
183
+ domainNameLabel : DomainNameLabel ,
184
+ allocationMethod : AllocationMethod ) ;
185
+
186
+ var loadBalancer = resourceGroup . CreateLoadBalancerConfig (
187
+ name : LoadBalancerName ) ;
188
+
189
+ var virtualNetwork = resourceGroup . CreateVirtualNetworkConfig (
190
+ name : VirtualNetworkName , addressPrefix : VnetAddressPrefix ) ;
191
+
192
+ var subnet = virtualNetwork . CreateSubnet ( SubnetName , SubnetAddressPrefix ) ;
193
+
194
+ /*
195
+ var networkSecurityGroup = resourceGroup.CreateNetworkSecurityGroupConfig(
196
+ name: SecurityGroupName,
197
+ openPorts: OpenPorts);
198
+
199
+ var networkInterface = resourceGroup.CreateNetworkInterfaceConfig(
200
+ Name, subnet, publicIpAddress, networkSecurityGroup);*/
201
+
202
+ var virtualMachineScaleSet = resourceGroup . CreateVirtualMachineScaleSetConfig (
203
+ name : VMScaleSetName ,
204
+ adminUsername : Credential . UserName ,
205
+ adminPassword : new NetworkCredential ( string . Empty , Credential . Password ) . Password ,
206
+ image : image . Image ) ;
207
+
208
+ var client = new Client ( DefaultProfile . DefaultContext ) ;
209
+
210
+ var current = virtualMachineScaleSet
211
+ . GetStateAsync ( client , new CancellationToken ( ) )
212
+ . GetAwaiter ( )
213
+ . GetResult ( ) ;
214
+
215
+ if ( Location == null )
216
+ {
217
+ Location = current . GetLocation ( virtualMachineScaleSet ) ;
218
+ if ( Location == null )
123
219
{
124
- string resourceGroupName = this . ResourceGroupName ;
125
- string vmScaleSetName = this . VMScaleSetName ;
126
- VirtualMachineScaleSet parameters = new VirtualMachineScaleSet ( ) ;
127
- ComputeAutomationAutoMapperProfile . Mapper . Map < PSVirtualMachineScaleSet , VirtualMachineScaleSet > ( this . VirtualMachineScaleSet , parameters ) ;
128
-
129
- var result = VirtualMachineScaleSetsClient . CreateOrUpdate ( resourceGroupName , vmScaleSetName , parameters ) ;
130
- var psObject = new PSVirtualMachineScaleSet ( ) ;
131
- ComputeAutomationAutoMapperProfile . Mapper . Map < VirtualMachineScaleSet , PSVirtualMachineScaleSet > ( result , psObject ) ;
132
- WriteObject ( psObject ) ;
220
+ Location = "eastus" ;
133
221
}
134
- } ) ;
222
+ }
223
+
224
+ var target = virtualMachineScaleSet . GetTargetState ( current , client . SubscriptionId , Location ) ;
225
+
226
+ if ( ShouldProcess ( VMScaleSetName , VerbsCommon . New ) )
227
+ {
228
+ var result = virtualMachineScaleSet
229
+ . UpdateStateAsync (
230
+ client ,
231
+ target ,
232
+ new CancellationToken ( ) ,
233
+ new ShouldProcessType ( this ) ,
234
+ new ProgressReportType ( this ) )
235
+ . GetAwaiter ( )
236
+ . GetResult ( ) ;
237
+ WriteObject ( result ) ;
238
+ }
135
239
}
136
240
137
241
[ Parameter (
@@ -142,6 +246,9 @@ protected override void ProcessRecord()
142
246
ValueFromPipeline = false ) ]
143
247
[ AllowNull ]
144
248
[ ResourceManager . Common . ArgumentCompleters . ResourceGroupCompleter ( ) ]
249
+ [ Parameter (
250
+ ParameterSetName = SimpleParameterSet ,
251
+ Mandatory = false ) ]
145
252
public string ResourceGroupName { get ; set ; }
146
253
147
254
[ Parameter (
@@ -152,6 +259,9 @@ protected override void ProcessRecord()
152
259
ValueFromPipeline = false ) ]
153
260
[ Alias ( "Name" ) ]
154
261
[ AllowNull ]
262
+ [ Parameter (
263
+ ParameterSetName = SimpleParameterSet ,
264
+ Mandatory = true ) ]
155
265
public string VMScaleSetName { get ; set ; }
156
266
157
267
[ Parameter (
@@ -162,5 +272,57 @@ protected override void ProcessRecord()
162
272
ValueFromPipeline = true ) ]
163
273
[ AllowNull ]
164
274
public PSVirtualMachineScaleSet VirtualMachineScaleSet { get ; set ; }
275
+
276
+ // SimpleParameterSet
277
+
278
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = true ) ]
279
+ public string ImageName { get ; set ; } //= "Win2016Datacenter";
280
+
281
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = true ) ]
282
+ public PSCredential Credential { get ; set ; }
283
+
284
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = false ) ]
285
+ public int ? InstanceCount { get ; set ; }
286
+
287
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = false ) ]
288
+ public string VirtualNetworkName { get ; set ; }
289
+
290
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = false ) ]
291
+ public string SubnetName { get ; set ; }
292
+
293
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = false ) ]
294
+ public string PublicIpAddressName { get ; set ; }
295
+
296
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = false ) ]
297
+ public string DomainNameLabel { get ; set ; }
298
+
299
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = false ) ]
300
+ public string SecurityGroupName { get ; set ; }
301
+
302
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = false ) ]
303
+ public string LoadBalancerName { get ; set ; }
304
+
305
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = false ) ]
306
+ public int [ ] BackendPorts { get ; set ; }
307
+
308
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = false ) ]
309
+ [ LocationCompleter ]
310
+ public string Location { get ; set ; }
311
+
312
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = false ) ]
313
+ public string VmSku { get ; set ; }
314
+
315
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = false ) ]
316
+ public UpgradeMode ? UpgradePolicyMode { get ; set ; }
317
+
318
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = false ) ]
319
+ [ ValidateSet ( "Static" , "Dynamic" ) ]
320
+ public string AllocationMethod { get ; set ; } = "Static" ;
321
+
322
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = false ) ]
323
+ public string VnetAddressPrefix { get ; set ; } = "192.168.0.0/16" ;
324
+
325
+ [ Parameter ( ParameterSetName = SimpleParameterSet , Mandatory = false ) ]
326
+ public string SubnetAddressPrefix { get ; set ; } = "192.168.1.0/24" ;
165
327
}
166
328
}
0 commit comments