Scalr\Model\Entity\CloudCredentials::bindToEnvironment PHP Method

bindToEnvironment() public method

Binds current cloud credential to environment
public bindToEnvironment ( Scalr_Environment $environment ) : CloudCredentials
$environment Scalr_Environment Environment which linked credentials
return CloudCredentials
    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;
    }

Usage Example

Exemplo n.º 1
0
 protected function run1($stage)
 {
     $envIds = $this->db->Execute("SELECT `id` FROM `client_environments`");
     $platformVariables = static::getCloudsCredentialProperties();
     foreach ($envIds as $row) {
         $environment = \Scalr_Environment::init()->loadById($row['id']);
         $platforms = [];
         foreach (array_keys(SERVER_PLATFORMS::getList()) as $platform) {
             if ($environment->getPlatformConfigValue($platform . '.is_enabled', false)) {
                 $platforms[] = $platform;
             }
         }
         foreach ($platforms as $platform) {
             try {
                 switch ($platform) {
                     case SERVER_PLATFORMS::RACKSPACE:
                         foreach (['rs-ORD1', 'rs-LONx'] as $location) {
                             $cloudCredentials = new Entity\CloudCredentials();
                             $cloudCredentials->accountId = $environment->getAccountId();
                             $cloudCredentials->envId = $environment->id;
                             $cloudCredentials->cloud = "{$location}.{$platform}";
                             $cloudCredentials->name = "{$environment->id}-{$environment->getAccountId()}-{$cloudCredentials->cloud}-" . \Scalr::GenerateUID(true);
                             foreach ($platformVariables[$platform] as $name => $newName) {
                                 $value = $environment->getPlatformConfigValue($name, true, $location);
                                 if ($value === null) {
                                     $value = false;
                                 }
                                 $cloudCredentials->properties[$newName] = $value;
                             }
                             $cloudCredentials->save();
                             $cloudCredentials->bindToEnvironment($environment);
                         }
                         break;
                     default:
                         $cloudCredentials = new Entity\CloudCredentials();
                         $cloudCredentials->accountId = $environment->getAccountId();
                         $cloudCredentials->envId = $environment->id;
                         $cloudCredentials->cloud = $platform;
                         $cloudCredentials->name = "{$environment->id}-{$environment->getAccountId()}-{$platform}-" . \Scalr::GenerateUID(true);
                         $cloudCredentials->status = Entity\CloudCredentials::STATUS_ENABLED;
                         foreach ($platformVariables[$platform] as $name => $newName) {
                             $value = $environment->getPlatformConfigValue($name);
                             if ($value === null) {
                                 $value = false;
                             }
                             $cloudCredentials->properties[$newName] = $value;
                         }
                         $cloudCredentials->save();
                         $cloudCredentials->bindToEnvironment($environment);
                         break;
                 }
             } catch (Exception $e) {
                 $this->console->error(get_class($e) . " in {$e->getFile()} on line {$e->getLine()}: " . $e->getMessage());
                 error_log(get_class($e) . " in {$e->getFile()} at line {$e->getLine()}: {$e->getMessage()}\n{$e->getTraceAsString()}");
             }
         }
     }
 }
All Usage Examples Of Scalr\Model\Entity\CloudCredentials::bindToEnvironment