Skip to content
This repository has been archived by the owner on Oct 9, 2019. It is now read-only.

Added Quota functionality #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ C:\>Get-WAPCloudService -Name DCs | Get-WAPVMRoleVM
#get additional information for VM Role deployed VM instance
Get-WAPCloudService -Name DCs | Get-WAPVMroleVM -VMMEnhanced

#get quotas of your subscriptions
Get-WAPSubscriptionQuota

#Confirm whether a specific request will meet a quota.
Get-WAPSubscription -Name "MySubscription" | Get-WAPVMRoleQuotaRequest -Size "Large" -NumberOfNodes 4

#connect to VM Role VM instances over RDP
C:\>Get-WAPCloudService -Name DCs | Get-WAPVMRoleVM | Connect-WAPVMRDP

Expand Down
232 changes: 217 additions & 15 deletions WapTenantPublicAPI.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ function IgnoreSSL {
$Provider = New-Object -TypeName Microsoft.CSharp.CSharpCodeProvider
$null = $Provider.CreateCompiler()
$Params = New-Object -TypeName System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable = $False
$Params.GenerateInMemory = $True
$Params.IncludeDebugInformation = $False
$Params.GenerateExecutable = $false
$Params.GenerateInMemory = $true
$Params.IncludeDebugInformation = $false
$Params.ReferencedAssemblies.Add('System.DLL') > $null
$TASource=@'
namespace Local.ToolkitExtensions.Net.CertificatePolicy
Expand Down Expand Up @@ -53,7 +53,7 @@ function TestJWTClaimNotExpired {
if ($Token.split('.').count -ne 3) {
throw 'Invalid token passed, run Get-WAPToken to fetch a new one'
}
$TokenData = $token.Split('.')[1] | ForEach-Object -Process {
$TokenData = $token.Split('.')[1] | foreach-Object -Process {
Copy link
Owner

Choose a reason for hiding this comment

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

ForEach-Object is a cmdlet. Should be PasCal case. I guess you did a search replace?

$data = $_ -as [String]
$data = $data.Replace('-', '+').Replace('_', '/')
switch ($data.Length % 4) {
Expand Down Expand Up @@ -549,6 +549,208 @@ function GetWAPSubscriptionQuota {

}

function Get-WAPSubscriptionQuota {
<#
.SYNOPSIS
Retrieves a VM Role Quota information object (WAP.Quota).

.PARAMETER Subscription
The Subscription(s) you wish to collect quota information for.

.EXAMPLE
PS C:\>$URL = 'https://publictenantapi.mydomain.com'
PS C:\>$creds = Get-Credential
PS C:\>Get-WAPToken -Credential $creds -URL 'https://sts.adfs.com' -ADFS
PS C:\>Connect-WAPAPI -URL $URL
PS C:\>$Sub = Get-WAPSubscription -Name MySubscription
PS C:\>Get-WAPSubscriptionQuota -Subscription $Sub
#>
[CmdletBinding()]
param(
[Parameter(
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[System.Management.Automation.PSTypeName('WAP.Subscription')]
[PSCustomObject] $Subscription = (Get-WAPSubscription)
Copy link
Owner

Choose a reason for hiding this comment

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

You can remove the Datatype for this. So you'll end up with:

[Parameter(Attributes)]
[System.Management.Automation.PSTypeName('WAP.Subscription')] $Subscription

)
process{

foreach ( $Sub in $Subscription ) {
Select-WAPSubscription -Subscription $Sub

$VMs = Get-WAPVM
$Cores = $Vms.CpuCount | Measure-Object -Sum
$Memory = $Vms.Memory | Measure-Object -Sum
$Services = $Sub.Services | Select BaseQuotaSettings

foreach ( $Service in $Services ) {

$BaseQuotaSettings = $Service.BaseQuotaSettings

foreach ( $Setting in $BaseQuotaSettings ) {

if ( $Setting.Key -eq "Clouds" ) {

[xml]$Data = $Setting.Value
$Output = $Data.Clouds.Cloud.Quota

[pscustomobject] $Object = @{
SubscriptionName = $Sub.SubscriptionName;
CurrentVms = $Cores.Count;
CurrentCores = $Cores.Sum;
CurrentMemory = $Memory.Sum;
QuotaRoleVMCount = $Output.RoleVMCount;
QuotaMemberVMCount = $Output.MemberVMCount;
QuotaRoleCPUCount = $Output.RoleCPUCount;
QuotaMemberCPUCount = $Output.MemberCPUCount;
QuotaMemberMemory = $Output.MemberMemoryMB;
QuotaRoleMemory = $Output.RoleMemoryMB;
QuotaRoleStorageGB = $Output.RoleStorageGB;
QuotaMemberStorageGB = $Output.MemberStorageGB
}

$Object.PSObject.TypeNames.Insert( 0,'WAP.QUOTA' )

Write-Output $Object
}
}
}
}
}
}

function Get-WAPVMRoleQuotaRequest {
<#
.SYNOPSIS
Gets a WAP.QuotaRequest object - determining whether or not a deployment will succeed.

.PARAMETER VMRoleSizeProfile
VM Size object to use as the basis.. Acquired via Get-WAPVMRoleVMSize.

.PARAMETER Size
VM Size name. Extra Small, Large, A7 etc.

.PARAMETER Subscription
WAP.SUbscription object to check. If left blank, returns the quota request for all of them.

.EXAMPLE
PS C:\>$URL = 'https://publictenantapi.mydomain.com'
PS C:\>$creds = Get-Credential
PS C:\>Get-WAPToken -Credential $creds -URL 'https://sts.adfs.com' -ADFS
PS C:\>Connect-WAPAPI -URL $URL
PS C:\>$Sub = Get-WAPSubscription -Name 'MySubscription'
PS C:\>Get-WAPVMRoleQuotaRequest -Subscription $Sub -Size Large
OR
PS C:\>Get-WAPVMRoleQuotaRequest -Subscription $Sub -VMRoleSizeProfile (Get-WAPVMRoleVMSize -Name A7)

#>
[CmdletBinding( DefaultParameterSetName="Size-Name" )]
param(
[Parameter(
Mandatory,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ParameterSetName = "Size-Object"
Copy link
Owner

Choose a reason for hiding this comment

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

Some comments as above. If you use string literals (no variable expansion), please use single quotes.

)]
[System.Management.Automation.PSTypeName('WAP.VMRoleSizeProfile')]
[PSCustomObject] $VMRoleSizeProfile,

[Parameter(
Mandatory,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ParameterSetName = "Size-Name"
)]
[string] $Size,

[Parameter(
Mandatory,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[System.Management.Automation.PSTypeName('WAP.Subscription')]
[PSCustomObject] $Subscription = (Get-WAPSubscription),

[Parameter(
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[int] $NumberOfNodes = 1
)

process{
foreach ( $Sub in $Subscription ) {

Select-WAPSubscription $Sub

if ($Size) {
$VMRoleSizeProfile = Get-WAPVMRoleVMSize -Name $Size
}

if ( $VMRoleSizeProfile ) {
$Size = $VMRoleSizeProfile.Name
}

$Limits = Get-WAPSubscriptionQuota -Subscription $Sub

[pscustomobject] $RequestObject = @{
SubscriptionName=$Limits.SubscriptionName;
Size=$Size;
NumberOfNodes=$NumberOfNodes
}

if ( $Limits.QuotaRoleMemory ) {

[int] $SizeMem = $VMRoleSizeProfile.MemoryInMB
[int] $CurrentMemory = $Limits.CurrentMemory
[int] $QuotaMemory = $Limits.QuotaRoleMemory
[int] $ExtraMem = $SizeMem * $NumberOfNodes
Write-Verbose "MemInRequest:$ExtraMem"
$RequestedMemory = $CurrentMemory + ($SizeMem * $NumberOfNodes)

Add-Member -InputObject $RequestObject -MemberType NoteProperty -Name MemoryRequired -Value $RequestedMemory -Force
Add-Member -InputObject $RequestObject -MemberType NoteProperty -Name QuotaMemory -Value $QuotaMemory -Force
Add-Member -InputObject $RequestObject -MemberType NoteProperty -Name MemoryCheck -Value ( $QuotaMemory -ge $RequestedMemory ) -Force

}

if ( $Limits.QuotaRoleCPUCount ) {

[int] $SizeCores = $VMRoleSizeProfile.CpuCount
[int] $CurrentCores = $Limits.CurrentCores
[int] $QuotaCores = $Limits.QuotaRoleCPUCount
[int] $ExtraCores = $SizeCores * $NumberOfNodes
Write-Verbose "CoresInRequest:$ExtraCores"
$RequestedCores = $CurrentCores + ($SizeCores * $NumberOfNodes)

Add-Member -InputObject $RequestObject -MemberType NoteProperty -Name CoresRequired -Value $RequestedCores -Force
Add-Member -InputObject $RequestObject -MemberType NoteProperty -Name QuotaCores -Value $QuotaCores -Force
Add-Member -InputObject $RequestObject -MemberType NoteProperty -Name CoresCheck -Value ( $QuotaCores -ge $RequestedCores ) -Force

}

if ( $Limits.QuotaMemberVMCount ) {

[int] $CurrentVMs = $Limits.CurrentVms
[int] $QuotaVMs = $Limits.QuotaRoleVMCount
Write-Verbose "VMsInRequest:$NumberOfNodes"
$RequestedVMs = $CurrentVMs + $NumberOfNodes

Add-Member -InputObject $RequestObject -MemberType NoteProperty -Name RequestedVMs -Value $RequestedVMs -force
Add-Member -InputObject $RequestObject -MemberType NoteProperty -Name QuotaVMs -Value $QuotaVMs -force
Add-Member -InputObject $RequestObject -MemberType NoteProperty -Name VMCheck -Value ( $QuotaVMs -ge $RequestedVMs ) -force

}
$Checks = @($RequestObject.CoresCheck,$RequestObject.MemoryCheck,$RequestObject.VMCheck)

Add-Member -InputObject $RequestObject -MemberType NoteProperty -Name WithinQuota -Value ( @( $Checks ) -notcontains $false )-force
$RequestObject.PSObject.TypeNames.Insert( 0,'WAP.QuotaRequest' )
Write-Output $RequestObject
}
}
}

function Get-WAPGalleryVMRole {
<#
.SYNOPSIS
Expand Down Expand Up @@ -709,8 +911,8 @@ function Get-WAPVMRoleOSDisk {
Write-Verbose -Message "Constructed VHD URI: $URI"

$Sections = $ViewDef.ViewDefinition.Sections
$Categories = $Sections | ForEach-Object -Process {$_.Categories}
$OSDiskParam = $Categories | ForEach-Object -Process {$_.Parameters} | Where-Object -FilterScript {$_.Type -eq 'OSVirtualHardDisk'}
$Categories = $Sections | foreach-Object -Process {$_.Categories}
$OSDiskParam = $Categories | foreach-Object -Process {$_.Parameters} | Where-Object -FilterScript {$_.Type -eq 'OSVirtualHardDisk'}

$Images = Invoke-RestMethod -Uri $URI -Headers $Headers -Method Get
Write-Verbose "Images are : $($Images.Value)"
Expand Down Expand Up @@ -1715,8 +1917,8 @@ function New-WAPVMRoleParameterObject {
}
if ($PSCmdlet.ShouldProcess($null,'Generating new ParameterObject')) {
$Sections = $VMRole.ViewDef.ViewDefinition.Sections
$Categories = $Sections | ForEach-Object -Process {$_.Categories}
$ViewDefParams = $Categories | ForEach-Object -Process {$_.Parameters}
$Categories = $Sections | foreach-Object -Process {$_.Categories}
$ViewDefParams = $Categories | foreach-Object -Process {$_.Parameters}
$Output = [pscustomobject]@{}
foreach ($P in $ViewDefParams) {
$p | Out-String | Write-Verbose
Expand All @@ -1730,7 +1932,7 @@ function New-WAPVMRoleParameterObject {
}
$values = $values.TrimEnd(',')
if ($P.DefaultValue) {
if(($result = Read-Host -Prompt "Press enter to accept default value $($P.DefaultValue) for $($P.Name). Valid entries: $values") -eq ''){
if (($result = Read-Host -Prompt "Press enter to accept default value $($P.DefaultValue) for $($P.Name). Valid entries: $values") -eq ''){
Add-Member -InputObject $Output -MemberType NoteProperty -Name $P.Name -Value $P.DefaultValue -Force
} else {
do {
Expand Down Expand Up @@ -2034,7 +2236,7 @@ function New-WAPVMRoleDeployment {
throw 'Object bound to ParameterObject parameter is of the wrong type'
}

$ParameterObject | Get-Member -MemberType Properties | ForEach-Object -Process {
$ParameterObject | Get-Member -MemberType Properties | foreach-Object -Process {
if ($null -eq $ParameterObject.($_.name)) {
throw "ParameterObject property: $($_.name) is NULL"
}
Expand Down Expand Up @@ -3916,7 +4118,7 @@ function Expand-WAPVMRoleVMDisk {
$SizeInMB = $Size * 1024
PreFlight -IncludeConnection -IncludeSubscription
$ParentVM = Get-WapVMRoleVM -CloudServiceName $Disk.ParentCloudService
If($ParentVM.RuntimeState -ne "Stopped"){
if ($ParentVM.RuntimeState -ne "Stopped"){
throw "Disk may not be expanded whilst machine is running"
}
# Note we copy the WAPack Tenant Portal behaviour where the $CloudServiceName and $VMRoleName are identical and there is only 1 VMRole per CloudService
Expand Down Expand Up @@ -3995,7 +4197,7 @@ function New-WAPVMRoleVMDisk {
# Note we copy the WAPack Tenant Portal behaviour where the $CloudServiceName and $VMRoleName are identical and there is only 1 VMRole per CloudService
$Service = Get-WAPCloudService -Name $CloudServiceName
$ParentVM = $Service | Get-WapVMRoleVM
If($ParentVM.RuntimeState -ne "Stopped"){
if ($ParentVM.RuntimeState -ne "Stopped"){
throw "Disk may not be added to running machine"
}
#Set the LUN
Expand Down Expand Up @@ -4044,13 +4246,13 @@ function Invoke-WAPVMRoleVMDiskExpansion{

This will stop the VM, expand the VHD named DDrive to 50GB and then restart the VM.
#>
[CmdletBinding(SupportsShouldProcess=$True)]
[CmdletBinding(SupportsShouldProcess=$true)]
Param(
[Parameter(Mandatory=$True)]
[Parameter(Mandatory=$true)]
[PSCustomObject]
$Disk,

[Parameter(Mandatory=$True)]
[Parameter(Mandatory=$true)]
[int]
[ValidateRange(1,2048)]
$Size
Expand Down