Skip to content

Commit 2b2bb00

Browse files
Merge pull request #139 from Azure/environmentname
Adding EnvironmentName throughout tools
2 parents abd6bdd + 54ec35c commit 2b2bb00

14 files changed

Lines changed: 407 additions & 356 deletions

File tree

CanaryValidator/Canary.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ while ($runCount -le $NumberOfIterations)
180180
{
181181
if (-not (Get-AzureRmVMImage -Location $ResourceLocation -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Sku "2016-Datacenter-Core" -ErrorAction SilentlyContinue))
182182
{
183-
New-Server2016VMImage -ISOPath $WindowsISOPath -TenantId $TenantID -ArmEndpoint $AdminArmEndpoint -Version Core -AzureStackCredentials $ServiceAdminCredentials -CreateGalleryItem $false
183+
New-Server2016VMImage -ISOPath $WindowsISOPath -TenantId $TenantID -EnvironmentName $SvcAdminEnvironmentName -Version Core -AzureStackCredentials $ServiceAdminCredentials -CreateGalleryItem $false
184184
}
185185
}
186186
}
@@ -200,7 +200,7 @@ while ($runCount -le $NumberOfIterations)
200200
}
201201
New-Item -Path $CanaryCustomImageFolder -ItemType Directory
202202
$CustomVHDPath = CopyImage -ImagePath $LinuxImagePath -OutputFolder $CanaryCustomImageFolder
203-
Add-VMImage -publisher $linuxImagePublisher -offer $linuxImageOffer -sku $LinuxOSSku -version $linuxImageVersion -osDiskLocalPath $CustomVHDPath -osType Linux -tenantID $TenantID -azureStackCredentials $ServiceAdminCredentials -CreateGalleryItem $false -ArmEndpoint $AdminArmEndpoint
203+
Add-VMImage -publisher $linuxImagePublisher -offer $linuxImageOffer -sku $LinuxOSSku -version $linuxImageVersion -osDiskLocalPath $CustomVHDPath -osType Linux -tenantID $TenantID -azureStackCredentials $ServiceAdminCredentials -CreateGalleryItem $false -EnvironmentName $SvcAdminEnvironmentName
204204
Remove-Item $CanaryCustomImageFolder -Force -Recurse
205205
}
206206
}

