VMware Cloud Community
ImranMughal01
Enthusiast
Enthusiast

read tags of an object from vrops api in vro

I've got a script which gathers various rest calls to vrops..but i'm trying to gather the tags associated to the object.

I can see that the api call to vrops (https://{vropsserver}/api/resources/{id of object}/properties) returns lots of values one of which is the summary|tag

 

   <ops:property name="summary|runtime|connectionState">connected</ops:property>

    <ops:property name="summary|runtime|powerState">Powered On</ops:property>

    <ops:property name="summary|tag">[&lt;Callout-Business&gt;]</ops:property>

    <ops:property name="System Properties|resource_kind_subtype">GENERAL</ops:property>

    <ops:property name="System Properties|resource_kind_type">GENERAL</ops:property>

 

How can i grab that array from the returned json? 

 

   <ops:property name="summary|runtime|connectionState">connected</ops:property>
    <ops:property name="summary|runtime|powerState">Powered On</ops:property>
    <ops:property name="summary|tag">[&lt;Callout-Business&gt;]</ops:property>
    <ops:property name="System Properties|resource_kind_subtype">GENERAL</ops:property>
    <ops:property name="System Properties|resource_kind_type">GENERAL</ops:property>
 
Reply
0 Kudos
7 Replies
iiliev
VMware Employee
VMware Employee

Hi,

The provided snippet is XML, not JSON. Anyway, both XML and JSON can be parsed in vRO scripting, using either E4X (for XML data) or JSON.parse() (for JSON data).

Also, could you clarify your question? What array do you want to grab? Are you asking for array containing arr <ops:property> elements? On only those that have a name "summary|tag" (assuming there can be more than one in the returned data)? Could you show the full response - the snippet you provided is not a well-formed XML document.

Reply
0 Kudos
ImranMughal01
Enthusiast
Enthusiast

Hi - So if i use postman i would see for example the returned xml like this: -

How can i use a vro script to select all the ops:name values? . Can i access it by parsing the xml or do i need to convert is to json?

Previously i would use something like this to return everything ...

var response = request.execute();
contentAsString = response.contentAsString;
var jsontxt = contentAsString;
var alarm = JSON.parse(jsontxt);
System.log("JSON string is: " + alarm);

...but i want to just save to variables the highlighted items in the xml below. Thanks!

--------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<ops:resources xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ops="http://webservice.vmware.com/vRealizeOpsMgr/1.0/">

    <ops:pageInfo totalCount="3">

        <ops:page>0</ops:page>

<ops:pageSize>1000</ops:pageSize>

    </ops:pageInfo>

    <ops:links>

        <ops:link href="https://communities.vmware.com/suite-api/internal/resources/groups/c6cadd3a-342b-4c2d-9327-ca6584267cb0/members?page=0&amp;pageSize=1000" rel="SELF" name="current"/>

        <ops:link href="https://communities.vmware.com/suite-api/internal/resources/groups/c6cadd3a-342b-4c2d-9327-ca6584267cb0/members?page=0&amp;pageSize=1000" rel="RELATED" name="first"/>

        <ops:link href="https://communities.vmware.com/suite-api/internal/resources/groups/c6cadd3a-342b-4c2d-9327-ca6584267cb0/members?page=0&amp;pageSize=1000" rel="RELATED" name="last"/>

    </ops:links>

    <ops:resource creationTime="1489051766826" identifier="27090d29-7516-4a8e-9a83-eea9097ae642">

        <ops:resourceKey>

<ops:name>atvrops01</ops:name>

<ops:adapterKindKey>VMWARE</ops:adapterKindKey>

<ops:resourceKindKey>VirtualMachine</ops:resourceKindKey>

            <ops:resourceIdentifiers>

                <ops:resourceIdentifier>

                    <ops:identifierType name="VMEntityName" dataType="STRING" isPartOfUniqueness="false"/>

<ops:value>atvrops01</ops:value>

                </ops:resourceIdentifier>

                <ops:resourceIdentifier>

                    <ops:identifierType name="VMEntityObjectID" dataType="STRING" isPartOfUniqueness="true"/>

<ops:value>vm-163</ops:value>

                </ops:resourceIdentifier>

                <ops:resourceIdentifier>

                    <ops:identifierType name="VMEntityVCID" dataType="STRING" isPartOfUniqueness="true"/>

<ops:value>77788e7d-f66f-45dc-b6e6-848435f41d3c</ops:value>

                </ops:resourceIdentifier>

            </ops:resourceIdentifiers>

        </ops:resourceKey>

        <ops:resourceStatusStates>

            <ops:resourceStatusState>

<ops:adapterInstanceId>77b196e3-f427-499a-8f0a-6ec17366bff8</ops:adapterInstanceId>

<ops:resourceStatus>DATA_RECEIVING</ops:resourceStatus>

<ops:resourceState>STARTED</ops:resourceState>

<ops:statusMessage></ops:statusMessage>

            </ops:resourceStatusState>

            <ops:resourceStatusState>

<ops:adapterInstanceId>07a638af-3aba-4883-a9eb-a2484a319134</ops:adapterInstanceId>

<ops:resourceStatus>DATA_RECEIVING</ops:resourceStatus>

<ops:resourceState>STARTED</ops:resourceState>

<ops:statusMessage></ops:statusMessage>

            </ops:resourceStatusState>

            <ops:resourceStatusState>

<ops:adapterInstanceId>380a314c-9aa8-4cdd-966c-e5290f76189e</ops:adapterInstanceId>

<ops:resourceStatus>DATA_RECEIVING</ops:resourceStatus>

<ops:resourceState>STARTED</ops:resourceState>

<ops:statusMessage></ops:statusMessage>

            </ops:resourceStatusState>

        </ops:resourceStatusStates>

<ops:resourceHealth>GREEN</ops:resourceHealth>

<ops:resourceHealthValue>100.0</ops:resourceHealthValue>

<ops:dtEnabled>true</ops:dtEnabled>

        <ops:badges>

            <ops:badge type="TIME_REMAINING" color="YELLOW" score="49.4917871414302"/>

            <ops:badge type="CAPACITY_REMAINING" color="RED" score="0.0"/>

            <ops:badge type="EFFICIENCY" color="GREEN" score="100.0"/>

            <ops:badge type="HEALTH" color="GREEN" score="100.0"/>

            <ops:badge type="ANOMALY" color="GREEN" score="6.0"/>

            <ops:badge type="RISK" color="YELLOW" score="25.0"/>

            <ops:badge type="COMPLIANCE" color="GREY" score="-1.0"/>

        </ops:badges>

        <ops:relatedResources/>

        <ops:links>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/27090d29-7516-4a8e-9a83-eea9097ae642" rel="SELF" name="linkToSelf"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/27090d29-7516-4a8e-9a83-eea9097ae642/relationships" rel="RELATED" name="relationsOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/27090d29-7516-4a8e-9a83-eea9097ae642/properties" rel="RELATED" name="propertiesOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/alerts?resourceId=27090d29-7516-4a8e-9a83-eea9097ae642" rel="RELATED" name="alertsOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/symptoms?resourceId=27090d29-7516-4a8e-9a83-eea9097ae642" rel="RELATED" name="symptomsOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/27090d29-7516-4a8e-9a83-eea9097ae642/statkeys" rel="RELATED" name="statKeysOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/27090d29-7516-4a8e-9a83-eea9097ae642/stats/latest" rel="RELATED" name="latestStatsOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/27090d29-7516-4a8e-9a83-eea9097ae642/properties" rel="RELATED" name="latestPropertiesOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/credentials/" rel="RELATED" name="credentialsOfResource"/>

        </ops:links>

    </ops:resource>

    <ops:resource creationTime="1520870173264" identifier="98047f5b-1aab-4dab-8ea2-b0edbd8d5e97">

        <ops:resourceKey>

<ops:name>testesxi01</ops:name>

<ops:adapterKindKey>VMWARE</ops:adapterKindKey>

<ops:resourceKindKey>HostSystem</ops:resourceKindKey>

            <ops:resourceIdentifiers>

                <ops:resourceIdentifier>

                    <ops:identifierType name="VMEntityName" dataType="STRING" isPartOfUniqueness="false"/>

<ops:value>testesxi01</ops:value>

                </ops:resourceIdentifier>

                <ops:resourceIdentifier>

                    <ops:identifierType name="VMEntityObjectID" dataType="STRING" isPartOfUniqueness="true"/>

<ops:value>host-635</ops:value>

                </ops:resourceIdentifier>

                <ops:resourceIdentifier>

                    <ops:identifierType name="VMEntityVCID" dataType="STRING" isPartOfUniqueness="true"/>

<ops:value>77788e7d-f66f-45dc-b6e6-848435f41d3c</ops:value>

                </ops:resourceIdentifier>

            </ops:resourceIdentifiers>

        </ops:resourceKey>

        <ops:resourceStatusStates>

            <ops:resourceStatusState>

<ops:adapterInstanceId>77b196e3-f427-499a-8f0a-6ec17366bff8</ops:adapterInstanceId>

<ops:resourceStatus>DATA_RECEIVING</ops:resourceStatus>

<ops:resourceState>STARTED</ops:resourceState>

<ops:statusMessage></ops:statusMessage>

            </ops:resourceStatusState>

            <ops:resourceStatusState>

<ops:adapterInstanceId>740558b6-e919-45e9-99b1-fa81d117bdd3</ops:adapterInstanceId>

<ops:resourceStatus>DATA_RECEIVING</ops:resourceStatus>

<ops:resourceState>STARTED</ops:resourceState>

<ops:statusMessage></ops:statusMessage>

            </ops:resourceStatusState>

            <ops:resourceStatusState>

<ops:adapterInstanceId>380a314c-9aa8-4cdd-966c-e5290f76189e</ops:adapterInstanceId>

<ops:resourceStatus>DATA_RECEIVING</ops:resourceStatus>

<ops:resourceState>STARTED</ops:resourceState>

<ops:statusMessage></ops:statusMessage>

            </ops:resourceStatusState>

        </ops:resourceStatusStates>

<ops:resourceHealth>GREEN</ops:resourceHealth>

<ops:resourceHealthValue>100.0</ops:resourceHealthValue>

<ops:dtEnabled>true</ops:dtEnabled>

        <ops:badges>

            <ops:badge type="TIME_REMAINING" color="GREEN" score="82.55756368548447"/>

            <ops:badge type="CAPACITY_REMAINING" color="GREEN" score="69.94697050425492"/>

            <ops:badge type="EFFICIENCY" color="GREEN" score="100.0"/>

            <ops:badge type="HEALTH" color="GREEN" score="100.0"/>

            <ops:badge type="ANOMALY" color="GREEN" score="7.0"/>

            <ops:badge type="RISK" color="GREEN" score="0.0"/>

            <ops:badge type="COMPLIANCE" color="GREEN" score="100.0"/>

        </ops:badges>

        <ops:relatedResources/>

        <ops:links>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/98047f5b-1aab-4dab-8ea2-b0edbd8d5e97" rel="SELF" name="linkToSelf"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/98047f5b-1aab-4dab-8ea2-b0edbd8d5e97/relationships" rel="RELATED" name="relationsOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/98047f5b-1aab-4dab-8ea2-b0edbd8d5e97/properties" rel="RELATED" name="propertiesOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/alerts?resourceId=98047f5b-1aab-4dab-8ea2-b0edbd8d5e97" rel="RELATED" name="alertsOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/symptoms?resourceId=98047f5b-1aab-4dab-8ea2-b0edbd8d5e97" rel="RELATED" name="symptomsOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/98047f5b-1aab-4dab-8ea2-b0edbd8d5e97/statkeys" rel="RELATED" name="statKeysOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/98047f5b-1aab-4dab-8ea2-b0edbd8d5e97/stats/latest" rel="RELATED" name="latestStatsOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/98047f5b-1aab-4dab-8ea2-b0edbd8d5e97/properties" rel="RELATED" name="latestPropertiesOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/credentials/" rel="RELATED" name="credentialsOfResource"/>

        </ops:links>

    </ops:resource>

    <ops:resource creationTime="1528102582771" identifier="edc9ff17-7b77-415d-a0b7-1c69820ef5da">

        <ops:resourceKey>

<ops:name>testdsc01</ops:name>

<ops:adapterKindKey>VMWARE</ops:adapterKindKey>

<ops:resourceKindKey>StoragePod</ops:resourceKindKey>

            <ops:resourceIdentifiers>

                <ops:resourceIdentifier>

                    <ops:identifierType name="VMEntityName" dataType="STRING" isPartOfUniqueness="false"/>

<ops:value>testdsc01</ops:value>

                </ops:resourceIdentifier>

                <ops:resourceIdentifier>

                    <ops:identifierType name="VMEntityObjectID" dataType="STRING" isPartOfUniqueness="true"/>

<ops:value>group-p725</ops:value>

                </ops:resourceIdentifier>

                <ops:resourceIdentifier>

                    <ops:identifierType name="VMEntityVCID" dataType="STRING" isPartOfUniqueness="true"/>

<ops:value>77788e7d-f66f-45dc-b6e6-848435f41d3c</ops:value>

                </ops:resourceIdentifier>

            </ops:resourceIdentifiers>

        </ops:resourceKey>

        <ops:resourceStatusStates>

            <ops:resourceStatusState>

<ops:adapterInstanceId>77b196e3-f427-499a-8f0a-6ec17366bff8</ops:adapterInstanceId>

<ops:resourceStatus>DATA_RECEIVING</ops:resourceStatus>

<ops:resourceState>STARTED</ops:resourceState>

<ops:statusMessage></ops:statusMessage>

            </ops:resourceStatusState>

        </ops:resourceStatusStates>

<ops:resourceHealth>GREEN</ops:resourceHealth>

<ops:resourceHealthValue>100.0</ops:resourceHealthValue>

<ops:dtEnabled>true</ops:dtEnabled>

        <ops:badges>

            <ops:badge type="EFFICIENCY" color="GREEN" score="100.0"/>

            <ops:badge type="HEALTH" color="GREEN" score="100.0"/>

            <ops:badge type="ANOMALY" color="GREEN" score="2.0"/>

            <ops:badge type="RISK" color="GREEN" score="0.0"/>

            <ops:badge type="COMPLIANCE" color="GREEN" score="100.0"/>

        </ops:badges>

        <ops:relatedResources/>

        <ops:links>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/edc9ff17-7b77-415d-a0b7-1c69820ef5da" rel="SELF" name="linkToSelf"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/edc9ff17-7b77-415d-a0b7-1c69820ef5da/relationships" rel="RELATED" name="relationsOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/edc9ff17-7b77-415d-a0b7-1c69820ef5da/properties" rel="RELATED" name="propertiesOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/alerts?resourceId=edc9ff17-7b77-415d-a0b7-1c69820ef5da" rel="RELATED" name="alertsOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/symptoms?resourceId=edc9ff17-7b77-415d-a0b7-1c69820ef5da" rel="RELATED" name="symptomsOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/edc9ff17-7b77-415d-a0b7-1c69820ef5da/statkeys" rel="RELATED" name="statKeysOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/edc9ff17-7b77-415d-a0b7-1c69820ef5da/stats/latest" rel="RELATED" name="latestStatsOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/resources/edc9ff17-7b77-415d-a0b7-1c69820ef5da/properties" rel="RELATED" name="latestPropertiesOfResource"/>

            <ops:link href="https://communities.vmware.com/suite-api/api/credentials/" rel="RELATED" name="credentialsOfResource"/>

        </ops:links>

    </ops:resource>

</ops:resources>

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

Here is a sample E4X code to fetch and dump the values of <ops:name> elements:

var contentAsString = ... // your content in XML format

var xml = new XML(contentAsString);

var ops = new Namespace("http://webservice.vmware.com/vRealizeOpsMgr/1.0/");

var names = xml.ops::resource.ops::resourceKey.ops::name;

for each (var name in names) {

  System.log(name.toString());

}

Reply
0 Kudos
ImranMughal01
Enthusiast
Enthusiast

Perfect that worked great.

Can i ask what this line does?

  1. var ops = new Namespace("http://webservice.vmware.com/vRealizeOpsMgr/1.0/");  

THanks again!

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

It defines a XML namespace.

Take a look at the root element of your XML document. It is

<ops:resources xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ops="http://webservice.vmware.com/vRealizeOpsMgr/1.0/">

xmlns attributes of this element define various namespaces used in the XML document (in this case, you have 3 namespaces defined - xsi, xs and ops). All elements of your XML document should be in one of these namespaces; for example <ops:name ...> element is defined as part of ops namespace.

When you search for element, or do whatever other operation with element, you need to specify what namespace you want to search your element in. The reason is that you can have elements with one and the same name but in different namespace, like <ops:name...> and <xs:name...>, and having a namespace helps to uniquely identify the element.

Reply
0 Kudos
ImranMughal01
Enthusiast
Enthusiast

Ok thats great , thanks for the help.

So i've noticed some elements have multiple values (a) within whereas most are just between <xxx> (b)

example of (a)

<ops:resource creationTime="1489051766826" identifier="27090d29-7516-4a8e-9a83-eea9097ae642">

example of (b)

<ops:name>SDDC Health Group</ops:name>

With the example you gave me earlier with E4X . How would i access the element in (a)? For example how would i extract the identifier?


Thanks

Imran

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

Element attributes can be accessed either by name using "@" prefix, or by attribute() method. So, to get the list of identifiers of resource elements, you can use one of the following:

var identifiers = xml.ops::resource.@identifier;

or

var identifiers = xml.ops::resource.attribute("identifier");

For your case, both variants will work identically. The latter must be used in cases when attribute name is either a reserved word or contain characters that would made it an invalid name (eg. '-')

Reply
0 Kudos