private function saveCloudstack()
{
$pars = array();
$enabled = false;
$platform = $this->getParam('platform');
$currentCloudCredentials = $this->env->keychain($platform);
$ccProps = $currentCloudCredentials->properties;
$bNew = !$currentCloudCredentials->isEnabled();
if (!$bNew) {
$oldUrl = $ccProps[Entity\CloudCredentialsProperty::CLOUDSTACK_API_URL];
}
if ($this->getParam("{$platform}_is_enabled")) {
$enabled = true;
$pars[Entity\CloudCredentialsProperty::CLOUDSTACK_API_URL] = $this->checkVar(Entity\CloudCredentialsProperty::CLOUDSTACK_API_URL, 'string', 'API URL required', $platform);
$pars[Entity\CloudCredentialsProperty::CLOUDSTACK_API_KEY] = $this->checkVar(Entity\CloudCredentialsProperty::CLOUDSTACK_API_KEY, 'string', 'API key required', $platform);
$pars[Entity\CloudCredentialsProperty::CLOUDSTACK_SECRET_KEY] = $this->checkVar(Entity\CloudCredentialsProperty::CLOUDSTACK_SECRET_KEY, 'password', 'Secret key required', $platform);
}
if (count($this->checkVarError)) {
$this->response->failure();
$this->response->data(array('errors' => $this->checkVarError));
} else {
if ($this->getParam("{$platform}_is_enabled")) {
$cs = new CloudStack($pars[Entity\CloudCredentialsProperty::CLOUDSTACK_API_URL], $pars[Entity\CloudCredentialsProperty::CLOUDSTACK_API_KEY], $pars[Entity\CloudCredentialsProperty::CLOUDSTACK_SECRET_KEY], $platform);
$listAccountsData = new ListAccountsData();
$listAccountsData->listall = true;
//$listAccountsData->accounttype = 0;
/* @var $config Yaml */
$config = $this->env->getContainer()->config;
if ($config->defined("scalr.{$platform}.use_proxy") && $config("scalr.{$platform}.use_proxy") && in_array($config('scalr.connections.proxy.use_on'), ['both', 'scalr'])) {
$proxySettings = $config('scalr.connections.proxy');
$cs->setProxy($proxySettings['host'], $proxySettings['port'], $proxySettings['user'], $proxySettings['pass'], $proxySettings['type'], $proxySettings['authtype']);
}
if (!$this->searchCloudstackUser($cs->listAccounts($listAccountsData), $pars)) {
throw new Exception("Cannot determine account name for provided keys");
}
}
$this->db->BeginTrans();
try {
$this->env->enablePlatform($platform, $enabled);
if ($enabled) {
$this->makeCloudCredentials($platform, $pars);
if ($this->getContainer()->analytics->enabled && ($bNew || $oldUrl !== $pars[Entity\CloudCredentialsProperty::CLOUDSTACK_API_URL])) {
$this->getContainer()->analytics->notifications->onCloudAdd($platform, $this->env, $this->user);
}
} else {
$currentCloudCredentials->environments[$this->env->id]->delete();
}
if (!$this->user->getAccount()->getSetting(Scalr_Account::SETTING_DATE_ENV_CONFIGURED)) {
$this->user->getAccount()->setSetting(Scalr_Account::SETTING_DATE_ENV_CONFIGURED, time());
}
$this->response->success('Cloud credentials have been ' . ($enabled ? 'saved' : 'removed from Scalr'));
$this->response->data(array('enabled' => $enabled));
} catch (Exception $e) {
$this->db->RollbackTrans();
throw new Exception(_('Failed to save ' . ucfirst($platform) . ' settings'));
}
$this->db->CommitTrans();
}
}