vCO Workflow Script to Create a vShield Load Balancer

The following is the script to create a vShield Load Balancer

 

var objVclAdminVdc;
	objVclAdminVdc = objVclVdc.toAdminObject();

var arrVclGateway;
	arrVclGateway = objVclAdminVdc.getGateways();

var objVclGateway;

for ( var i = 0; i < arrVclGateway.length; i++ )
{
	var objVclGatewayLoop;
		objVclGatewayLoop = arrVclGateway[i];

	var strVclGatewayName;
		strVclGatewayName = objVclGatewayLoop.name

	var strDataCenterName;
		strDataCenterName = strVclGatewayName.substring(0, 3);

	if ( strDataCenterName == strDeployDataCentre.toLowerCase() )
	{
		objVclGateway = objVclGatewayLoop;

		break;
	}
}

System.log("===== Trying to Lock " + objVclGateway.name + " for vCD Admin");

LockingSystem.lockAndWait( objVclGateway.name , "vcdadmin" );

objVclGateway.updateInternalState();

System.log("===== Locked " + objVclGateway.name + " for vCD Admin");

var objVclAdminOrgVdcNetwork;
	objVclAdminOrgVdcNetwork = objVclOrgVdcNetwork.toAdminObject();

var objVclNetworkConfiguration;
	objVclNetworkConfiguration = objVclAdminOrgVdcNetwork.configuration;

var objVclIpScopes;
	objVclIpScopes = objVclNetworkConfiguration.ipScopes;

var objVclObjectList;
	objVclObjectList = objVclIpScopes.ipScope;

var arrVclObjectList;
	arrVclObjectList = objVclObjectList.enumerate();

var objVclIpScope;
	objVclIpScope = arrVclObjectList[0];

var strGatewayIp;
	strGatewayIp = objVclIpScope.gateway;

var objRegExpIp;
	objRegExpIp = /^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)[0-9]{1,3}$/;

var arrGatewayIp;
	arrGatewayIp = objRegExpIp.exec(strGatewayIp);

var strNetwork;
	strNetwork = arrGatewayIp[1];

var strNetworkAddress;
	strNetworkAddress = strNetwork + "0";

var strLoadBalancerIpAddress;
	strLoadBalancerIpAddress = ""; getNextAvailableIP

System.log("===== The Next Available IP is: " + strLoadBalancerIpAddress);

var arrLoadBalancerIPAddress;
	arrLoadBalancerIPAddress = strLoadBalancerIpAddress.split('.');

var strOctetA = System.decimalToHex( arrLoadBalancerIPAddress[0] ).toLowerCase();
var strOctetB = System.decimalToHex( arrLoadBalancerIPAddress[1] ).toLowerCase();
var strOctetC = System.decimalToHex( arrLoadBalancerIPAddress[2] ).toLowerCase();
var strOctetD = System.decimalToHex( arrLoadBalancerIPAddress[3] ).toLowerCase();

if ( strOctetA.length == 1 ) { strOctetA = "0" + strOctetA; }
if ( strOctetB.length == 1 ) { strOctetB = "0" + strOctetB; }
if ( strOctetC.length == 1 ) { strOctetC = "0" + strOctetC; }
if ( strOctetD.length == 1 ) { strOctetD = "0" + strOctetD; }

var strLoadBalancerHostName;
	strLoadBalancerHostName = phase.toLowerCase() + product.toLowerCase() + "-" + strOctetB + strOctetC + strOctetD + "-vip.vcoflow.co.uk";	

var objVclLBPoolHealthCheck = new VclLBPoolHealthCheck();
	objVclLBPoolHealthCheck["timeout"] = "15";
	objVclLBPoolHealthCheck["uri"] = "/";
	objVclLBPoolHealthCheck["mode"] = "HTTP";
	objVclLBPoolHealthCheck["interval"] = "5";
	objVclLBPoolHealthCheck["healthThreshold"] = "2";
	objVclLBPoolHealthCheck["unhealthThreshold"] = "3";

var objVclLBPoolServicePort;
	objVclLBPoolServicePort = new VclLBPoolServicePort();
	objVclLBPoolServicePort["isEnabled"] = true;
	objVclLBPoolServicePort["port"] = "80";
	objVclLBPoolServicePort["protocol"] = "HTTP";
	objVclLBPoolServicePort["healthCheckPort"] = "80";
	objVclLBPoolServicePort["algorithm"] = "LEAST_CONN";
	objVclLBPoolServicePort["healthCheck"].add(objVclLBPoolHealthCheck);

var objVclLBPoolServicePortMember;
	objVclLBPoolServicePortMember = new VclLBPoolServicePort();
	objVclLBPoolServicePortMember["port"] = "80";
	objVclLBPoolServicePortMember["protocol"] = "HTTP";
	objVclLBPoolServicePortMember["healthCheckPort"] = "80";

