Created
January 16, 2013 21:22
-
-
Save underscorephil/4551086 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 | |
include('softlayer-api-php-client/SoftLayer/SoapClient.class.php'); | |
$apiUser = ''; | |
$apiKey = ''; | |
$cciId = ; // ID of the cloud instance | |
$loadBalancerId = ; // ID of the VIP | |
// Create the needed API interfaces | |
$loadBalancerClient = SoftLayer_SoapClient::getClient('SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress', $loadBalancerId, $apiUser, $apiKey); | |
$cciClient = SoftLayer_SoapClient::getClient('SoftLayer_Virtual_Guest', $cciId, $apiUser, $apiKey); | |
$ipAddressClient = SoftLayer_SoapClient::getClient('SoftLayer_Network_Subnet_IpAddress', Null, $apiUser, $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]->serviceGroups = array(); | |
$newLoadBalancer->virtualServers[$virtualServerIndex]->serviceGroups[0] = $serviceGroup; | |
try { | |
$loadBalancerClient->editObject($newLoadBalancer); | |
} catch (Exception $e) { | |
print $e->getMessage(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment