Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions samples/ClassroomLabs/Modules/Library/Az.LabServices.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,29 @@ 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(
Expand Down Expand Up @@ -1709,6 +1732,7 @@ Export-ModuleMember -Function Get-AzLabAccount,
Get-AzLabForVm,
New-AzLabAccountSharedGallery,
Remove-AzLabAccountSharedGallery,
Get-AzLabAccountSharedGallery,
Get-AzLabAccountPricingAndAvailability,
Stop-AzLabTemplateVm,
Start-AzLabTemplateVm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,29 +338,55 @@ function Publish-Labs {
$ConfigObject
)

$lacs = $ConfigObject | Select-Object -Property ResourceGroupName, LabAccountName -Unique
$lacs = $ConfigObject | Select-Object -Property ResourceGroupName, LabAccountName, SharedGalleryResourceGroupName, SharedGalleryName -Unique
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should do it this way... This means that the bulk lab creation scripts can only use a shared image gallery from the current subscription. We should either add a "SharedGallerySubscriptionId" or we should consider just having a single field with the full shared image gallery resource ID. (I like the single field idea, because it could just be an empty column or missing entirely if the user doesn't need it - vs needing to do validation across 3 columns to make sure there isn't a missing field...


Write-Host "Operating on the following Lab Accounts:"
Write-Host $lacs

$block = {
param($path, $ResourceGroupName, $LabAccountName)
param($path, $ResourceGroupName, $LabAccountName, $SharedGalleryResourceGroupName, $SharedGalleryName)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

$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 | Out-Null
Write-Host "$LabAccountName lab account created."
}
else {
Write-Host "$LabAccountName lab account found - skipping create."
}

if ($SharedGalleryResourceGroupName -ne $null && $SharedGalleryName -ne $null){

$gallery = $labAccount | Get-AzLabAccountSharedGallery
if ($gallery -ne $null) {
Write-Host "$LabAccountName lab account already has attached gallery $gallery."
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's the wrong gallery that's attached to the lab account, should we change it to what's in the CSV file? I think currently we don't 'fix' settings in other places - but I'm wondering what the user's expectation would be. Perhaps we should consider this as a "Upsert" (update/insert) operation when running the scripts for any changes? The downside is that changing the SIG if there are labs & student VMs means that users couldn't reset the student VMs from SIG anymore...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was hesitant changing it if it's already set for the reasons you mentioned. I was thinking that if there are failures of some sort (for example, 503 which i've hit occassionally), which we could also include when there is an attached SIG already, that we could output this in a .csv...similar to what you're planning. When you're done with your code, we can likely reuse it here too.

}
else {
$gallery = Get-AzGallery -ResourceGroupName $SharedGalleryResourceGroupName -Name $SharedGalleryName

if ($gallery -ne $null) {
Write-Host "$SharedGalleryName shared gallery found."
New-AzLabAccountSharedGallery -LabAccount $labAccount -SharedGallery $gallery
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to do this by Resource ID instead of by object... The reason is because if the SIG is in a different subscription- we don't want to manually swap subscriptions back and forth to get the object if we don't have to (and I think the API just needs the SIG ID)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - I've changed it to look by resource id...but, i am still having issues with it working to find a SIG successfully across subs. I've tested it in my subs, but I have different tentants involved. Also, tried it with Roger where we used our Microsoft subs, and still couldn't get it to work. I'd like to talk to you more to see what I'm missing.

Write-Host "$SharedGalleryName shared gallery attached."
}
else {
Write-Host "$SharedGalleryName shared gallery not found - skipping attach."
}
}
}
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
$jobs += Start-ThreadJob -ScriptBlock $block -ArgumentList $PSScriptRoot, $_.ResourceGroupName, $_.LabAccountName, $_.SharedGalleryResourceGroupName, $_.SharedGalleryName -Name $_.LabAccountName -ThrottleLimit $ThrottleLimit
}

$hours = 1
Expand Down