Ejemplo n.º 1
0
 /**
  * xGetPlatformEndpointsAction
  *
  * @param    string    $platform  The cloud platform
  */
 public function xGetPlatformEndpointsAction($platform)
 {
     if (PlatformFactory::isOpenstack($platform)) {
         $key = Entity\CloudCredentialsProperty::OPENSTACK_KEYSTONE_URL;
     } else {
         if (PlatformFactory::isCloudstack($platform)) {
             $key = Entity\CloudCredentialsProperty::CLOUDSTACK_API_URL;
         }
     }
     if (isset($key)) {
         $pm = PlatformFactory::NewPlatform($platform);
         $rs = $this->db->Execute("\n                SELECT DISTINCT ce.`id`, cecc.`cloud_credentials_id`\n                FROM client_environments ce\n                JOIN clients c ON c.id = ce.client_id\n                JOIN environment_cloud_credentials cecc ON ce.`id` = cecc.`env_id` AND cecc.`cloud` = ?\n                JOIN cloud_credentials_properties ccp ON cecc.`cloud_credentials_id` = ccp.`cloud_credentials_id`\n                LEFT JOIN client_environment_properties cep ON ce.`id` = cep.`env_id` AND cep.`name` = ?\n                WHERE c.status = ? AND ccp.name = ? AND ce.status = ? AND cep.`value` IS NULL OR cep.`value` = ''\n                GROUP BY ccp.`value`\n            ", [$platform, "{$platform}.suspended", Scalr_Account::STATUS_ACTIVE, $key, Scalr_Environment::STATUS_ACTIVE]);
         $endpoints = [];
         $cloudCredsIds = [];
         $envs = [];
         while ($rec = $rs->FetchRow()) {
             $cloudCredsId = $rec['cloud_credentials_id'];
             $envs[$cloudCredsId] = $rec['id'];
             $cloudCredsIds[] = $cloudCredsId;
         }
         $cloudCredentialsEntity = new Entity\CloudCredentials();
         $idFieldType = $cloudCredentialsEntity->getIterator()->getField('id')->getType();
         foreach (array_chunk($cloudCredsIds, 128) as $chunk) {
             /* @var $cloudCredentials Entity\CloudCredentials */
             foreach (Entity\CloudCredentials::find([['id' => ['$in' => $chunk]]]) as $cloudCredentials) {
                 $url = $this->getContainer()->analytics->prices->normalizeUrl($cloudCredentials->properties[$key]);
                 if (!array_key_exists($url, $endpoints)) {
                     $endpoints[$url] = array('envId' => $envs[$cloudCredentials->id], 'url' => $url);
                 }
             }
         }
     } else {
         $endpoints[0] = $platform;
     }
     $this->response->data(['data' => array_values($endpoints)]);
 }
Ejemplo n.º 2
0
 /**
  * Gets cloud credentials for listed clouds
  *
  * @param   string[]    $clouds             optional Clouds list
  * @param   array       $credentialsFilter  optional Criteria to filter by CloudCredentials properties
  * @param   array       $propertiesFilter   optional Criteria to filter by CloudCredentialsProperties
  * @param   bool        $cacheResult        optional Cache result
  *
  * @return 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();
     $envCloudCredentials = new Entity\EnvironmentCloudCredentials();
     $cloudCredProps = new Entity\CloudCredentialsProperty();
     $criteria = array_merge($credentialsFilter, [\Scalr\Model\AbstractEntity::STMT_FROM => $cloudCredentials->table(), \Scalr\Model\AbstractEntity::STMT_WHERE => '']);
     if (!empty($clouds)) {
         $criteria[\Scalr\Model\AbstractEntity::STMT_FROM] .= "\n                JOIN {$envCloudCredentials->table('cecc')} ON\n                    {$cloudCredentials->columnId()} = {$envCloudCredentials->columnCloudCredentialsId('cecc')} AND\n                    {$cloudCredentials->columnCloud()} = {$envCloudCredentials->columnCloud('cecc')}\n            ";
         $clouds = implode(", ", array_map(function ($cloud) use($envCloudCredentials) {
             return $envCloudCredentials->qstr('cloud', $cloud);
         }, $clouds));
         $criteria[\Scalr\Model\AbstractEntity::STMT_WHERE] = "\n                {$envCloudCredentials->columnEnvId('cecc')} = {$envCloudCredentials->qstr('envId', $this->id)} AND\n                {$envCloudCredentials->columnCloud('cecc')} IN ({$clouds})\n            ";
     }
     if (!empty($propertiesFilter)) {
         foreach ($propertiesFilter as $property => $propCriteria) {
             $criteria[\Scalr\Model\AbstractEntity::STMT_FROM] .= "\n                    LEFT JOIN {$cloudCredProps->table('ccp')} ON\n                        {$cloudCredentials->columnId()} = {$cloudCredProps->columnCloudCredentialsId('ccp')} AND\n                        {$cloudCredProps->columnName('ccp')} = {$cloudCredProps->qstr('name', $property)}\n                ";
             $conjunction = empty($criteria[\Scalr\Model\AbstractEntity::STMT_WHERE]) ? "" : "AND";
             $criteria[\Scalr\Model\AbstractEntity::STMT_WHERE] .= "\n                    {$conjunction} {$cloudCredProps->_buildQuery($propCriteria, 'AND', 'ccp')}\n                ";
         }
     }
     /* @var $cloudsCredentials Entity\CloudCredentials[] */
     $cloudsCredentials = Entity\CloudCredentials::find($criteria);
     $result = [];
     $cont = \Scalr::getContainer();
     foreach ($cloudsCredentials as $cloudCredentials) {
         $result[$cloudCredentials->cloud] = $cloudCredentials;
         if ($cacheResult) {
             $cloudCredentials->bindEnvironment($this->id);
             $cloudCredentials->cache($cont);
         }
     }
     return $result;
 }
Ejemplo n.º 3
0
 /**
  * Gets cloud credentials for listed clouds
  *
  * @param   string[]    $clouds             optional Clouds list
  * @param   array       $credentialsFilter  optional Criteria to filter by CloudCredentials properties
  * @param   array       $propertiesFilter   optional Criteria to filter by CloudCredentialsProperties
  *
  * @return  EntityIterator|CloudCredentials[]
  */
 public function cloudCredentialsList(array $clouds = null, array $credentialsFilter = [], array $propertiesFilter = [])
 {
     if (!is_array($clouds)) {
         $clouds = (array) $clouds;
     }
     $cloudCredentials = new CloudCredentials();
     $cloudCredProps = new 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]];
     $criteria[] = ['accountId' => $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) . ")";
     }
     return CloudCredentials::find($criteria);
 }
Ejemplo n.º 4
0
 /**
  * Gets cloud credentials for listed clouds
  *
  * @param   string[]    $clouds             optional Clouds list
  * @param   array       $credentialsFilter  optional Criteria to filter by CloudCredentials properties
  * @param   array       $propertiesFilter   optional Criteria to filter by CloudCredentialsProperties
  * @param   bool        $cacheResult        optional Cache result
  *
  * @return 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();
     $envCloudCredentials = new Entity\EnvironmentCloudCredentials();
     $criteria = $credentialsFilter;
     $from[] = empty($criteria[AbstractEntity::STMT_FROM]) ? " {$cloudCredentials->table()} " : $criteria[AbstractEntity::STMT_FROM];
     $where = empty($criteria[AbstractEntity::STMT_WHERE]) ? [] : [$criteria[AbstractEntity::STMT_WHERE]];
     $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;
 }