Skip to content

Instantly share code, notes, and snippets.

@DexterPOSH
Last active September 21, 2018 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DexterPOSH/ee175d96e5a4ce8fe657fe7e8b7c6028 to your computer and use it in GitHub Desktop.
Save DexterPOSH/ee175d96e5a4ce8fe657fe7e8b7c6028 to your computer and use it in GitHub Desktop.
test for a VMSKU deployment does not exceed VM SKU Family & Region Quota
Describe "VM Sku Quota validation. Region - $Location" -Tag "Azure","Cloud", "Quota" {
#TODO - wrap these as helper functions
# Credit - Irwin for refactoring the logic below.
$vmSKU = Get-AzureRmComputeResourceSku | Where-Object -FilterScript {
$PSItem.Locations.Contains($Location) -and
$PSitem.ResourceType -eq 'VirtualMachines' -and
$PSitem.Name -eq $VMSkuName
}
$vmResourceUsageQuota = Get-AzureRmVMUsage -Location $Location
$vmFamilySKULimit = $vmResourceUsageQuota.Where({$PSitem.Name.value -eq $vmSKU.Family})
$requiredvCPU = $vmSKU.Capabilities.Where({$_.Name -eq 'vCPUs'}).Value
$TotalRegionalvCPUsLimit = $VMResourceUsageQuota.Where({$PSitem.Name.value -eq 'Cores'})
Context "Compute Quota validation. Region - $Location" {
It "Should not exceed the VM Family SKU limit $($vmFamilySKULimit.Limit)" {
$newVCPUcount = $vmFamilySKULimit.CurrentValue + $requiredvCPU
$newVCPUcount |
Should -BeLessOrEqual $vmFamilySKULimit.Limit -Because "Deploying the VM with $requiredvCPU would exceed the Family SKU limit $($vmFamilySKULimit.Limit)"
}
It "Should not exceed the Total regional vCPUs limit $($TotalRegionalvCPUsLimit.Limit)" {
$newVCPUcount = $TotalRegionalvCPUsLimit.CurrentValue + $requiredvCPU
$newVCPUcount |
Should -BeLessThan $TotalRegionalvCPUsLimit.Limit -Because "Deploying the VM with $requiredvCPU would exceed the Total cores limit $($TotalRegionalvCPUsLimit.Limit)"
}
}
}
@irwins
Copy link

irwins commented Sep 20, 2018

I like it! So I looked into the familySize equation. Turns out some of them are quite funky...
Fun fact: the familysize is a property... ;-)

You can also update gist right?

@irwins
Copy link

irwins commented Sep 20, 2018

$Location = 'westus2'
$VMSkuName = 'Standard_D2s_v3'

Describe "VM Sku Quota validation. Region - $Location" -Tag "Azure","Cloud", "Quota" {
    
    #TODO - wrap these as helper functions
    $vmSKU = Get-AzureRmComputeResourceSku | Where-Object -FilterScript {
        $PSItem.Locations.Contains($Location) -and
        $PSitem.ResourceType -eq 'VirtualMachines' -and
        $PSitem.Name -eq $VMSkuName
    }

    $vmResourceUsageQuota = Get-AzureRmVMUsage -Location $Location
    
    $vmFamilySKULimit = $vmResourceUsageQuota.Where({$PSitem.Name.value -eq $vmSKU.Family})
    $requiredvCPU = $vmSKU.Capabilities.Where({$_.Name -eq 'vCPUs'}).Value
    $TotalRegionalvCPUsLimit = $VMResourceUsageQuota.Where({$PSitem.Name.value -eq 'Cores'})

    Context "Compute Quota validation. Region - $Location" {

        It "Should not exceed the VM Family SKU limit $($vmFamilySKULimit.Limit)" {
            $newVCPUcount = $vmFamilySKULimit.CurrentValue + $requiredvCPU
            $newVCPUcount |
                Should -BeLessOrEqual $vmFamilySKULimit.Limit -Because "Deploying the VM with $requiredvCPU would exceed the Family SKU limit $($vmFamilySKULimit.Limit)"
        }

        It "Should not exceed the Total regional vCPUs limit $($TotalRegionalvCPUsLimit.Limit)" {
            
            $newVCPUcount = $TotalRegionalvCPUsLimit.CurrentValue + $requiredvCPU
            $newVCPUcount |
                Should -BeLessThan $TotalRegionalvCPUsLimit.Limit -Because "Deploying the VM with $requiredvCPU would exceed the Total cores limit $($TotalRegionalvCPUsLimit.Limit)"
        }
    }
}

@DexterPOSH
Copy link
Author

DexterPOSH commented Sep 21, 2018

@irwins Thanks mate!
I think I did not look into the VMSKU for the family this simplifies the logic 👍
Editing the gist and giving you credits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment