Solving problems invented by others...
My journey to automate vCloud Director with PowerShell – part #3  : create an organization

My journey to automate vCloud Director with PowerShell – part #3 : create an organization

create an organization

To create an organization you can use the commandlet New-Org (https://code.vmware.com/docs/11794/cmdlet-reference/doc/New-Org.html). It provides some basic parameters for the organization.

 

$org = New-Org -Name <new organization name> -FullName <new organization full name> -Description <new organization description>


What I missed here is the ability to set the policy settings of the organization. For example the vApp and vApp template leases.
But I found that if I dive into the sub-objects of the organization object there are some methods which give me what I want. I analyzed the sub-object ExtensionData.Settings where I found two methods which sounded interesting.

Get-Member -InputObject $org.ExtensionData.Settings

GetVAppLeaseSettings            Method     VMware.VimAutomation.Cloud.Views.OrgLeaseSettings GetVAppLeaseSettings()
GetVAppTemplateLeaseSettings    Method     VMware.VimAutomation.Cloud.Views.OrgVAppTemplateLeaseSettings GetVAppTemplateLeaseSettings()


This methods return the needed lease settings which I then could modify. Because this modifications are only done on a local copy of the settings object, they must be written back to the vCloud Director. This could be done with the UpdateServerData() method of the settings object.

Example:

# get vAPP lease settings of the new org
$vappleases = $org.ExtensionData.Settings.GetVAppLeaseSettings()

# set vAPP max storage lease to unlimited
$vappleases.StorageLeaseSeconds = 0

# set vAPP max runtime lease to unlimited
$vappleases.DeploymentLeaseSeconds = 0

# save changes
$vappleases.UpdateServerData()


I also wanted to add an user to the organization. While searching the sub-objects of the organization I found a usable method in the ExtensionData sub-object:

Get-Member -InputObject $org.ExtensionData -Name CreateUser

   TypeName: VMware.VimAutomation.Cloud.Views.AdminOrg

Name       MemberType Definition
----       ---------- ----------
CreateUser Method     VMware.VimAutomation.Cloud.Views.User CreateUser(VMware.VimAutomation.Cloud.Views.User user)


This method takes a VMware.VimAutomation.Cloud.Views.User object as parameter. So I first have to create one.

# create new userobject
$newuser = New-Object -TypeName VMware.VimAutomation.Cloud.Views.User


First I filled the obvious parameters of the userobject:

# set the parameters for the new user
$newuser.Name = "<new username>"
$newuser.Password =  "<new password>"
$newuser.IsEnabled = $true
$newuser.FullName = "<new user full name>"


Soon I realized that this is not enough. A user needs of course a access role assigned. The parameter “Role” of the user object should get a reference to an existing access role of the organization.
I found a list of available access roles in the sub-object ExtensionData.RoleReferences.RoleReference. Because I want the new user to have the Organization Administrator role, I simply had to filter the available roles. Then I added the role to the user object.

# get a reference to the organization administrator role
$role = $org.ExtensionData.RoleReferences.RoleReference | Where-Object -FilterScript { $_.Name -eq "Organization Administrator" }

# assign this role to the new user
$newuser.Role = $role


Now the new user object had all needed parameters and could be added to the organization object with the help of the CreateUser method.

# create the user in the organization
$org.ExtensionData.CreateUser($newuser)

The whole script:

######################
# create the new org #
######################

# create new org
$org = New-Org -Name <new organization name> -FullName <new organization full name> -Description <new organization description>

##############################
# modify vAPP lease settings #
##############################

# get vAPP lease settings of the new org
$vappleases = $org.ExtensionData.Settings.GetVAppLeaseSettings()

# set vAPP max storage lease to unlimited
$vappleases.StorageLeaseSeconds = 0

# set vAPP max runtime lease to unlimited
$vappleases.DeploymentLeaseSeconds = 0

# save changes
$vappleases.UpdateServerData()

#######################################
# modify vAPP template lease settings #
#######################################

# get vAPP template lease settings of the new org
$vapptemplateleases = $org.ExtensionData.Settings.GetVAppTemplateLeaseSettings()

# set vAPP template max storage lease to unlimited
$vapptemplateleases.StorageLeaseSeconds = 0

# save changes
$vapptemplateleases.UpdateServerData()

#########################
# create org admin user #
#########################

# create new userobject
$newuser = New-Object -TypeName VMware.VimAutomation.Cloud.Views.User

# set the parameters for the new user
$newuser.Name = "<new username>"
$newuser.Password =  "<new password>"
$newuser.IsEnabled = $true
$newuser.FullName = "<new user full name>"

# get a reference to the organization administrator role
$role = $org.ExtensionData.RoleReferences.RoleReference | Where-Object -FilterScript { $_.Name -eq "Organization Administrator" }

# assign this role to the new user
$newuser.Role = $role

# create the user in the organization
$org.ExtensionData.CreateUser($newuser)


Next article in this series:

Create an organization virtual datacenter

Leave a Reply

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

ninety one ÷ = thirteen