var objVclLoadBalancerPool = new VclLoadBalancerPool();
	objVclLoadBalancerPool["operational"] = true;
	objVclLoadBalancerPool["name"] = strVclVAppName + "-Pool";
	objVclLoadBalancerPool["description"] = strLoadBalancerHostName;
	objVclLoadBalancerPool["errorDetails"] = "";
	objVclLoadBalancerPool["servicePort"].add(objVclLBPoolServicePort);

var arrVclVM;
	arrVclVM = objVclVApp.getChildrenVms();

var arrLbVclVM;
	arrLbVclVM = new Array();

for ( var a = 0; a < arrVclVM.length; a++ )
{
	var objVclVM;
		objVclVM = arrVclVM[a];

	System.log("objVclVM.name = " + objVclVM.name);

	for ( b = 0; b < objPropertiesLB.keys.length; b++ )
	{
		var strPropertyKey;
			strPropertyKey = objPropertiesLB.keys[b];

		if ( objVclVM.name == strPropertyKey )
		{
			arrLbVclVM.push(objVclVM);
		}
	}
}

for ( var c = 0; c < arrLbVclVM.length; c++ )
{
	var objVclVM;
		objVclVM = arrLbVclVM[c];

	System.log("===== objVclVM.name = " + objVclVM.name);

	var arrVclVirtualNetworkCard;
		arrVclVirtualNetworkCard = objVclVM.getNetworkCards();

	var objVclVirtualNetworkCard;

	for ( var d = 0; d < arrVclVirtualNetworkCard.length; d++ )
	{
		var objclVirtualNetworkCard;
			objclVirtualNetworkCard = arrVclVirtualNetworkCard[d];

		if ( objclVirtualNetworkCard.primaryNetworkConnection == true )
		{
			break;
		}
	}

	System.log("===== objVclVirtualNetworkCard.ipAddress = " + objVclVirtualNetworkCard.ipAddress);

	var objVclLBPoolMember;
		objVclLBPoolMember = new VclLBPoolMember();
		objVclLBPoolMember["weight"] = "1";
		objVclLBPoolMember["ipAddress"] = objVclVirtualNetworkCard.ipAddress;
		objVclLBPoolMember["servicePort"].add(objVclLBPoolServicePortMember);

	objVclLoadBalancerPool["member"].add(objVclLBPoolMember);
}

System.log("===== objVclLoadBalancerPool.member.size() = " + objVclLoadBalancerPool.member.size());

var objVclLBPersistence = new VclLBPersistence();
	objVclLBPersistence["method"] = "COOKIE";
	objVclLBPersistence["cookieName"] = "VCOSESSIONID";
	objVclLBPersistence["cookieMode"] = "INSERT";

var objVclLBVirtualServerServiceProfile = new VclLBVirtualServerServiceProfile();
	objVclLBVirtualServerServiceProfile["isEnabled"] = true;
	objVclLBVirtualServerServiceProfile["protocol"] = "HTTP";
	objVclLBVirtualServerServiceProfile["port"] = "80";
	objVclLBVirtualServerServiceProfile["persistence"] = objVclLBPersistence;

var objVclLoadBalancerVirtualServer = new VclLoadBalancerVirtualServer();
	objVclLoadBalancerVirtualServer["isEnabled"] = true;
	objVclLoadBalancerVirtualServer["logging"] = false;
	objVclLoadBalancerVirtualServer["pool"] = strVclVAppName + "-Pool";
	objVclLoadBalancerVirtualServer["name"] = strVclVAppName + "-VirtualServer";
	objVclLoadBalancerVirtualServer["description"] = strLoadBalancerHostName;
	objVclLoadBalancerVirtualServer["ipAddress"] = strLoadBalancerIpAddress;
	objVclLoadBalancerVirtualServer["interface"] = objVclOrgVdcNetwork.getReference();
	objVclLoadBalancerVirtualServer["serviceProfile"].add(objVclLBVirtualServerServiceProfile);

var objVclLoadBalancerService;
	objVclLoadBalancerService = new VclLoadBalancerService();
	objVclLoadBalancerService["isEnabled"] = true;
	objVclLoadBalancerService["pool"].add(objVclLoadBalancerPool);
	objVclLoadBalancerService["virtualServer"].add(objVclLoadBalancerVirtualServer);

System.log("===== objVclLoadBalancerService.virtualServer.size() = " + objVclLoadBalancerService.virtualServer.size());

System.log("===== Preperation Completed, next phase of applying the configuration is about to run.");

var objVclGatewayConfiguration;
	objVclGatewayConfiguration = objVclGateway.configuration;

var objVclGatewayFeatures
	objVclGatewayFeatures = objVclGatewayConfiguration.edgeGatewayServiceConfiguration;

if (objVclGatewayFeatures == null)
{
	System.warn("===== No features found on gateway: " + objVclGateway.name);

	objVclGatewayFeatures = new VclGatewayFeatures();
}

