use of com.cloud.agent.api.routing.GlobalLoadBalancerConfigAnswer in project cloudstack by apache.
the class NetscalerResource method execute.
private Answer execute(final GlobalLoadBalancerConfigCommand gslbCmd, final int numRetries) {
final String lbMethod = gslbCmd.getLoadBalancerMethod();
final String persistenceType = gslbCmd.getPersistenceType();
final String serviceType = gslbCmd.getServiceType();
final boolean forRevoke = gslbCmd.isForRevoke();
final long gslbId = gslbCmd.getGslbId();
final List<SiteLoadBalancerConfig> sites = gslbCmd.getSiteDetails();
final String domainName = gslbCmd.getDomainName();
final String vserverName = GSLB.generateVirtualServerName(domainName);
try {
if (!forRevoke) {
//check if the global load balancer rule is being added
// Add a GSLB virtual server
GSLB.createVirtualServer(_netscalerService, vserverName, lbMethod, persistenceType, gslbId, serviceType);
if (sites != null) {
// check if there are any sites that are participating in global load balancing
for (final SiteLoadBalancerConfig site : sites) {
final String sitePrivateIP = site.getGslbProviderPrivateIp();
final String sitePublicIP = site.getGslbProviderPublicIp();
final String servicePublicIp = site.getServicePublicIp();
final String servicePublicPort = site.getServicePort();
final String siteName = GSLB.generateUniqueSiteName(sitePrivateIP, sitePublicIP, site.getDataCenterId());
// Add/Delete GSLB local and remote sites that are part of GSLB virtual server
if (!site.forRevoke()) {
final String siteType = site.isLocal() ? "LOCAL" : "REMOTE";
if (GSLB.getSiteObject(_netscalerService, siteName) != null) {
GSLB.updateSite(_netscalerService, siteType, siteName, site.getGslbProviderPrivateIp(), site.getGslbProviderPublicIp());
} else {
GSLB.createSite(_netscalerService, siteName, siteType, site.getGslbProviderPrivateIp(), site.getGslbProviderPublicIp());
}
}
// Add/Delete GSLB service corresponding the service running on each site
final String serviceName = GSLB.generateUniqueServiceName(siteName, servicePublicIp, servicePublicPort);
final String monitorName = GSLB.generateGslbServiceMonitorName(servicePublicIp);
if (!site.forRevoke()) {
// create a 'gslbservice' object
GSLB.createService(_netscalerService, serviceName, site.getServiceType(), servicePublicIp, servicePublicPort, siteName);
// Bind 'gslbservice' service object to GSLB virtual server
GSLB.createVserverServiceBinding(_netscalerService, serviceName, vserverName, site.getWeight());
// create a monitor for the service running on the site
GSLB.createGslbServiceMonitor(_netscalerService, servicePublicIp, serviceName);
// bind the monitor to the GSLB service
GSLB.createGslbServiceGslbMonitorBinding(_netscalerService, monitorName, serviceName);
} else {
// delete GSLB service and GSLB monitor binding
GSLB.deleteGslbServiceGslbMonitorBinding(_netscalerService, monitorName, serviceName);
// Unbind GSLB service with GSLB virtual server
GSLB.deleteVserverServiceBinding(_netscalerService, serviceName, vserverName);
GSLB.getServiceObject(_netscalerService, serviceName);
GSLB.deleteService(_netscalerService, serviceName);
// delete the GSLB service monitor
GSLB.deleteGslbServiceMonitor(_netscalerService, monitorName);
}
if (site.forRevoke()) {
// delete the site if its for revoke
GSLB.deleteSite(_netscalerService, siteName);
}
}
}
// Bind GSLB vserver to domain
GSLB.createVserverDomainBinding(_netscalerService, vserverName, domainName);
} else {
// global load balancer rule is being deleted, so clean up all objects created
// remove binding between virtual server and the domain name
GSLB.deleteVserverDomainBinding(_netscalerService, vserverName, domainName);
if (sites != null) {
for (final SiteLoadBalancerConfig site : sites) {
final String sitePrivateIP = site.getGslbProviderPrivateIp();
final String sitePublicIP = site.getGslbProviderPublicIp();
final String servicePublicIp = site.getServicePublicIp();
final String servicePublicPort = site.getServicePort();
final String siteName = GSLB.generateUniqueSiteName(sitePrivateIP, sitePublicIP, site.getDataCenterId());
final String serviceName = GSLB.generateUniqueServiceName(siteName, servicePublicIp, servicePublicPort);
final String monitorName = GSLB.generateGslbServiceMonitorName(servicePublicIp);
// delete GSLB service and GSLB monitor binding
GSLB.deleteGslbServiceGslbMonitorBinding(_netscalerService, monitorName, serviceName);
// remove binding between virtual server and services
GSLB.deleteVserverServiceBinding(_netscalerService, serviceName, vserverName);
// delete service object
GSLB.deleteService(_netscalerService, serviceName);
// delete GSLB site object
GSLB.deleteSite(_netscalerService, siteName);
// delete the GSLB service monitor
GSLB.deleteGslbServiceMonitor(_netscalerService, monitorName);
}
}
// delete GSLB virtual server
GSLB.deleteVirtualServer(_netscalerService, vserverName);
}
saveConfiguration();
} catch (final Exception e) {
final String errMsg = "Failed to apply GSLB configuration due to " + e.getMessage();
if (shouldRetry(numRetries)) {
return retry(gslbCmd, numRetries);
}
return new GlobalLoadBalancerConfigAnswer(false, errMsg);
}
return new GlobalLoadBalancerConfigAnswer(true, "Successfully applied GSLB configuration.");
}
Aggregations