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

My journey to automate vCloud Director with PowerShell – part #6 : create an edge gateway

At first, i searched if there is a commandlet for creating an edge. Sadly the only edge commandlet I got was Get-EdgeGateway. So I had to find another way.
Following the UI, an edge is created inside an OrgVDC. Therefore I searched for a method in the OrgVDC object. Easily I found it as always beneath the ExtensionData Sub-Object. A method called CreateEdgeGateway.

Get-Member -InputObject $orgvdc.ExtensionData -Name CreateEdgeGateway


TypeName: VMware.VimAutomation.Cloud.Views.AdminVdc

Name MemberType Definition
---- ---------- ----------
CreateEdgeGateway Method VMware.VimAutomation.Cloud.Views.Gateway CreateEdgeGateway(VMware.VimAutomation.Cloud.Views.Gateway gateway)

As i do such things in the last blog posts, I created a new VMware.VimAutomation.Cloud.Views.Gateway object and tried to pass it to the CreateEdgeGateway method. Of course I failed first because the Gateway object has no filled properties.

A look in the API documentation (https://code.vmware.com/apis/912/vmware-cloud-director/doc/doc/types/GatewayType.html ) shows, that there are some required properties. The name of course and the Configuration property which consists of a VMware.VimAutomation.Cloud.Views.GatewayConfiguration object.
Again I created a new object of type VMware.VimAutomation.Cloud.Views.GatewayConfiguration. But this time I first looked in the documentation (https://code.vmware.com/apis/912/vmware-cloud-director/doc/doc//types/GatewayConfigurationType.html) which properties are required. I saw that I had to fill GatewayBackingConfig and GatewayInterfaces.
GatewayBackingConfig is a simple string which defines the size of the edge. I choosed compact, because I wanted the smallest type of edge here.
GatewayInterfaces is of type VMware.VimAutomation.Cloud.Views.GatewayInterfaces
which is an array for objects of type VMware.VimAutomation.Cloud.Views.GatewayInterface. So, first I needed to create a GatewayInterfaces object and had to add a GatewayInterface object.
At last, this GatewayInterface object needs an ip configuration. (An edge without an external ip doesn’t really make sense to me.) Therefore I had to fill the SubnetParticipation property for which I also had to create an object of type VMware.VimAutomation.Cloud.Views.SubnetParticipation. (https://code.vmware.com/apis/912/vmware-cloud-director/doc/doc/types/SubnetParticipationType.html)

In Summary i had to create the following objects in order to create a functional edge gateway:

Gateway -> GatewayConfiguration -> GatewayInterfaces -> GatewayInterface -> SubnetParticipation

After all this chained objects are filled with properties I could create the edge with the CreateEdgeGateway method of the OrgVDC object.


Full code:

#######################
# create a empty edge #
#######################

# get a fresh copy of the orgvdc
$orgvdc = Get-OrgVdc -Name $orgvdcname

# create new edge object
$newedge = New-Object -TypeName VMware.VimAutomation.Cloud.Views.Gateway
$newedge.Name = $edgename # set the name from the initial settings

# create new edge configuration object
$newedge.Configuration = New-Object -TypeName VMware.VimAutomation.Cloud.Views.GatewayConfiguration
$newedge.Configuration.GatewayBackingConfig = "compact" # smallest possible edge
$newedge.Configuration.UseDefaultRouteForDnsRelay = $false # do not use the default gw for dns
$newedge.Configuration.HaEnabled = $false # no high availability

# create new interface object array for the nics of the new edge
$newedge.Configuration.GatewayInterfaces = New-Object -TypeName VMware.VimAutomation.Cloud.Views.GatewayInterfaces

# create new interface object for the external nic of the edge
$newedge.Configuration.GatewayInterfaces.GatewayInterface = New-Object -TypeName VMware.VimAutomation.Cloud.Views.GatewayInterface

# get the external network
$extnetwork = Get-ExternalNetwork -Name "external-net"

# fill the parameters of the new nic object
$newedge.Configuration.GatewayInterfaces.GatewayInterface[0].Name = $extnetwork.Name # give the nic a name
$newedge.Configuration.GatewayInterfaces.GatewayInterface[0].ApplyRateLimit = $true # activate rate limit
$newedge.Configuration.GatewayInterfaces.GatewayInterface[0].OutRateLimit = 100 # set output rate limit in MBit/s
$newedge.Configuration.GatewayInterfaces.GatewayInterface[0].InRateLimit = 100 # set input rate limit in MBit/s
$newedge.Configuration.GatewayInterfaces.GatewayInterface[0].InterfaceType = "uplink" # set interface type to uplink
$newedge.Configuration.GatewayInterfaces.GatewayInterface[0].UseForDefaultRoute = $true # use this interface as default route for the edge
$newedge.Configuration.GatewayInterfaces.GatewayInterface[0].Network = $extnetwork.Href # reference to the external network object of vcloud director
$newedge.Configuration.GatewayInterfaces.GatewayInterface[0].DisplayName = $extnetwork.Name # display name of the nic

# create new object for the ip settings of the external nic of the edge
$subnet = New-Object -TypeName VMware.VimAutomation.Cloud.Views.SubnetParticipation

# set the ip settings of the external nic
$subnet.Gateway = $extnetwork.Gateway # use the default gateway of the external network
$subnet.Netmask = $extnetwork.Netmask # use the netmask of the external network
$subnet.IpAddress = [string] "1.2.3.4" # set the ip of the external nic of the edge

# assign ip settings to the external nic object of the edge
$newedge.Configuration.GatewayInterfaces.GatewayInterface[0].SubnetParticipation = $subnet

# after all settings been made, create the edge
$orgvdc.ExtensionData.CreateEdgeGateway($newedge)

Next article in this series:

create a routed network

Leave a Reply

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

− two = five