ComputeAdmin/AzureStack.ComputeAdmin.psm1

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ Function Add-VMImage{
112112
[Parameter(Mandatory=$true, ParameterSetName='VMImageFromAzure')]
113113
[System.Management.Automation.PSCredential] $azureStackCredentials,
114114

115-
[Parameter(ParameterSetName='VMImageFromLocal')]
116-
[Parameter(ParameterSetName='VMImageFromAzure')]
117-
[string] $ArmEndpoint = 'https://api.local.azurestack.external',
115+
[Parameter(Mandatory=$true, ParameterSetName='VMImageFromLocal')]
116+
[Parameter(Mandatory=$true, ParameterSetName='VMImageFromAzure')]
117+
[string] $EnvironmentName,
118118

119119
[Parameter(ParameterSetName='VMImageFromLocal')]
120120
[Parameter(ParameterSetName='VMImageFromAzure')]
@@ -129,15 +129,7 @@ Function Add-VMImage{
129129
[bool] $CreateGalleryItem = $true
130130
)
131131

132-
if(!$ARMEndpoint.Contains('https://')){
133-
if($ARMEndpoint.Contains('http://')){
134-
$ARMEndpoint = $ARMEndpoint.Substring(7)
135-
$ARMEndpoint = 'https://' + $ARMEndpoint
136-
137-
}else{
138-
$ARMEndpoint = 'https://' + $ARMEndpoint
139-
}
140-
}
132+
$ARMEndpoint = GetARMEndpoint -EnvironmentName $EnvironmentName -ErrorAction Stop
141133

142134
if($CreateGalleryItem -eq $false -and $PSBoundParameters.ContainsKey('title')) {
143135
Write-Error -Message "The title parameter only applies to creating a gallery item." -ErrorAction Stop
@@ -151,11 +143,11 @@ Function Add-VMImage{
151143
$storageAccountName = "addvmimagestorageaccount"
152144
$containerName = "addvmimagecontainer"
153145

154-
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -ArmEndpoint $ArmEndpoint)
146+
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -EnvironmentName $EnvironmentName)
155147

156148
#pre validate if image is not already deployed
157149
$VMImageAlreadyAvailable = $false
158-
if ($(Get-VMImage -publisher $publisher -offer $offer -sku $sku -version $version -ArmEndpoint $ArmEndpoint -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $location -ErrorAction SilentlyContinue).Properties.ProvisioningState -eq 'Succeeded') {
150+
if ($(Get-VMImage -publisher $publisher -offer $offer -sku $sku -version $version -EnvironmentName $EnvironmentName -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $location -ErrorAction SilentlyContinue).Properties.ProvisioningState -eq 'Succeeded') {
159151
$VMImageAlreadyAvailable = $true
160152
Write-Verbose -Message ('VM Image with publisher "{0}", offer "{1}", sku "{2}", version "{3}" already is present.' -f $publisher,$offer,$sku,$version) -Verbose -ErrorAction Stop
161153
}
@@ -273,7 +265,7 @@ Function Add-VMImage{
273265
Invoke-RestMethod -Method PUT -Uri $uri -Body $RequestBody -ContentType 'application/json' -Headers $Headers
274266
}
275267

276-
$platformImage = Get-VMImage -publisher $publisher -offer $offer -sku $sku -version $version -ArmEndpoint $ArmEndpoint -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $location
268+
$platformImage = Get-VMImage -publisher $publisher -offer $offer -sku $sku -version $version -EnvironmentName $EnvironmentName -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $location
277269

278270
$downloadingStatusCheckCount = 0
279271
while($platformImage.Properties.ProvisioningState -ne 'Succeeded') {
@@ -290,7 +282,7 @@ Function Add-VMImage{
290282
$downloadingStatusCheckCount++
291283
if($downloadingStatusCheckCount % 30 -eq 0){
292284
Write-Verbose -Message "Obtaining refreshed token..."
293-
$subscription, $Headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -ArmEndpoint $ArmEndpoint)
285+
$subscription, $Headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -EnvironmentName $EnvironmentName)
294286
}
295287
$platformImage = Invoke-RestMethod -Method GET -Uri $uri -ContentType 'application/json' -Headers $Headers
296288
}
@@ -348,23 +340,17 @@ Function Remove-VMImage{
348340

349341
[switch] $KeepMarketplaceItem,
350342

351-
[string] $ArmEndpoint = 'https://api.local.azurestack.external'
343+
[Parameter(Mandatory=$true)]
344+
[string] $EnvironmentName
352345

353346
)
354347

355-
if(!$ARMEndpoint.Contains('https://')){
356-
if($ARMEndpoint.Contains('http://')){
357-
$ARMEndpoint = $ARMEndpoint.Substring(7)
358-
$ARMEndpoint = 'https://' + $ARMEndpoint
359-
}else{
360-
$ARMEndpoint = 'https://' + $ARMEndpoint
361-
}
362-
}
348+
$ARMEndpoint = GetARMEndpoint -EnvironmentName $EnvironmentName -ErrorAction Stop
363349

364-
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -ArmEndpoint $ArmEndpoint)
350+
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -EnvironmentName $EnvironmentName)
365351

