Scalr_Environment::cloudCredentialsList PHP Method

cloudCredentialsList() public method

Gets cloud credentials for listed clouds
public cloudCredentialsList ( array $clouds = null, array $credentialsFilter = [], array $propertiesFilter = [], boolean $cacheResult = true ) : CloudCredentials[]
$clouds array optional Clouds list
$credentialsFilter array optional Criteria to filter by CloudCredentials properties
$propertiesFilter array optional Criteria to filter by CloudCredentialsProperties
$cacheResult boolean optional Cache result
return Scalr\Model\Entity\CloudCredentials[]
    public function cloudCredentialsList(array $clouds = null, array $credentialsFilter = [], array $propertiesFilter = [], $cacheResult = true)
    {
        if (!is_array($clouds)) {
            $clouds = (array) $clouds;
        }
        $cloudCredentials = new Entity\CloudCredentials();
        $cloudCredProps = new Entity\CloudCredentialsProperty();
        $criteria = $credentialsFilter;
        $from[] = empty($criteria[AbstractEntity::STMT_FROM]) ? " {$cloudCredentials->table()} " : $criteria[AbstractEntity::STMT_FROM];
        $where = empty($criteria[AbstractEntity::STMT_WHERE]) ? [] : [$criteria[AbstractEntity::STMT_WHERE]];
        $envCloudCredentials = new Entity\EnvironmentCloudCredentials();
        $from[] = "\n            JOIN {$envCloudCredentials->table('cecc')} ON\n                {$cloudCredentials->columnId()} = {$envCloudCredentials->columnCloudCredentialsId('cecc')} AND\n                {$cloudCredentials->columnCloud()} = {$envCloudCredentials->columnCloud('cecc')}\n        ";
        $where[] = "{$envCloudCredentials->columnEnvId('cecc')} = {$envCloudCredentials->qstr('envId', $this->id)}";
        if (!empty($clouds)) {
            $clouds = implode(", ", array_map(function ($cloud) use($cloudCredentials) {
                return $cloudCredentials->qstr('cloud', $cloud);
            }, $clouds));
            $where[] = "{$cloudCredentials->columnCloud()} IN ({$clouds})";
        }
        if (!empty($propertiesFilter)) {
            foreach ($propertiesFilter as $property => $propCriteria) {
                $alias = "ccp_" . trim($cloudCredentials->db()->qstr($property), "'");
                $from[] = "\n                    LEFT JOIN {$cloudCredProps->table($alias)} ON\n                        {$cloudCredentials->columnId()} = {$cloudCredProps->columnCloudCredentialsId($alias)} AND\n                        {$cloudCredProps->columnName($alias)} = {$cloudCredProps->qstr('name', $property)}\n                ";
                $built = $cloudCredProps->_buildQuery($propCriteria, 'AND', $alias);
                if (!empty($built['where'])) {
                    $where[] = $built['where'];
                }
            }
        }
        $criteria[AbstractEntity::STMT_FROM] = implode("\n", $from);
        if (!empty($where)) {
            $criteria[AbstractEntity::STMT_WHERE] = "(" . implode(") AND (", $where) . ")";
        }
        /* @var $cloudsCredentials Entity\CloudCredentials[] */
        $cloudsCredentials = Entity\CloudCredentials::find($criteria);
        $result = [];
        foreach ($cloudsCredentials as $cloudCredentials) {
            $result[$cloudCredentials->cloud] = $cloudCredentials;
            if ($cacheResult) {
                $cloudCredentials->bindEnvironment($this->id);
                $cloudCredentials->cache();
            }
        }
        return $result;
    }

Usage Example

Example #1
0
 public function xSaveRackspaceAction()
 {
     $pars = array();
     $locations = array('rs-ORD1', 'rs-LONx');
     $enabled = false;
     /* @var $currentCloudCredentials Entity\CloudCredentials[] */
     $currentCloudCredentials = $this->env->cloudCredentialsList(array_map(function ($location) {
         return "{$location}." . SERVER_PLATFORMS::RACKSPACE;
     }, $locations));
     foreach ($currentCloudCredentials as $cloudCredential) {
         if ($cloudCredential->isEnabled()) {
             $enabled = true;
             break;
         }
     }
     if (!$enabled) {
         throw new Scalr_Exception_Core('Rackspace cloud has been deprecated. Please use Rackspace Open Cloud instead.');
     } else {
         $enabled = false;
     }
     foreach ($locations as $location) {
         if ($this->getParam("rackspace_is_enabled_{$location}")) {
             $enabled = true;
             $pars[$location][Entity\CloudCredentialsProperty::RACKSPACE_USERNAME] = $this->checkVar(Entity\CloudCredentialsProperty::RACKSPACE_USERNAME, 'string', "Username required", $location . SERVER_PLATFORMS::RACKSPACE);
             $pars[$location][Entity\CloudCredentialsProperty::RACKSPACE_API_KEY] = $this->checkVar(Entity\CloudCredentialsProperty::RACKSPACE_API_KEY, 'string', "API Key required", $location . SERVER_PLATFORMS::RACKSPACE);
             $pars[$location][Entity\CloudCredentialsProperty::RACKSPACE_IS_MANAGED] = $this->checkVar(Entity\CloudCredentialsProperty::RACKSPACE_IS_MANAGED, 'bool', "", $location . SERVER_PLATFORMS::RACKSPACE);
         } else {
             $pars[$location][Entity\CloudCredentialsProperty::RACKSPACE_USERNAME] = false;
             $pars[$location][Entity\CloudCredentialsProperty::RACKSPACE_API_KEY] = false;
             $pars[$location][Entity\CloudCredentialsProperty::RACKSPACE_IS_MANAGED] = false;
         }
     }
     if (count($this->checkVarError)) {
         $this->response->failure();
         $this->response->data(array('errors' => $this->checkVarError));
     } else {
         $this->db->BeginTrans();
         try {
             $this->env->enablePlatform(SERVER_PLATFORMS::RACKSPACE, $enabled);
             foreach ($pars as $cloud => $prs) {
                 $this->makeCloudCredentials("{$cloud}." . SERVER_PLATFORMS::RACKSPACE, $prs);
                 $this->env->setPlatformConfig([Entity\Account\EnvironmentProperty::RACKSPACE_LOCATIONS => 'enabled'], true, $cloud);
             }
             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 Rackspace settings'));
         }
         $this->db->CommitTrans();
     }
 }