Advanced

Automating creation of vCD Organizations, Users and Org vDCs

AlanFeb2012_thumb_thumb1_thumb_thumb
Posted by
Alan Renouf
Technical Marketing

In Part 1 of this blog series I wrote about how I presented together with a colleague of mine Vladimir Goranov at Partner Exchange 2012.  I already showed you the reporting script and demonstration on how to create graphs straight from PowerCLI which detail the resources and statistics in your Cloud Infrastructure (CI).

Following the reporting side of the vCloud Director snap-in we explained that although in PowerCLI 5.0.1 the cmdlets were mainly “GET” cmdlets which retrieved data from the Cloud there was also a cmdlet called Get-CIView and a property on the returned objects called .ExtensionData.

The Get-CIView cmdlet and ExtensionData property exposes the entire vCloud Director API and allows us to make changes to the CI, this opens up multiple possibilities and moves us away from just retrieving data and allows us to create and alter our CI.

In our presentation we showed how the current cmdlets could be used to create some advanced functions which would look just like PowerShell cmdlets and could be used to create new Organizations, Users and Organizational vDC's.

We then used the scenario of a service provider having to add multiple new Organizations into their CI, would they add these one by one using the GUI ? Maybe they already have their own product which integrates into the vCloud Director APIs and they didn’t need to do this but for those looking for a free and easy way to create multiple Orgs we showed how easy it was to create 50 new orgs in less than 2 minutes.

Creating 50 new Organizations in less than 2 minutes

The below video was used in the presentation we gave at Partner Exchange, follow through to see the creation of a single Org, User and Org vDC and then how we can create 50 orgs in less than 2 minutes from an Excel spreadsheet:

The Scripts

Below are the scripts and examples we used in the presentation, please note these are not fully formed cmdlets and are shown as examples of how you can use the Get-CIView cmdlet and ExtensionData to create these CI objects, you may need to adjust these functions to perform extra tasks or to work in your CI.

Function New-Org {
    Param (
        $Name,
        $FullName,
        $Description,
        [Switch]$Enabled,
        [Switch]$PublishCatalogs
    )
    Process {
        $vcloud = $DefaultCIServers[0].ExtensionData
       
        $AdminOrg = New-Object VMware.VimAutomation.Cloud.Views.AdminOrg
        $adminOrg.Name = $name
        $adminOrg.FullName = $FullName
        $adminOrg.Description = $description
        $adminOrg.IsEnabled = $Enabled

        $orgSettings = New-Object VMware.VimAutomation.Cloud.Views.OrgSettings
        $orgGeneralSettings = New-Object VMware.VimAutomation.Cloud.Views.OrgGeneralSettings
        $orgGeneralSettings.CanPublishCatalogs = $PublishCatalogs
        $orgSettings.OrgGeneralSettings = $orgGeneralSettings

        $adminOrg.Settings = $orgSettings

        $org = $vcloud.CreateOrg($adminOrg)
        Get-Org -Name $name
    }
}

Function New-CIUser {
    Param (
        $Name,
        $Pasword,
        $FullName,
        [Switch]$Enabled,
        $Org,
        $Role
    )
    Process {
        $OrgED = (Get-Org $Org).ExtensionData
        $orgAdminUser = New-Object VMware.VimAutomation.Cloud.Views.User
        $orgAdminUser.Name = $Name
        $orgAdminUser.FullName = $FullName
        $orgAdminUser.Password = $Pasword
        $orgAdminUser.IsEnabled = $Enabled

        $vcloud = $DefaultCIServers[0].ExtensionData
       
        $orgAdminRole = $vcloud.RoleReferences.RoleReference | where {$_.Name -eq $Role}
        $orgAdminUser.Role = $orgAdminRole
       
        $user = $orgED.CreateUser($orgAdminUser)
        Get-CIUser -Org $Org -Name $Name
    }
}   

Function New-OrgVDC {
    Param (
        $Name,
        [Switch]$Enabled,
        $Org,
        $ProviderVDC,
        $AllocationModel,
        $CPULimit,
        $CPUAllocated,
        $MEMAllocated,
        $MEMLimit,
        $StoraqeLimit
    )
    Process {
        $adminVdc = New-Object VMware.VimAutomation.Cloud.Views.AdminVdc
        $adminVdc.Name = $name
        $adminVdc.IsEnabled = $Enabled
        $providerVdc = Get-ProviderVdc $ProviderVDC
        $providerVdcRef = New-Object VMware.VimAutomation.Cloud.Views.Reference
        $providerVdcRef.Href = $providerVdc.Href
        $adminVdc.ProviderVdcReference =$providerVdcRef
        $adminVdc.AllocationModel = $AllocationModel
        $adminVdc.ComputeCapacity = New-Object VMware.VimAutomation.Cloud.Views.ComputeCapacity
        $adminVdc.ComputeCapacity.Cpu = New-Object VMware.VimAutomation.Cloud.Views.CapacityWithUsage
        $adminVdc.ComputeCapacity.Cpu.Units = "MHz"
        $adminVdc.ComputeCapacity.Cpu.Limit = $CPULimit
        $adminVdc.ComputeCapacity.Cpu.Allocated = $CPUAllocated
        $adminVdc.ComputeCapacity.Memory = New-Object VMware.VimAutomation.Cloud.Views.CapacityWithUsage
        $adminVdc.ComputeCapacity.Memory.Units = "MB"
        $adminVdc.ComputeCapacity.Memory.Limit = $MEMLimit
        $adminVdc.ComputeCapacity.Memory.Allocated = $MEMAllocated
        $adminVdc.StorageCapacity = New-Object VMware.VimAutomation.Cloud.Views.CapacityWithUsage
        $adminVdc.StorageCapacity.Units = "MB"
        $adminVdc.StorageCapacity.Limit = $StorageLimit
       
        $OrgED = (Get-Org $Org).ExtensionData
        $orgVdc = $orgED.CreateVdc($adminVdc)
        Get-OrgVdc $name
    }
}

# Connect to our Cloud Infrastructure
Connect-CIServer vCloud

# Create a new Org
New-Org -Name "PowerCLIRocks" -FullName "PowerCLI Rocks hard!" -description "PowerCLI really rocks hard." -Enabled

# Create a new Administrator for that Org
New-CIUser -Enabled -Name "PowerCLIRocksAdmin" -FullName "PowerCLI Rocks Administrator"
    -Pasword "Pa$$w0rd" -Org "PowerCLIRocks" -Role "Organization Administrator"

# Create a new Org VDC for that Org
New-OrgVDC -Name "PowerCLIRocksVDC" -Org "PowerCLIRocks"-AllocationModel "AllocationPool" -Enabled

    -CPUAllocated 500 -CPULimit 1000 -MEMAllocated 1000 -MEMLimit 2000 -ProviderVDC "Bronze" -StorageLimit 1024

# Create all the Urgent Orgs
Import-Csv c:\UrgentWork\UrgentOrgs.csv | Foreach {
    New-Org -Name $_.OrgName -FullName $_.OrgFullName -description $_.OrgDesc -Enabled
}

Get notification of new blog postings and more by following VMware PowerCLI on Twitter: @PowerCLI