Orchestrator Function: Create a Load Balancer on an Edge

by Simon Sparks · 5 September 2013

To use this function add it to the class library file named vCloudService.ts

GitHub Repository: https://github.com/SimonSparksUK/Orchestrator

Filename: vCloudService.ts

Description: Orchestrator Function to Create a Load Balancer on an Edge

Function VclLoadBalancerVirtualServerCreate

publicVclLoadBalancerVirtualServerCreate(objVclVdc:VclVdc,objVclOrgVdcNetwork:VclOrgVdcNetwork,objVclVApp:VclVApp,strDeployDataCentre:string,strVclLoadBalancerName:string,strLoadBalancerIpAddress:string):void{

letobjVclGateway:VclGateway=this.VclGatewayGet(objVclVdc,strDeployDataCentre);

objVclGateway.updateInternalState();

this.objLogger.info("The Next Available IP is:"+strLoadBalancerIpAddress);

letarrLoadBalancerIPAddress:string[]=strLoadBalancerIpAddress.split('.');

letstrOctetA:string=System.decimalToHex(parseInt(arrLoadBalancerIPAddress[0])).toLowerCase();
letstrOctetB:string=System.decimalToHex(parseInt(arrLoadBalancerIPAddress[1])).toLowerCase();
letstrOctetC:string=System.decimalToHex(parseInt(arrLoadBalancerIPAddress[2])).toLowerCase();
letstrOctetD:string=System.decimalToHex(parseInt(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;}

letstrLoadBalancerHostName:string=strOctetA+strOctetB+strOctetC+strOctetD+"-vip.cloudbuildtools.com";

letobjVclLBPoolHealthCheck:VclLBPoolHealthCheck=newVclLBPoolHealthCheck();
objVclLBPoolHealthCheck.timeout="15";
objVclLBPoolHealthCheck.uri="/";
objVclLBPoolHealthCheck.mode="HTTP";
objVclLBPoolHealthCheck.interval="5";
objVclLBPoolHealthCheck.healthThreshold="2";
objVclLBPoolHealthCheck.unhealthThreshold="3";

letobjVclLBPoolServicePort:VclLBPoolServicePort=newVclLBPoolServicePort();
objVclLBPoolServicePort.isEnabled=true;
objVclLBPoolServicePort.port="80";
objVclLBPoolServicePort.protocol="HTTP";
objVclLBPoolServicePort.healthCheckPort="80";
objVclLBPoolServicePort.algorithm="LEAST_CONN";
objVclLBPoolServicePort.healthCheck.add(objVclLBPoolHealthCheck);

letobjVclLBPoolServicePortMember:VclLBPoolServicePort=newVclLBPoolServicePort();
objVclLBPoolServicePortMember.port="80";
objVclLBPoolServicePortMember.protocol="HTTP";
objVclLBPoolServicePortMember.healthCheckPort="80";

letobjVclLoadBalancerPool:VclLoadBalancerPool=newVclLoadBalancerPool();
objVclLoadBalancerPool.operational=true;
objVclLoadBalancerPool.name=strVclLoadBalancerName+"-Pool";
objVclLoadBalancerPool.description=strLoadBalancerHostName;
objVclLoadBalancerPool.errorDetails="";
objVclLoadBalancerPool.servicePort.add(objVclLBPoolServicePort);

letarrVclVM:VclVM[]=objVclVApp.getChildrenVms();

arrVclVM.forEach((objVclVM:VclVM):void=>{

this.objLogger.info("objVclVM.name ="+objVclVM.name);

letarrVclVirtualNetworkCard:VclVirtualNetworkCard[]=objVclVM.getNetworkCards();

letobjVclVirtualNetworkCard:VclVirtualNetworkCard=arrVclVirtualNetworkCard.find((objVclVirtualNetworkCard:VclVirtualNetworkCard):boolean=>{

returnobjVclVirtualNetworkCard.primaryNetworkConnection==true;

});

this.objLogger.info("objVclVirtualNetworkCard.ipAddress ="+objVclVirtualNetworkCard.ipAddress);

letobjVclLBPoolMember:VclLBPoolMember=newVclLBPoolMember();
objVclLBPoolMember.weight="1";
objVclLBPoolMember.ipAddress=objVclVirtualNetworkCard.ipAddress;
objVclLBPoolMember.servicePort.add(objVclLBPoolServicePortMember);

objVclLoadBalancerPool.member.add(objVclLBPoolMember);

this.objLogger.info("objVclLoadBalancerPool.member.size() ="+objVclLoadBalancerPool.member.size());
});

letobjVclLBPersistence:VclLBPersistence=newVclLBPersistence();
objVclLBPersistence.method="COOKIE";
objVclLBPersistence.cookieName="VCOSESSIONID";
objVclLBPersistence.cookieMode="INSERT";

letobjVclLBVirtualServerServiceProfile:VclLBVirtualServerServiceProfile=newVclLBVirtualServerServiceProfile();
objVclLBVirtualServerServiceProfile.isEnabled=true;
objVclLBVirtualServerServiceProfile.protocol="HTTP";
objVclLBVirtualServerServiceProfile.port="80";
objVclLBVirtualServerServiceProfile.persistence=objVclLBPersistence;

letobjVclLoadBalancerVirtualServer:VclLoadBalancerVirtualServer=newVclLoadBalancerVirtualServer();
objVclLoadBalancerVirtualServer.isEnabled=true;
objVclLoadBalancerVirtualServer.logging=false;
objVclLoadBalancerVirtualServer.pool=strVclLoadBalancerName+"-Pool";
objVclLoadBalancerVirtualServer.name=strVclLoadBalancerName+"-VirtualServer";
objVclLoadBalancerVirtualServer.description=strLoadBalancerHostName;
objVclLoadBalancerVirtualServer.ipAddress=strLoadBalancerIpAddress;
objVclLoadBalancerVirtualServer.interface=objVclOrgVdcNetwork.getReference();
objVclLoadBalancerVirtualServer.serviceProfile.add(objVclLBVirtualServerServiceProfile);

letobjVclLoadBalancerService:VclLoadBalancerService=newVclLoadBalancerService();
objVclLoadBalancerService.isEnabled=true;
objVclLoadBalancerService.pool.add(objVclLoadBalancerPool);
objVclLoadBalancerService.virtualServer.add(objVclLoadBalancerVirtualServer);

this.objLogger.info("objVclLoadBalancerService.virtualServer.size() ="+objVclLoadBalancerService.virtualServer.size());

this.objLogger.info("Preperation Completed, next phase of applying the configuration is about to run.");

letobjVclGatewayConfiguration:VclGatewayConfiguration=objVclGateway.configuration;

letobjVclGatewayFeatures:VclGatewayFeatures=objVclGatewayConfiguration.edgeGatewayServiceConfiguration;

letobjVclAbstractObjectSet:VclAbstractObjectSet=objVclGatewayFeatures.networkService;

letarrVclLoadBalancerService:VclLoadBalancerService[]=objVclAbstractObjectSet.find(newVclLoadBalancerService());

letarrVclLoadBalancerPool:VclLoadBalancerPool[];
letarrLoadBalancerVirtualServer:VclLoadBalancerVirtualServer[];

if (arrVclLoadBalancerService.length==0){
this.objLogger.info("Load Balancer Service Not Found on Gateway");

objVclGatewayFeatures.networkService.add(objVclLoadBalancerService);

arrVclLoadBalancerPool=objVclLoadBalancerService.pool.enumerate();;

this.objLogger.info("Pool to be Applied:"+arrVclLoadBalancerPool[0].name);

arrLoadBalancerVirtualServer=objVclLoadBalancerService.virtualServer.enumerate();

this.objLogger.info("Virtual Server to be applied:"+arrLoadBalancerVirtualServer[0].name);
}
else{
letobjVclLoadBalancerService:VclLoadBalancerService=arrVclLoadBalancerService[0];

letintLoadBalancerPoolSize:number=objVclLoadBalancerService.pool.size();

letintLoadBalancerVirtualServerSize:number=objVclLoadBalancerService.virtualServer.size();

if ((intLoadBalancerPoolSize==64)|| (intLoadBalancerVirtualServerSize==64)){
this.objLogger.info("Load Balancer Pools Before Service is Applied:"+intLoadBalancerPoolSize);
this.objLogger.info("Load Balancer Virtual Servers Before Service is Applied:"+intLoadBalancerVirtualServerSize);
this.objLogger.info("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{
this.objLogger.info("Load Balancer Pools Before Service is Applied:"+intLoadBalancerPoolSize);
this.objLogger.info("Load Balancer Virtual Servers Before Service is Applied:"+intLoadBalancerVirtualServerSize);

letarrVclLoadBalancerPool:VclLoadBalancerPool[]=objVclLoadBalancerService.pool.enumerate();

if (intLoadBalancerPoolSize==0){
this.objLogger.info("No existing Pools found.");
this.objLogger.info("New Pool"+strVclLoadBalancerName+"-Pool will be deployed.");

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

arrVclLoadBalancerPool.forEach((objVclLoadBalancerPool:VclLoadBalancerPool)=>{

if (objVclLoadBalancerPool.name===strVclLoadBalancerName+"-Pool"){
this.objLogger.info("A Match was Found for"+strVclLoadBalancerName+"-Pool, will NOT be deployed");
}
else{
// TO DO - SS - The logic here is not good

this.objLogger.info("No Match was Found for"+strVclLoadBalancerName+"-Pool, will be deployed.");

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

letarrVclLoadBalancerVirtualServer:VclLoadBalancerVirtualServer[]=objVclLoadBalancerService.virtualServer.enumerate();

if (arrVclLoadBalancerVirtualServer.length===0){
this.objLogger.info("New Virtual Server"+strVclLoadBalancerName+"-VirtualServer will be deployed.");

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

arrLoadBalancerVirtualServer.forEach((objVclLoadBalancerVirtualServer:VclLoadBalancerVirtualServer):void=>{

if (objVclLoadBalancerVirtualServer.name==strVclLoadBalancerName+"-VirtualServer"){
this.objLogger.info("A Match was Found for"+strVclLoadBalancerName+"-VirtualServer will NOT be deployed.");
}
else{
// TO DO - SS - The logic here is not good

this.objLogger.info("No Match was Found for"+strVclLoadBalancerName+"-VirtualServer will be deployed.");

objVclLoadBalancerService.virtualServer.add(objVclLoadBalancerVirtualServer);
}

});

}
}
}

this.objLogger.info("objVclGateway.update() is the next command to be run.");

letobjVclTask:VclTask=objVclGateway.update();

this.WaitForVclTask(objVclTask,1);
}

Discover more from Cloud Build Tools

Subscribe to get the latest posts sent to your email.

You may also like