366352
$VMImageExists = $false
367-
if (Get-VMImage -publisher $publisher -offer $offer -sku $sku -version $version -ArmEndpoint $ArmEndpoint -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $location -ErrorAction SilentlyContinue) {
353+
if (Get-VMImage -publisher $publisher -offer $offer -sku $sku -version $version -EnvironmentName $EnvironmentName -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $location -ErrorAction SilentlyContinue) {
368354
Write-Verbose "VM Image is present in Azure Stack - continuing to remove" -Verbose
369355
$VMImageExists = $true
370356
}
@@ -423,8 +409,8 @@ function New-Server2016VMImage {
423409
[Parameter(ParameterSetName = 'ManualCUPath')]
424410
[string] $CUPath,
425411

426-
[Parameter()]
427-
[string] $ArmEndpoint = 'https://api.local.azurestack.external',
412+
[Parameter(Mandatory)]
413+
[string] $EnvironmentName,
428414

429415
[Parameter()]
430416
[string] $VHDSizeInMB = 40960,
@@ -537,7 +523,7 @@ function New-Server2016VMImage {
537523

538524
Write-Verbose -Message "Checking authorization against your Azure Stack environment" -Verbose
539525

540-
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -ArmEndpoint $ArmEndpoint -ErrorAction Stop)
526+
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -EnvironmentName $EnvironmentName -ErrorAction Stop)
541527

542528
Write-Verbose -Message "Authorization verified" -Verbose
543529

@@ -604,7 +590,7 @@ function New-Server2016VMImage {
604590
osType = 'Windows'
605591
tenantID = $tenantID
606592
azureStackCredentials = $AzureStackCredentials
607-
ArmEndpoint = $ArmEndpoint
593+
EnvironmentName = $EnvironmentName
608594
location = $location
609595
}
610596

@@ -614,7 +600,7 @@ function New-Server2016VMImage {
614600

615601
#Pre-validate that the VM Image is not already available
616602
$VMImageAlreadyAvailable = $false
617-
if ($(Get-VMImage -publisher $PublishArguments.publisher -offer $PublishArguments.offer -sku $sku -version $PublishArguments.version -ArmEndpoint $ArmEndpoint -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $PublishArguments.location -ErrorAction SilentlyContinue).Properties.ProvisioningState -eq 'Succeeded') {
603+
if ($(Get-VMImage -publisher $PublishArguments.publisher -offer $PublishArguments.offer -sku $sku -version $PublishArguments.version -EnvironmentName $EnvironmentName -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $PublishArguments.location -ErrorAction SilentlyContinue).Properties.ProvisioningState -eq 'Succeeded') {
618604
$VMImageAlreadyAvailable = $true
619605
Write-Verbose -Message ('VM Image with publisher "{0}", offer "{1}", sku "{2}", version "{3}" already is present.' -f $publisher,$offer,$sku,$version) -Verbose -ErrorAction Stop
620606
}
@@ -647,7 +633,7 @@ function New-Server2016VMImage {
647633

648634
#Pre-validate that the VM Image is not already available
649635
$VMImageAlreadyAvailable = $false
650-
if ($(Get-VMImage -publisher $PublishArguments.publisher -offer $PublishArguments.offer -sku $sku -version $PublishArguments.version -ArmEndpoint $ArmEndpoint -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $PublishArguments.location -ErrorAction SilentlyContinue).Properties.ProvisioningState -eq 'Succeeded') {
636+
if ($(Get-VMImage -publisher $PublishArguments.publisher -offer $PublishArguments.offer -sku $sku -version $PublishArguments.version -EnvironmentName $EnvironmentName -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $PublishArguments.location -ErrorAction SilentlyContinue).Properties.ProvisioningState -eq 'Succeeded') {
651637
$VMImageAlreadyAvailable = $true
652638
Write-Verbose -Message ('VM Image with publisher "{0}", offer "{1}", sku "{2}", version "{3}" already is present.' -f $publisher,$offer,$sku,$version) -Verbose -ErrorAction Stop
653639
}
@@ -801,22 +787,14 @@ Function Get-VMImage{
801787

802788
[System.Management.Automation.PSCredential] $azureStackCredentials,
803789

804-
[string] $ArmEndpoint = 'https://api.local.azurestack.external'
790+
[Parameter(Mandatory=$true)]
791+
[string] $EnvironmentName
805792

806793
)
807794

808-
if(!$ARMEndpoint.Contains('https://')){
809-
if($ARMEndpoint.Contains('http://')){
810-
$ARMEndpoint = $ARMEndpoint.Substring(7)
811-
$ARMEndpoint = 'https://' + $ARMEndpoint
812-
}else{
813-
$ARMEndpoint = 'https://' + $ARMEndpoint
814-
}
815-
}
795+
$ARMEndpoint = GetARMEndpoint -EnvironmentName $EnvironmentName -ErrorAction Stop
816796

817-
$ArmEndpoint = $ArmEndpoint.TrimEnd("/")
818-
819-
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -ArmEndpoint $ArmEndpoint)
797+
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -EnvironmentName $EnvironmentName)
820798

821799
$uri = $armEndpoint + '/subscriptions/' + $subscription + '/providers/Microsoft.Compute.Admin/locations/' + $location + '/artifactTypes/platformImage/publishers/' + $publisher
822800
$uri = $uri + '/offers/' + $offer + '/skus/' + $sku + '/versions/' + $version + '?api-version=2015-12-01-preview'
@@ -881,16 +859,19 @@ Function Add-VMExtension{
881859
[Parameter(Mandatory=$true, ParameterSetName='VMExtesionFromAzure')]
882860
[System.Management.Automation.PSCredential] $azureStackCredentials,
883861

884-
[Parameter(ParameterSetName='VMExtensionFromLocal')]
885-
[Parameter(ParameterSetName='VMExtesionFromAzure')]
886-
[string] $ArmEndpoint = 'https://adminmanagement.local.azurestack.external'
862+
[Parameter(Mandatory=$true, ParameterSetName='VMExtensionFromLocal')]
863+
[Parameter(Mandatory=$true, ParameterSetName='VMExtesionFromAzure')]
864+
[string] $EnvironmentName
865+
887866
)
888867

868+
$ARMEndpoint = GetARMEndpoint -EnvironmentName $EnvironmentName -ErrorAction Stop
869+
889870
$resourceGroupName = "addvmextresourcegroup"
890871
$storageAccountName = "addvmextstorageaccount"
891872
$containerName = "addvmextensioncontainer"
892873

893-
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -ArmEndpoint $ArmEndpoint)
874+
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -EnvironmentName $EnvironmentName)
894875

895876
#potentially the RG was not cleaned up when exception happened in previous run. Test for exist
896877
if (-not (Get-AzureRmResourceGroup -Name $resourceGroupName -Location $location -ErrorAction SilentlyContinue)) {
@@ -995,10 +976,14 @@ Function Remove-VMExtension{
995976
[Parameter(Mandatory=$true)]
996977
[System.Management.Automation.PSCredential] $azureStackCredentials,
997978

998-
[string] $ArmEndpoint = 'https://adminmanagement.local.azurestack.external'
979+
[Parameter(Mandatory=$true)]
980+
[string] $EnvironmentName
981+
999982
)
1000983

1001-
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -ArmEndpoint $ArmEndpoint)
984+
$ARMEndpoint = GetARMEndpoint -EnvironmentName $EnvironmentName -ErrorAction Stop
985+
986+
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -EnvironmentName $EnvironmentName)
1002987

1003988
$ArmEndpoint = $ArmEndpoint.TrimEnd("/")
1004989
$uri = $armEndpoint + '/subscriptions/' + $subscription + '/providers/Microsoft.Compute.Admin/locations/' + $location + '/artifactTypes/VMExtension/publishers/' + $publisher
@@ -1014,3 +999,21 @@ Function Remove-VMExtension{
1014999
}
10151000
}
10161001

1002+
Function GetARMEndpoint{
1003+
param(
1004+
# Azure Stack environment name
1005+
[Parameter(Mandatory=$true)]
1006+
[string] $EnvironmentName
1007+
1008+
)
1009+
1010+
$armEnv = Get-AzureRmEnvironment -Name $EnvironmentName
1011+
if($armEnv -ne $null) {
1012+
$ARMEndpoint = $armEnv.ResourceManagerUrl
1013+
}
1014+
else {
1015+
Write-Error "The Azure Stack environment with the name $EnvironmentName does not exist. Create one with Add-AzureStackAzureRmEnvironment." -ErrorAction Stop
1016+
}
1017+
1018+
$ARMEndpoint
1019+
}

0 commit comments

Comments
 (0)