VMware Cloud Community
eric_krejci
Enthusiast
Enthusiast

[vRA 7.4] how to get a list of VM names from the deployment ID

Hello,

in the post expiration step of disposing a machine, I have several software components that have uninstall sequence that need to be executed (unregister the machine from a centralise maangement tool, etc...). by default the expired machine is powered off and deleted when the archiving period is over. thus the uninstall sequence of software components are not executed.

I'm trying to power on the machine when the event (component request) DESTROY on specific componentID happend. the thing is I can only retreive the deploymentID from this event. no direct info of its parent machine.

do you have a way to find the VM that are in the deployment based on its ID?

thank you in advance for your help

sincerely

Eric

0 Kudos
7 Replies
eric_krejci
Enthusiast
Enthusiast

I did some interresting discovery.

I can get ids from vCACCAFEEntitiesFinder.findCatalogResources("VCAHOST") for components and machines. the thing I found is when I want to use an event subscription (ComponentRequest) based on a specific condition, the variable deploymentId that come with the event as nothing to do with the ones I retreive from the  vCACCAFEEntitiesFinder.findCatalogResources(). Thus I cannot do any matching of any kind.

my initial idea was to use the deploymentId coming with the event to find the component from  vCACCAFEEntitiesFinder.findCatalogResources() and getting the VMs on which the component is linked with the parentResourceRef 

anyone know how I could do this matching from the delploymentId that comes with the event?

 

many thanks

Eric

0 Kudos
qc4vmware
Virtuoso
Virtuoso

Have you looked at using the machine lifecycle event instead of the component action?  You get a ton more information in those events.  You should be able to grab the catalog resource for the deployment though from the information in that event.  Can you post a sample of your code?

Paul

0 Kudos
qc4vmware
Virtuoso
Virtuoso

This code should return a list of all the child resources associated with that deploymentID.  Give it a shot.

var id = deploymentId;

var host = my_vCACCAFE:VCACHost;  // set this to your host

var resources = new Array();

var filter = new Array();

filter[0] = vCACCAFEFilterParam.equal("parentResource", vCACCAFEFilterParam.string(id));

var query = vCACCAFEOdataQuery.query().addFilter(filter);

var odataRequest = new vCACCAFEPageOdataRequest(1, 10000, query);

var service = host.createCatalogClient().getCatalogConsumerResourceService();

var resources = service.getResourcesList(odataRequest);

if (resources) {

     for each (var resource in resources) {

          System.debug("resourceTypeRef Label: " + resource.resourceTypeRef.getLabel());

          System.debug("resourceTypeRef Id: " + resource.resourceTypeRef.getId());

     }

}

return resources;

0 Kudos
eric_krejci
Enthusiast
Enthusiast

Hello Paul,

 

thank you for your reply,

I check on the machine lifecycle event already but it is initialized after the effective component destroy upon which I want to retreive its parent machine.

I also tried your script and the result is empty as the deploymentId generated by the DESTROY event as nothing to do with the deployment ID you can find in the deployment itself when querying the VCACHost.

 

for instance the deploymentId from the event is: b98e5151-e165-4327-a1dd-04307221119f 

 

and the one from the component beeing destroyed is: c667ab11-24f7-48db-9f0c-be984d4462fd

 

I also receive an error in the workflow for the:

return resources;

it says that it's an invalid return

thank you

 

Eric

0 Kudos
qc4vmware
Virtuoso
Virtuoso

Try using this subscription

Event Topic = Machine Lifecycle

Conditions

     Data > Lifecycle State > State phase = PRE

     Data > Lifecycle State > Lifecycle state name = VMPSMasterWorkflow32.disposing

Make it blocking.

This should allow you to do whatever you need before anything is destroyed.  It will run once per machine.

Paul

0 Kudos
eric_krejci
Enthusiast
Enthusiast

I tried. its the first direction I took but the event based on VMPSMasterWorkflow32.disposing is triggered when the machine itself is set for disposing. this happens after the child components of the machine are "uninstall".

in my context when an uninstallation of a software component must be executed from within the machine, and the machine is off, it doesn't work properly. that is the reason why I want to trap the suppression's event of the component in order to power on the machine, and beeing able to have the component uninstalled properly.

to be honest I'm suprised that the deploymentId from the event as nothing to do with the one set in the conponent/machine itself. I really wonder how the system can find the proper items.

actually I'm completely stuck...

0 Kudos
qc4vmware
Virtuoso
Virtuoso

I'd expect that code to fail if you are giving it the deployment id from the new request... I don't have time to run through all of this at the moment.  Another thing you might try is this... use a property of the component __Cafe.Root.Request.Id .  Try this in your subscription workflow:

//Collect info from payload input

lifecycleState = payload.get("lifecycleState");

componentId = payload.get("componentId");

blueprintName = payload.get("blueprintName");

componentTypeId = payload.get("componentTypeId");

machine = payload.get("machine");

machineProperties= machine.get("properties");

requestId = payload.get("requestId");

endpointId = payload.get("endpointId");

//COLLECT vRA INFO TO USE IN LOGGING

System.log("------List Properties------");

System.log("requestId: " + requestId);

System.log("machine.id: " + machine.get("id"))

System.log("machine.name: " + machine.get("name"))

System.log("machine.type: " + machine.get("type"))

System.log("machine.owner: " + machine.get("owner"))

System.log("machine.externalReference: " + machine.get("externalReference"))

System.log("lifecycleState.event: " + lifecycleState.get("event"))

System.log("lifecycleState.phase: " + lifecycleState.get("phase"))

System.log("lifecycleState.state: " + lifecycleState.get("state"))

System.log("componentId: " + componentId);

System.log("blueprintName: " + blueprintName );

System.log("componentTypeId: " + componentTypeId);

System.log("endpointId: " + endpointId);

vmName = machine.get("name");

//Collect vRA VM properties from VM Entity

var properties = new Properties();

properties.put("VirtualMachineID", machine.get("id"));

virtualMachineEntity = vCACEntityManager.readModelEntity(host.id, "ManagementModelEntities.svc", "VirtualMachines", properties, null);

var vCACVm = virtualMachineEntity.getInventoryObject();

vmProperties = new Properties();

var virtualMachinePropertiesEntities = virtualMachineEntity.getLink(host, "VirtualMachineProperties");

for each (var virtualMachinePropertiesEntity in virtualMachinePropertiesEntities) {

var propertyName = virtualMachinePropertiesEntity.getProperty("PropertyName");

var propertyValue = virtualMachinePropertiesEntity.getProperty("PropertyValue");

System.debug("Found property " + propertyName + " = " + propertyValue);

vmProperties.put(propertyName, propertyValue);

}

var requestId = vmProperties.get("__Cafe.Root.Request.Id");

var host = myvCACCAFE:VCACHost;

var resources = new Array();

var requestService = host.createCatalogClient().getCatalogConsumerRequestService();

var resources = requestService.getResourcesProvisionedByRequest(requestId,null).getContent();

//Loop through results

//Only return vms for now

var vms = new Array();

var deployments = new Array();

for (var i = 0; i < resources.length; i++){

System.debug("Id: " + resources[i].id);

System.debug("Name: " + resources[i].name);

System.debug("resourceTypeRef Label: " + resources[i].resourceTypeRef.getLabel());

System.debug("resourceTypeRef Id: " + resources[i].resourceTypeRef.getId());

if (resources[i].resourceTypeRef.getLabel() === "Deployment") deployments.push(resources[i]);

if (resources[i].resourceTypeRef.getLabel() === "Virtual Machine") vms.push(resources[i]);

}

0 Kudos