Skip to content

New-AzureRmVm(Vmss): simple parameter set: Zones #5791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ResourceManager/Compute/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Current Release
* `New-AzureRmVm` and `New-AzureRmVmss` (simple parameter set) support availability zones.
* Updated to the latest version of the Azure ClientRuntime

## Version 4.5.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System;

namespace Microsoft.Azure.Commands.Compute.Automation
{
Expand Down Expand Up @@ -153,10 +153,16 @@ public async Task<ResourceConfig<VirtualMachineScaleSet>> CreateConfigAsync()

var resourceGroup = ResourceGroupStrategy.CreateResourceGroupConfig(_cmdlet.ResourceGroupName);

var noZones = _cmdlet.Zone == null || _cmdlet.Zone.Count == 0;

var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig(
name: _cmdlet.PublicIpAddressName,
domainNameLabel: _cmdlet.DomainNameLabel,
allocationMethod: _cmdlet.AllocationMethod);
allocationMethod: _cmdlet.AllocationMethod,
sku: noZones
? PublicIPAddressStrategy.Sku.Basic
: PublicIPAddressStrategy.Sku.Standard,
zones: null);

var virtualNetwork = resourceGroup.CreateVirtualNetworkConfig(
name: _cmdlet.VirtualNetworkName,
Expand All @@ -166,11 +172,13 @@ public async Task<ResourceConfig<VirtualMachineScaleSet>> CreateConfigAsync()
_cmdlet.SubnetName, _cmdlet.SubnetAddressPrefix);

var loadBalancer = resourceGroup.CreateLoadBalancerConfig(
name: _cmdlet.LoadBalancerName);
name: _cmdlet.LoadBalancerName,
sku: noZones
? LoadBalancerStrategy.Sku.Basic
: LoadBalancerStrategy.Sku.Standard);

var frontendIpConfiguration = loadBalancer.CreateFrontendIPConfiguration(
name: _cmdlet.FrontendPoolName,
zones: _cmdlet.Zone,
publicIpAddress: publicIpAddress);

var backendAddressPool = loadBalancer.CreateBackendAddressPool(
Expand All @@ -195,25 +203,34 @@ public async Task<ResourceConfig<VirtualMachineScaleSet>> CreateConfigAsync()
var inboundNatPoolName = _cmdlet.VMScaleSetName;
var PortRangeSize = _cmdlet.InstanceCount * 2;

var inboundNatPools = _cmdlet.NatBackendPort
?.Select((port, i) =>
{
var portRangeStart = FirstPortRangeStart + i * 2000;
return loadBalancer.CreateInboundNatPool(
name: inboundNatPoolName + port.ToString(),
frontendIpConfiguration: frontendIpConfiguration,
frontendPortRangeStart: portRangeStart,
frontendPortRangeEnd: portRangeStart + PortRangeSize,
backendPort: port);
})
var ports = _cmdlet
.NatBackendPort
?.Select((port, i) => Tuple.Create(
port,
FirstPortRangeStart + i * 2000))
.ToList();

var inboundNatPools = ports
?.Select(p => loadBalancer.CreateInboundNatPool(
name: inboundNatPoolName + p.Item1.ToString(),
frontendIpConfiguration: frontendIpConfiguration,
frontendPortRangeStart: p.Item2,
frontendPortRangeEnd: p.Item2 + PortRangeSize,
backendPort: p.Item1))
.ToList();

var networkSecurityGroup = noZones
? null
: resourceGroup.CreateNetworkSecurityGroupConfig(
_cmdlet.VMScaleSetName,
_cmdlet.NatBackendPort.Concat(_cmdlet.BackendPort).ToList());

return resourceGroup.CreateVirtualMachineScaleSetConfig(
name: _cmdlet.VMScaleSetName,
subnet: subnet,
frontendIpConfigurations: new[] { frontendIpConfiguration },
subnet: subnet,
backendAdressPool: backendAddressPool,
inboundNatPools: inboundNatPools,
networkSecurityGroup: networkSecurityGroup,
imageAndOsType: ImageAndOsType,
adminUsername: _cmdlet.Credential.UserName,
adminPassword: new NetworkCredential(string.Empty, _cmdlet.Credential.Password).Password,
Expand All @@ -222,7 +239,8 @@ public async Task<ResourceConfig<VirtualMachineScaleSet>> CreateConfigAsync()
upgradeMode: _cmdlet.MyInvocation.BoundParameters.ContainsKey(nameof(UpgradePolicyMode))
? _cmdlet.UpgradePolicyMode
: (UpgradeMode?)null,
dataDisks: _cmdlet.DataDiskSizeInGb);
dataDisks: _cmdlet.DataDiskSizeInGb,
zones: _cmdlet.Zone);
}
}

