AddServiceToServiceGroup.php
AddServiceToServiceGroup.php
<?php
require_once __DIR__.'/vendor/autoload.php';
$username = "set me";
$apiKey = "set me";
$vipAddress = "173.193.117.96";
$virtualPort = 80;
$destinationIp = "50.23.6.44";
$destPort = 817;
$healthCheck = "HTTP-CUSTOM";
$weight = 99;
$notes = "testRcv17";
$type = "GET";
$location = "/index.php";
$response = "error";
$accountService = \SoftLayer\SoapClient::getClient('SoftLayer_Account', null, $username, $apiKey);
$balancerService = \SoftLayer\SoapClient::getClient('SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress', null, $username, $apiKey);
$healthCheckService = \SoftLayer\SoapClient::getClient('SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Health_Check_Type', null, $username, $apiKey);
$ipService = \SoftLayer\SoapClient::getClient('SoftLayer_Network_Subnet_IpAddress', null, $username, $apiKey);
$filter = new stdClass();
$filter -> adcLoadBalancers = new stdClass();
$filter -> adcLoadBalancers -> ipAddress = new stdClass();
$filter -> adcLoadBalancers -> ipAddress -> ipAddress = new stdClass();
$filter -> adcLoadBalancers -> ipAddress -> ipAddress -> operation = $vipAddress;
$accountService -> setObjectFilter($filter);
$healthCheckFilter = new stdClass();
$healthCheckFilter -> name = new stdClass();
$healthCheckFilter -> name -> operation = $healthCheck;
$healthCheckService -> setObjectFilter($healthCheckFilter);
$objectMask = "mask[virtualServers[serviceGroups[services[healthChecks[type], groups, groupReferences, ipAddress]]]]";
$accountService -> setObjectMask($objectMask);
try {
$healthCheckObjects = $healthCheckService -> getAllObjects();
$ipAddress= $ipService -> findByIpv4Address($destinationIp);
$templateObject = new stdClass();
$templateObject -> enabled = 1;
$templateObject -> groupReferences = array(array("weight" => $weight));
$templateObject -> healthChecks = array(array("healthCheckTypeId" => $healthCheckObjects[0] -> id));
if($healthCheck == "HTTP-CUSTOM")
{
$templateObject -> healthChecks = array(array("healthCheckTypeId" => $healthCheckObjects[0] -> id, "attributes" => array(array("healthAttributeTypeId" => 22,
"value" => $type), array("healthAttributeTypeId" => 23, "value" => $location), array("healthAttributeTypeId" => 24, "value" => $response))));
}
$templateObject -> ipAddressId = $ipAddress -> id;
$templateObject -> port = $destPort;
$templateObject -> notes = $notes;
$loadBalancers = $accountService -> getAdcLoadBalancers();
foreach($loadBalancers[0] -> virtualServers as $virtualServer)
{
if($virtualServer -> port == $virtualPort)
{
array_push($virtualServer -> serviceGroups[0] -> services, $templateObject);
}
}
$balancerService -> setInitParameter($loadBalancers[0] -> id);
$result = $balancerService -> editObject($loadBalancers[0]);
echo "Has been added the service to the service group from load balancer?: " . $result;
} catch(Exception $e) {
echo "Unable to add the service: " . $e -> getMessage();
}