Skip to content

Commit 1336843

Browse files
author
Hovsep Mkrtchyan
committed
Added VMSS Simple cmdlet
1 parent 2ea8fa2 commit 1336843

File tree

5 files changed

+967
-16
lines changed

5 files changed

+967
-16
lines changed

src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,10 @@
275275
<Compile Include="Strategies\Compute\ComputeStrategy.cs" />
276276
<Compile Include="Strategies\Compute\Image.cs" />
277277
<Compile Include="Strategies\Compute\Images.cs" />
278+
<Compile Include="Strategies\Compute\VirtualMachineScaleSetStrategy.cs" />
278279
<Compile Include="Strategies\Compute\VirtualMachineStrategy.cs" />
279280
<Compile Include="Strategies\IAsyncCmdlet.cs" />
281+
<Compile Include="Strategies\Network\LoadBalancerStrategy.cs" />
280282
<Compile Include="Strategies\Network\NetworkInterfaceStrategy.cs" />
281283
<Compile Include="Strategies\Network\NetworkSecurityGroupPolicy.cs" />
282284
<Compile Include="Strategies\Network\NetworkStrategy.cs" />

src/ResourceManager/Compute/Commands.Compute/Generated/VirtualMachineScaleSet/VirtualMachineScaleSetCreateOrUpdateMethod.cs

Lines changed: 210 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919
// Changes to this file may cause incorrect behavior and will be lost if the
2020
// code is regenerated.
2121

22+
using Microsoft.Azure.Commands.Common.Strategies;
23+
using Microsoft.Azure.Commands.Common.Strategies.Compute;
24+
using Microsoft.Azure.Commands.Common.Strategies.Network;
25+
using Microsoft.Azure.Commands.Common.Strategies.ResourceManager;
2226
using Microsoft.Azure.Commands.Compute.Automation.Models;
27+
using Microsoft.Azure.Commands.Compute.Strategies;
28+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2329
using Microsoft.Azure.Management.Compute;
2430
using Microsoft.Azure.Management.Compute.Models;
2531
using Microsoft.WindowsAzure.Commands.Utilities.Common;
@@ -28,6 +34,9 @@
2834
using System.Collections.Generic;
2935
using System.Linq;
3036
using System.Management.Automation;
37+
using System.Net;
38+
using System.Threading;
39+
using System.Threading.Tasks;
3140

