Access Internal REST API’s in vROPS with PowerShell

Access Internal REST API’s in vROPS with PowerShell

Access Internal REST API’s in vROPS with PowerShell

Its been a long time since i blogged as have been very busy with my New Job Role at VMware :).

Being a customer facing role, i see lots of challenges that customers face and my role is to save the world!, one customer at a time 🙂

Recently we had a challenge with one of our customer where we had about 2000 custom-groups which had to be created in vROPS, and it had to be done fast!, so no time for manual efforts.

The REST API exposed for this was an internal API for vROPS and the sad part was that PowerCLI the tool that i love!! and can solve almost anything did not have these exposed at the time being.

Hence i resorted to the PowerShell way to invoke REST API’s

Invoke-RestMethod

If you explore the vROPS internal API reference guide, you can find details on the XML body which needs to be passed for this invoke.

 

Once you get this output!, you need to build an XML body to be passed to create this custom group, but the tough part is how to you get the values on what you need to pass, for this what i did was basically create a custom group manunally and then i sent a GET request to the internal API to identify the contents of the XML and voila!, i got the details of the XML node data which needs to be passed along with the POST request.

First identify the API call from the API guide.

Next, lets create the REST call using Invoke-RestMethod to call it.

#Create URL string for Invoke-RestMethod
$urlsend = 'https://' + 'vrops.vmwareebc.in' + '/suite-api/internal/resources/groups'

#Credential

$cred = Get-Credential

## Debug 
echo $urlsend

#Send Attribute data to vRops.
$ContentType = "application/xml;charset=utf-8"
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("Accept", 'application/xml')
$header.Add("X-vRealizeOps-API-use-unsupported", 'true')

try {
        $result = Invoke-RestMethod -Method GET -uri $urlsend -Credential $cred -ContentType $ContentType -Headers $header
}
catch {
        $result = $_.Exception.Response.GetResponseStream()
        $reader = New-Object System.IO.StreamReader($result)
        $reader.BaseStream.Position = 0
        $reader.DiscardBufferedData()
        $responseBody = $reader.ReadToEnd();
}

Write-Output $result.InnerXml

The results for this would be saved in $result.InnerXML.

Next i figured out only the data which i required,

</ops:group><ops:group
identifier="f3316192-42ed-40e5-9f44-068cdc9af241"><ops:resourceKey><ops:name>BIS Branch -
07380</ops:name><ops:adapterKindKey>Container</ops:adapterKindKey><ops:resourceKindKey>BIS - Branch
Codes</ops:resourceKindKey><ops:resourceIdentifiers /></ops:resourceKey><ops:membershipDefinition><ops:includedResource
s></ops:includedResources><ops:excludedResources></ops:excludedResources><ops:rule-group><ops:resourceKindKey><ops:reso
urceKind>AppSession</ops:resourceKind><ops:adapterKind>V4V</ops:adapterKind></ops:resourceKindKey><ops:attributeRules
xsi:type="ops:metric-key-condition" key="client|machine_name"><ops:stringValue>07380</ops:stringValue><ops:compareOpera
tor>CONTAINS</ops:compareOperator></ops:attributeRules></ops:rule-group></ops:membershipDefinition></ops:group><ops:gro
up identifier="d5b0741e-1478-4f2f-b2e8-c315990a1cf6"><ops:resourceKey><ops:name>Unlicensed Group</ops:name><ops:adapter
KindKey>Container</ops:adapterKindKey><ops:resourceKindKey>Licensing</ops:resourceKindKey><ops:resourceIdentifiers /></
ops:resourceKey><ops:membershipDefinition><ops:includedResources></ops:includedResources><ops:excludedResources></ops:e
xcludedResources></ops:membershipDefinition></ops:group>

And once i got the required values for the nodes from the above content, i created the POST request to invoke this.

#Create XML Structure and populate variables from the dump

$XMLFile = @('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ops:group xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ops="http://webservice.vmware.com/vRealizeOpsMgr/1.0/">>
	<ops:resourceKey>
		<ops:name>BIS Branch -11041</ops:name>
		<ops:adapterKindKey>Container</ops:adapterKindKey>
		<ops:resourceKindKey>BIS - Branch Codes</ops:resourceKindKey>
	</ops:resourceKey>
	<ops:membershipDefinition>
				<ops:rule-group>
					<ops:resourceKindKey>
						<ops:resourceKind>VirtualMachine</ops:resourceKind>
						<ops:adapterKind>VMWARE</ops:adapterKind>
 					</ops:resourceKindKey>
					<ops:attributeRules xsi:type="ops:metric-key-condition" key="cpu|usage_average">
						<ops:compareOperator>CONTAINS</ops:compareOperator>
						<ops:stringValue>11041</ops:stringValue>
					</ops:attributeRules>
				</ops:rule-group>
	 </ops:membershipDefinition>
 </ops:group>'
)


[xml]$xmlSend = $XMLFile

#Create URL string for Invoke-RestMethod
$urlsend = 'https://' + 'vrops.vmwareebc.in' + '/suite-api/internal/resources/groups'

## Debug 
echo $urlsend

#Send Attribute data to vRops.
$ContentType = "application/xml;charset=utf-8"
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("Accept", 'application/xml')
$header.Add("X-vRealizeOps-API-use-unsupported", 'true')

try {
        $result = Invoke-RestMethod -Method POST -uri $urlsend -Body $xmlSend -Credential $cred -ContentType $ContentType -Headers $header
}
catch {
        $result = $_.Exception.Response.GetResponseStream()
        $reader = New-Object System.IO.StreamReader($result)
        $reader.BaseStream.Position = 0
        $reader.DiscardBufferedData()
        $responseBody = $reader.ReadToEnd();
}

now if you check $result, you should see a success

and now if you check your vROPS dashboard you can see the custom group created successfully!, woah!! now with the magic of for-loop iterate it across 2000 objects and get all things automated! 🙂

I hope you enjoyed this blog post and found this useful for accessing Internal REST API’s in vROPS with PowerShell .

Share this post

One thought on “Access Internal REST API’s in vROPS with PowerShell

Post Comment