vCO Workflow Script to Create a Distributed Virtual Switch – Part 2

The following is a multi-part series of vCO Workflow Scripts to Create a Distributed Virtual Switch – Part 2

 

System.log("====================================================================================================");

System.log("===== Building dvSwitch Uplink Port Policy");

var objVcDVSNameArrayUplinkPortPolicy;
	objVcDVSNameArrayUplinkPortPolicy = new VcDVSNameArrayUplinkPortPolicy();
	objVcDVSNameArrayUplinkPortPolicy.uplinkPortName = arrUplinkPortNames;

System.log("===== Building dvSwitch Contact Info");

var objVcDVSContactInfo;
	objVcDVSContactInfo = new VcDVSContactInfo();
	objVcDVSContactInfo.name = "Administrator";
	objVcDVSContactInfo.contact = "Administrator";

System.log("===== Building dvSwitch Policy");

var objVcDVSPolicy;
	objVcDVSPolicy = new VcDVSPolicy();
	objVcDVSPolicy.autoPreInstallAllowed = true;
	objVcDVSPolicy.autoUpgradeAllowed = true;
	objVcDVSPolicy.partialUpgradeAllowed = true;

System.log("===== Building dvSwitch Port Group Boolean Policy");

var objVcBoolPolicy;
	objVcBoolPolicy = new VcBoolPolicy();
	objVcBoolPolicy.inherited = false;
	objVcBoolPolicy.value = false;

System.log("===== Building dvSwitch Port Group String Policy");

var objVcStringPolicy;
	objVcStringPolicy = new VcStringPolicy();
	objVcStringPolicy.inherited = false;
	objVcStringPolicy.value = "-1";

System.log("===== Building an Array of VLANs");

var arrVcNumericRangeVLANs;
	arrVcNumericRangeVLANs = new Array();

for ( var i = 0; i < arrPortGroups.length; i++ )
{
	var arrPortGroup;
		arrPortGroup = arrPortGroups[i];
		
	var intPortGroupVLAN;
		intPortGroupVLAN = arrPortGroup[1];
		
	var blnExistsInAllowedTrunk;
		blnExistsInAllowedTrunk = false;
		
	for ( var ii = 0; ii < arrVcNumericRangeVLANs.length; ii++ )
	{
		var objVcNumericRangeVLANs;
			objVcNumericRangeVLANs = arrVcNumericRangeVLANs[ii];
		
		if ( (objVcNumericRangeVLANs.start == intPortGroupVLAN) || (objVcNumericRangeVLANs.end == intPortGroupVLAN) ) 
		{
			blnExistsInAllowedTrunk = true;
			
			break;
		}	
	}

	if ( blnExistsInAllowedTrunk == false )
	{
		var objVcNumericRange;
			objVcNumericRange = new VcNumericRange();
			objVcNumericRange.start = intPortGroupVLAN;
			objVcNumericRange.end = intPortGroupVLAN;
		
		System.log("===== Adding VLAN " + intPortGroupVLAN + " to the Trunk Allowed VLANs");
		
		arrVcNumericRangeVLANs.push(objVcNumericRange);		
	}
}

System.log("===== Building dvSwitch Trunk VLAN Specification");

var objVcVmwareDistributedVirtualSwitchTrunkVlanSpec;
	objVcVmwareDistributedVirtualSwitchTrunkVlanSpec = new VcVmwareDistributedVirtualSwitchTrunkVlanSpec();
	objVcVmwareDistributedVirtualSwitchTrunkVlanSpec.inherited = false;
	objVcVmwareDistributedVirtualSwitchTrunkVlanSpec.vlanId = arrVcNumericRangeVLANs;

System.log("===== Building dvSwitch Port Setting");

var objVcVMwareDVSPortSetting;
	objVcVMwareDVSPortSetting = new VcVMwareDVSPortSetting();
	objVcVMwareDVSPortSetting.vlan = objVcVmwareDistributedVirtualSwitchTrunkVlanSpec;
	objVcVMwareDVSPortSetting.blocked = objVcBoolPolicy;
	objVcVMwareDVSPortSetting.vmDirectPathGen2Allowed = objVcBoolPolicy;
	objVcVMwareDVSPortSetting.networkResourcePoolKey = objVcStringPolicy;


System.log("===== Building dvSwitch Link Discovery Protocol Configuration");

var objVcLinkDiscoveryProtocolConfig;
	objVcLinkDiscoveryProtocolConfig = new VcLinkDiscoveryProtocolConfig();
	objVcLinkDiscoveryProtocolConfig.protocol = "cdp";
	objVcLinkDiscoveryProtocolConfig.operation = "both";

System.log("===== Building an Array of dvSwitch Host Member Config Specifications");

var arrVcDistributedVirtualSwitchHostMemberConfigSpec;
	arrVcDistributedVirtualSwitchHostMemberConfigSpec = new Array();