Expand Down Expand Up @@ -260,7 +278,7 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
var range =
FirstPortRangeStart.ToString() +
".." +
(FirstPortRangeStart + InstanceCount * 2).ToString();
(FirstPortRangeStart + InstanceCount * 2 - 1).ToString();

asyncCmdlet.WriteVerbose(
Resources.VmssUseConnectionString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public static SubResource GetReference(
this IEngine engine, ResourceConfig<AvailabilitySet> availabilitySet)
=> engine.GetSubResourceReference(availabilitySet);

public static SubResource GetReference(
this IEngine engine, ResourceConfig<N.NetworkSecurityGroup> networkSecurityGroup)
=> engine.GetSubResourceReference(networkSecurityGroup);

public static SubResource GetReference(
this IEngine engine,
NestedResourceConfig<N.BackendAddressPool, N.LoadBalancer> backendAddressPool)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,23 @@ internal static ResourceConfig<VirtualMachineScaleSet> CreateVirtualMachineScale
this ResourceConfig<ResourceGroup> resourceGroup,
string name,
NestedResourceConfig<Subnet, VirtualNetwork> subnet,
IEnumerable<NestedResourceConfig<FrontendIPConfiguration, LoadBalancer>> frontendIpConfigurations,
NestedResourceConfig<BackendAddressPool, LoadBalancer> backendAdressPool,
IEnumerable<NestedResourceConfig<InboundNatPool, LoadBalancer>> inboundNatPools,
ResourceConfig<NetworkSecurityGroup> networkSecurityGroup,
ImageAndOsType imageAndOsType,
string adminUsername,
string adminPassword,
string vmSize,
int instanceCount,
UpgradeMode? upgradeMode,
IEnumerable<int> dataDisks)
IEnumerable<int> dataDisks,
IList<string> zones)
=> Strategy.CreateResourceConfig(
resourceGroup: resourceGroup,
name: name,
createModel: engine => new VirtualMachineScaleSet()
{
Zones = frontendIpConfigurations
?.Select(f => f.CreateModel(engine))
?.Where(z => z?.Zones != null)
.SelectMany(z => z.Zones)
.Where(z => z != null)
.ToList(),
Zones = zones,

UpgradePolicy = new UpgradePolicy
{
Expand Down Expand Up @@ -109,7 +105,8 @@ internal static ResourceConfig<VirtualMachineScaleSet> CreateVirtualMachineScale
.ToList()
}
},
Primary = true
Primary = true,
NetworkSecurityGroup = engine.GetReference(networkSecurityGroup)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
string adminPassword,
string size,
ResourceConfig<AvailabilitySet> availabilitySet,
IEnumerable<int> dataDisks)
IEnumerable<int> dataDisks,
IList<string> zones)
=> Strategy.CreateResourceConfig(
resourceGroup: resourceGroup,
name: name,
Expand Down Expand Up @@ -76,7 +77,8 @@ public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
DataDisks = DataDiskStrategy.CreateDataDisks(
imageAndOsType?.DataDiskLuns, dataDisks)
},
AvailabilitySet = engine.GetReference(availabilitySet)
AvailabilitySet = engine.GetReference(availabilitySet),
Zones = zones,
});

public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
Expand All @@ -87,7 +89,8 @@ public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
ResourceConfig<Disk> disk,
string size,
ResourceConfig<AvailabilitySet> availabilitySet,
IEnumerable<int> dataDisks)
IEnumerable<int> dataDisks,
IList<string> zones)
=> Strategy.CreateResourceConfig(
resourceGroup: resourceGroup,
name: name,
Expand Down Expand Up @@ -115,7 +118,8 @@ public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
},
DataDisks = DataDiskStrategy.CreateDataDisks(null, dataDisks)
},
AvailabilitySet = engine.GetReference(availabilitySet)
AvailabilitySet = engine.GetReference(availabilitySet),
Zones = zones
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ static class FrontendIPConfigurationStrategy
public static NestedResourceConfig<FrontendIPConfiguration, LoadBalancer> CreateFrontendIPConfiguration(
this ResourceConfig<LoadBalancer> loadBalancer,
string name,
IList<string> zones,
ResourceConfig<PublicIPAddress> publicIpAddress)
=> loadBalancer.CreateNested(
strategy: Strategy,
name: name,
createModel: engine => new FrontendIPConfiguration
{
Zones = zones,
PublicIPAddress = engine.GetReference(publicIpAddress)
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,25 @@ public static class LoadBalancerStrategy
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken),
_ => 30);

public enum Sku
{
Basic,
Standard,
}

