Here's some sample code from a workflow I wrote for vCloud DIrector 1 that imported users from a csv file that was uploaded to the vCO server. The variable "csvfile" is my MimeAttachment input, the server was configured to allow read/write in the c:\orchestrator folder....
var csvContent = csvFile.content; var csvFileName = "c:\\Orchestrator\\csvFile.csv"; csvFile.write("c:\\Orchestrator\\","csvFile.csv"); System.log("csvFileName: "+csvFileName); var host = organization.parent; // Now use file to create FileReader object: var fileReader = new FileReader(csvFileName); if (fileReader.exists){ fileReader.open(); var line=fileReader.readLine(); System.log("Headers: \n"+line); line = fileReader.readLine(); while (line != null){ var lineValues = line.split(","); var username = lineValues[0]; var username = lineValues[0]; var password = lineValues[1]; var enabled = lineValues[2]; var role = lineValues[3]; var fullname = lineValues[4]; var email = lineValues[5]; var phone = lineValues[6]; var im = lineValues[7]; var storedvmquota = lineValues[8]; var runningvmquota = lineValues[9]; var vcdRole = System.getModule("com.vmware.pso.vcd.roles").getRoleByName(host,role); System.log("Role name found: "+vcdRole.name); System.log("Attempting to create User Params"); var userParams = System.getModule("com.vmware.library.vCloud.Admin.User").createUserParams( username, fullname, enabled, false, email, phone, null, password, vcdRole, false, runningvmquota, false, storedvmquota ); var userOut = System.getModule("com.vmware.library.vCloud.Admin.User").createUser(organization, userParams); userOut.im = im; userOut.update(); System.log(userOut.name + " imported..."); line = fileReader.readLine(); } fileReader.close(); // now cleanup by deleting the uploaded file from the server var file = new File(csvFileName); System.log("Filename: "+file.name); deleteFile(csvFileName); }else{ System.log("Error! File not found! "+csvFileName); } function deleteFile(fileName) { var file = new File(fileName); if (file.exists) { file.deleteFile(); } }
And here's sample code that retrieves a VC:VirtualMachine object by vm name where "vmName" is the String input containing the Name to search for and "vCenterVM" is the resulting VC:VirtualMachine object:
var vms = VcPlugin.getAllVirtualMachines(null, "xpath:name='"+vmName+"'"); var vCenterVM = null; if (vms != null){ if (vms.length == 1){ System.log("Match found for vm named: "+vmName); vCenterVM = vms[0]; }else{ System.log("More than one VM found with that name! "+vmName); for each (vm in vms){ System.log("VM ID: "+vm.id); } } }