Example #1
0
 /**
  * Binds current cloud credential to environment
  *
  * @param Scalr_Environment $environment Environment which linked credentials
  *
  * @return CloudCredentials
  *
  * @throws Exception
  * @throws \Scalr\Exception\ModelException
  */
 public function bindToEnvironment(Scalr_Environment $environment)
 {
     if (empty($this->id)) {
         throw new UnderflowException("CloudCredentials entity must be saved before!");
     }
     /* @var $previousCloudCreds CloudCredentials */
     $previousCloudCreds = $environment->keychain($this->cloud);
     if (!empty($previousCloudCreds->id)) {
         if ($previousCloudCreds->id == $this->id) {
             return $this;
         } else {
             $previousCloudCreds->release();
         }
     }
     $envCloudCreds = EnvironmentCloudCredentials::findPk($environment->id, $this->cloud);
     if (empty($envCloudCreds)) {
         $envCloudCreds = new EnvironmentCloudCredentials();
         $envCloudCreds->envId = $environment->id;
         $envCloudCreds->cloud = $this->cloud;
     }
     $db = $this->db();
     try {
         $db->BeginTrans();
         $envCloudCreds->cloudCredentialsId = $this->id;
         $envCloudCreds->save();
         $db->CommitTrans();
     } catch (Exception $e) {
         $db->RollbackTrans();
         throw $e;
     }
     $this->_envBinds[$envCloudCreds->envId] = $envCloudCreds;
     return $this;
 }