Home > Blogs > VMware PowerCLI Blog


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

This entry was posted in Advanced and tagged , , , on by .
Alan Renouf

About Alan Renouf

Alan Renouf is a Product Line Manager at VMware focusing on API's, SDK's and CLI's, He is responsible for providing the architects and operators of private and public cloud infrastructure with the toolkits/frameworks and command-line interfaces they require to build a fully automated software-defined datacenter. Alan is a frequent blogger at http://blogs.vmware.com/PowerCLI a book author and has a personal blog at http://virtu-al.net. You can follow Alan on twitter as @alanrenouf.

3 thoughts on “Automating creation of vCD Organizations, Users and Org vDCs

  1. d.couturier

    Thanks for this script ! But I can’t find a way to create a fully orgvdc.
    StorageCapacity cannot be set :'( Still 0 MB !
    Do you have an idea ?

    Reply
    1. Stephane Bourdeaud

      This is a late answer, but putting it out here for all those lost souls banging their heads against the wall about this “StorageCapacity cannot be set” error.
      You MUST have the *any storage capacity in your Provider vDC or it won’t work.

      Reply
    2. Patrick

      Hi,
      here is an example function how to create an org vdc with an individual storage profile:

      Function New-CIOrgVdc {
      Param (
      $OrgName,
      $OrgvDCName,
      $ProviderVDC,
      $CPuGHz,
      $CPuReservation,
      $MEmMB,
      $MEmReservation,
      $StorageLimitMB,
      $StorageProfile,
      $NetworkPool
      )
      Process {

      #Storage Profile “Any” is needed for initial VDC creation – will be deleted afterwards
      New-OrgVdc -Name $orgvDCName -AllocationModelAllocationPool -CPUAllocationGHz $CPuGHz -MemoryAllocationGB $MEmMB -Org $orgName -ProviderVDC $ProviderVDC -StorageAllocationGB 1

      # Add a Storage Profile to the newly created Org VDC
      # Find the desired Storage Profile in the Provider vDC to be added to the Org vDC
      $orgPvDCProfile = search-cloud -QueryType ProviderVdcStorageProfile -Name $storageProfile | Get-CIView

      # Create a new object of type VdcStorageProfileParams and configure the parameters for the Storage Profile
      $spParams = new-object VMware.VimAutomation.Cloud.Views.VdcStorageProfileParams
      $spParams.Limit = $StorageLimitMB
      $spParams.Units = “MB”
      $spParams.ProviderVdcStorageProfile = $orgPvDCProfile.href
      $spParams.Enabled = $true
      $spParams.Default = $false

      # Create an UpdateVdcStorageProfiles object and put the new parameters into the AddStorageProfile element
      $UpdateParams = new-object VMware.VimAutomation.Cloud.Views.UpdateVdcStorageProfiles
      $UpdateParams.AddStorageProfile = $spParams

      # Get the Org VDC and create the Storage Profile
      #Get-OrgVcd $name -> not working with ISE
      $orgVdc = Get-OrgVdc -Name $orgvDCName

      $orgVdc.ExtensionData.CreateVdcStorageProfile($UpdateParams)

      #Set the new storage profile as default
      $orgvDCStorageProfile = search-cloud -querytype AdminOrgVdcStorageProfile | where {($_.Name -match $storageProfile) -and ($_.VdcName -eq $orgvDCName)} | Get-CIView
      $orgvDCStorageProfile.Default = $True
      $orgvDCStorageProfile.UpdateServerData()

      # Delete the *(Any) Storage Profile
      # Get object representing the * (Any) Profile in the Org vDC
      $orgvDCAnyProfile = search-cloud -querytype AdminOrgVdcStorageProfile | where {($_.Name -match ‘\*’) -and ($_.VdcName -eq $orgvDCName)} | Get-CIView

      # Disable the “* (any)” Profile
      $orgvDCAnyProfile.Enabled = $False
      $orgvDCAnyProfile.UpdateServerData()

      # Remove the “* (any)” profile form the Org vDC completely
      $ProfileUpdateParams = new-object VMware.VimAutomation.Cloud.Views.UpdateVdcStorageProfiles
      $ProfileUpdateParams.RemoveStorageProfile = $orgvDCAnyProfile.href
      $orgvDC.extensiondata.CreatevDCStorageProfile($ProfileUpdateParams)

      #Set Org VDC Params
      $orgvDC | Set-OrgVdc -CpuGuaranteedPercent $CPuReservation -MemoryGuaranteedPercent $MEmReservation -VMMaxCount $null -ThinProvisioned $true -UseFastProvisioning $false -NetworkMaxCount “5” -NetworkPool $NetworkPool

      }

      }

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*