System.log("===== Building an Array of vCenter Sdk COnnections");

var arrVcSdkConnection;
	arrVcSdkConnection = VcPlugin.allSdkConnections;

System.log("===== Looping through an Array of vCenter Sdk COnnections");

for ( var i = 0; i < arrVcSdkConnection.length; i++ )
{
	var objVcSdkConnection;
		objVcSdkConnection = arrVcSdkConnection[i];
		
	System.log("===== Building an Array of vSphere ESXi Host Systems");

	var arrVcHostSystem;
		arrVcHostSystem = objVcSdkConnection.allHostSystems;

	System.log("===== Looping through an Array of vSphere ESXi Host Systems");

	for ( var ii = 0; ii < arrVcHostSystem.length; ii++ )
	{
		var objVcHostSystem;
			objVcHostSystem = arrVcHostSystem[ii];

		System.log("===== Building a dvSwitch Host Member Config Specification for: " + objVcHostSystem.name);

		var objVcDistributedVirtualSwitchHostMemberConfigSpec;
			objVcDistributedVirtualSwitchHostMemberConfigSpec = new VcDistributedVirtualSwitchHostMemberConfigSpec();
			objVcDistributedVirtualSwitchHostMemberConfigSpec.operation = "add";
			objVcDistributedVirtualSwitchHostMemberConfigSpec.host = objVcHostSystem.reference;	
			
		System.log("===== Adding a dvSwitch Host Member Config Specification to an Array");

		arrVcDistributedVirtualSwitchHostMemberConfigSpec.push(objVcDistributedVirtualSwitchHostMemberConfigSpec);
	}
}

System.log("===== Building dvSwitch Configuration Specification");

var objVcVMwareDVSConfigSpec;
	objVcVMwareDVSConfigSpec = new VcVMwareDVSConfigSpec();
	objVcVMwareDVSConfigSpec.name = "dvSwitch-vSphere-Services";
	objVcVMwareDVSConfigSpec.description = "dvSwitch-vSphere-Services";
	objVcVMwareDVSConfigSpec.uplinkPortPolicy = objVcDVSNameArrayUplinkPortPolicy;
	objVcVMwareDVSConfigSpec.configVersion = "5.1.0";
	objVcVMwareDVSConfigSpec.contact = objVcDVSContactInfo;
	objVcVMwareDVSConfigSpec.defaultProxySwitchMaxNumPorts = 1024;
	objVcVMwareDVSConfigSpec.policy = objVcDVSPolicy;
	objVcVMwareDVSConfigSpec.defaultPortConfig = objVcVMwareDVSPortSetting;
	objVcVMwareDVSConfigSpec.maxMtu = 9000;
	objVcVMwareDVSConfigSpec.linkDiscoveryProtocolConfig = objVcLinkDiscoveryProtocolConfig;
	objVcVMwareDVSConfigSpec.host = arrVcDistributedVirtualSwitchHostMemberConfigSpec;

System.log("===== Building dvSwitch Product Specification");

var objVcDistributedVirtualSwitchProductSpec;
	objVcDistributedVirtualSwitchProductSpec = new VcDistributedVirtualSwitchProductSpec();
	objVcDistributedVirtualSwitchProductSpec.name = "DVS";
	objVcDistributedVirtualSwitchProductSpec.vendor = "VMware";
	objVcDistributedVirtualSwitchProductSpec.version = "5.1.0";

System.log("===== Building dvSwitch Host Product Specification");

var arrVcDistributedVirtualSwitchHostProductSpec;
	arrVcDistributedVirtualSwitchHostProductSpec = new Array();

System.log("===== Building dvSwitch Capability");

var objVcDVSCapability;
	objVcDVSCapability = new VcDVSCapability();
	objVcDVSCapability.compatibleHostComponentProductInfo = arrVcDistributedVirtualSwitchHostProductSpec;
	objVcDVSCapability.dvPortGroupOperationSupported = true;
	objVcDVSCapability.dvPortOperationSupported = true;
	objVcDVSCapability.dvsOperationSupported = true;

System.log("===== Building dvSwitch Creation Specification");

var objVcDVSCreateSpec;
	objVcDVSCreateSpec = new VcDVSCreateSpec();
	objVcDVSCreateSpec.configSpec = objVcVMwareDVSConfigSpec;
	objVcDVSCreateSpec.productInfo = objVcDistributedVirtualSwitchProductSpec;
	objVcDVSCreateSpec.capability = objVcDVSCapability;

System.log("====================================================================================================");

System.log("===== Executing the dvSwitch Creation Task");

var objVcTask;
	objVcTask = objVcNetworkFolder.createDVS_Task(objVcDVSCreateSpec);
	
System.log("====================================================================================================");

var objVcVMwareDistributedVirtualSwitch;
	objVcVMwareDistributedVirtualSwitch = objModule.WaitForVcTask(objVcTask);

Leave a Reply