Tuesday 11 November 2014

PowerShell for CaaS 1.1 - Batch operations, new cmdlets and filtering

For the last 3 days, I have been working on improving the PowerShell module for Caas. I have added more server operations, improved the filtering experience and support to batch operations.

[Update] Windows Installer MSI (64-bit) is now available
[Update] Latest release now on GitHub

Before we start, please download the new ZIP file to your desktop. CaaS-PowerShell.1.1.0.0.zip. Extract this ZIP to the PowerShell modules folder on Windows (C:\Program Files\WindowsPowerShell\Modules) This will create a sub-directory called 'CaaS' with the module DLLs and a schema so that PowerShell knows what to do. Overwrite any previous version.

Firstly, I tidied up some of the cmdlets and parameters for a more consistent experience. Here are the changes

  • Cmdlet: New-CaasVM renamed to New-CaasServer
  • Cmdlet: Remove-CaasVM renamed to Remove-CaasServer
  • Parameter: -NetworkWithLocations was renamed to -Network on some cmdlets
  • Parameter: ServerWithBackupType renamed to -Server on some cmdlets
New-CaasServer uses a new API  method which includes support for disk speed on creation

I have created aliases for the renamed cmdlets, which will be created when you load the module. However, if you have created any script with cmdlets with the old parameter names, the new version will break your scripts.

New cmdlets
  • Set-CaasServer : modify server name, description, memory, CPU and private ip
  • Add-CaasServerDisk: add new disk to server
  • Resize-CaasServerDisk: increase the size of existing disks
  • Remove-CaasServerDisk: remove a disk from a server
  • Set-CaasServerDiskSpeed: changes the speed of an existing disk

I have added new cmdlets:

Another welcome change is the ability to filter based on Name, Location and/or Network on most cmdlets. The most important change here is the Get-CaasDeployedServer which got 4 filtering options: ServerId, Name, Location and Network. The filtering happens on the Caas API level, therefore the list comes filtered by Caas.


#get an a server name Server1
Get-CaasDeployedServer -Name Server1

#get all servers from AU1 location
Get-CaasDeployedServer -Location AU1
#get an ACL rule with name Test
Get-CaasAclRules -Name Test 

#get a network with name Network1 and pass it to the Get-CaaSAclRules
$network=Get-CaasNetwork -Name "Network1"
Get-CaasAclRules -Network $network 

Please check the examples to see more of these in use.


The most common challenge that we face is to queue up many Caas operations and execute them at once. You must wait until the previous operation to finish so you can start the next one.


Such as
  1. Deploy a new server with high performance disk as system drive
  2. Update CPU and memory server settings
  3. Resize the system drive to 100GB
  4. Add a additional cheap disk for logs 
  5. Start the server


Now it is possible to execute all these at once, by using the PassThru parameter along with Out-CaasWaitForOperation cmdlet. All the status updates from Caas will be shown on the PowerShell progress bar:



Here is an example that you will find the ps1 file on GitHub.


$servername = "TestServer"
$networkname = "TestNetwork"
$osimagename = "Win2012 DC 64-bit 2 CPU"
$administratorPassword= "password123"

#import Caas Module
Import-Module CaaS

#capture the Caas credentials and create a new Caas conneciton
$login = Get-Credential
New-CaasConnection -ApiCredentials $login -ApiBaseUri https://api-au.dimensiondata.com/oec/0.9/

#Get the network with a specific name
$network=Get-CaasNetworks -Name $networkname

#Get a OS image with a specific name
$os=Get-CaasOsImages -Network $network -Name $osimagename

#create a new server details configuration
$serverdetails = New-CaasServerDetails -Name $servername  -AdminPassword $administratorPassword -IsStarted $false -OsServerImage $os -Network $network

#set the the disk speed on the server to be provisioned. Disk speeds can retrieved by (Get-CaasDataCentre).hypervisor.diskSpeed
$serverdetails = Set-CaasServerDiskDetails -ServerDetails $serverdetails -ScsiId 0 -SpeedId "HIGHPERFORMANCE"


#
#Create new Caas server using the server details created before, using the -PassThru parameter will return a Server object that can be pipped to Out-CaasWaitForOperation
#Out-CaasWaitForOperation cmdlet will monitor the progress of any server provisioning operation, you MUST use the -PassThru parameter
#The command below will perform the following actions in one goal:
#1) Create a CaaS server with a High Performance disk as system drive (based on the server details created above)
#2) Modify the CPU and memory to 4 CPUs and 8GB RAM
#3) Resize the System drive to 100GB
#4) Add another Economy drive to the server
#5) Start the server



New-CaasServer -ServerDetails $serverdetails -PassThru | Out-CaasWaitForOperation |`
Set-CaasServer -CPUCount 4 -MemoryInMB 8192 -PassThru | Out-CaasWaitForOperation |`
Resize-CaasServerDisk -ScsiId 0 -NewSizeInGB 100 -PassThru | Out-CaasWaitForOperation |`
Add-CaasServerDisk -SizeInGB 200 -SpeedId "ECONOMY" -PassThru | Out-CaasWaitForOperation |`
Set-CaasServerState -Action PowerOn


#Add a nat rule so the server
$server = Get-CaasDeployedServer -Name $servername
$natrule=Add-CaasNatRule -NatRuleName Test -Network $network -SourceIpAddress $server.privateIp

#Add firewall/ACL rule to permit TCP traffic on port 80 from any network
Add-CaasAclRule -AclRuleName "AllowPort80" -Network $network -Position 120 -DestinationIpAddress $natrule.natIp -Action PERMIT -Protocol TCP -PortRangeType EQUAL_TO -Port1 80 -AclType OUTSIDE_ACL


I hope these changes can make your automation task easier!