posted

2 Comments

AlanFeb2012_thumb_thumb1_thumb_thumb[1]
Posted by
Alan Renouf
Technical Marketing

Following my previous post where I showed how easy and quick it was to create Organizations, Users and Organization VDC’s I thought it would be good to take it one step further or in this case backwards, to create the items we created in the last post we must first need a Provider VDC, we need this to provide the resources to the Organization VDC.

The script detailed further in this post can be used to create a Provider VDC, to do this we will obviously need to provide resources from a vCenter, for this to be done we need to connect to both the Cloud Infrastructure and the vSphere Infrastructure which is not an issue for PowerCLI.

In the script I have assumed that all hosts in the cluster will have access to the same shared storage and that this is the storage which will be used in the Provider VDC, I have also assumed that we will be using a Cluster to back our Provider VDC and not a resource pool… Although technically this is the root resource pool anyway, to learn more about the root resource pool make sure you read William Lams great introductory posts to the API here.

Creating a Provider VDC

The following script can be used to create a Provider VDC, it is shown as an example and not a complete function which provides all parameters and functions needed in a more complex situation:

Function New-ProviderVDC {

    Param (
        $Cluster,
        $Description,
        [Switch]$Enabled,
        $HWVersion
    )
   
    Process {

        $vcloud = $DefaultCIServers[0].ExtensionData
        $admin = $vcloud.GetAdmin()
        $ext = $admin.GetExtension()
        $ProvidervDCDetails = New-Object VMware.VimAutomation.Cloud.Views.VMWProviderVdc
        $ProvidervDCDetails.Name = $Cluster
        $VC = $ext.GetVimServerReferences()
        $VimServer = $vc.VimServerReference[0]
        $ProvidervDCDetails.VimServer = $VimServer
        $ProvidervDCDetails.Description =$Description
        $ProvidervDCDetails.IsEnabled = $Enabled
        $Datastorerefs = New-Object VMware.VimAutomation.Cloud.Views.VimObjectRefs
        $Datastores = @()
        Foreach ($DS in (Get-Cluster $Cluster | Get-VMHost | Select -First 1 | Get-Datastore | Where {$_.Extensiondata.Summary.MultipleHostAccess})) {
            $Datastore = New-Object VMware.VimAutomation.Cloud.Views.VimObjectRef
            $Datastore.VimServerRef = $VimServer
            $Datastore.MoRef = (($DS).Id).Trim("Datastore-")
            $Datastore.VimObjectType = "DATASTORE"
            $Datastores += $Datastore
        }
        $Datastorerefs.VimObjectRef = $Datastores
        $ProvidervDCDetails.DataStoreRefs = $Datastorerefs
        $RPrefs = New-Object VMware.VimAutomation.Cloud.Views.VimObjectRefs
        $RPrefs.VimObjectRef = @(New-Object VMware.VimAutomation.Cloud.Views.VimObjectRef)
        $RPrefs.VimObjectRef[0].VimServerRef = $VimServer
        $RPrefs.VimObjectRef[0].MoRef = ((Get-Cluster $Cluster | Get-ResourcePool Resources).ExtensionData).Moref.Value
        $RPrefs.VimObjectRef[0].VimObjectType = "RESOURCE_POOL"
        $ProvidervDCDetails.ResourcePoolRefs = $RPrefs
        #If HWVersion is not defined then use 7 by default
        If ($HWVersion) {
            $HWVer = "vmx-0$($HWVersion)"
        } Else {
            $HWVer = "vmx-07"
        }
        $ProvidervDCDetails.HighestSupportedHardwareVersion = $HWVer

        $ext.CreateProvidervdc($ProvidervDCDetails)
    }
}

Running the script

As mentioned before, we must fist connect to both the Cloud Infrastructure as well as the Virtual Infrastructure, once this has been done we can use the above function to create our provider VDC by passing it the cluster information and the hardware version:

# Adding PowerCLI vCD snapin
if (!(get-pssnapin -name VMware.VimAutomation.Cloud -erroraction silentlycontinue)) {
    add-pssnapin VMware.VimAutomation.Cloud
}

# Adding PowerCLI snapin
if (!(get-pssnapin -name VMware.VimAutomation.Core -erroraction silentlycontinue)) {
    add-pssnapin VMware.VimAutomation.Core
}

# Connectiong to CI and VI
Connect-VIServer myvc -User Administrator -Password vmware
Connect-CIServer myvcd -User Administrator –Password P@ssw0rd

# Create the Provider VDC
New-ProviderVDC -Cluster Cluster01 -Description "A Description for my cluster" -Enabled -HWVersion 7

The Result

A shiny new Provider VDC

DataStoreRefs                   : VMware.VimAutomation.Cloud.Views.VimObjectRefs
ResourcePoolRefs                : VMware.VimAutomation.Cloud.Views.VimObjectRefs
VimServer                       : {VC01}
HostReferences                  : VMware.VimAutomation.Cloud.Views.VMWHostReferences
HighestSupportedHardwareVersion : vmx-07
Status                          : 0
ComputeCapacity                 : VMware.VimAutomation.Cloud.Views.RootComputeCapacity
StorageCapacity                 : VMware.VimAutomation.Cloud.Views.ProviderVdcCapacity
AvailableNetworks               : VMware.VimAutomation.Cloud.Views.AvailableNetworks
Capabilities                    :
Vdcs                            :
IsEnabled                       : True
NetworkPoolReferences           : VMware.VimAutomation.Cloud.Views.NetworkPoolReferences
Name                            : Cluster01
Id                              : urn:vcloud:providervdc:b2746976-c674-45bc-89b1-c6c38bad42f7
Description                     : A Description for my cluster
Tasks                           : VMware.VimAutomation.Cloud.Views.TasksInProgress
Client                          : VMware.VimAutomation.Cloud.Views.CloudClient
Href                            :
https://labvcd/api/admin/extension/providervdc/b2746976-c674-45bc-89b1-c6c38bad42f7
Type                            : application/vnd.vmware.admin.vmwprovidervdc+xml
Link                            : {, , , …}
AnyAttr                         : {xsi:schemaLocation}
VCloudExtension                 :

Or as can be seen from the GUI:

image

Of course the more attentive of you will notice the system alerts on the status, this is because we now need to prepare the hosts in this Provider VDC but that’s a subject for another blog post !

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