-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathNew-AzCloudService.ps1
More file actions
645 lines (559 loc) · 33.6 KB
/
New-AzCloudService.ps1
File metadata and controls
645 lines (559 loc) · 33.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# ----------------------------------------------------------------------------------
#
# 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.
# ----------------------------------------------------------------------------------
<#
.Synopsis
Create a CloudService Resource
.Description
Create a CloudService Resource
#>
function New-AzCloudService {
[OutputType('Microsoft.Azure.PowerShell.Cmdlets.CloudService.Models.ICloudService')]
[CmdletBinding(PositionalBinding=$false, SupportsShouldProcess, ConfirmImpact='Medium')]
param(
[Parameter(ParameterSetName='quickCreateParameterSetWithoutStorage', Mandatory)]
[Parameter(ParameterSetName='quickCreateParameterSetWithStorage', Mandatory)]
[Alias('CloudServiceName')]
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.Category('Path')]
[System.String]
# Name of the cloud service.
${Name},
[Parameter(ParameterSetName='quickCreateParameterSetWithoutStorage', Mandatory)]
[Parameter(ParameterSetName='quickCreateParameterSetWithStorage', Mandatory)]
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.Category('Path')]
[System.String]
# Name of the resource group.
${ResourceGroupName},
[Parameter(ParameterSetName='quickCreateParameterSetWithoutStorage')]
[Parameter(ParameterSetName='quickCreateParameterSetWithStorage')]
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.Category('Path')]
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.Runtime.DefaultInfo(Script='(Get-AzContext).Subscription.Id')]
[System.String]
# Subscription credentials which uniquely identify Microsoft Azure subscription.
# The subscription ID forms part of the URI for every service call.
${SubscriptionId},
[Parameter(ParameterSetName='quickCreateParameterSetWithoutStorage', Mandatory)]
[Parameter(ParameterSetName='quickCreateParameterSetWithStorage', Mandatory)]
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.Category('Body')]
[System.String]
# Resource location.
${Location},
[Parameter(ParameterSetName='quickCreateParameterSetWithoutStorage', Mandatory)]
[Parameter(ParameterSetName='quickCreateParameterSetWithStorage', Mandatory)]
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.Category('Body')]
[System.String]
# Specifies the XML service configuration (.cscfg) for the cloud service.
${ConfigurationFile},
[Parameter(ParameterSetName='quickCreateParameterSetWithoutStorage', Mandatory, HelpMessage="Path to .csdef file.")]
[Parameter(ParameterSetName='quickCreateParameterSetWithStorage', Mandatory, HelpMessage="Path to .csdef file.")]
[System.String]
# Specifies the XML service definitions (.csdef) for the cloud service.
${DefinitionFile},
[Parameter(ParameterSetName='quickCreateParameterSetWithoutStorage', Mandatory, HelpMessage='URL that refers to the location of the service package in the Blob service.')]
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.Category('Body')]
[System.String]
# Specifies a URL that refers to the location of the service package in the Blob service.
# The service package URL can be Shared Access Signature (SAS) URI from any storage account.This is a write-only property and is not returned in GET calls.
${PackageUrl},
[Parameter(ParameterSetName='quickCreateParameterSetWithStorage', Mandatory, HelpMessage='Path to .cspkg file. It will be uploaded to a blob')]
[System.String]
${PackageFile},
[Parameter(ParameterSetName='quickCreateParameterSetWithStorage', Mandatory, HelpMessage='Name of the storage account that will store the Package file.')]
[System.String]
${StorageAccount},
[Parameter(ParameterSetName='quickCreateParameterSetWithoutStorage', HelpMessage="Describes a cloud service extension profile.")]
[Parameter(ParameterSetName='quickCreateParameterSetWithStorage', HelpMessage="Describes a cloud service extension profile.")]
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.Category('Body')]
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.Models.ICloudServiceExtensionProfile]
# Describes a cloud service extension profile.
# To construct, see NOTES section for EXTENSIONPROFILE properties and create a hash table.
${ExtensionProfile},
[Parameter(ParameterSetName='quickCreateParameterSetWithoutStorage', HelpMessage="Indicates whether to start the cloud service immediately after it is created.")]
[Parameter(ParameterSetName='quickCreateParameterSetWithStorage', HelpMessage="Indicates whether to start the cloud service immediately after it is created.")]
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.Category('Body')]
[System.Management.Automation.SwitchParameter]
# (Optional) Indicates whether to start the cloud service immediately after it is created.
# The default value is `true`.If false, the service model is still deployed, but the code is not run immediately.
# Instead, the service is PoweredOff until you call Start, at which time the service will be started.
# A deployed service still incurs charges, even if it is poweredoff.
${StartCloudService},
[Parameter(ParameterSetName='quickCreateParameterSetWithoutStorage')]
[Parameter(ParameterSetName='quickCreateParameterSetWithStorage')]
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.Category('Body')]
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.Runtime.Info(PossibleTypes=([Microsoft.Azure.PowerShell.Cmdlets.CloudService.Models.ICloudServiceTags]))]
[System.Collections.Hashtable]
# Resource tags.
${Tag},
[Parameter(ParameterSetName='quickCreateParameterSetWithoutStorage', HelpMessage="Update mode for the cloud service.")]
[Parameter(ParameterSetName='quickCreateParameterSetWithStorage', HelpMessage="Update mode for the cloud service.")]
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.PSArgumentCompleterAttribute("Auto", "Manual", "Simultaneous")]
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.Category('Body')]
[System.String]
# Update mode for the cloud service.
# Role instances are allocated to update domains when the service is deployed.
# Updates can be initiated manually in each update domain or initiated automatically in all update domains.Possible Values are <br /><br />**Auto**<br /><br />**Manual** <br /><br />**Simultaneous**<br /><br />If not specified, the default value is Auto.
# If set to Manual, PUT UpdateDomain must be called to apply the update.
# If set to Auto, the update is automatically applied to each update domain in sequence.
${UpgradeMode},
[Parameter(ParameterSetName='quickCreateParameterSetWithoutStorage', HelpMessage= "Name of Dns to be used for the CloudService resource.")]
[Parameter(ParameterSetName='quickCreateParameterSetWithStorage', HelpMessage= "Name of Dns to be used for the CloudService resource.")]
[System.String]
# Name of Dns to be used for the CloudService resource
${DnsName},
[Parameter(ParameterSetName='quickCreateParameterSetWithoutStorage', HelpMessage= "Name of the KeyVault to be used for the CloudService resource.")]
[Parameter(ParameterSetName='quickCreateParameterSetWithStorage', HelpMessage= "Name of the KeyVault to be used for the CloudService resource.")]
[System.String]
# Name of the KeyVault to be used for the CloudService resource
${KeyVaultName}
)
process {
Import-Module Az.Network
Import-Module Az.KeyVault
Import-Module Az.Storage
# extract csdef/cscfg
try {
$getCS = Get-azcloudservice -resourcegroupname $ResourceGroupName -name $name -ErrorAction Stop
}
catch {
# CloudService does not exist in that name/resource group
}
finally {
if ($null -ne $getCS){
throw "A Cloud Service resource with name: '" +$name + "' already exists in Resource Group: '" + $ResourceGroupName + "'. Please try another name."
}
}
if (-not (Test-Path $ConfigurationFile))
{
throw "Cannot find file: " + $ConfigurationFile
}
if (-not (Test-Path $DefinitionFile))
{
throw "Cannot find file: " + $DefinitionFile
}
if ($PSBoundParameters.ContainsKey("PackageFile")){
if (-not (Test-Path $PackageFile))
{
throw "Cannot find file: " + $PackageFile
}
$extn = [IO.Path]::GetExtension($PackageFile)
if ($extn -ne ".cspkg" )
{
throw "The Definition File must have the file extension '.cspkg'"
}
}
[xml]$csdef = Get-Content -Path $DefinitionFile
[xml]$cscfg = Get-Content -Path $ConfigurationFile
$Configuration = Get-Content -Path $ConfigurationFile | Out-String
# do validation
$passMemory = @{}
validation $cscfg $csdef $PSBoundParameters ([ref]$passMemory)
# create resources
If ($passMemory.ipFound -eq $false){
Write-Host("Creating ReservedIP")
$null = New-AzPublicIpAddress -ResourceGroupName $ResourceGroupName -Name $passMemory.ipName -location $location -Sku Basic -AllocationMethod Static -WarningAction SilentlyContinue
}
If ($passMemory.vNetFound -eq $False){
# create subnets first
$subnetsList = @()
$subnetCount = 0
If ($True -eq $passMemory.CreateInternalLoadBalancer){
$aSubnet = New-AzVirtualNetworkSubnetConfig -Name $cscfg.ServiceConfiguration.NetworkConfiguration.loadBalancers.Loadbalancer.FrontendIPConfiguration.subnet -AddressPrefix "10.0.0.0/24" -WarningAction SilentlyContinue
$subnetsList = $subnetsList + @($aSubnet)
$subnetCount = $subnetCount + 1
$passMemory.Add("theSubnet", $aSubnet)
}
foreach ($instaceAddress in $cscfg.ServiceConfiguration.NetworkConfiguration.AddressAssignments.InstanceAddress) {
if ( ($subnetsList.count -eq 0) -or (-not ($subnetsList.name.tolower()).contains($instaceAddress.subnets.subnet.Name.tolower())) ){
$addressPrefix = "10.0." + $subnetCount + ".0/24"
$aSubnet = New-AzVirtualNetworkSubnetConfig -Name $instaceAddress.subnets.subnet.Name -AddressPrefix $addressPrefix -WarningAction SilentlyContinue
$subnetsList = $subnetsList + @($aSubnet)
$subnetCount = $subnetCount + 1
}
}
# vnet
Write-Host("Creating Virtual Network")
$null = New-AzVirtualNetwork -name $passMemory.vnetName -resourcegroupname $resourcegroupname -location $location -AddressPrefix 10.0.0.0/16 -Subnet $subnetsList
}
# if -storageaccount is given, upload to packageUrl to blob
if ($PSBoundParameters.ContainsKey("StorageAccount"))
{
Write-Host("Uploading the csdef to a blob in the Storage Account.")
$storageAccountObjs = Get-AzStorageAccount
foreach ($storageAccountObj in $storageAccountObjs) {
if ($storageAccountObj.StorageAccountName.tolower() -eq $storageAccount.tolower()){
break
}
}
$containerName = "cloudservicecontainer"
# check if container exists
try {
$container = get-azstorageContainer -context $storageAccountObj.context -name $containerName -ErrorAction Stop
}
catch {
# does not exist
$container = New-AzStorageContainer -Name $containerName -Context $storageAccountObj.Context -Permission Blob
}
# Upload your Cloud Service package (cspkg) to the storage account.
$tokenStartTime = Get-Date
$tokenEndTime = $tokenStartTime.AddYears(1)
$cspkgBlob = Set-AzStorageBlobContent -File $PackageFile -Container $container.name -Blob ($name + ".cspkg") -Context $storageAccountObj.Context
$cspkgToken = New-AzStorageBlobSASToken -Container $container.name -Blob $cspkgBlob.Name -Permission rwd -StartTime $tokenStartTime -ExpiryTime $tokenEndTime -Context $storageAccountObj.Context
$cspkgUrl = $cspkgBlob.ICloudBlob.Uri.AbsoluteUri + $cspkgToken
$null = $PSBoundParameters.Remove("StorageAccount")
$null = $PSBoundParameters.Remove("PackageFile")
$null = $PSBoundParameters.Add("packageURL", $cspkgURL)
}
# network profile
if ( $null -eq $cscfg.ServiceConfiguration.NetworkConfiguration.AddressAssignments.ReservedIPs.ReservedIP ){
# Create a public IP address and (optionally) set the DNS label property of the public IP address. If you are using a static IP, it needs to referenced as a Reserved IP in Service Configuration file.
$publicIpName = $name + "Ip"
if ($PSBoundParameters.ContainsKey("DnsName"))
{
$publicIp = New-AzPublicIpAddress -Name $publicIPName -ResourceGroupName $ResourceGroupName -Location $Location -AllocationMethod Dynamic -IpAddressVersion IPv4 -DomainNameLabel $DnsName -Sku Basic -WarningAction SilentlyContinue
$null = $PSBoundParameters.Remove("DnsName")
}
else {
$publicIp = New-AzPublicIpAddress -Name $publicIpName -ResourceGroupName $ResourceGroupName -Location $Location -AllocationMethod Dynamic -IpAddressVersion IPv4 -Sku Basic -WarningAction SilentlyContinue
}
}
else {
$publicIpName = $cscfg.ServiceConfiguration.NetworkConfiguration.AddressAssignments.ReservedIPs.ReservedIP.Name
}
# Create Network Profile Object and associate public IP address to the frontend of the platform created load balancer.
$publicIP = Get-AzPublicIpAddress -ResourceGroupName $ResourceGroupName -Name $publicIpName
$feIpConfig = New-AzCloudServiceLoadBalancerFrontendIPConfigurationObject -Name ($name+'LbFe') -PublicIPAddressId $publicIP.Id
$loadBalancerConfig = New-AzCloudServiceLoadBalancerConfigurationObject -Name ($name + 'LB') -FrontendIPConfiguration $feIpConfig
$networkProfile = @{loadBalancerConfiguration = $loadBalancerConfig}
If ( $null -ne $cscfg.ServiceConfiguration.NetworkConfiguration.loadBalancers.loadBalancer){
$privateLB = $cscfg.ServiceConfiguration.NetworkConfiguration.loadBalancers.loadBalancer
$feIpConfig2 = New-AzCloudServiceLoadBalancerFrontendIPConfigurationObject -Name ($privateLB.name + 'Fe') -privateIPAddress $privateLB.FrontendIPConfiguration.staticVirtualNetworkIPAddress -subnetId $passMemory.theSubnet.Id
$loadBalancerConfig2 = New-AzCloudServiceLoadBalancerConfigurationObject -Name $privateLB.name -FrontendIPConfiguration $feIpConfig2
$networkProfile = @{loadBalancerConfiguration = @($loadBalancerConfig, $loadBalancerConfig2)}
}
$null = $PSBoundParameters.Add("NetworkProfile", $networkProfile)
# OS Profile
if ($PSBoundParameters.ContainsKey("KeyVaultName")) {
$keyVault = $passMemory.KeyVault
$certSecretList = $passMemory.certSecretList
$secretGroup = New-AzCloudServiceVaultSecretGroupObject -Id $keyVault.ResourceId -CertificateUrl $certSecretList
$osProfile = @{secret = @($secretGroup)}
$null = $PSBoundParameters.Remove("keyvaultname")
$null = $PSBoundParameters.Add("OSProfile", $osProfile)
}
# Role Profile
$roleProfileList = @()
foreach ($role in $cscfg.ServiceConfiguration.Role) {
# find in csdef
$RoleFoundinCsDef = $false
foreach ($webRole in $csdef.ServiceDefinition.WebRole) {
if ($role.name -eq $webRole.name){
$RoleFoundinCsDef = $true
$defRole = $webRole
break
}
}
if (-not $RoleFoundinCsDef){
foreach ($workerRole in $csdef.ServiceDefinition.WorkerRole) {
if($role.name -eq $workerRole.name){
$RoleFoundinCsDef = $true
$defRole = $workerRole
break
}
}
}
$newRole = New-AzCloudServiceRoleProfilePropertiesObject -Name $defRole.Name -SkuName $defRole.vmsize -SkuTier 'Standard' -SkuCapacity $role.Instances.count
$roleProfileList = $roleProfileList + @($newRole)
}
$roleProfile = @{role = $roleProfileList}
$null = $PSBoundParameters.Add("roleProfile", $RoleProfile)
$null = $PSBoundParameters.Remove("DefinitionFile")
$null = $PSBoundParameters.Remove("ConfigurationFile")
$null = $PSBoundParameters.Add("Configuration", $Configuration)
# Perform action
Write-Host("Creating the Cloud Service resource.")
Az.CloudService\New-AzCloudService @PSBoundParameters
}
}
function validation
{
[Microsoft.Azure.PowerShell.Cmdlets.CloudService.DoNotExportAttribute()]
param(
[Parameter()]
[object]
${cscfg},
[Parameter()]
[object]
${csdef},
[Parameter()]
[Hashtable]
$params,
[Parameter()]
[Hashtable]
[ref]$passMemory
)
Write-Host("Checking validations on the .cscfg and .csdef files.")
# Network configuration missing in configuration
If ( ($null -eq $cscfg.ServiceConfiguration.NetworkConfiguration) -or (($cscfg.ServiceConfiguration.NetworkConfiguration.VirtualNetworkSite | Measure-Object | Select-Object -expandproperty count) -eq 0) -or (($cscfg.ServiceConfiguration.NetworkConfiguration.AddressAssignments.InstanceAddress.Subnets | Measure-Object | Select-Object -ExpandProperty count) -eq 0) )
{
throw "The network configuration is missing from the configuration file. Please add the network configuration to the configuration file."
}
# CS definition and configuration match
if (($cscfg.ServiceConfiguration.Role | Measure-Object | Select-Object -ExpandProperty count) -eq 1){
$csCfgRoleNames = @($cscfg.ServiceConfiguration.Role.name.tolower())
}elseif(($cscfg.ServiceConfiguration.Role | Measure-Object | Select-Object -ExpandProperty count) -gt 1){
$csCfgRoleNames = $cscfg.ServiceConfiguration.Role.name.tolower()
}
$csDefRoleNames = @()
if (($csdef.ServiceDefinition.WebRole | Measure-Object | select-object -expandproperty count) -eq 1){
$csDefRoleNames = @($csdef.ServiceDefinition.WebRole.name.tolower())
}elseif (($csdef.ServiceDefinition.WebRole | Measure-Object | select-object -expandproperty count) -gt 1) {
$csDefRoleNames = $csdef.ServiceDefinition.WebRole.name.tolower()
}
if (($csdef.ServiceDefinition.WorkerRole | Measure-Object | select-object -expandproperty count) -eq 1){
$csDefRoleNames = $csDefRoleNames + @($csdef.ServiceDefinition.WorkerRole.name.tolower())
}elseif (($csdef.ServiceDefinition.WorkerRole | Measure-Object | select-object -expandproperty count) -gt 1) {
$csDefRoleNames = $csDefRoleNames + $csdef.ServiceDefinition.WorkerRole.name.tolower()
}
foreach ($aRoleName in $csCfgRoleNames){
if (-not $csDefRoleNames.contains($aRoleName)){
throw "The CSCFG did not match the CSDEF. More details: No role named '" + $aRoleName + "' found in the service definition file. For more details please refer to : https://aka.ms/cses-cscfg-csdef"
}
}
foreach ($aRoleName in $csDefRoleNames){
if (-not $csCfgRoleNames.contains($aRoleName)){
throw "The CSCFG did not match the CSDEF. More details: No role named '" + $aRoleName + "' found in the service configuration file. For more details please refer to : https://aka.ms/cses-cscfg-csdef"
}
}
$certList = @()
foreach ($role in $cscfg.ServiceConfiguration.Role){
$defCerts = ($csdef.ServiceDefinition.childnodes | where-object {$_.name.tolower() -eq $role.name.tolower()}).Certificates.Certificate
If ( 1 -eq $defCerts.count ){
$defCerts = @($defCerts)
}
foreach ($cert in $role.Certificates.Certificate){
if ( "Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption" -ne $cert.Name){
# CS definition and configuration match
if ( -not $defCerts.name.tolower().Contains($cert.Name.tolower())){
throw "The service definition file does not provide a certificate definition for certificate '" + $cert.name + "' for role '"+ $role.name +"'. For more details please refer to : https://aka.ms/cses-cscfg-csdef"
}
if ($certList.Count -eq 0 -or -not $certList.thumbprint.Contains($cert.thumbprint))
{
$certList = $certList + $cert
}
}
}
}
# Existing Virtual Network Location Mismatch
# check if vnet exists
$vnetNameSplitCount = ($cscfg.ServiceConfiguration.NetworkConfiguration.VirtualNetworkSite.name).split().count
if (3 -eq $vnetNameSplitCount){
$vnetNameFormat = ($cscfg.ServiceConfiguration.NetworkConfiguration.VirtualNetworkSite.name).split()
if ("group" -ne $vnetNameFormat[0].tolower()){
throw "VirtualNetworkSite name should be formated either ""{Name}"" or ""Group {ResourceGroupName} {Name}""."
}
$passMemory.Add("vnetName", $vnetNameFormat[2])
# look for the vnet
try {
$thevnet = Get-AzVirtualNetwork -ResourceGroupName $vnetNameFormat[1] -Name $vnetNameFormat[2] -ErrorAction Stop
if ($thevnet.location.replace(" ","").tolower() -eq $Location.replace(" ","").tolower()){
$vnetFound = $true
}else {
$vnetLocationMatch = $false
}
}
catch {
$vnetFound = $false
}
} elseif (1 -eq $vnetNameSplitCount) {
$passMemory.Add("vnetName", $cscfg.ServiceConfiguration.NetworkConfiguration.VirtualNetworkSite.name)
try {
$thevnet = Get-AzVirtualNetwork -name $cscfg.ServiceConfiguration.NetworkConfiguration.VirtualNetworkSite.name -ResourceGroupName $ResourceGroupName -ErrorAction Stop
if ($thevnet.location.replace(" ","").tolower() -eq $Location.replace(" ","").tolower()){
$vnetFound = $true
}
else {
$vnetLocationMatch = $false
}
}
catch {
$vnetFound = $false
}
}else {
throw "VirtualNetworkSite name should be formated either ""{Name}"" or ""Group {ResourceGroupName} {Name}""."
}
If($false -eq $vnetLocationMatch){
throw "The location for the cloud service (" + $location + ") and virtual network ("+ $thevnet.location +") are different. The location of the cloud service needs to match the location of the virtual network. Change the location of the cloud service to match the virtual network or change the resource group of the cloud service to try to resolve this issue."
}
$passMemory.Add("vnetFound", $vnetFound)
If ($vnetFound){
If (1 -eq $theVNet.subnets.count){
$vnetSubnets = @($theVnet.Subnets)
}
else {
$vnetSubnets = $theVnet.subnets
}
# Existing Virtual Network Missing Subnets
foreach ($instaceAddress in $cscfg.ServiceConfiguration.NetworkConfiguration.AddressAssignments.InstanceAddress) {
if (-not ($vnetSubnets.name.tolower()).contains($instaceAddress.subnets.subnet.Name.tolower())){
throw "Subnet defined in the CSCFG file: '" + $instaceAddress.subnets.subnet.Name + "' could not be found in the Virtual Network: '" + $theVNet.name + "'. Please add the subnet to the virtual network."
}
}
}
# Internal load balancer private ip contained in subnet
If ( $null -ne $cscfg.ServiceConfiguration.NetworkConfiguration.loadBalancers.loadBalancer){
$InternalLBFEConfig = $cscfg.ServiceConfiguration.NetworkConfiguration.loadBalancers.Loadbalancer.FrontendIPConfiguration
If ($vnetFound){
$theSubnet = $thevnet.Subnets | where-object {$_.Name.tolower() -eq $InternalLBFEConfig.subnet.tolower()}
If ($null -eq $theSubnet){
throw "Subnet defined in the CSCFG file: '" + $InternalLBFEConfig.subnet + "' could not be found in the Virtual Network: '" + $theVNet.name + "'. Please add the subnet to the virtual network."
}
$passMemory.Add("theSubnet", $theSubnet)
$addressPrefix = $theSubnet.AddressPrefix
}
else{
$passMemory.Add("CreateInternalLoadBalancer", $true)
$addressPrefix = "10.0.0.0/24"
}
$maskNumber = $addressPrefix.split("/")[1]
$subnetAddress = $addressPrefix.split("/")[0]
$subnetBinary = -join ($subnetAddress -split '\.' | ForEach-Object {
[System.Convert]::ToString($_,2).PadLeft(8,'0')
})
$LBIP = $InternalLBFEConfig.staticVirtualNetworkIPAddress
$LBIPBinary = -join ($LBIP -split '\.' | ForEach-Object {
[System.Convert]::ToString($_,2).PadLeft(8,'0')
})
If ($subnetBinary.substring(0,$maskNumber) -ne $LBIPbinary.substring(0,$maskNumber)){
If ($vnetFound){
throw "The internal load balancer subnet '" + $InternalLBFEConfig.subnet + "' does not contain the private IP " + $LBIP + ". Update the subnet within the Virtual Network to include the Private IP."
}else{
throw "The default internal load balancer subnet which will be created: '"+ $addressPrefix +"' does not contain the private IP " + $LBIP + ". Either update private IP or provided an already created virtual network and subnet."
}
}
}
if ( $null -ne $cscfg.ServiceConfiguration.NetworkConfiguration.AddressAssignments.ReservedIPs.ReservedIP ){
$IpNameSplitCount = ($cscfg.ServiceConfiguration.NetworkConfiguration.AddressAssignments.ReservedIPs.ReservedIP.Name).split().count
if (3 -eq $IpNameSplitCount){
$IpNameFormat = ($cscfg.ServiceConfiguration.NetworkConfiguration.AddressAssignments.ReservedIPs.ReservedIP.Name).split()
if ("group" -ne $IpNameFormat[0].tolower()){
throw "ReservedIP name should be formated either ""{Name}"" or ""Group {ResourceGroupName} {Name}""."
}
$passMemory.Add("ipName", $IpNameFormat[2])
# look for the Ip
try {
$theIpObj = Get-AzPublicIpAddress -ResourceGroupName $IpNameFormat[1] -Name $IpNameFormat[2] -ErrorAction Stop
if ($theIpObj.location.replace(" ","").tolower() -eq $Location.replace(" ","").tolower()){
$ipFound = $true
}else {
$ipLocationMatch = $false
}
}
catch {
$ipFound = $false
}
}elseif (1 -eq $IpNameSplitCount) {
$passMemory.Add("ipName", $cscfg.ServiceConfiguration.NetworkConfiguration.AddressAssignments.ReservedIPs.ReservedIP.Name)
try {
$theIpObj = Get-AzPublicIpAddress -name $cscfg.ServiceConfiguration.NetworkConfiguration.AddressAssignments.ReservedIPs.ReservedIP.Name -ResourceGroupName $ResourceGroupName -ErrorAction Stop
# Existing Reserved (Static) IP Location Mismatch
if ($theIpObj.Location.replace(" ","").tolower() -eq $location.replace(" ","").tolower()) {
$ipFound = $true
} else {
$ipLocationMatch = $false
}
}
catch {
$ipFound = $false
}
} else {
throw "ReservedIP name should be formated either ""{Name}"" or ""Group {ResourceGroupName} {Name}""."
}
If ($false -eq $IpLocationMatch){
throw "The location for the Cloud Service (" + $location + ") and the Public IP Address (" + $theIPObj.location + ") are different. The location of the Cloud Service needs to match the location of the Public IP Address. Change the location of the Cloud Service to match the Public IP Address or change the resource group of the Cloud Service to try to resolve the issue."
}
$passMemory.Add("ipFound", $ipFound)
If ($ipFound){
# Existing Reserved (Static) IP In Use
if ($null -ne $theIPObj.IPConfiguration){
throw "The Public IP provided in the CSCFG: '" + $theIPObj.name + "' is currently in use by another resource."
}
# Existing Reserved (Static) IP Incorrect Sku
if ("Basic" -ne $theIPObj.Sku.Name){
throw "The Public IP provided in the CSCFG: '" + $theIPObj.name + "' must have a 'Basic' SKU."
}
# Existing Reserved (Static) IP Address Incorrect Version
if ("IPv4" -ne $theIPObj.PublicIPAddressVersion){
throw "The Public IP provided in the CSCFG: '" + $theIPObj.name + "' uses IPv6 and an IPv4 public IP address is needed."
}
# Existing Reserved (Static) IP Incorrect Allocation
if ("Static" -ne $theIPObj.PublicIPAllocationMethod){
throw "The Public IP provided in the CSCFG: '" + $theIPObj.name + "' uses a dynamic allocation and a static allocation is needed."
}
}
}
if ($params.ContainsKey("KeyVaultName")) {
# Keyvault in same location
$keyVaultsWithName = Get-AzKeyVault -vaultName $keyvaultname
$keyvaultFound = $false
foreach ($kv in $keyVaultsWithName) {
if ($kv.location.replace(" ","").tolower() -eq $location.replace(" ","").tolower()) {
$keyvaultFound = $true
$theKV = Get-AzKeyVault -vaultName $keyvaultname -resourceGroupName $kv.resourcegroupname
$passMemory.Add("KeyVault", $theKV)
}
}
If (-not $keyvaultFound){
throw "No KeyVault named '" + $keyvaultname + "' was found in " + $Location
}
# Keyvault has virtual machine deployment permission and user has list and get permissions
If (-not $theKV.EnabledForDeployment){
throw "The Key vault is not enabled for deployment. The Key Vault must have 'Azure Virtual Machines for deployment' access enabled. Please run the following cmdlets to enable access: Set-AzKeyVaultAccessPolicy -VaultName " + $keyvaultname + " -ResourceGroupName " +$resourcegroupname +" -EnabledForDeployment"
}
try {
$certsInKV = Get-AzKeyVaultCertificate -VaultName $keyvaultname -ErrorAction Stop
}
catch [Microsoft.Azure.KeyVault.Models.KeyVaultErrorException]{
$KVnoPolicy = $true
}
finally {
If ($KVnoPolicy){
throw "The certificates must have 'Get' and 'List' permissions enabled on the Key Vault. Please run the following cmdlets to enable access: Set-AzKeyVaultAccessPolicy -VaultName " + $keyvaultname +" -ResourceGroupName " + $theKV.resourcegroupname + " -UserPrincipalName 'user@domain.com' -PermissionsToCertificates create,get,list,delete "
}
}
# All certificates are found in the keyvault
$certsObjsFromKeyvault = @()
$certSecretList = @()
foreach ($cert in $CertsInKV) {
$certsObjsFromKeyvault = $certsObjsFromKeyvault + (Get-AzKeyVaultCertificate -VaultName $keyvaultname -name $cert.name)
}
foreach ($certFromFiles in $certList){
$thumbprintFound = $false
foreach ($certFromKV in $certsObjsFromKeyvault){
if ($certFromFiles.thumbprint -eq $certFromKV.thumbprint){
$thumbprintFound = $true
$certSecretList = $certSecretList + $certFromKV.SecretId
}
}
if (-not $thumbprintFound){
throw "The thumbprints specified in the CSCFG could not be found in the Key Vault. Add the missing certificates in Key Vault: '" + $keyvaultName + "'. Missing thumbprint: '" + $certFromFiles.name + " " + $certFromFiles.thumbprint +"'. To understand more about how to use KeyVault for certificates, please follow the documentation at https://aka.ms/cses-kv"
}
}
$passMemory.Add("certSecretList", $certSecretList)
}
if ($params.ContainsKey("StorageAccount")) {
$storAccs = Get-AzStorageAccount
if (-not ($storAccs.StorageAccountName.tolower()).contains($storageAccount.tolower())){
throw "The provided Storage Account: '" + $storageAccount + "' does not exist."
}
}
}