Example #1
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;
 }
Example #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();
     $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;
 }
Example #3
0
File: di.php Project: mheydt/scalr
 /* @var $cont \Scalr\DependencyInjection\Container */
 $cloud = array_shift($arguments);
 if (empty($cloud)) {
     throw new BadFunctionCallException('Cloud value must be provided!');
 }
 $envId = array_shift($arguments) ?: ($cont->environment ? $cont->environment->id : null);
 if (empty($envId)) {
     throw new BadFunctionCallException('Environment value must be provided!');
 }
 $cloudCredentials = null;
 $envCloudCredId = "env_cloud_creds.{$envId}.{$cloud}";
 /* @var $cloudCredentials Entity\CloudCredentials */
 if (!$cont->initialized($envCloudCredId)) {
     $cont->setShared($envCloudCredId, function ($cont) use($envId, $cloud, &$cloudCredentials) {
         $cloudCredentials = new Entity\CloudCredentials();
         $envCloudCredentials = new Entity\EnvironmentCloudCredentials();
         /* @var $cloudCredentials Entity\CloudCredentials */
         $cloudCredentials = Entity\CloudCredentials::findOne([\Scalr\Model\AbstractEntity::STMT_FROM => "{$cloudCredentials->table()} JOIN {$envCloudCredentials->table('cecc')} ON {$cloudCredentials->columnId()} = {$envCloudCredentials->columnCloudCredentialsId('cecc')} AND {$cloudCredentials->columnCloud()} = {$envCloudCredentials->columnCloud('cecc')}", \Scalr\Model\AbstractEntity::STMT_WHERE => "{$envCloudCredentials->columnEnvId('cecc')} = {$envCloudCredentials->qstr('envId', $envId)} AND {$envCloudCredentials->columnCloud('cecc')} = {$envCloudCredentials->qstr('cloud', $cloud)}"]);
         if (!empty($cloudCredentials)) {
             $cloudCredId = $cloudCredentials->id;
             $cloudCredentials->bindEnvironment($envId);
             return $cloudCredId;
         }
         return null;
     });
 }
 $cloudCredId = $cont->get($envCloudCredId);
 $contCloudCredId = "cloud_creds.{$cloudCredId}";
 if (!$cont->initialized($contCloudCredId)) {
     $cont->setShared($contCloudCredId, function ($cont) use($envId, $cloud, $cloudCredId, &$cloudCredentials) {
         if (!(isset($cloudCredentials) || empty($cloudCredentials = Entity\CloudCredentials::findPk($cloudCredId)))) {
Example #4
0
 /**
  * {@inheritdoc}
  * @see Scalr_Model::delete()
  */
 public function delete($id = null)
 {
     if ($this->db->GetOne("SELECT COUNT(*) FROM farms WHERE env_id = ?", array($this->id))) {
         throw new Exception("Cannot remove environment. You need to remove all your farms first.");
     }
     if ($this->db->GetOne("SELECT COUNT(*) FROM client_environments WHERE client_id = ?", array($this->clientId)) < 2) {
         throw new Exception('At least one environment should be in account. You cannot remove the last one.');
     }
     parent::delete();
     try {
         $this->db->Execute("DELETE FROM client_environment_properties WHERE env_id=?", array($this->id));
         $this->db->Execute("DELETE FROM apache_vhosts WHERE env_id=?", array($this->id));
         $this->db->Execute("DELETE FROM autosnap_settings WHERE env_id=?", array($this->id));
         $this->db->Execute("DELETE FROM bundle_tasks WHERE env_id=?", array($this->id));
         $this->db->Execute("DELETE FROM dns_zones WHERE env_id=?", array($this->id));
         $this->db->Execute("DELETE FROM ec2_ebs WHERE env_id=?", array($this->id));
         $this->db->Execute("DELETE FROM elastic_ips WHERE env_id=?", array($this->id));
         $this->db->Execute("DELETE FROM farms WHERE env_id=?", array($this->id));
         $this->db->Execute("DELETE FROM roles WHERE env_id=?", array($this->id));
         $servers = \DBServer::listByFilter(['envId' => $this->id]);
         foreach ($servers as $server) {
             /* @var $server \DBServer */
             $server->Remove();
         }
         Entity\EnvironmentCloudCredentials::deleteByEnvId($this->id);
         Entity\CloudCredentials::deleteByEnvId($this->id);
         $this->db->Execute("DELETE FROM `account_team_envs` WHERE env_id = ?", array($this->id));
     } catch (Exception $e) {
         throw new Exception(sprintf(_("Cannot delete record. Error: %s"), $e->getMessage()), $e->getCode());
     }
 }