diff --git a/samples/ClassroomLabs/Modules/Library/Az.LabServices.psm1 b/samples/ClassroomLabs/Modules/Library/Az.LabServices.psm1 index acbdc6498..4a301faf8 100644 --- a/samples/ClassroomLabs/Modules/Library/Az.LabServices.psm1 +++ b/samples/ClassroomLabs/Modules/Library/Az.LabServices.psm1 @@ -996,20 +996,107 @@ function Publish-AzLab { end { } } +function Get-AzLabAccountSharedGallery { + [CmdletBinding()] + param( + [parameter(Mandatory = $true, HelpMessage = "Lab Account to get attached Shared Gallery.", ValueFromPipeline = $true)] + [ValidateNotNullOrEmpty()] + $LabAccount + ) + + begin { . BeginPreamble } + process { + try { + foreach ($la in $LabAccount) { + $uri = (ConvertToUri -resource $la) + "/SharedGalleries/" + return InvokeRest -Uri $uri -Method 'Get' + } + } + catch { + Write-Error -ErrorRecord $_ -EA $callerEA + } + } + end { } +} + function Get-AzLabAccountSharedImage { [CmdletBinding()] param( [parameter(Mandatory = $true, HelpMessage = "Lab Account to get shared images from", ValueFromPipeline = $true)] [ValidateNotNullOrEmpty()] - $LabAccount + $LabAccount, + + [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = "Are the images enabled? Enabled = Yes, and Disabled = No")] + [ValidateSet('Enabled', 'Disabled', 'All')] + [string] $EnableState = "Enabled" ) begin { . BeginPreamble } process { try { + foreach ($la in $LabAccount) { $uri = (ConvertToUri -resource $la) + "/SharedImages" - return InvokeRest -Uri $uri -Method 'Get' | Where-Object { $_.properties.EnableState -eq 'Enabled' } + + if ($EnableState -eq "All") { + $response = InvokeRest -Uri $uri -Method 'Get' + } + else { + $response = InvokeRest -Uri $uri -Method 'Get' | Where-Object { $_.properties.EnableState -eq $EnableState } + } + + return $response + } + } + catch { + Write-Error -ErrorRecord $_ -EA $callerEA + } + } + end { } +} + +function Set-AzLabAccountSharedImage { + [CmdletBinding()] + param( + [parameter(Mandatory = $true, HelpMessage = "Shared image to update.", ValueFromPipeline = $true)] + [ValidateNotNullOrEmpty()] + $SharedImage, + + [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = "Should this image be enabled? Enabled = Yes, and Disabled = No")] + [ValidateSet('Enabled', 'Disabled')] + [string] $EnableState = "Enabled" + ) + + begin { . BeginPreamble } + process { + try { + + foreach ($image in $SharedImage) { + $ResourceGroupName = $image.id.split('/')[4] + $LabAccountName = $image.id.split('/')[8] + $LabAccount = Get-AzLabAccount -ResourceGroupName $ResourceGroupName -LabAccountName $LabAccountName + + Write-Verbose "Image to update:\n$($image | ConvertTo-Json)" + + $body = @{ + id = $image.id + name = $image.name + properties = + @{ + EnableState = $EnableState + sharedGalleryId = $image.properties.sharedGalleryId + osType = $image.properties.osType + imageType = $image.properties.imageType + displayName = $image.properties.displayName + definitionName = $image.properties.definitionName + } + } | ConvertTo-Json + + $uri = (ConvertToUri -resource $LabAccount) + "/SharedImages/"+ $image.name + + $updatedImage = InvokeRest -Uri $uri -Method 'PUT' -Body ($body) + Write-Verbose "Updated image\n$($updatedImage | ConvertTo-Json)" + return WaitProvisioning -uri $uri -delaySec 60 -retryCount 120 } } catch { @@ -1686,6 +1773,7 @@ Export-ModuleMember -Function Get-AzLabAccount, Get-AzLab, New-AzLab, Get-AzLabAccountSharedImage, + Set-AzLabAccountSharedImage, Get-AzLabAccountGalleryImage, Remove-AzLab, Get-AzLabTemplateVM, @@ -1709,6 +1797,7 @@ Export-ModuleMember -Function Get-AzLabAccount, Get-AzLabForVm, New-AzLabAccountSharedGallery, Remove-AzLabAccountSharedGallery, + Get-AzLabAccountSharedGallery, Get-AzLabAccountPricingAndAvailability, Stop-AzLabTemplateVm, Start-AzLabTemplateVm, diff --git a/samples/ClassroomLabs/Modules/Library/Tools/LabCreationLibrary.psm1 b/samples/ClassroomLabs/Modules/Library/Tools/LabCreationLibrary.psm1 index c32ef5558..379b5f73d 100644 --- a/samples/ClassroomLabs/Modules/Library/Tools/LabCreationLibrary.psm1 +++ b/samples/ClassroomLabs/Modules/Library/Tools/LabCreationLibrary.psm1 @@ -338,12 +338,22 @@ function Publish-Labs { $ConfigObject ) - $lacs = $ConfigObject | Select-Object -Property ResourceGroupName, LabAccountName -Unique + $lacs = $ConfigObject | Select-Object -Property ResourceGroupName, LabAccountName, SharedGalleryResourceId, EnableSharedGalleryImages | Sort-Object -Property ResourceGroupName, LabAccountName + $lacNames = $lacs | Select-Object -Property ResourceGroupName, LabAccountName -Unique + + # Get the first unique resource group\lab account and attempt to attach a shared gallery if one is specified in the csv. If the resource group\lab account exists + # more than once in the csv, we only attempt to attach to the first one and skip the rest. + $lacsToCreate = @() + foreach ($lacName in $lacNames){ + $lac = $lacs | Where-Object {$_.ResourceGroupName -eq $lacName.ResourceGroupName -and $_.LabAccountName -eq $lacName.LabAccountName} | Select-Object -First 1 + $lacsToCreate += $lac + } + Write-Host "Operating on the following Lab Accounts:" - Write-Host $lacs + Write-Host $lacsToCreate $block = { - param($path, $ResourceGroupName, $LabAccountName) + param($path, $ResourceGroupName, $LabAccountName, $SharedGalleryResourceId, $EnableSharedGalleryImages) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' @@ -351,16 +361,55 @@ function Publish-Labs { $modulePath = Join-Path $path '..\Az.LabServices.psm1' Import-Module $modulePath - if ((Get-AzLabAccount -ResourceGroupName $ResourceGroupName -LabAccountName $LabAccountName) -eq $null ){ - New-AzLabAccount -ResourceGroupName $ResourceGroupName -LabAccountName $LabAccountName | Out-Null + $labAccount = Get-AzLabAccount -ResourceGroupName $ResourceGroupName -LabAccountName $LabAccountName + + if ($labAccount -eq $null ){ + $labAccount = New-AzLabAccount -ResourceGroupName $ResourceGroupName -LabAccountName $LabAccountName + Write-Host "$LabAccountName lab account created." + } + else { + Write-Host "$LabAccountName lab account found - skipping create." + } + + if ($SharedGalleryResourceId -ne $null){ + + $gallery = $labAccount | Get-AzLabAccountSharedGallery + if ($gallery -ne $null) { + Write-Host "$LabAccountName lab account already has attached gallery $gallery." + } + else { + $gallery = Get-AzGallery -ResourceId $SharedGalleryResourceId + + if ($gallery -ne $null) { + Write-Host "$SharedGalleryResourceId shared gallery found." + New-AzLabAccountSharedGallery -LabAccount $labAccount -SharedGallery $gallery + Write-Host "$SharedGalleryResourceId shared gallery attached." + } + else { + Write-Host "$SharedGalleryResourceId shared gallery not found - skipping attach." + } + } + + Write-Host "Enabling images for lab account: $labAccount" + if ($EnableSharedGalleryImages -ne $null) + { + $imageNames = $EnableSharedGalleryImages.Split(',') + } + + Write-Host "Images to enable: $imageNames" + $images = $labAccount | Get-AzLabAccountSharedImage -EnableState All + foreach ($imageName in $imageNames) + { + $image = $images | Where-Object { $_.name -like $imageName } | Set-AzLabAccountSharedImage -EnableState Enabled + Write-Host "Image enabled: $image" + } } - Write-Host "$LabAccountName lab account created or found." } Write-Host "Starting lab accounts creation in parallel. Can take a while." $jobs = @() - $lacs | ForEach-Object { - $jobs += Start-ThreadJob -ScriptBlock $block -ArgumentList $PSScriptRoot, $_.ResourceGroupName, $_.LabAccountName -Name $_.LabAccountName -ThrottleLimit $ThrottleLimit + $lacsToCreate | ForEach-Object { + $jobs += Start-ThreadJob -ScriptBlock $block -ArgumentList $PSScriptRoot, $_.ResourceGroupName, $_.LabAccountName, $_.SharedGalleryResourceId, $_.EnableSharedGalleryImages -Name $_.LabAccountName -ThrottleLimit $ThrottleLimit } $hours = 1 @@ -419,7 +468,6 @@ function Publish-Labs { } } - # Needs to create resources in this order, aka parallelize in these three groups, otherwise we get contentions: # i.e. different jobs trying to create the same common resource (RG or lab account) New-ResourceGroups -ConfigObject $aggregateLabs