public function xSaveCloudstackAction()
{
$pars = array();
$enabled = false;
$platform = $this->getParam('platform');
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, 'string', '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);
$accounts = $cs->listAccounts();
foreach ($accounts as $account) {
foreach ($account->user as $user) {
if ($user->apikey == $pars[Entity\CloudCredentialsProperty::CLOUDSTACK_API_KEY]) {
$pars[Entity\CloudCredentialsProperty::CLOUDSTACK_ACCOUNT_NAME] = $user->account;
$pars[Entity\CloudCredentialsProperty::CLOUDSTACK_DOMAIN_NAME] = $user->domain;
$pars[Entity\CloudCredentialsProperty::CLOUDSTACK_DOMAIN_ID] = $user->domainid;
}
}
}
if (empty($pars[Entity\CloudCredentialsProperty::CLOUDSTACK_ACCOUNT_NAME])) {
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);
} else {
$this->env->cloudCredentials($platform)->properties->saveSettings([Entity\CloudCredentialsProperty::CLOUDSTACK_ACCOUNT_NAME => false, Entity\CloudCredentialsProperty::CLOUDSTACK_API_KEY => false, Entity\CloudCredentialsProperty::CLOUDSTACK_API_URL => false, Entity\CloudCredentialsProperty::CLOUDSTACK_DOMAIN_ID => false, Entity\CloudCredentialsProperty::CLOUDSTACK_DOMAIN_NAME => false, Entity\CloudCredentialsProperty::CLOUDSTACK_SECRET_KEY => false, Entity\CloudCredentialsProperty::CLOUDSTACK_SHARED_IP => false, Entity\CloudCredentialsProperty::CLOUDSTACK_SHARED_IP_ID => false, Entity\CloudCredentialsProperty::CLOUDSTACK_SHARED_IP_INFO => false, Entity\CloudCredentialsProperty::CLOUDSTACK_SZR_PORT_COUNTER => false]);
}
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('Environment saved');
$this->response->data(array('enabled' => $enabled));
} catch (Exception $e) {
$this->db->RollbackTrans();
throw new Exception(_('Failed to save ' . ucfirst($platform) . ' settings'));
}
$this->db->CommitTrans();
}
}