3241
namespace Microsoft.Azure.Commands.Compute.Automation
3342
{
@@ -116,23 +125,128 @@ protected PSArgument[] CreateVirtualMachineScaleSetCreateOrUpdateParameters()
116125
[OutputType(typeof(PSVirtualMachineScaleSet))]
117126
public partial class NewAzureRmVmss : ComputeAutomationBaseCmdlet
118127
{
128+
public const string SimpleParameterSet = "SimpleParameterSet";
129+
119130
public override void ExecuteCmdlet()
120131
{
121-
ExecuteClientAction(() =>
132+
switch (ParameterSetName)
122133
{
123-
if (ShouldProcess(this.VMScaleSetName, VerbsCommon.New))
134+
case SimpleParameterSet:
135+
this.StartAndWait(SimpleParameterSetExecuteCmdlet);
136+
break;
137+
default:
138+
ExecuteClientAction(() =>
139+
{
140+
if (ShouldProcess(this.VMScaleSetName, VerbsCommon.New))
141+
{
142+
string resourceGroupName = this.ResourceGroupName;
143+
string vmScaleSetName = this.VMScaleSetName;
144+
VirtualMachineScaleSet parameters = new VirtualMachineScaleSet();
145+
ComputeAutomationAutoMapperProfile.Mapper.Map<PSVirtualMachineScaleSet, VirtualMachineScaleSet>(this.VirtualMachineScaleSet, parameters);
146+
147+
var result = VirtualMachineScaleSetsClient.CreateOrUpdate(resourceGroupName, vmScaleSetName, parameters);
148+
var psObject = new PSVirtualMachineScaleSet();
149+
ComputeAutomationAutoMapperProfile.Mapper.Map<VirtualMachineScaleSet, PSVirtualMachineScaleSet>(result, psObject);
150+
WriteObject(psObject);
151+
}
152+
});
153+
break;
154+
}
155+
}
156+
157+
async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
158+
{
159+
ResourceGroupName = ResourceGroupName ?? VMScaleSetName;
160+
VirtualNetworkName = VirtualNetworkName ?? VMScaleSetName;
161+
SubnetName = SubnetName ?? VMScaleSetName;
162+
PublicIpAddressName = PublicIpAddressName ?? VMScaleSetName;
163+
DomainNameLabel = DomainNameLabel ?? (VMScaleSetName + ResourceGroupName).ToLower();
164+
SecurityGroupName = SecurityGroupName ?? VMScaleSetName;
165+
LoadBalancerName = LoadBalancerName ?? VMScaleSetName;
166+
FrontendPoolName = FrontendPoolName ?? VMScaleSetName;
167+
BackendPoolName = BackendPoolName ?? 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+
var isWindows = image.OsType == "Windows";
177+
BackendPort = BackendPort
178+
?? (image.OsType == "Windows" ? new[] { 3389, 5985 } : new[] { 22 });
179+
180+
var resourceGroup = ResourceGroupStrategy.CreateResourceGroupConfig(ResourceGroupName);
181+
182+
var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig(
183+
name: PublicIpAddressName,
184+
domainNameLabel: DomainNameLabel,
185+
allocationMethod: AllocationMethod);
186+
187+
var virtualNetwork = resourceGroup.CreateVirtualNetworkConfig(
188+
name: VirtualNetworkName, addressPrefix: VnetAddressPrefix);
189+
190+
var subnet = virtualNetwork.CreateSubnet(SubnetName, SubnetAddressPrefix);
191+
192+
var loadBalancer = resourceGroup.CreateLoadBalancerConfig(
193+
name: LoadBalancerName,
194+
froontendPoolName: FrontendPoolName,
195+
backendPoolName: BackendPoolName,
196+
zones: Zone,
197+
publicIPAddress: publicIpAddress,
198+
subnet: subnet);
199+
200+
var virtualMachineScaleSet = resourceGroup.CreateVirtualMachineScaleSetConfig(
201+
name: VMScaleSetName,
202+
subnet: subnet,
203+
loadBalancer: loadBalancer,
204+
isWindows: isWindows,
205+
adminUsername: Credential.UserName,
206+
adminPassword: new NetworkCredential(string.Empty, Credential.Password).Password,
207+
image: image.Image,
208+
vmSize: VmSize,
209+
instanceCount: InstanceCount,
210+
upgradeMode: (MyInvocation.BoundParameters.ContainsKey("UpgradePolicyMode") == true ) ? UpgradePolicyMode : (UpgradeMode?) null);
211+
212+
var client = new Client(DefaultProfile.DefaultContext);
213+
214+
var current = virtualMachineScaleSet
215+
.GetStateAsync(client, new CancellationToken())
216+
.GetAwaiter()
217+
.GetResult();
218+
219+
if (Location == null)
220+
{
221+
Location = current.GetLocation(virtualMachineScaleSet);
222+
if (Location == null)
124223
{
125-
string resourceGroupName = this.ResourceGroupName;
126-
string vmScaleSetName = this.VMScaleSetName;
127-
VirtualMachineScaleSet parameters = new VirtualMachineScaleSet();
128-
ComputeAutomationAutoMapperProfile.Mapper.Map<PSVirtualMachineScaleSet, VirtualMachineScaleSet>(this.VirtualMachineScaleSet, parameters);
129-
130-
var result = VirtualMachineScaleSetsClient.CreateOrUpdate(resourceGroupName, vmScaleSetName, parameters);
131-
var psObject = new PSVirtualMachineScaleSet();
132-
ComputeAutomationAutoMapperProfile.Mapper.Map<VirtualMachineScaleSet, PSVirtualMachineScaleSet>(result, psObject);
133-
WriteObject(psObject);
224+
Location = "eastus";
134225
}
135-
});
226+
}
227+
228+
var target = virtualMachineScaleSet.GetTargetState(current, client.SubscriptionId, Location);
229+
230+
var newState = await virtualMachineScaleSet
231+
.UpdateStateAsync(
232+
client,
233+
target,
234+
new CancellationToken(),
235+
new ShouldProcess(asyncCmdlet),
236+
new ProgressReport(asyncCmdlet));
237+
238+
var result = newState.Get(virtualMachineScaleSet);
239+
if(result == null)
240+
{
241+
result = current.Get(virtualMachineScaleSet);
242+
}
243+
244+
if (result != null)
245+
{
246+
var psObject = new PSVirtualMachineScaleSet();
247+
ComputeAutomationAutoMapperProfile.Mapper.Map<VirtualMachineScaleSet, PSVirtualMachineScaleSet>(result, psObject);
248+
asyncCmdlet.WriteObject(psObject);
249+
}
136250
}
137251

138252
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
@@ -146,6 +260,9 @@ public override void ExecuteCmdlet()
146260
ValueFromPipeline = false)]
147261
[AllowNull]
148262
[ResourceManager.Common.ArgumentCompleters.ResourceGroupCompleter()]
263+
[Parameter(
264+
ParameterSetName = SimpleParameterSet,
265+
Mandatory = false)]
149266
public string ResourceGroupName { get; set; }
150267