public static ResourceConfig<LoadBalancer> CreateLoadBalancerConfig(
this ResourceConfig<ResourceGroup> resourceGroup,
string name)
string name,
Sku sku)
=> Strategy.CreateResourceConfig(
resourceGroup: resourceGroup,
name: name);
name: name,
createModel: engine => new LoadBalancer
{
Sku = new LoadBalancerSku
{
Name = sku.ToString()
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.Azure.Commands.Common.Strategies;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.Azure.Commands.Compute.Strategies.ComputeRp;
using System.Collections.Generic;

namespace Microsoft.Azure.Commands.Compute.Strategies.Network
{
Expand All @@ -36,7 +37,7 @@ static class NetworkSecurityGroupStrategy
createTime: _ => 15);

public static ResourceConfig<NetworkSecurityGroup> CreateNetworkSecurityGroupConfig(
this ResourceConfig<ResourceGroup> resourceGroup, string name, int[] openPorts)
this ResourceConfig<ResourceGroup> resourceGroup, string name, IList<int> openPorts)
=> Strategy.CreateResourceConfig(
resourceGroup: resourceGroup,
name: name,
Expand All @@ -57,14 +58,5 @@ public static ResourceConfig<NetworkSecurityGroup> CreateNetworkSecurityGroupCon
})
.ToList()
});

public static ResourceConfig<NetworkSecurityGroup> CreateNetworkSecurityGroupConfig(
this ResourceConfig<ResourceGroup> resourceGroup,
string name,
int[] openPorts,
ImageAndOsType imageAndOsType)
=> resourceGroup.CreateNetworkSecurityGroupConfig(
name,
imageAndOsType.UpdatePorts(openPorts));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01;
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01.Models;
using Microsoft.Azure.Management.Internal.Resources.Models;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.Compute.Strategies.Network
Expand All @@ -32,11 +33,19 @@ static class PublicIPAddressStrategy
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken),
createTime: _ => 15);

public enum Sku
{
Basic,
Standard,
}

public static ResourceConfig<PublicIPAddress> CreatePublicIPAddressConfig(
this ResourceConfig<ResourceGroup> resourceGroup,
string name,
string domainNameLabel,
string allocationMethod)
string allocationMethod,
Sku sku,
IList<string> zones)
=> Strategy.CreateResourceConfig(
resourceGroup: resourceGroup,
name: name,
Expand All @@ -46,7 +55,12 @@ public static ResourceConfig<PublicIPAddress> CreatePublicIPAddressConfig(
DnsSettings = new PublicIPAddressDnsSettings
{
DomainNameLabel = domainNameLabel,
}
},
Sku = new PublicIPAddressSku
{
Name = sku.ToString(),
},
Zones = zones,
});

public static async Task<string> UpdateDomainNameLabelAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
Mandatory = false,
Position = 3,
ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
[ValidateNotNullOrEmpty]
public string[] Zone { get; set; }

Expand Down Expand Up @@ -273,11 +274,15 @@ public async Task<ResourceConfig<VirtualMachine>> CreateConfigAsync()
var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig(
name: _cmdlet.PublicIpAddressName,
domainNameLabel: _cmdlet.DomainNameLabel,
allocationMethod: _cmdlet.AllocationMethod);
allocationMethod: _cmdlet.AllocationMethod,
sku: PublicIPAddressStrategy.Sku.Basic,
zones: _cmdlet.Zone);

_cmdlet.OpenPorts = ImageAndOsType.UpdatePorts(_cmdlet.OpenPorts);

var networkSecurityGroup = resourceGroup.CreateNetworkSecurityGroupConfig(
name: _cmdlet.SecurityGroupName,
openPorts: _cmdlet.OpenPorts,
imageAndOsType: ImageAndOsType);
openPorts: _cmdlet.OpenPorts);

var networkInterface = resourceGroup.CreateNetworkInterfaceConfig(
_cmdlet.Name, subnet, publicIpAddress, networkSecurityGroup);
Expand All @@ -297,7 +302,8 @@ public async Task<ResourceConfig<VirtualMachine>> CreateConfigAsync()
new NetworkCredential(string.Empty, _cmdlet.Credential.Password).Password,
size: _cmdlet.Size,
availabilitySet: availabilitySet,
dataDisks: _cmdlet.DataDiskSizeInGb);
dataDisks: _cmdlet.DataDiskSizeInGb,
zones: _cmdlet.Zone);
}
else
{
Expand All @@ -312,7 +318,8 @@ public async Task<ResourceConfig<VirtualMachine>> CreateConfigAsync()
disk: disk,
size: _cmdlet.Size,
availabilitySet: availabilitySet,
dataDisks: _cmdlet.DataDiskSizeInGb);
dataDisks: _cmdlet.DataDiskSizeInGb,
zones: _cmdlet.Zone);
}
}
}
Expand Down