Skip to content

Commit db0f83e

Browse files
author
Hovsep Mkrtchyan
committed
VMSS simple cmdlets
1 parent dba029c commit db0f83e

File tree

8 files changed

+343
-14
lines changed

8 files changed

+343
-14
lines changed

src/ResourceManager/Common/Commands.Common.Strategies/Commands.Common.Strategies.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@
6969
<ItemGroup>
7070
<Compile Include="Compute\Image.cs" />
7171
<Compile Include="Compute\Images.cs" />
72+
<Compile Include="Compute\VirtualMachineScaleSetStrategy.cs" />
7273
<Compile Include="IReportProgress.cs" />
7374
<Compile Include="IResourceConfig.cs" />
7475
<Compile Include="IResourceConfigVisitor.cs" />
7576
<Compile Include="IShouldProcess.cs" />
77+
<Compile Include="Network\LoadBalancerStrategy.cs" />
7678
<Compile Include="StateOperationContext.cs" />
7779
<Compile Include="Compute\ComputeStrategy.cs" />
7880
<Compile Include="Compute\VirtualMachineStrategy.cs" />
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Management.Compute;
16+
using Microsoft.Azure.Management.Compute.Models;
17+
using Microsoft.Azure.Management.Network.Models;
18+
using Microsoft.Azure.Management.ResourceManager.Models;
19+
20+
namespace Microsoft.Azure.Commands.Common.Strategies.Compute
21+
{
22+
public static class VirtualMachineScaleSetStrategy
23+
{
24+
public static ResourceStrategy<VirtualMachineScaleSet> Strategy { get; }
25+
= ComputePolicy.Create(
26+
"virtual machine scale set",
27+
"virtualMachines",
28+
client => client.VirtualMachineScaleSets,
29+
(o, p) => o.GetAsync(
30+
p.ResourceGroupName, p.Name, p.CancellationToken),
31+
(o, p) => o.CreateOrUpdateAsync(
32+
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));
33+
34+
public static ResourceConfig<VirtualMachineScaleSet> CreateVirtualMachineScaleSetConfig(
35+
this ResourceConfig<ResourceGroup> resourceGroup,
36+
string name,
37+
string adminUsername,
38+
string adminPassword,
39+
Image image)
40+
=> Strategy.CreateConfig(
41+
resourceGroup,
42+
name,
43+
subscription => new VirtualMachineScaleSet
44+
{
45+
46+
});
47+
}
48+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using Microsoft.Azure.Management.Network;
2+
using Microsoft.Azure.Management.Network.Models;
3+
using Microsoft.Azure.Management.ResourceManager.Models;
4+
using System.Linq;
5+
6+
namespace Microsoft.Azure.Commands.Common.Strategies.Network
7+
{
8+
public static class LoadBalancerStrategy
9+
{
10+
public static ResourceStrategy<LoadBalancer> Strategy { get; }
11+
= NetworkStrategy.Create(
12+
"load balancer",
13+
"loadBalancers",
14+
client => client.LoadBalancers,
15+
(o, p) => o.GetAsync(
16+
p.ResourceGroupName, p.Name, null, p.CancellationToken),
17+
(o, p) => o.CreateOrUpdateAsync(
18+
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));
19+
20+
public static ResourceConfig<LoadBalancer> CreateLoadBalancerConfig(
21+
this ResourceConfig<ResourceGroup> resourceGroup, string name)
22+
=> Strategy.CreateConfig(
23+
resourceGroup,
24+
name,
25+
_ => new LoadBalancer());
26+
}
27+
28+
/*
29+
public static class NetworkSecurityGroupStrategy
30+
{
31+
public static ResourceStrategy<NetworkSecurityGroup> Strategy { get; }
32+
= NetworkStrategy.Create(
33+
"network security group",
34+
"networkSecurityGroups",
35+
client => client.NetworkSecurityGroups,
36+
(o, p) => o.GetAsync(
37+
p.ResourceGroupName, p.Name, null, p.CancellationToken),
38+
(o, p) => o.CreateOrUpdateAsync(
39+
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));
40+
41+
public static ResourceConfig<NetworkSecurityGroup> CreateNetworkSecurityGroupConfig(
42+
this ResourceConfig<ResourceGroup> resourceGroup, string name, int[] openPorts)
43+
=> Strategy.CreateConfig(
44+
resourceGroup,
45+
name,
46+
_ => new NetworkSecurityGroup
47+
{
48+
SecurityRules = openPorts
49+
.Select((port, index) => new SecurityRule
50+
{
51+
Name = name + port,
52+
Protocol = "Tcp",
53+
Priority = index + 1000,
54+
Access = "Allow",
55+
Direction = "Inbound",
56+
SourcePortRange = "*",
57+
SourceAddressPrefix = "*",
58+
DestinationPortRange = port.ToString(),
59+
DestinationAddressPrefix = "*"
60+
})
61+
.ToList()
62+
});
63+
}
64+
*/
65+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
<Compile Include="Common\ComputeCloudException.cs" />
154154
<Compile Include="Common\DiagnosticsHelper.cs" />
155155
<Compile Include="Common\StorageManagementClient.cs" />
156+
<Compile Include="Common\StrategyClient.cs" />
156157
<Compile Include="ExtensionImages\GetAzureVMExtensionImageTypeCommand.cs" />
157158
<Compile Include="ExtensionImages\GetAzureVMExtensionImageCommand.cs" />
158159
<Compile Include="ExtensionImages\VirtualMachineExtensionImageBaseCmdlet.cs" />

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

Lines changed: 174 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
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.Compute;
2223
using Microsoft.Azure.Commands.Compute.Automation.Models;
2324
using Microsoft.Azure.Management.Compute;
2425
using Microsoft.Azure.Management.Compute.Models;
@@ -27,6 +28,13 @@
2728
using System.Collections.Generic;
2829
using System.Linq;
2930
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;
3038

3139
namespace Microsoft.Azure.Commands.Compute.Automation
3240
{
@@ -115,23 +123,119 @@ protected PSArgument[] CreateVirtualMachineScaleSetCreateOrUpdateParameters()
115123
[OutputType(typeof(PSVirtualMachineScaleSet))]
116124
public partial class NewAzureRmVmss : ComputeAutomationBaseCmdlet
117125
{
126+
public const string SimpleParameterSet = "SimpleParameterSet";
127+
118128
protected override void ProcessRecord()
119129
{
120-
ExecuteClientAction(() =>
130+
switch (ParameterSetName)
121131
{
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)
123219
{
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";
133221
}
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+
}
135239
}
136240

137241
[Parameter(
@@ -142,6 +246,9 @@ protected override void ProcessRecord()
142246
ValueFromPipeline = false)]
143247
[AllowNull]
144248
[ResourceManager.Common.ArgumentCompleters.ResourceGroupCompleter()]
249+
[Parameter(
250+
ParameterSetName = SimpleParameterSet,
251+
Mandatory = false)]
145252
public string ResourceGroupName { get; set; }
146253

147254
[Parameter(
@@ -152,6 +259,9 @@ protected override void ProcessRecord()
152259
ValueFromPipeline = false)]
153260
[Alias("Name")]
154261
[AllowNull]
262+
[Parameter(
263+
ParameterSetName = SimpleParameterSet,
264+
Mandatory = true)]
155265
public string VMScaleSetName { get; set; }
156266

157267
[Parameter(
@@ -162,5 +272,57 @@ protected override void ProcessRecord()
162272
ValueFromPipeline = true)]
163273
[AllowNull]
164274
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";
165327
}
166328
}

src/ResourceManager/Network/Commands.Network/Commands.Network.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,10 @@
554554
<Project>{5ee72c53-1720-4309-b54b-5fb79703195f}</Project>
555555
<Name>Commands.Common</Name>
556556
</ProjectReference>
557+
<ProjectReference Include="..\..\Common\Commands.Common.Strategies\Commands.Common.Strategies.csproj">
558+
<Project>{EEA69772-D41B-482A-9252-2B4595C59E53}</Project>
559+
<Name>Commands.Common.Strategies</Name>
560+
</ProjectReference>
557561
<ProjectReference Include="..\..\Common\Commands.ResourceManager.Common\Commands.ResourceManager.Common.csproj">
558562
<Project>{3819d8a7-c62c-4c47-8ddd-0332d9ce1252}</Project>
559563
<Name>Commands.ResourceManager.Common</Name>

0 commit comments

Comments
 (0)