151268
[Parameter(
@@ -156,6 +273,9 @@ public override void ExecuteCmdlet()
156273
ValueFromPipeline = false)]
157274
[Alias("Name")]
158275
[AllowNull]
276+
[Parameter(
277+
ParameterSetName = SimpleParameterSet,
278+
Mandatory = true)]
159279
public string VMScaleSetName { get; set; }
160280

161281
[Parameter(
@@ -166,5 +286,83 @@ public override void ExecuteCmdlet()
166286
ValueFromPipeline = true)]
167287
[AllowNull]
168288
public PSVirtualMachineScaleSet VirtualMachineScaleSet { get; set; }
289+
290+
// SimpleParameterSet
291+
292+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
293+
[PSArgumentCompleter(
294+
"CentOS",
295+
"CoreOS",
296+
"Debian",
297+
"openSUSE-Leap",
298+
"RHEL",
299+
"SLES",
300+
"UbuntuLTS",
301+
"Win2016Datacenter",
302+
"Win2012R2Datacenter",
303+
"Win2012Datacenter",
304+
"Win2008R2SP1")]
305+
public string ImageName { get; set; } = "Win2016Datacenter";
306+
307+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = true)]
308+
public PSCredential Credential { get; set; }
309+
310+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
311+
public int InstanceCount { get; set; } = 2;
312+
313+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
314+
public string VirtualNetworkName { get; set; }
315+
316+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
317+
public string SubnetName { get; set; }
318+
319+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
320+
public string PublicIpAddressName { get; set; }
321+
322+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
323+
public string DomainNameLabel { get; set; }
324+
325+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
326+
public string SecurityGroupName { get; set; }
327+
328+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
329+
public string LoadBalancerName { get; set; }
330+
331+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
332+
public int[] BackendPort { get; set; }
333+
334+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
335+
[LocationCompleter("Microsoft.Compute/virtualMachines")]
336+
public string Location { get; set; }
337+
338+
// this corresponds to VmSku in the Azure CLI
339+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
340+
public string VmSize { get; set; } = "Standard_DS1_v2";
341+
342+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
343+
public UpgradeMode UpgradePolicyMode { get; set; }
344+
345+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
346+
[ValidateSet("Static", "Dynamic")]
347+
public string AllocationMethod { get; set; } = "Static";
348+
349+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
350+
public string VnetAddressPrefix { get; set; } = "192.168.0.0/16";
351+
352+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
353+
public string SubnetAddressPrefix { get; set; } = "192.168.1.0/24";
354+
355+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
356+
public string FrontendPoolName { get; set; }
357+
358+
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
359+
public string BackendPoolName { get; set; }
360+
361+
[Parameter(
362+
ParameterSetName = SimpleParameterSet,
363+
Mandatory = false,
364+
HelpMessage = "A list of availability zones denoting the IP allocated for the resource needs to come from.",
365+
ValueFromPipelineByPropertyName = true)]
366+
public List<string> Zone { get; set; }
169367
}
170368
}

0 commit comments

Comments
 (0)