var objVclAbstractObjectSet;
	objVclAbstractObjectSet = objVclGatewayFeatures.networkService;

var arrVclLoadBalancerService;
	arrVclLoadBalancerService = objVclAbstractObjectSet.find(new VclLoadBalancerService());

if (arrVclLoadBalancerService.length == 0)
{
	System.log("===== Load Balancer Service Not Found on Gateway");

	objVclGatewayFeatures.networkService.add(objVclLoadBalancerService);

	var arrVclLoadBalancerPool;
		arrVclLoadBalancerPool = objVclLoadBalancerService.pool.enumerate();;

	System.log("===== Pool to be Applied: " + arrVclLoadBalancerPool[0].name);

	var arrLoadBalancerVirtualServer;
		arrLoadBalancerVirtualServer = objVclLoadBalancerService.virtualServer.enumerate();

	System.log("===== Virtual Server to be applied: " + arrLoadBalancerVirtualServer[0].name);
}
else
{
	var objVclLoadBalancerService;
		objVclLoadBalancerService = arrVclLoadBalancerService[0];

	var intLoadBalancerPoolSize;
		intLoadBalancerPoolSize = objVclLoadBalancerService.pool.size();

	var intLoadBalancerVirtualServerSize;
		intLoadBalancerVirtualServerSize = objVclLoadBalancerService.virtualServer.size();

	if ( ( intLoadBalancerPoolSize == 64 ) || ( intLoadBalancerVirtualServerSize == 64 ) )
	{
		System.error("===== Load Balancer Pools Before Service is Applied: " + intLoadBalancerPoolSize);
		System.error("===== Load Balancer Virtual Servers Before Service is Applied: " + intLoadBalancerVirtualServerSize);
		System.error("===== No more Load Balancer Services can be Applied to this vShield Edge Device.");

		throw "No more Load Balancer Services can be Applied to this vShield Edge Device.";
	}
	else
	{
		System.log("===== Load Balancer Pools Before Service is Applied: " + intLoadBalancerPoolSize);
		System.log("===== Load Balancer Virtual Servers Before Service is Applied: " + intLoadBalancerVirtualServerSize);

		var arrLoadBalancerPool;
			arrLoadBalancerPool = objVclLoadBalancerService.pool.enumerate();

		if (intLoadBalancerPoolSize == 0)
		{
			System.log("===== No existing Pools found. ");
			System.log("===== New Pool " + strVclVAppName + "-Pool will be deployed.");

			objVclLoadBalancerService.pool.add(objVclLoadBalancerPool);
		}
		else
		{
			for (p = 0; p < intLoadBalancerPoolSize; p++)
			{
				var objLoadBalancerPool;
					objLoadBalancerPool = arrLoadBalancerPool[p];

				if (objLoadBalancerPool.name == strVclVAppName + "-Pool")
				{
					System.error("===== A Match was Found for " + strVclVAppName + "-Pool, will NOT be deployed");
				}
				else
				{
					var pp = p + 1;

					if (pp == intLoadBalancerPoolSize)
					{
						System.log("===== No Match was Found for " + strVclVAppName + "-Pool, will be deployed.");

						objVclLoadBalancerService.pool.add(objVclLoadBalancerPool);
					}
				}
			}
		}

		var arrLoadBalancerVirtualServer;
			arrLoadBalancerVirtualServer = objVclLoadBalancerService.virtualServer.enumerate();

		if (intLoadBalancerVirtualServerSize == 0)
		{
			System.log("===== New Virtual Server " + strVclVAppName + "-VirtualServer will be deployed.");

			objVclLoadBalancerService.virtualServer.add(objVclLoadBalancerVirtualServer);
		}
		else
		{
			for (v = 0; v < intLoadBalancerVirtualServerSize; v++)
			{
				var objLoadBalancerVirtualServer;
					objLoadBalancerVirtualServer = arrLoadBalancerVirtualServer[v];

				if (objVclLoadBalancerService.name == strVclVAppName + "-VirtualServer")
				{
					System.warn("===== A Match was Found for " + strVclVAppName + "-VirtualServer will NOT be deployed.");
				}
				else
				{
					var vv = v + 1;

					if(vv == intLoadBalancerVirtualServerSize)
					{
						System.log("===== No Match was Found for " + strVclVAppName + "-VirtualServer will be deployed.");

						objVclLoadBalancerService.virtualServer.add(objVclLoadBalancerVirtualServer);
					}
				}
			}
		}
	}
}

System.log("===== objVclGateway.update() is the next command to be run.");

var objVclTask;
	objVclTask = objVclGateway.update();

objCustomActions.waitVclTask(objVclTask);

System.log("===== Trying to Un-Lock " + objVclGateway.name + " for vCD Admin");

LockingSystem.unlock( objVclGateway.name , "vcdadmin" );

System.log("===== Un-Locked " + objVclGateway.name + " for vCD Admin");

Leave a Reply