Created
May 16, 2013 15:37
-
-
Save underscorephil/5592644 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once './softlayer/SoftLayer/SoapClient.class.php'; | |
$apiUsername = ''; | |
$apiKey = ''; | |
$loadBalancerId = ; | |
$cciId = ; | |
addToLB($cciId, $loadBalancerId, $apiUsername, $apiKey); | |
function addToLB($cciId, $loadBalancerId, $apiUsername, $apiKey) { | |
// Create the needed API interfaces | |
$loadBalancerClient = SoftLayer_SoapClient::getClient('SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress', $loadBalancerId, $apiUsername, $apiKey); | |
$cciClient = SoftLayer_SoapClient::getClient('SoftLayer_Virtual_Guest', $cciId, $apiUsername, $apiKey); | |
$ipAddressClient = SoftLayer_SoapClient::getClient('SoftLayer_Network_Subnet_IpAddress', Null, $apiUsername, $apiKey); | |
// Pull in the existing load balancer configuraiton | |
$objectMask = new SoftLayer_ObjectMask(); | |
$objectMask->ipAddress; | |
$objectMask->virtualServers->serviceGroups->services; | |
$objectMask->virtualServers->serviceGroups->services->ipAddress; | |
$loadBalancerClient->setObjectMask($objectMask); | |
$loadBalancer = $loadBalancerClient->getObject(); | |
// Retrieve ID for the CCI's primary IP address | |
$ipAddressId = $ipAddressClient->findByIpv4Address($cciClient->getPrimaryIpAddress())->id; | |
// Find the virtual service to modify | |
$servicePortToModify = 80; | |
$virtualServerIndex = null; | |
foreach ($loadBalancer->virtualServers as $index => $virtualServer) { | |
if ($virtualServer->port == $servicePortToModify) { | |
$virtualServerIndex = $index; | |
break; | |
} | |
} | |
// GOTCHA: remove the recursing object created by the object mask | |
unset($loadBalancer->virtualServers[$virtualServerIndex]->virtualIpAddress); | |
// Get the service group to be modified | |
$serviceGroup = $loadBalancer->virtualServers[$virtualServerIndex]->serviceGroups[0]; | |
// Create a fresh load balancer object with ID | |
$newLoadBalancer = new stdClass(); | |
$newLoadBalancer->id = $loadBalancer->id; | |
// Define the new service entry | |
$serviceGroup->services = array(0 => new stdClass()); | |
$serviceGroup->services[0] = new stdClass(); | |
$serviceGroup->services[0]->enabled = 1; | |
$serviceGroup->services[0]->port = 80; | |
$serviceGroup->services[0]->notes = 'test'; | |
$serviceGroup->services[0]->ipAddressId = $ipAddressId; | |
$serviceGroup->services[0]->groupReferences = array(0 => new stdClass()); | |
$serviceGroup->services[0]->groupReferences[0]->weight = 50; | |
$serviceGroup->services[0]->healthChecks = array(0 => new stdClass()); | |
$serviceGroup->services[0]->healthChecks[0]->healthCheckTypeId = 21; // DEFAULT | |
// Attach the new service entry to the new load balancer object | |
$newLoadBalancer->virtualServers = array(); | |
$newLoadBalancer->virtualServers[$virtualServerIndex] = new stdClass(); | |
$newLoadBalancer->virtualServers[$virtualServerIndex]->id = $loadBalancer->virtualServers[$virtualServerIndex]->id; | |
$newLoadBalancer->virtualServers[$virtualServerIndex]->port = $loadBalancer->virtualServers[$virtualServerIndex]->port; | |
$newLoadBalancer->virtualServers[$virtualServerIndex]->allocation = $loadBalancer->virtualServers[$virtualServerIndex]->allocation; | |
$newLoadBalancer->virtualServers[$virtualServerIndex]->serviceGroups = array(); | |
$newLoadBalancer->virtualServers[$virtualServerIndex]->serviceGroups[0] = $serviceGroup; | |
try { | |
$loadBalancerClient->editObject($newLoadBalancer); | |
echo "\nLoad Balancer successfully added server id: $cciId\n\n"; | |
} catch (Exception $e) { | |
echo "\nUnable to add to Load Balancer server id: $cciId " . $e->getMessage() . "\n\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment