|
23 | 23 | using Microsoft.Azure.Management.Compute.Models;
|
24 | 24 | using Microsoft.Azure.Management.Internal.Network.Version2017_10_01.Models;
|
25 | 25 | using System.Collections.Generic;
|
| 26 | +using System.Linq; |
26 | 27 | using System.Management.Automation;
|
27 | 28 | using System.Net;
|
28 | 29 | using System.Threading;
|
29 | 30 | using System.Threading.Tasks;
|
| 31 | +using System; |
30 | 32 |
|
31 | 33 | namespace Microsoft.Azure.Commands.Compute.Automation
|
32 | 34 | {
|
@@ -112,141 +114,149 @@ public partial class NewAzureRmVmss : ComputeAutomationBaseCmdlet
|
112 | 114 | [Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false)]
|
113 | 115 | public int[] NatBackendPort { get; set; }
|
114 | 116 |
|
115 |
| - async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet) |
| 117 | + const int FirstPortRangeStart = 50000; |
| 118 | + |
| 119 | + sealed class Parameters : IParameters<VirtualMachineScaleSet> |
116 | 120 | {
|
117 |
| - ResourceGroupName = ResourceGroupName ?? VMScaleSetName; |
118 |
| - VirtualNetworkName = VirtualNetworkName ?? VMScaleSetName; |
119 |
| - SubnetName = SubnetName ?? VMScaleSetName; |
120 |
| - PublicIpAddressName = PublicIpAddressName ?? VMScaleSetName; |
121 |
| - SecurityGroupName = SecurityGroupName ?? VMScaleSetName; |
122 |
| - LoadBalancerName = LoadBalancerName ?? VMScaleSetName; |
123 |
| - FrontendPoolName = FrontendPoolName ?? VMScaleSetName; |
124 |
| - BackendPoolName = BackendPoolName ?? VMScaleSetName; |
| 121 | + NewAzureRmVmss _cmdlet { get; } |
125 | 122 |
|
126 |
| - var imageAndOsType = new ImageAndOsType(OperatingSystemTypes.Windows, null); |
| 123 | + Client _client { get; } |
127 | 124 |
|
128 |
| - var resourceGroup = ResourceGroupStrategy.CreateResourceGroupConfig(ResourceGroupName); |
| 125 | + public Parameters(NewAzureRmVmss cmdlet, Client client) |
| 126 | + { |
| 127 | + _cmdlet = cmdlet; |
| 128 | + _client = client; |
| 129 | + } |
129 | 130 |
|
130 |
| - var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig( |
131 |
| - name: PublicIpAddressName, |
132 |
| - getDomainNameLabel: () => DomainNameLabel, |
133 |
| - allocationMethod: AllocationMethod); |
| 131 | + public string Location |
| 132 | + { |
| 133 | + get { return _cmdlet.Location; } |
| 134 | + set { _cmdlet.Location = value; } |
| 135 | + } |
134 | 136 |
|
135 |
| - var virtualNetwork = resourceGroup.CreateVirtualNetworkConfig( |
136 |
| - name: VirtualNetworkName, addressPrefix: VnetAddressPrefix); |
| 137 | + public ImageAndOsType ImageAndOsType { get; set; } |
137 | 138 |
|
138 |
| - var subnet = virtualNetwork.CreateSubnet(SubnetName, SubnetAddressPrefix); |
| 139 | + public async Task<ResourceConfig<VirtualMachineScaleSet>> CreateConfigAsync() |
| 140 | + { |
| 141 | + ImageAndOsType = await _client.UpdateImageAndOsTypeAsync( |
| 142 | + ImageAndOsType, _cmdlet.ImageName, Location); |
139 | 143 |
|
140 |
| - var loadBalancer = resourceGroup.CreateLoadBalancerConfig( |
141 |
| - name: LoadBalancerName); |
| 144 | + // generate a domain name label if it's not specified. |
| 145 | + _cmdlet.DomainNameLabel = await PublicIPAddressStrategy.UpdateDomainNameLabelAsync( |
| 146 | + domainNameLabel: _cmdlet.DomainNameLabel, |
| 147 | + name: _cmdlet.VMScaleSetName, |
| 148 | + location: Location, |
| 149 | + client: _client); |
142 | 150 |
|
143 |
| - var frontendIpConfiguration = loadBalancer.CreateFrontendIPConfiguration( |
144 |
| - name: FrontendPoolName, |
145 |
| - zones: Zone, |
146 |
| - publicIpAddress: publicIpAddress); |
| 151 | + var resourceGroup = ResourceGroupStrategy.CreateResourceGroupConfig(_cmdlet.ResourceGroupName); |
147 | 152 |
|
148 |
| - var backendAddressPool = loadBalancer.CreateBackendAddressPool( |
149 |
| - name: BackendPoolName); |
| 153 | + var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig( |
| 154 | + name: _cmdlet.PublicIpAddressName, |
| 155 | + domainNameLabel: _cmdlet.DomainNameLabel, |
| 156 | + allocationMethod: _cmdlet.AllocationMethod); |
150 | 157 |
|
151 |
| - if (BackendPort != null) |
152 |
| - { |
153 |
| - var LoadBalancingRuleName = LoadBalancerName; |
154 |
| - foreach (var backendPort in BackendPort) |
155 |
| - { |
156 |
| - loadBalancer.CreateLoadBalancingRule( |
157 |
| - name: LoadBalancingRuleName + backendPort.ToString(), |
158 |
| - fronendIpConfiguration: frontendIpConfiguration, |
159 |
| - backendAddressPool: backendAddressPool, |
160 |
| - frontendPort: backendPort, |
161 |
| - backendPort: backendPort); |
162 |
| - } |
163 |
| - } |
| 158 | + var virtualNetwork = resourceGroup.CreateVirtualNetworkConfig( |
| 159 | + name: _cmdlet.VirtualNetworkName, |
| 160 | + addressPrefix: _cmdlet.VnetAddressPrefix); |
164 | 161 |
|
165 |
| - var inboundNatPools = new List<NestedResourceConfig<InboundNatPool, LoadBalancer>>(); |
166 |
| - |
167 |
| - var virtualMachineScaleSet = resourceGroup.CreateVirtualMachineScaleSetConfig( |
168 |
| - name: VMScaleSetName, |
169 |
| - subnet: subnet, |
170 |
| - frontendIpConfigurations: new[] { frontendIpConfiguration }, |
171 |
| - backendAdressPool: backendAddressPool, |
172 |
| - inboundNatPools: inboundNatPools, |
173 |
| - getImageAndOsType: () => imageAndOsType, |
174 |
| - adminUsername: Credential.UserName, |
175 |
| - adminPassword: new NetworkCredential(string.Empty, Credential.Password).Password, |
176 |
| - vmSize: VmSize, |
177 |
| - instanceCount: InstanceCount, |
178 |
| - upgradeMode: MyInvocation.BoundParameters.ContainsKey(nameof(UpgradePolicyMode)) |
179 |
| - ? UpgradePolicyMode |
180 |
| - : (UpgradeMode?)null); |
| 162 | + var subnet = virtualNetwork.CreateSubnet( |
| 163 | + _cmdlet.SubnetName, _cmdlet.SubnetAddressPrefix); |
181 | 164 |
|
182 |
| - var client = new Client(DefaultProfile.DefaultContext); |
| 165 | + var loadBalancer = resourceGroup.CreateLoadBalancerConfig( |
| 166 | + name: _cmdlet.LoadBalancerName); |
183 | 167 |
|
184 |
| - // get current Azure state |
185 |
| - var current = await virtualMachineScaleSet.GetStateAsync(client, new CancellationToken()); |
186 |
| - |
187 |
| - Location = current.UpdateLocation(Location, virtualMachineScaleSet); |
188 |
| - |
189 |
| - imageAndOsType = await client.UpdateImageAndOsTypeAsync(ImageName, Location); |
190 |
| - |
191 |
| - NatBackendPort = imageAndOsType.OsType.UpdatePorts(NatBackendPort); |
192 |
| - |
193 |
| - var inboundNatPoolName = VMScaleSetName; |
194 |
| - const int FirstPortRangeStart = 50000; |
195 |
| - var portRangeStart = FirstPortRangeStart; |
196 |
| - var PortRangeSize = InstanceCount * 2; |
197 |
| - foreach (var natBackendPort in NatBackendPort) |
198 |
| - { |
199 |
| - inboundNatPools.Add( |
200 |
| - loadBalancer.CreateInboundNatPool( |
201 |
| - name: inboundNatPoolName + natBackendPort.ToString(), |
202 |
| - frontendIpConfiguration: frontendIpConfiguration, |
203 |
| - frontendPortRangeStart: portRangeStart, |
204 |
| - frontendPortRangeEnd: portRangeStart + PortRangeSize, |
205 |
| - backendPort: natBackendPort)); |
206 |
| - portRangeStart += 2000; |
207 |
| - } |
| 168 | + var frontendIpConfiguration = loadBalancer.CreateFrontendIPConfiguration( |
| 169 | + name: _cmdlet.FrontendPoolName, |
| 170 | + zones: _cmdlet.Zone, |
| 171 | + publicIpAddress: publicIpAddress); |
208 | 172 |
|
209 |
| - // generate a domain name label if it's not specified. |
210 |
| - DomainNameLabel = await PublicIPAddressStrategy.UpdateDomainNameLabelAsync( |
211 |
| - domainNameLabel: DomainNameLabel, |
212 |
| - name: VMScaleSetName, |
213 |
| - location: Location, |
214 |
| - client: client); |
| 173 | + var backendAddressPool = loadBalancer.CreateBackendAddressPool( |
| 174 | + name: _cmdlet.BackendPoolName); |
215 | 175 |
|
216 |
| - var fqdn = PublicIPAddressStrategy.Fqdn(DomainNameLabel, Location); |
| 176 | + if (_cmdlet.BackendPort != null) |
| 177 | + { |
| 178 | + var loadBalancingRuleName = _cmdlet.LoadBalancerName; |
| 179 | + foreach (var backendPort in _cmdlet.BackendPort) |
| 180 | + { |
| 181 | + loadBalancer.CreateLoadBalancingRule( |
| 182 | + name: loadBalancingRuleName + backendPort.ToString(), |
| 183 | + fronendIpConfiguration: frontendIpConfiguration, |
| 184 | + backendAddressPool: backendAddressPool, |
| 185 | + frontendPort: backendPort, |
| 186 | + backendPort: backendPort); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + _cmdlet.NatBackendPort = ImageAndOsType.UpdatePorts(_cmdlet.NatBackendPort); |
| 191 | + |
| 192 | + var inboundNatPoolName = _cmdlet.VMScaleSetName; |
| 193 | + var PortRangeSize = _cmdlet.InstanceCount * 2; |
| 194 | + |
| 195 | + var inboundNatPools = _cmdlet.NatBackendPort |
| 196 | + ?.Select((port, i) => |
| 197 | + { |
| 198 | + var portRangeStart = FirstPortRangeStart + i * 2000; |
| 199 | + return loadBalancer.CreateInboundNatPool( |
| 200 | + name: inboundNatPoolName + port.ToString(), |
| 201 | + frontendIpConfiguration: frontendIpConfiguration, |
| 202 | + frontendPortRangeStart: portRangeStart, |
| 203 | + frontendPortRangeEnd: portRangeStart + PortRangeSize, |
| 204 | + backendPort: port); |
| 205 | + }) |
| 206 | + .ToList(); |
| 207 | + |
| 208 | + return resourceGroup.CreateVirtualMachineScaleSetConfig( |
| 209 | + name: _cmdlet.VMScaleSetName, |
| 210 | + subnet: subnet, |
| 211 | + frontendIpConfigurations: new[] { frontendIpConfiguration }, |
| 212 | + backendAdressPool: backendAddressPool, |
| 213 | + inboundNatPools: inboundNatPools, |
| 214 | + imageAndOsType: ImageAndOsType, |
| 215 | + adminUsername: _cmdlet.Credential.UserName, |
| 216 | + adminPassword: new NetworkCredential(string.Empty, _cmdlet.Credential.Password).Password, |
| 217 | + vmSize: _cmdlet.VmSize, |
| 218 | + instanceCount: _cmdlet.InstanceCount, |
| 219 | + upgradeMode: _cmdlet.MyInvocation.BoundParameters.ContainsKey(nameof(UpgradePolicyMode)) |
| 220 | + ? _cmdlet.UpgradePolicyMode |
| 221 | + : (UpgradeMode?)null); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet) |
| 226 | + { |
| 227 | + ResourceGroupName = ResourceGroupName ?? VMScaleSetName; |
| 228 | + VirtualNetworkName = VirtualNetworkName ?? VMScaleSetName; |
| 229 | + SubnetName = SubnetName ?? VMScaleSetName; |
| 230 | + PublicIpAddressName = PublicIpAddressName ?? VMScaleSetName; |
| 231 | + SecurityGroupName = SecurityGroupName ?? VMScaleSetName; |
| 232 | + LoadBalancerName = LoadBalancerName ?? VMScaleSetName; |
| 233 | + FrontendPoolName = FrontendPoolName ?? VMScaleSetName; |
| 234 | + BackendPoolName = BackendPoolName ?? VMScaleSetName; |
217 | 235 |
|
218 |
| - var engine = new SdkEngine(client.SubscriptionId); |
219 |
| - var target = virtualMachineScaleSet.GetTargetState(current, engine, Location); |
| 236 | + var client = new Client(DefaultProfile.DefaultContext); |
220 | 237 |
|
221 |
| - var newState = await virtualMachineScaleSet |
222 |
| - .UpdateStateAsync( |
223 |
| - client, |
224 |
| - target, |
225 |
| - new CancellationToken(), |
226 |
| - new ShouldProcess(asyncCmdlet), |
227 |
| - asyncCmdlet.ReportTaskProgress); |
| 238 | + var parameters = new Parameters(this, client); |
228 | 239 |
|
229 |
| - var result = newState.Get(virtualMachineScaleSet); |
230 |
| - if (result == null) |
231 |
| - { |
232 |
| - result = current.Get(virtualMachineScaleSet); |
233 |
| - } |
| 240 | + var result = await StrategyCmdlet.RunAsync( |
| 241 | + client, parameters, asyncCmdlet, new CancellationToken()); |
234 | 242 |
|
235 | 243 | if (result != null)
|
236 | 244 | {
|
| 245 | + var fqdn = PublicIPAddressStrategy.Fqdn(DomainNameLabel, Location); |
| 246 | + |
237 | 247 | var psObject = new PSVirtualMachineScaleSet();
|
238 | 248 | ComputeAutomationAutoMapperProfile.Mapper.Map(result, psObject);
|
239 | 249 | psObject.FullyQualifiedDomainName = fqdn;
|
240 | 250 |
|
241 | 251 | var port = "<port>";
|
242 |
| - var connectionString = imageAndOsType.GetConnectionString( |
| 252 | + var connectionString = parameters.ImageAndOsType.GetConnectionString( |
243 | 253 | fqdn,
|
244 | 254 | Credential.UserName,
|
245 | 255 | port);
|
246 | 256 | var range =
|
247 | 257 | FirstPortRangeStart.ToString() +
|
248 | 258 | ".." +
|
249 |
| - (FirstPortRangeStart + PortRangeSize).ToString(); |
| 259 | + (FirstPortRangeStart + InstanceCount * 2).ToString(); |
250 | 260 |
|
251 | 261 | asyncCmdlet.WriteVerbose(
|
252 | 262 | Resources.VmssUseConnectionString,
|
|
0 commit comments