/**
* Associates IP Address to the server
*
* @param DBServer $dbServer DBServer object
* @param string $ipAddress Public IP address to associate with server.
* @throws Exception
*/
private static function associateIpAddress(DBServer $dbServer, $ipAddress, $allocationId = null)
{
$platform = PlatformFactory::NewPlatform($dbServer->platform);
$cs = Scalr_Service_Cloud_Cloudstack::newCloudstack($platform->getConfigVariable(Modules_Platforms_Cloudstack::API_URL, $dbServer->GetEnvironmentObject()), $platform->getConfigVariable(Modules_Platforms_Cloudstack::API_KEY, $dbServer->GetEnvironmentObject()), $platform->getConfigVariable(Modules_Platforms_Cloudstack::SECRET_KEY, $dbServer->GetEnvironmentObject()), $dbServer->platform);
$assign_retries = 1;
$retval = false;
// Remove OLD static NAT
if ($dbServer->remoteIp) {
try {
$info = $cs->listPublicIpAddresses(null, null, null, null, null, $dbServer->remoteIp);
$info = $info->publicipaddress[0];
if ($info->issystem && $info->isstaticnat) {
$cs->disableStaticNat($info->id);
}
} catch (Exception $e) {
Logger::getLogger('Cloudstack_Helper')->error("Unable to disable old static NAT: {$e->getMessage()}");
}
}
try {
$cs->disableStaticNat($allocationId);
} catch (Exception $e) {
}
$assignRetries = 0;
while (true) {
try {
$assignRetries++;
$cs->enableStaticNat($allocationId, $dbServer->GetCloudServerID());
$retval = true;
break;
} catch (Exception $e) {
if (!stristr($e->getMessage(), "already assigned to antoher vm") || $assignRetries == 3) {
throw new Exception($e->getMessage());
} else {
sleep(1);
}
}
//break;
}
return $retval;
}