VMware Cloud Community
BScott66
Enthusiast
Enthusiast

vRealize Automation API - Create a Machine Prefix

Dose anyone know if there is a way VIA the REST API to create vRA Machine Prefixes?

7 Replies
sbeaver
Leadership
Leadership

Not sure about the API but this is what I have been using in my environment with great success and does work with 6.x -- http://dailyhypervisor.com/vcloud-automation-center-vcac-5-2-custom-hostnaming-extension/#more-3632

Steve Beaver
VMware Communities User Moderator
VMware vExpert 2009 - 2020
VMware NSX vExpert - 2019 - 2020
====
Co-Author of "VMware ESX Essentials in the Virtual Data Center"
(ISBN:1420070274) from Auerbach
Come check out my blog: [www.virtualizationpractice.com/blog|http://www.virtualizationpractice.com/blog/]
Come follow me on twitter http://www.twitter.com/sbeaver

**The Cloud is a journey, not a project.**
0 Kudos
kericmiles
Enthusiast
Enthusiast

No way to do it via REST AFIK but you can do it through vRO:

var modelName = "ManagementModelEntities.svc";

var entitySetName = "HostNamePrefixes";

var entities = vCACEntityManager.readModelEntitiesByCustomFilter(host.id, modelName, entitySetName);

  var inputProperties = {

  MachinePrefix:"prefix",

  NextMachineNo:"1",

  MachineNumberLength:"3"

  };

  entity = vCACEntityManager.createModelEntity(host.id, modelName, entitySetName, inputProperties, entities[0].getLinks(host));

BScott66
Enthusiast
Enthusiast

Kericmiles,

I'm trying to understand this...

What I believe I would need to do is create a vRO Action with this snippet in it.

var modelName = "ManagementModelEntities.svc";

var entitySetName = "HostNamePrefixes";

var entities = vCACEntityManager.readModelEntitiesByCustomFilter(host.id, modelName, entitySetName);

var inputProperties = {

  MachinePrefix:Prefix,

  NextMachineNo:NextNumber,

  MachineNumberLength:NumberofDigits

};

entity = vCACEntityManager.createModelEntity(host.id, modelName, entitySetName, inputProperties, entities[0].getLinks(host));

Supporting it I have created 3 parameters:

Prefix: String

NextNumber: Number

NumberofDigits: Number

Are there any other items that I should be providing the data for, such as host, which my code seems to be failing at?

If so what should it be pointing at?

Others?

Thanks,

Bill S.

0 Kudos
rszymczak
Hot Shot
Hot Shot

Hi,

host ist your vCAC:VCACHost object which you configured in the VCAC IaaS plugin in vRO.

Without trying: the rest looks good to me.

0 Kudos
kericmiles
Enthusiast
Enthusiast

rszymczak is correct, host is your vRA host as a vCAC:VCACHost object.

0 Kudos
BenNeise
Contributor
Contributor

You could maybe do it via the oData service on the IaaS server

Function New-IaaSHostNamePrefix {

        <#

        .SYNOPSIS

        Creates a new vRO naming prefix using the IaaS oData interface via REST

        .DESCRIPTION

        Creates a new vRO naming prefix using the IaaS oData interface via REST. Tested on vCO 6.2.1 with PowerShell 4

        .PARAMETER Server

        The name of the IaaS server on which the operation shouldbe performed.

        .PARAMETER Prefix

        The Machine Prefix to use

        .PARAMETER NextMachineNumber

        The Next Number to use

        .PARAMETER MachineNumberLength

        Number of digits to be used

        .PARAMETER Credential

        A PowerShell PSCredential object

        .EXAMPLE

        Create a prefix called "TST" with a next machine number of 100 and a machine length of 4 on the IaaS server IAAS01.

        The first machine to use this prefix will be TST0124.

        New-IaaSHostNamePrefix -Server "IAAS01" -Prefix "TST" -nextMachineNumber 123 -machineNumberLength 4 -Credential $cred

       

        .NOTES

        Ben Neise

    #>

    [cmdletbinding()]

    param (

        [parameter(Mandatory = $true,

            Position = 0

        )]

        [String]

        $Server,

        [parameter(Mandatory = $true,

            Position = 1

        )]

        [String]

        $Prefix,

        [parameter(Mandatory = $true,

            Position = 2

        )]

        [int32]

        $NextMachineNumber,

        [parameter(Mandatory = $true,

            Position = 3

        )]

        [int32]

        $MachineNumberLength,

        [parameter(Mandatory = $true,

            Position = 4

        )]

        [PSCredential]

        $Credential

    )

    begin {

        $baseUri = "https://" + $Server + "/Repository/data/ManagementModelEntities.svc/"

        $uri = $baseUri + "HostNamePrefixes"

        $method = "POST"

        $xmlContentType = "application/atom+xml"

        $strDate = Get-Date -Format s

        $guid = [guid]::NewGuid()

    }

    process {

        $xml = [xml]'<?xml version="1.0" encoding="utf-8"?><entry></entry>'

        $xmlTitle = $xml.CreateElement("title")

        $xml.get_ChildNodes().Item(1).AppendChild($xmlTitle) | Out-Null

        $xml.entry.SetAttribute('xmlns','http://www.w3.org/2005/Atom')

        $xml.entry.SetAttribute('xmlns:d','http://schemas.microsoft.com/ado/2007/08/dataservices')

        $xml.entry.SetAttribute('xmlns:m','http://schemas.microsoft.com/ado/2007/08/dataservices/metadata')

        $xmlNamespaceManager = New-Object System.Xml.XmlNamespaceManager($xml.nametable)

        $xmlNamespaceManager.addnamespace("d", $xml.entry.GetNamespaceOfPrefix("d"))

        $xmlNamespaceManager.addnamespace("m", $xml.entry.GetNamespaceOfPrefix("m"))

        $xmlUpdated = $xml.CreateElement("updated")

        $xmlUpdatedNode = $xml.entry.AppendChild($xmlUpdated)

        $xmlText = $xml.CreateTextNode($strDate)

        $xmlUpdatedNode.AppendChild($xmlText) | Out-Null

        $xmlAuthor = $xml.CreateElement("author")

        $xml.entry.AppendChild($xmlAuthor) | Out-Null

        $xmlName = $xml.CreateElement("name")

        $xmlAuthor.AppendChild($xmlName) | Out-Null

        $xmlCategory = $xml.CreateElement("category")

        $xmlCategory.SetAttribute("scheme", "http://schemas.microsoft.com/ado/2007/08/dataservices/scheme");

        $xmlCategory.SetAttribute("term", "DynamicOps.ManagementModel.HostNamePrefix");

        $xml.entry.AppendChild($xmlCategory) | Out-Null

        $xmlContent = $xml.CreateElement("content")

        $xmlContent.SetAttribute('type','application/xml') | Out-Null

        $xml.entry.AppendChild($xmlContent) | Out-Null

        $xmlProperties = $xml.CreateElement('m:properties',$xmlNamespaceManager.LookupNamespace("m"))

        $xmlContent.AppendChild($xmlProperties) | Out-Null

        $xmlHostnamePrefixID = $xml.CreateElement('d:HostnamePrefixID',$xmlNamespaceManager.LookupNamespace("d"))

        $xmlProperties.AppendChild($xmlHostnamePrefixID) | Out-Null

        $xmlHostnamePrefixID.SetAttribute('type',$xmlNamespaceManager.LookupNamespace("m"),'Edm.Guid') | Out-Null

        $xmlHostnamePrefix = $xml.CreateTextNode($guid)

        $xmlHostnamePrefixID.AppendChild($xmlHostnamePrefix) | Out-Null

        $xmlMachinePrefix = $xml.CreateElement('d:MachinePrefix',$xmlNamespaceManager.LookupNamespace("d"))

        $xmlProperties.AppendChild($xmlMachinePrefix) | Out-Null

        $xmlMachinePrefixText = $xml.CreateTextNode($prefix)

        $xmlMachinePrefix.AppendChild($xmlMachinePrefixText) | Out-Null

        $xmlNextMachineNo = $xml.CreateElement('d:NextMachineNo',$xmlNamespaceManager.LookupNamespace("d"))

        $xmlProperties.AppendChild($xmlNextMachineNo) | Out-Null

        $xmlNextMachineNo.SetAttribute('type',$xmlNamespaceManager.LookupNamespace("m"),'Edm.Int32') | Out-Null

        $xmlNextMachineNoText = $xml.CreateTextNode($nextMachineNumber)

        $xmlNextMachineNo.AppendChild($xmlNextMachineNoText) | Out-Null

        $xmlMachineNumberLength = $xml.CreateElement('d:MachineNumberLength',$xmlNamespaceManager.LookupNamespace("d"))

        $xmlProperties.AppendChild($xmlMachineNumberLength) | Out-Null

        $xmlMachineNumberLength.SetAttribute('type',$xmlNamespaceManager.LookupNamespace("m"),'Edm.Int32') | Out-Null

        $xmlMachineNumberLengthText = $xml.CreateTextNode($MachineNumberLength)

        $xmlMachineNumberLength.AppendChild($xmlMachineNumberLengthText) | Out-Null

        $body = $xml.outerXML

        $result = try {

            Invoke-WebRequest -Uri $uri -Method $method -ContentType $xmlContentType -Credential $Credential -Body $body

        }

        catch {

            Write-Error -Message ([xml]$_.ErrorDetails.Message).error.innererror.internalException.internalException.message

            return

        }

    }

    end {

        return $result

    }

}

0 Kudos
mmumshad
Contributor
Contributor

Did you manage to get this working? Is how? Know if REST API is available in newer versions? vRA 7.1 perhaps?

0 Kudos