VM Annotations + Moving a VM to Different Folder

Version 5
    Task NameVM Annotations + Moving a VM to Different Folder
    DescriptionVM Annotations + Moving a VM to Different Folder
    Prerequisites
    1. Tested on 5.2.0.2
    CategoryWorkflow (and tasks)
    Components

    VM Annotations + Moving a VM to Different Folder

    Additional workflow

    can also create a new folder

    Instructions for Regular Workflow Use:

    1. Download the attached .ZIP file below to your computer. *Remember the location of the saved file on your computer.
    2. Unzip the file on your computer. Should end up with a .WFD file.
    3. Log in to UCS Director as a user that has "system-admin" privileges.
    4. Navigate to "Policies-->Orchestration" and click on "Import".
    5. Click "Browse" and navigate to the location on your computer where the .WFD file resides. Choose the .WFD file and click "Open".
    6. Click "Upload" and then "OK" once the file upload is completed. Then click "Next".
    7. Click the "Select" button next to "Import Workflows". Click the "Check All" button to check all checkboxes and then the "Select" button.
    8. Click "Submit".
    9. A new folder should appear in "Policies-->Orchestration" that contains the imported workflow.

     

    Inspired by work done by Harsha Ghanta.

     

    The Workflow input:

     

    Screen Shot 2015-04-07 at 6.40.49 PM.png

     

     

    The current state of a VM:

     

    Screen Shot 2015-04-07 at 6.40.01 PM.png

     

    The workflow execution:

     

    Screen Shot 2015-04-07 at 6.41.37 PM.png

     

    The Moved VM:

     

    Screen Shot 2015-04-07 at 6.42.25 PM.png

     

    The annotation:

     

    Screen Shot 2015-04-07 at 6.42.37 PM.png

     

    The log file:

     

    Service Request ID: 4652

    Workflow Inputs:

    - vmid

    - Folder

    - AnnotationsField

    - AnnotationsValue

     

     

     

     

    Apr 07, 2015 18:33:16 CDT Request submitted

    Apr 07, 2015 18:33:17 CDT Executing workflow item number 1

    Apr 07, 2015 18:33:17 CDT Completed workflow item number 1, with status Completed

    Apr 07, 2015 18:33:23 CDT Executing workflow item number 2

    Apr 07, 2015 18:33:23 CDT Trigger context executeWorkFlowStep called

    Apr 07, 2015 18:33:23 CDT Executing custom action VM Folder Move (custom_VM Annotations)

    Apr 07, 2015 18:33:23 CDT Executing custom action VM Folder Move (custom_VM Annotations) for VM 1749 (epsilon0002)

    Apr 07, 2015 18:33:23 CDT Executing custom script for VM Annotations

    Apr 07, 2015 18:33:25 CDT VM ID = 1749

    Apr 07, 2015 18:33:25 CDT Folder = undefined

    Apr 07, 2015 18:33:25 CDT Custom Name: SAP3

    Apr 07, 2015 18:33:25 CDT Custom Value: Stats Server1

    Apr 07, 2015 18:33:25 CDT Gvm = com.cloupia.model.cIM.GenericVM@65eda2ba

    Apr 07, 2015 18:33:25 CDT Account Name = Dallas

    Apr 07, 2015 18:33:25 CDT Host Node = 172.17.32.113

    Apr 07, 2015 18:33:25 CDT Cluster Name = Dallas Cluster1

    Apr 07, 2015 18:33:26 CDT Root Folder = Folder:group-d1 @ https://172.17.32.110/sdk

    Apr 07, 2015 18:33:26 CDT Key: 103

    Apr 07, 2015 18:33:26 CDT Value: SAP3

    Apr 07, 2015 18:33:34 CDT Annotation added successfully

    Apr 07, 2015 18:33:34 CDT Task #1 (VM Folder Move (custom_VM Annotations)) completed successfully in 10 seconds

    Apr 07, 2015 18:33:34 CDT Input/Output values for Task #1 (VM Folder Move (custom_VM Annotations)):

    Apr 07, 2015 18:33:34 CDT [Template Input:vmid = ${VM_ID}]

    Apr 07, 2015 18:33:34 CDT [Resolved Template Input: vmid = 1749]

    Apr 07, 2015 18:33:34 CDT [Local Input: vmid = 1749]

    Apr 07, 2015 18:33:34 CDT [Mapped Input: AnnotationsField = SAP3]

    Apr 07, 2015 18:33:34 CDT [Mapped Input: AnnotationsValue = Stats Server1]

    Apr 07, 2015 18:33:34 CDT Completed workflow item number 2, with status Completed

    Apr 07, 2015 18:33:39 CDT Executing workflow item number 3

    Apr 07, 2015 18:33:39 CDT Trigger context executeWorkFlowStep called

    Apr 07, 2015 18:33:39 CDT Executing custom action VM Folder Move (custom_VM Folder Move)

    Apr 07, 2015 18:33:39 CDT Executing custom action VM Folder Move (custom_VM Folder Move) for VM 1749 (epsilon0002)

    Apr 07, 2015 18:33:39 CDT Executing custom script for VM Folder Move

    Apr 07, 2015 18:33:41 CDT VM ID = 1749

    Apr 07, 2015 18:33:41 CDT Folder = Level1

    Apr 07, 2015 18:33:41 CDT Gvm = com.cloupia.model.cIM.GenericVM@70f0eb69

    Apr 07, 2015 18:33:42 CDT Account Name = Dallas

    Apr 07, 2015 18:33:42 CDT Host Node = 172.17.32.113

    Apr 07, 2015 18:33:42 CDT Cluster Name = Dallas Cluster1

    Apr 07, 2015 18:33:42 CDT Root Folder = Folder:group-d1 @ https://172.17.32.110/sdk

    Apr 07, 2015 18:33:42 CDT FD = Folder:group-v1405 @ https://172.17.32.110/sdk

    Apr 07, 2015 18:33:42 CDT Task #2 (VM Folder Move (custom_VM Folder Move)) completed successfully in 2 seconds

    Apr 07, 2015 18:33:42 CDT Input/Output values for Task #2 (VM Folder Move (custom_VM Folder Move)):

    Apr 07, 2015 18:33:42 CDT [Template Input:vmid = ${VM_ID}]

    Apr 07, 2015 18:33:42 CDT [Resolved Template Input: vmid = 1749]

    Apr 07, 2015 18:33:42 CDT [Local Input: vmid = 1749]

    Apr 07, 2015 18:33:42 CDT [Mapped Input: Folder = Level1]

    Apr 07, 2015 18:33:42 CDT Completed workflow item number 3, with status Completed

    Apr 07, 2015 18:33:45 CDT Executing workflow item number 4

    Apr 07, 2015 18:33:45 CDT Completed workflow item number 4, with status Completed

     

     

    The Workflow:

     

    Screen Shot 2015-04-07 at 6.48.16 PM.png

     

     

    The Custom Tasks:

     

    Screen Shot 2015-04-07 at 6.49.01 PM.png

     

     

     

     

    The code for the folder move VM:

     

    importPackage(java.util);

    importPackage(java.lang);

    importPackage(java.io);

    importPackage(com.cloupia.lib.util);

    importPackage(com.cloupia.model.cIM);

    importPackage(com.cloupia.service.cIM.inframgr);

    importPackage(org.apache.commons.httpclient);

    importPackage(org.apache.commons.httpclient.cookie);

    importPackage(org.apache.commons.httpclient.methods);

    importPackage(org.apache.commons.httpclient.auth);

    importPackage(com.cloupia.model.cEvent.notify);

    importPackage(com.cloupia.lib.util.mail);

    importPackage(com.cloupia.fw.objstore);

    importPackage(com.cloupia.service.cIM.inframgr);

    importPackage(com.cloupia.lib.cIaaS.vmware);

    importPackage(com.vmware.vim25);

    importPackage(com.vmware.vim25.mo);

    importPackage(com.cloupia.lib.util.managedreports);

     

     

    var VMID=input.vmid;

    logger.addInfo("VM ID  = "+VMID);

    var folder=input.folder;

    logger.addInfo("Folder = " + folder);

     

     

     

     

    var gvm = InfraPersistenceUtil.getVMById(VMID);

    var accountName = gvm.getAccountName();

    var account = InfraPersistenceUtil.getAccount(accountName);

    var hostNode = gvm.getParentHost();

    var hosts = InfraPersistenceUtil.getVMWareHostsByAccountAndHostNode(accountName, hostNode);

    var clusterName = hosts[0].getClusterName();

    var si = new VCenterConnectionManager(account).getServiceInstance();

    var mgr = si.getCustomFieldsManager();

    var rootFolder = si.getRootFolder();

     

     

    logger.addInfo("Gvm = " + gvm);

    logger.addInfo("Account Name = " + accountName);

    logger.addInfo("Host Node = " + hostNode);

    logger.addInfo("Cluster Name = " + clusterName);

    logger.addInfo("Root Folder = " + rootFolder);

     

     

    //

    // Moving VM to different folder

    //

     

     

    var vm = new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", gvm.getInstanceId());

    var fd= new InventoryNavigator(rootFolder).searchManagedEntity("Folder", folder);

    logger.addInfo("FD = " + fd);

    if ( fd == null ) {

      logger.addInfo("The selected Folder does not exist = " + folder);

      logger.addInfo("Please Create folder in vCenter at desired Level = " + folder);

      //var fd2 = new InventoryNavigator(rootFolder).searchManagedEntity("Folder", "/root");

      //fd2.CreateFolder(folder);

      }

      else

      {

      fd.moveIntoFolder_Task([vm]);

      }

     

     

    The Code for the VM annotations:

     

    importPackage(java.util);

    importPackage(java.lang);

    importPackage(java.io);

    importPackage(com.cloupia.lib.util);

    importPackage(com.cloupia.model.cIM);

    importPackage(com.cloupia.service.cIM.inframgr);

    importPackage(org.apache.commons.httpclient);

    importPackage(org.apache.commons.httpclient.cookie);

    importPackage(org.apache.commons.httpclient.methods);

    importPackage(org.apache.commons.httpclient.auth);

    importPackage(com.cloupia.model.cEvent.notify);

    importPackage(com.cloupia.lib.util.mail);

    importPackage(com.cloupia.fw.objstore);

    importPackage(com.cloupia.service.cIM.inframgr);

    importPackage(com.cloupia.lib.cIaaS.vmware);

    importPackage(com.vmware.vim25);

    importPackage(com.vmware.vim25.mo);

    importPackage(com.cloupia.lib.util.managedreports);

     

     

    var VMID=input.vmid;

    logger.addInfo("VM ID  = "+VMID);

    var folder=input.folder;

    logger.addInfo("Folder = " + folder);

    var customName=input.AnnotationsField;

    logger.addInfo("Custom Name: "+customName);

    var customValue = input.AnnotationsValue;

    logger.addInfo("Custom Value: "+customValue);

     

     

    var gvm = InfraPersistenceUtil.getVMById(VMID);

    var accountName = gvm.getAccountName();

    var account = InfraPersistenceUtil.getAccount(accountName);

    var hostNode = gvm.getParentHost();

    var hosts = InfraPersistenceUtil.getVMWareHostsByAccountAndHostNode(accountName, hostNode);

    var clusterName = hosts[0].getClusterName();

    var si = new VCenterConnectionManager(account).getServiceInstance();

    var mgr = si.getCustomFieldsManager();

    var rootFolder = si.getRootFolder();

     

     

    logger.addInfo("Gvm = " + gvm);

    logger.addInfo("Account Name = " + accountName);

    logger.addInfo("Host Node = " + hostNode);

    logger.addInfo("Cluster Name = " + clusterName);

    logger.addInfo("Root Folder = " + rootFolder);

     

     

    //

    // VM annotations

    //

     

     

    var vm = new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", gvm.getInstanceId());

    var customFieldsVMMap = VMWareUtils.getCustomFieldsVMMap(si);

    if(customFieldsVMMap != null && customFieldsVMMap.containsValue(customName))

    {

      var key = 0;

      var fieldSet = customFieldsVMMap.keySet();

      var it = fieldSet.iterator();

      while(it.hasNext())

      {

        key = it.next();

        var value = customFieldsVMMap.get(key);

        if(customName.equals(value)) {

          logger.addInfo("Key: "+key);

          logger.addInfo("Value: "+value);

          break;

        }

      }

      mgr.setField(vm, parseInt(key), customValue);

      }

      else

      {

      var  def = mgr.addCustomFieldDef(customName, "VirtualMachine", null, null);

      mgr.setField(vm, def.getKey(), customValue);

      }

     

     

    VMWareUtils.collectHostorClusterInventory(account.getAccountName(), hostNode, clusterName);

    logger.addInfo("Annotation added successfully");

     

     

    Another example presented by (

     

    (Presented by Jeremy Guthrie (cdw))

     

    If you have a folder structure like:

     

    A\X

    B\X

    C\X

     

    Example #5 will put the VM into the folder you point it to, e.g. B\X puts the VM int folder B\ folder X using that vmwarefolderidentity type.