VMware Cloud Community
Brad85
Contributor
Contributor
Jump to solution

Remove a NIC from multiple vCloud VMs

Hi,

I am a newbie to vCO and extremely raw with JavaScript (a great combination).  My production environment consist of vCenter 5.0, vCD 5.1 and vCO 5.1. I have the vCD plugin installed and working with Orchestrator.  I am trying to build a workflow which removes NIC1 (Network adapter 1) from an array of vCloud VMs.  I took a copy of the vCloud Director Library Workflow (Remove a NIC) and edited the input parameter vms to be an Array/vCloud:VM.  I have amended the scriptable taks so that it loops through the VMs in the array and returns their network cards.  It appears that the NICs are being returned as a UID (I returned this to the screen via a System.Log).  This UID obviously does not match with what I input to the workflow when executing - I specify "Network adapter 1" and my workflow fails with an error "No primary network connection found; majorErrorCode=400; minorErrorCode=BAD_REQUEST; vendorSpecificErrorCode=null; (Workflow:Remove a NIC 1 / Remove Nic (item0)#22)"  Here is the code I am running:

if (vm.Status != VclVMStatus.POWERED_OFF.value) {
throw "VM is not powered off";
}
var found = false;
var len=vms.length;
for (var i=0; i < len; i++ )
{
var VM = vms[i];
var nics = VM.getNetworkCards();
System.log(nics);
var newNics = new Array();
for (i = 0; i < nics.length; i++) {
if (nics[i].itemResource.elementName.value != nicName) {
newNics.push(nics[i]);
} else {
found = true;
}
}
if (!found) {
throw "Invalid NIC name";
}
task = VM.updateNetworkCards(newNics);
}

As I mentioned earlier I am a complete newb to vCO\JavaScript and would really appreciate some input\advice.  If there is a better method of removing network adapter 1 from an array of vCloud VMs, or if somebody has already developed a workflow for this, I would be most grateful for your help.

Thanks


0 Kudos
1 Solution

Accepted Solutions
cdecanini_
VMware Employee
VMware Employee
Jump to solution

Try this :

var section = vm.getNetworkConnectionSection();
System.log("Primary card was NIC : " + section.primaryNetworkConnectionIndex);
section.primaryNetworkConnectionIndex = 1;
task = vm.updateSection(section);

Worked here.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter

View solution in original post

0 Kudos
8 Replies
tschoergez
Leadership
Leadership
Jump to solution

moved to the Orchestrator forums (away for the Plugin Development forums)

0 Kudos
cdecanini_
VMware Employee
VMware Employee
Jump to solution

Welcome.

The way you would typically use vCO would be different :

  • Create a "Delete a NIC with exception handling" workflow where you insert the "Delete a nic workflow" create an exception link to a Server.warn log element. This way if agiven nic cannot be deleted the workflow will still complete successfully but with a warning.

  • Create a workflow taking the nic name and an array of VMs as input (or maybe a container such as a vApp, an array of vApps). Use the for each element to pass the VMs of your array to the "Delete a NIC with exception handling" workflow.

This way you everage library elements wihtout having to write much code. The warning will give you the error message on each VM so you can check if this was specific to a VM or if you have a mismatch for all of them.

Christophe.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
Brad85
Contributor
Contributor
Jump to solution

Thanks cdecanini,

I have had made steady progress using the Foreach element as you suggested.  I am now passing an array of VMs which are successfully looping and calling the "Remove a NIC".  I am trying to remove Network adapter 1, but this is set to the primary NIC so my workflow errors with "No primary network connection found".  If I change the primary NIC to nic0 in vCD and run my workflow again, Network adapter 1 successfully removes for all VMs in the array.  Do you know how the primary NIC can be changed in vCO via a workflow or JavaScript?  I would like to incorporate this into my Remove a NIC workflow so the primary NIC is changed prior to removing Network adapter 1.  Your help again would be most appreciated.

Thanks

0 Kudos
cdecanini_
VMware Employee
VMware Employee
Jump to solution

I will try to have a look at that next week since it was not possible today.

Christophe.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
0 Kudos
Brad85
Contributor
Contributor
Jump to solution

Thanks Christophe that would be much appreciated.

0 Kudos
cdecanini_
VMware Employee
VMware Employee
Jump to solution

Try this :

var section = vm.getNetworkConnectionSection();
System.log("Primary card was NIC : " + section.primaryNetworkConnectionIndex);
section.primaryNetworkConnectionIndex = 1;
task = vm.updateSection(section);

Worked here.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
0 Kudos
Brad85
Contributor
Contributor
Jump to solution

Thanks Christophe,

I am currently testing this and will let you know how it goes. Your input is very much appreciated.

0 Kudos
Brad85
Contributor
Contributor
Jump to solution

Worked a treat.

Thanks Christophe

0 Kudos