VMware Cloud Community
qc4vmware
Virtuoso
Virtuoso

Merged Blueprint Properties how do I extract these?

I'm trying to use my blueprint definitions data as the source of information for some workflows I am generating.  I need to get the results of the merged property groups on the vm components of the blueprint.  I can easily get the component and the property groups assigned to the component but I'm not sure how to get the merged result which is what I need.

Reply
0 Kudos
5 Replies
eoinbyrne
Expert
Expert

You can do this if you use the ContextPropertyService to load the groups and extract their contents. You can use either the plugin scripting classes (via vCACCAFEHost.createPropertyClient().getContextPropertyGroupService() ) or by using the REST API directly.

API doc for vCACCAFEContextPropertyGroup - vRO API Explorer by Dr Ruurd and Flores of ITQ

The only trick is to be careful to load the property groups into a Properties object in the order specified on the Blueprint/Component . This way you'll be accurately respecting any overridden property that might appear in more than one group

HTH

qc4vmware
Virtuoso
Virtuoso

I'm reluctant to reconstruct this myself without someone confirming that the property group listing will always be returned in the correct order.  Since this is just an array of the property group names and not an object with an index or some other sort of key to confirm the sort on it makes me a little nervous.  I've hit other scenarios like this where the data returned is not sorted as expected.

Reply
0 Kudos
eoinbyrne
Expert
Expert

I figure it must be an ordering convention as the Design Canvas for a Machine component lets you use 'Move Up' & 'Move Down' when adding property groups to the list. If the order were not respected at all times within the system then provisioning VMs would be a lottery....

eoinbyrne
Expert
Expert

Knew I had this code lying around somewhere - this gets a dump of all property groups and prints them to CSV rows in the logs using the vRA REST API directly (vRA as a RESTHost).

function findPropertyGroup(grps, name)

{

    for each(var grp in grps)

    {

        if(grp.label == name)

        {

            //System.log("found grp!");

            return grp;

        }

    }

    return null;

}

function printGroupPropertyNamesAndValues(grp)

{

    var name = grp.label;

    //System.log(grp.properties.length);

    for each(var p in Object.keys(grp.properties))

    {

        //System.log(p);

        //var pName = p.keys[0];

        var v = null;

        //System.log(JSON.stringify(grp["properties"][p], null, 2));

        if(grp["properties"][p]["facets"]["defaultValue"]["value"])

        {

            v = grp["properties"][p]["facets"]["defaultValue"]["value"]["value"];

        }

        else

        {

            v = "--NO VALUE--"

        }

        System.log(name + "," + p + "," + v);

    }

}

var authContent = {};

authContent.username = username;

authContent.password = password;

authContent.tenant = tenantName;

var authContentStr = JSON.stringify(authContent);

var loginRequest = vraRestHost.createRequest("POST", "/identity/api/tokens", authContentStr);

loginRequest.setHeader("Accept", "application/json");

loginRequest.setHeader("Content-type", "application/json");

var bearerToken;

var loginResponse = loginRequest.execute();

if(loginResponse.statusCode == 200)

{

    var tokenData = JSON.parse(loginResponse.contentAsString);

    bearerToken = tokenData.id;

    //System.log(bearerToken);

}

else

{

    throw "Auth failed! " + loginResponse.contentAsString;

}

//href": "https://<vra-cafe-host>/properties-service/api/propertygroups?page=1&limit=1000"

var propertyGroupsRequest = vraRestHost.createRequest("GET", "/properties-service/api/propertygroups?page=1&limit=1500", null);

propertyGroupsRequest.setHeader("Authorization", "Bearer " + bearerToken);

var propertyGroupsResponse = propertyGroupsRequest.execute();

var grps;

//System.log(propertyGroupsResponse.statusCode);

//System.log(propertyGroupsResponse.contentAsString);

if(propertyGroupsResponse.statusCode == 200)

{

    grps = JSON.parse(propertyGroupsResponse.contentAsString);

    System.log(grps.content.length);

    //System.log(JSON.stringify(grps, null, 2));

}

else

{

    throw "property groups list failed!";

}

var names = [];

if(grps)

{

    for each(var grp in grps.content)

    {

        names.push(grp.label);

    }

}

names = names.sort();

for each(var name in names)

{

    var grp = findPropertyGroup(grps.content, name);

    if(grp)

    {

        printGroupPropertyNamesAndValues(grp);

    }

}

You may also be able to get this done with the RestClient object you get from the vCACCAFEHost

Anyway, HTH

qc4vmware
Virtuoso
Virtuoso

For now, until I find a better way, I am indeed reconstructing the properties myself.  The issues I have run into in the past with some products is order is not always guaranteed everywhere.  So in this instance we simply have a list of property group names.  We assume there is some sort of indexing or consistency on how they are stored or retrieved.  I've run into issues where a list in one area of a gui or api may not have order enforced.  Even though it should be logically.  In some instances a list could be stored as some meta data, that was not ordered or maybe ends up in order of records in a database, one of any scenario where sort is not as expected.  I'll later discover that items in the database are indexed and lets say during the blueprint deployment the services associated with the deployment sort/merge everything as expected.  Viewing in the gui and hitting the "Show merged properties" will do the same thing.  But maybe the value at vCACCAFE:ComponentDeclaration.getPropertyGroups() is not in a guaranteed order.  Hopefully it is and always will be!  I'm telling you this "sometimes" sorted info has bitten me more than once that why I was hoping for a method in one of the services that would do it for me.

This was my solution where bp is a composite blueprint:

var myComponent = bp.components.get("my_component");

var propertyGroups = myComponent.getPropertyGroups();

var mergedProps = {};

for (var i=propertyGroups.length - 1; i >= 0; i--) {

     System.debug("Looking up property group with ID: " + propertyGroups[i]);

     // below is an action I created for retrieving property groups

     var pg = System.getModule("com.qualcomm.common.vcaccafe.propertygroup").QCgetPropertyGroups(host,propertyGroups[i])[0];

     System.debug("Got PG: " + pg.id);

     var props = pg.properties;

     System.debug(props.keys);

     for each (var key in props.keys) {

          mergedProps[key] = props.get(key).getValue().getValue();

     }

}

Reply
0 Kudos