Skip to content

Commit

Permalink
VMSS simple cmdlets
Browse files Browse the repository at this point in the history
  • Loading branch information
Hovsep Mkrtchyan committed Nov 30, 2017
1 parent dba029c commit db0f83e
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@
<ItemGroup>
<Compile Include="Compute\Image.cs" />
<Compile Include="Compute\Images.cs" />
<Compile Include="Compute\VirtualMachineScaleSetStrategy.cs" />
<Compile Include="IReportProgress.cs" />
<Compile Include="IResourceConfig.cs" />
<Compile Include="IResourceConfigVisitor.cs" />
<Compile Include="IShouldProcess.cs" />
<Compile Include="Network\LoadBalancerStrategy.cs" />
<Compile Include="StateOperationContext.cs" />
<Compile Include="Compute\ComputeStrategy.cs" />
<Compile Include="Compute\VirtualMachineStrategy.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.Azure.Management.Network.Models;
using Microsoft.Azure.Management.ResourceManager.Models;

namespace Microsoft.Azure.Commands.Common.Strategies.Compute
{
public static class VirtualMachineScaleSetStrategy
{
public static ResourceStrategy<VirtualMachineScaleSet> Strategy { get; }
= ComputePolicy.Create(
"virtual machine scale set",
"virtualMachines",
client => client.VirtualMachineScaleSets,
(o, p) => o.GetAsync(
p.ResourceGroupName, p.Name, p.CancellationToken),
(o, p) => o.CreateOrUpdateAsync(
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));

public static ResourceConfig<VirtualMachineScaleSet> CreateVirtualMachineScaleSetConfig(
this ResourceConfig<ResourceGroup> resourceGroup,
string name,
string adminUsername,
string adminPassword,
Image image)
=> Strategy.CreateConfig(
resourceGroup,
name,
subscription => new VirtualMachineScaleSet
{
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using Microsoft.Azure.Management.ResourceManager.Models;
using System.Linq;

namespace Microsoft.Azure.Commands.Common.Strategies.Network
{
public static class LoadBalancerStrategy
{
public static ResourceStrategy<LoadBalancer> Strategy { get; }
= NetworkStrategy.Create(
"load balancer",
"loadBalancers",
client => client.LoadBalancers,
(o, p) => o.GetAsync(
p.ResourceGroupName, p.Name, null, p.CancellationToken),
(o, p) => o.CreateOrUpdateAsync(
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));

public static ResourceConfig<LoadBalancer> CreateLoadBalancerConfig(
this ResourceConfig<ResourceGroup> resourceGroup, string name)
=> Strategy.CreateConfig(
resourceGroup,
name,
_ => new LoadBalancer());
}

/*
public static class NetworkSecurityGroupStrategy
{
public static ResourceStrategy<NetworkSecurityGroup> Strategy { get; }
= NetworkStrategy.Create(
"network security group",
"networkSecurityGroups",
client => client.NetworkSecurityGroups,
(o, p) => o.GetAsync(
p.ResourceGroupName, p.Name, null, p.CancellationToken),
(o, p) => o.CreateOrUpdateAsync(
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));
public static ResourceConfig<NetworkSecurityGroup> CreateNetworkSecurityGroupConfig(
this ResourceConfig<ResourceGroup> resourceGroup, string name, int[] openPorts)
=> Strategy.CreateConfig(
resourceGroup,
name,
_ => new NetworkSecurityGroup
{
SecurityRules = openPorts
.Select((port, index) => new SecurityRule
{
Name = name + port,
Protocol = "Tcp",
Priority = index + 1000,
Access = "Allow",
Direction = "Inbound",
SourcePortRange = "*",
SourceAddressPrefix = "*",
DestinationPortRange = port.ToString(),
DestinationAddressPrefix = "*"
})
.ToList()
});
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
<Compile Include="Common\ComputeCloudException.cs" />
<Compile Include="Common\DiagnosticsHelper.cs" />
<Compile Include="Common\StorageManagementClient.cs" />
<Compile Include="Common\StrategyClient.cs" />
<Compile Include="ExtensionImages\GetAzureVMExtensionImageTypeCommand.cs" />
<Compile Include="ExtensionImages\GetAzureVMExtensionImageCommand.cs" />
<Compile Include="ExtensionImages\VirtualMachineExtensionImageBaseCmdlet.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// Changes to this file may cause incorrect behavior and will be lost if the
// code is regenerated.

using Microsoft.Azure.Commands.Common.Strategies.Compute;
using Microsoft.Azure.Commands.Compute.Automation.Models;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
Expand All @@ -27,6 +28,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.Common.Strategies;
using Microsoft.Azure.Commands.Common.Strategies.ResourceManager;
using System.Threading;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Commands.Common.Strategies.Network;
using Microsoft.Azure.Commands.Compute.Common;
using System.Net;

namespace Microsoft.Azure.Commands.Compute.Automation
{
Expand Down Expand Up @@ -115,23 +123,119 @@ protected PSArgument[] CreateVirtualMachineScaleSetCreateOrUpdateParameters()
[OutputType(typeof(PSVirtualMachineScaleSet))]
public partial class NewAzureRmVmss : ComputeAutomationBaseCmdlet
{
public const string SimpleParameterSet = "SimpleParameterSet";

protected override void ProcessRecord()
{
ExecuteClientAction(() =>
switch (ParameterSetName)
{
if (ShouldProcess(this.VMScaleSetName, VerbsCommon.New))
case SimpleParameterSet:
SimpleParameterSetExecuteCmdlet();
break;
default:
ExecuteClientAction(() =>
{
if (ShouldProcess(this.VMScaleSetName, VerbsCommon.New))
{
string resourceGroupName = this.ResourceGroupName;
string vmScaleSetName = this.VMScaleSetName;
VirtualMachineScaleSet parameters = new VirtualMachineScaleSet();
ComputeAutomationAutoMapperProfile.Mapper.Map<PSVirtualMachineScaleSet, VirtualMachineScaleSet>(this.VirtualMachineScaleSet, parameters);
var result = VirtualMachineScaleSetsClient.CreateOrUpdate(resourceGroupName, vmScaleSetName, parameters);
var psObject = new PSVirtualMachineScaleSet();
ComputeAutomationAutoMapperProfile.Mapper.Map<VirtualMachineScaleSet, PSVirtualMachineScaleSet>(result, psObject);
WriteObject(psObject);
}
});
break;
}
}

public void SimpleParameterSetExecuteCmdlet()
{
ResourceGroupName = ResourceGroupName ?? VMScaleSetName;
InstanceCount = InstanceCount ?? 2;
VmSku = VmSku ?? "Standard_DS2";
UpgradePolicyMode = UpgradePolicyMode ?? UpgradeMode.Automatic;

VirtualNetworkName = VirtualNetworkName ?? VMScaleSetName;
SubnetName = SubnetName ?? VMScaleSetName;
PublicIpAddressName = PublicIpAddressName ?? VMScaleSetName;
DomainNameLabel = DomainNameLabel ?? (VMScaleSetName + ResourceGroupName).ToLower();
SecurityGroupName = SecurityGroupName ?? VMScaleSetName;
LoadBalancerName = LoadBalancerName ?? VMScaleSetName;

// get image
var image = Images
.Instance
.Select(osAndMap =>
new { OsType = osAndMap.Key, Image = osAndMap.Value.GetOrNull(ImageName) })
.First(osAndImage => osAndImage.Image != null);

BackendPorts = BackendPorts
?? (image.OsType == "Windows" ? new[] { 3389, 5985 } : new[] { 22 });

var resourceGroup = ResourceGroupStrategy.CreateResourceGroupConfig(ResourceGroupName);

var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig(
name: PublicIpAddressName,
domainNameLabel: DomainNameLabel,
allocationMethod: AllocationMethod);

var loadBalancer = resourceGroup.CreateLoadBalancerConfig(
name: LoadBalancerName);

var virtualNetwork = resourceGroup.CreateVirtualNetworkConfig(
name: VirtualNetworkName, addressPrefix: VnetAddressPrefix);

var subnet = virtualNetwork.CreateSubnet(SubnetName, SubnetAddressPrefix);

/*
var networkSecurityGroup = resourceGroup.CreateNetworkSecurityGroupConfig(
name: SecurityGroupName,
openPorts: OpenPorts);
var networkInterface = resourceGroup.CreateNetworkInterfaceConfig(
Name, subnet, publicIpAddress, networkSecurityGroup);*/

var virtualMachineScaleSet = resourceGroup.CreateVirtualMachineScaleSetConfig(
name: VMScaleSetName,
adminUsername: Credential.UserName,
adminPassword: new NetworkCredential(string.Empty, Credential.Password).Password,
image: image.Image);

var client = new Client(DefaultProfile.DefaultContext);

var current = virtualMachineScaleSet
.GetStateAsync(client, new CancellationToken())
.GetAwaiter()
.GetResult();

if (Location == null)
{
Location = current.GetLocation(virtualMachineScaleSet);
if (Location == null)
{
string resourceGroupName = this.ResourceGroupName;
string vmScaleSetName = this.VMScaleSetName;
VirtualMachineScaleSet parameters = new VirtualMachineScaleSet();
ComputeAutomationAutoMapperProfile.Mapper.Map<PSVirtualMachineScaleSet, VirtualMachineScaleSet>(this.VirtualMachineScaleSet, parameters);
var result = VirtualMachineScaleSetsClient.CreateOrUpdate(resourceGroupName, vmScaleSetName, parameters);
var psObject = new PSVirtualMachineScaleSet();
ComputeAutomationAutoMapperProfile.Mapper.Map<VirtualMachineScaleSet, PSVirtualMachineScaleSet>(result, psObject);
WriteObject(psObject);
Location = "eastus";
}
});
}

var target = virtualMachineScaleSet.GetTargetState(current, client.SubscriptionId, Location);

if (ShouldProcess(VMScaleSetName, VerbsCommon.New))
{
var result = virtualMachineScaleSet
.UpdateStateAsync(
client,
target,
new CancellationToken(),
new ShouldProcessType(this),
new ProgressReportType(this))
.GetAwaiter()
.GetResult();
WriteObject(result);
}
}

[Parameter(
Expand All @@ -142,6 +246,9 @@ protected override void ProcessRecord()
ValueFromPipeline = false)]
[AllowNull]
[ResourceManager.Common.ArgumentCompleters.ResourceGroupCompleter()]
[Parameter(
ParameterSetName = SimpleParameterSet,
Mandatory = false)]
public string ResourceGroupName { get; set; }

[Parameter(
Expand All @@ -152,6 +259,9 @@ protected override void ProcessRecord()
ValueFromPipeline = false)]
[Alias("Name")]
[AllowNull]
[Parameter(
ParameterSetName = SimpleParameterSet,
Mandatory = true)]
public string VMScaleSetName { get; set; }

[Parameter(
Expand All @@ -162,5 +272,57 @@ protected override void ProcessRecord()
ValueFromPipeline = true)]
[AllowNull]
public PSVirtualMachineScaleSet VirtualMachineScaleSet { get; set; }

// SimpleParameterSet

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = true)]
public string ImageName { get; set; } //= "Win2016Datacenter";

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = true)]
public PSCredential Credential { get; set; }

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
public int? InstanceCount { get; set; }

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
public string VirtualNetworkName { get; set; }

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
public string SubnetName { get; set; }

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
public string PublicIpAddressName { get; set; }

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
public string DomainNameLabel { get; set; }

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
public string SecurityGroupName { get; set; }

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
public string LoadBalancerName { get; set; }

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
public int[] BackendPorts { get; set; }

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
[LocationCompleter]
public string Location { get; set; }

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
public string VmSku { get; set; }

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
public UpgradeMode? UpgradePolicyMode { get; set; }

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
[ValidateSet("Static", "Dynamic")]
public string AllocationMethod { get; set; } = "Static";

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
public string VnetAddressPrefix { get; set; } = "192.168.0.0/16";

[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
public string SubnetAddressPrefix { get; set; } = "192.168.1.0/24";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,10 @@
<Project>{5ee72c53-1720-4309-b54b-5fb79703195f}</Project>
<Name>Commands.Common</Name>
</ProjectReference>
<ProjectReference Include="..\..\Common\Commands.Common.Strategies\Commands.Common.Strategies.csproj">
<Project>{EEA69772-D41B-482A-9252-2B4595C59E53}</Project>
<Name>Commands.Common.Strategies</Name>
</ProjectReference>
<ProjectReference Include="..\..\Common\Commands.ResourceManager.Common\Commands.ResourceManager.Common.csproj">
<Project>{3819d8a7-c62c-4c47-8ddd-0332d9ce1252}</Project>
<Name>Commands.ResourceManager.Common</Name>
Expand Down
Loading

0 comments on commit db0f83e

Please sign in to comment.