/** * Creates new http Request object * * @return Request Returns new http Request object */ protected function createHttpRequest() { $req = new Request(); $req->setOptions(['redirect' => 10, 'cookiesession' => true]); $req->setSslOptions(['verifypeer' => false, 'verifyhost' => false]); $proxySettings = $this->cloudstack->getProxy(); if ($proxySettings !== false) { $req->setOptions(['proxyhost' => $proxySettings['host'], 'proxyport' => $proxySettings['port'], 'proxytype' => $proxySettings['type']]); if ($proxySettings['user']) { $req->setOptions(['proxyauth' => "{$proxySettings['user']}:{$proxySettings['pass']}", 'proxyauthtype' => $proxySettings['authtype']]); } } return $req; }
/** * @test */ public function testGetAvailableServices() { $avail = CloudStack::getAvailableServices(); $this->assertNotEmpty($avail); $this->assertInternalType('array', $avail); $this->assertArrayHasKey('network', $avail); $this->assertArrayNotHasKey('abstract', $avail); }
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(); } }
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(); } }
/** * Checks Elastic IP availability * * @param string $ipaddress public IP address * @param \Scalr\Service\CloudStack\CloudStack $cs CloudStack instance * @return boolean Returns true if IP address is available. */ private static function checkStaticNatIp($ipaddress, CloudStack $cs) { \Scalr::getContainer()->logger('Cloudstack_Helpers')->debug(sprintf(_("Checking IP: %s"), $ipaddress)); try { $requestObject = new ListIpAddressesData(); $requestObject->ipaddress = $ipaddress; $info = $cs->listPublicIpAddresses($requestObject); if (count($info) > 0) { return true; } else { return false; } } catch (Exception $e) { return false; } }
/** * @param Entity\CloudCredentials $entity * @param Entity\CloudCredentials $prevConfig * * @throws ApiErrorException */ public function validateEntity($entity, $prevConfig = null) { parent::validateEntity($entity, $prevConfig); $ccProps = $entity->properties; $prevCcProps = isset($prevConfig) ? $prevConfig->properties : null; if ($this->needValidation($ccProps, $prevCcProps)) { try { $cs = new CloudStack($ccProps[Entity\CloudCredentialsProperty::CLOUDSTACK_API_URL], $ccProps[Entity\CloudCredentialsProperty::CLOUDSTACK_API_KEY], $ccProps[Entity\CloudCredentialsProperty::CLOUDSTACK_SECRET_KEY], $entity->cloud); $listAccountsData = new ListAccountsData(); $listAccountsData->listall = true; } catch (Exception $e) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Failed to verify your Cloudstack credentials: {$e->getMessage()}"); } if (!$this->searchCloudstackUser($cs->listAccounts($listAccountsData), $ccProps)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Cannot determine account name for provided keys"); } $entity->status = Entity\CloudCredentials::STATUS_ENABLED; } }
/** * @param Entity\CloudCredentials $entity * @param Entity\CloudCredentials $prevConfig * * @throws ApiErrorException */ public function validateEntity($entity, $prevConfig = null) { parent::validateEntity($entity, $prevConfig); $ccProps = $entity->properties; $prevCcProps = isset($prevConfig) ? $prevConfig->properties : null; if ($this->needValidation($ccProps, $prevCcProps)) { try { $cs = new CloudStack($ccProps[Entity\CloudCredentialsProperty::CLOUDSTACK_API_URL], $ccProps[Entity\CloudCredentialsProperty::CLOUDSTACK_API_KEY], $ccProps[Entity\CloudCredentialsProperty::CLOUDSTACK_SECRET_KEY], $entity->cloud); $listAccountsData = new ListAccountsData(); $listAccountsData->listall = true; } catch (Exception $e) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Failed to verify your Cloudstack credentials: {$e->getMessage()}"); } /* @var $config Yaml */ $config = $this->controller->getContainer()->config; if ($config->defined("scalr.{$entity->cloud}.use_proxy") && $config("scalr.{$entity->cloud}.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), $ccProps)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Cannot determine account name for provided keys"); } $entity->status = Entity\CloudCredentials::STATUS_ENABLED; } }
private function saveCloudstack() { $pars = array(); $enabled = false; $platform = $this->getParam('platform'); $bNew = !$this->env->isPlatformEnabled($platform); if (!$bNew) { $oldUrl = $this->env->getPlatformConfigValue("{$platform}." . CloudstackPlatformModule::API_URL); } if ($this->getParam("{$platform}_is_enabled")) { $enabled = true; $pars["{$platform}." . CloudstackPlatformModule::API_URL] = $this->checkVar(CloudstackPlatformModule::API_URL, 'string', 'API URL required'); $pars["{$platform}." . CloudstackPlatformModule::API_KEY] = $this->checkVar(CloudstackPlatformModule::API_KEY, 'string', 'API key required'); $pars["{$platform}." . CloudstackPlatformModule::SECRET_KEY] = $this->checkVar(CloudstackPlatformModule::SECRET_KEY, 'password', 'Secret key required', '', false, $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["{$platform}." . CloudstackPlatformModule::API_URL], $pars["{$platform}." . CloudstackPlatformModule::API_KEY], $pars["{$platform}." . CloudstackPlatformModule::SECRET_KEY], $platform); $listAccountsData = new ListAccountsData(); $listAccountsData->listall = true; //$listAccountsData->accounttype = 0; $accounts = $cs->listAccounts($listAccountsData); foreach ($accounts as $account) { foreach ($account->user as $user) { if ($user->apikey == $pars["{$platform}." . CloudstackPlatformModule::API_KEY]) { $dPars["{$platform}." . CloudstackPlatformModule::ACCOUNT_NAME] = $user->account; $dPars["{$platform}." . CloudstackPlatformModule::DOMAIN_NAME] = $user->domain; $dPars["{$platform}." . CloudstackPlatformModule::DOMAIN_ID] = $user->domainid; } } } if (!$dPars["{$platform}." . CloudstackPlatformModule::ACCOUNT_NAME]) { throw new Exception("Cannot determine account name for provided keys"); } } $this->db->BeginTrans(); try { $this->env->enablePlatform($platform, $enabled); if ($enabled) { $this->env->setPlatformConfig($pars); $this->env->setPlatformConfig($dPars, false); if ($this->getContainer()->analytics->enabled && ($bNew || $oldUrl !== $pars["{$platform}." . CloudstackPlatformModule::API_URL])) { $this->getContainer()->analytics->notifications->onCloudAdd($platform, $this->env, $this->user); } } else { $this->env->setPlatformConfig(array("{$platform}." . CloudstackPlatformModule::ACCOUNT_NAME => false, "{$platform}." . CloudstackPlatformModule::API_KEY => false, "{$platform}." . CloudstackPlatformModule::API_URL => false, "{$platform}." . CloudstackPlatformModule::DOMAIN_ID => false, "{$platform}." . CloudstackPlatformModule::DOMAIN_NAME => false, "{$platform}." . CloudstackPlatformModule::SECRET_KEY => false, "{$platform}." . CloudstackPlatformModule::SHARED_IP => false, "{$platform}." . CloudstackPlatformModule::SHARED_IP_ID => false, "{$platform}." . CloudstackPlatformModule::SHARED_IP_INFO => false, "{$platform}." . CloudstackPlatformModule::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('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(); } }