Monday 27 October 2014

PowerShell for CaaS

The R&D team in Sydney have assembled a .NET client library for the CaaS REST API on Github (here), which is freely available and open source. We use this in day-to-day automation tasks such as spinning up lab environments, testing backup clients and the backup server (as a consumer) and scaling out our Virtual Machines. We have also assembled a set of PowerShell Cmdlets for doing simple tasks in CaaS and published this online free for any customers or internal usage.

Installing the Module for Windows desktop or server

Requirements First download the ZIP file to your desktop. CaaS-PowerShell.1.0.0.1.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.
PS C:\Users\anthony> Get-Module -ListAvailable CaaS


    Directory: C:\Program Files\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Binary     1.0.0.1    CaaS                                {Add-CaasBackupClient, Add-CaasAclRule, Add-CaasNatRule, R...

To get a list of commands available use the default PowerShell command Get-Command
PS C:\Users\anthony> Get-Command -Module CaaS

CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Cmdlet          Add-CaasAclRule                                    CaaS
Cmdlet          Add-CaasBackupClient                               CaaS
Cmdlet          Add-CaasNatRule                                    CaaS
Cmdlet          Get-CaasAclRules                                   CaaS
Cmdlet          Get-CaasBackupClients                              CaaS
Cmdlet          Get-CaasBackupClientTypes                          CaaS
Cmdlet          Get-CaasBackupSchedulePolicies                     CaaS
Cmdlet          Get-CaasBackupStoragePolicies                      CaaS
Cmdlet          Get-CaasCustomerImageImports                       CaaS
Cmdlet          Get-CaasCustomerImages                             CaaS
Cmdlet          Get-CaasDataCentre                                 CaaS
Cmdlet          Get-CaasDeployedServer                             CaaS
Cmdlet          Get-CaasNatRules                                   CaaS
Cmdlet          Get-CaasNetworks                                   CaaS
Cmdlet          Get-CaasOsImages                                   CaaS
Cmdlet          Get-CaasOvfPackages                                CaaS
Cmdlet          New-CaasBackupJob                                  CaaS
Cmdlet          New-CaasConnection                                 CaaS
Cmdlet          New-CaasImportCustomerImage                        CaaS
Cmdlet          New-CaasNetwork                                    CaaS
Cmdlet          New-CaasServerDetails                              CaaS
Cmdlet          New-CaasVM                                         CaaS
Cmdlet          Remove-CaasAclRule                                 CaaS
Cmdlet          Remove-CaasBackupClient                            CaaS
Cmdlet          Remove-CaasBackupJob                               CaaS
Cmdlet          Remove-CaasNatRule                                 CaaS
Cmdlet          Remove-CaasNetwork                                 CaaS
Cmdlet          Remove-CaasVM                                      CaaS
Cmdlet          Set-CaasBackupClient                               CaaS
Cmdlet          Set-CaasBackupPlan                                 CaaS
Cmdlet          Set-CaasProvisionBackup                            CaaS
Cmdlet          Set-CaasServerState                                CaaS

As you can see there are some commands for basic utilities in CaaS, such as creating VMs, getting lists of servers, networks or datacenters. The first command you need to use is the New-CaaSConnection command, this creates a connection to the API with your credentials. Of course, you will need a CaaS account first. The CaaSConnection command takes a PSCredential object, you can initialize one by either prompting for a login in Windows, e.g.
PS C:\Users\anthony> $login = Get-Credential

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
PS C:\Users\anthony> $login

UserName                                                                                                       Password
--------                                                                                                       --------
cbu-test-lab-01                                                                                System.Security.SecureString
Or by creating one in the script:
$secpasswd = ConvertTo-SecureString "myPassword123" -AsPlainText -Force
$login= New-Object System.Management.Automation.PSCredential ("cbu-test-lab-01", $secpasswd)
Once you have a login object, you need the API Base URI for your CaaS tenant, if this is a private hosted or private cloud, this will be published seperately. All public instances are listed on the REST API documentation For reference the Dimension Data URLs are: North America (NA) api-na.dimensiondata.com Europe (EU) api-eu.dimensiondata.com Australia (AU) api-au.dimensiondata.com Africa (AF) api-mea.dimensiondata.com Asia Pacific (AP) api-ap.dimensiondata.com South America (SA) api-latam.dimensiondata.com Canada(CA) api-canada.dimensiondata.com For this example, I will use the Dimension Data Australia instance as https://api-au.dimensiondata.com/oec/0.9/
PS C:\Users\anthony> New-CaasConnection -ApiCredentials $login -ApiBaseUri https://api-au.dimensiondata.com/oec/0.9/

Account
-------
DD.CBU.Compute.Api.Contracts.Directory.Account
So you see we now have a client connection object, this is used for the other commands to send the credentials and the API connection details. The client connection is stored in the runspace, so you don't have to pass it as a parameter. Now, we can run commands such as :
PS C:\Users\anthony> Get-CaasNetworks | Format-Table

id                  name                description         location            privateNet          multicast
--                  ----                -----------         --------            ----------          ---------
9eb09828-e2f7-11... My Example Network 1                     AU1                 10.208.253.0        false
9eb09f1c-e2f7-11... My Example Network 2                     AU1                 10.208.254.0        false

Commands can be chained, for example to get a list of your networks and then for each network a list of ACL rules:
PS C:\Users\anthony> Get-CaasNetworks | Get-CaasAclRules | Format-Table

id          name        status         position      action protocol    sourceIpRan destination portRange          type
                                                                        ge          IpRange
--          ----        ------         --------      ------ --------    ----------- ----------- ---------          ----
70818fcb... default-98  NORMAL               98        DENY TCP         IpRangeType IpRangeType PortRang...  INSIDE_ACL
82ef4a7e... default-99  NORMAL               99        DENY TCP         IpRangeType IpRangeType PortRang...  INSIDE_ACL
a650bbc2... default-100 NORMAL              100      PERMIT ICMP        IpRangeType IpRangeType PortRang... OUTSIDE_ACL

Servers can be listed using the Get-CaaSDeployedServer command
PS C:\Users\anthony> Get-CaasDeployedServer | Where name -eq "CMS"


backup          :
name            : CMS
description     :
operatingSystem : OSType
cpuCount        : 4
memoryMb        : 8192
disk            : {3f73f797-d72c-4c32-aa5a-933ea1c11974, 207d8751-0eec-4616-89d3-6e251610b3df}
softwareLabel   : {MSSQL2012R2E}
sourceImageId   : f1433fd7-957f-4940-ab00-873ff953f056
networkId       : e4cc9bde-59ba-11e1-9153-001b21cfdbe1
machineName     : 10-218-122-12
privateIp       : 10.218.122.12
publicIp        : 17.184.202.224
created         : 4/14/2014 5:34:27 AM
isDeployed      : True
isStarted       : True
state           : NORMAL
status          :
machineStatus   : {vmwareToolsVersionStatus, vmwareToolsRunningStatus, vmwareToolsApiVersion}
id              : 30515bba-d7a8-4a55-8a7b-4c71144876c4
location        : AU1
Here are some other examples: Get a list of virtual machines that have backup and print the service plan they are in:
$serversWithBackup = Get-CaasDeployedServer | Where backup -NotLike $Null
foreach ( $server in $serversWithBackup) { Write-Host $server.name is $server.backup.servicePlan }
Servers in a bad way
$badServers = Get-CaasDeployedServer | Where state -NotLike "NORMAL"
I will follow up with some additional posts on common tasks using the PowerShell commands. Anthony Shaw

1 comment: