Scalr\Farm\Role\FarmRoleStorageConfig::create PHP Method

create() public method

Create new FarmRoleStorageConfig based on input config
public create ( FarmRoleStorageConfig $config )
$config FarmRoleStorageConfig
    public function create(FarmRoleStorageConfig $config)
    {
        $deleteFlag = false;
        if ($config->id) {
            $this->loadById($config->id);
            if ($this->status == self::STATE_PENDING_CREATE) {
                if ($config->status == self::STATE_PENDING_DELETE) {
                    // mark for delete on save
                    $deleteFlag = true;
                } else {
                    $this->type = $config->type;
                    $this->fs = $config->fs;
                    $this->reUse = $config->reUse;
                    $this->rebuild = $config->rebuild;
                    $this->mount = $config->mount;
                    $this->mountPoint = $config->mountPoint;
                    $this->mountOptions = $config->mountOptions;
                    $this->label = $config->label;
                }
            } elseif ($config->status == self::STATE_PENDING_DELETE) {
                $this->status = self::STATE_PENDING_DELETE;
            }
        } else {
            $this->id = \Scalr::GenerateUID();
            $this->type = $config->type;
            $this->fs = $config->fs;
            $this->reUse = $config->reUse;
            $this->rebuild = $config->rebuild;
            $this->mount = $config->mount;
            $this->mountPoint = $config->mountPoint;
            $this->mountOptions = $config->mountOptions;
            $this->label = $config->label;
            $this->status = self::STATE_PENDING_CREATE;
        }
        if ($deleteFlag) {
            $this->delete();
            return;
        }
        $this->settings = $config->settings;
        $this->save();
    }

Usage Example

Esempio n. 1
0
 /**
  * Save storage configs
  *
  * @param   array   $configs    Array of storage config
  * @param   bool    $validate   optional    If true validate config before save
  * @throws  FarmRoleStorageException
  */
 public function setConfigs($configs, $validate = true)
 {
     $configs = is_array($configs) ? $configs : [];
     $ephemeralEc2 = [];
     $ephemeralGce = [];
     foreach ($configs as $value) {
         if (!is_array($value) || !is_array($value['settings'])) {
             continue;
         }
         $object = new FarmRoleStorageConfig($this->farmRole);
         $object->apply($value);
         if ($validate) {
             $r = $object->validate();
             if ($r !== true) {
                 throw new FarmRoleStorageException($r);
             }
         }
         $config = new FarmRoleStorageConfig($this->farmRole);
         $config->create($object);
         if ($config->type == FarmRoleStorageConfig::TYPE_EC2_EPHEMERAL) {
             $ephemeralEc2[$config->settings[FarmRoleStorageConfig::SETTING_EC2_EPHEMERAL_NAME]] = $config->id;
         } else {
             if ($config->type == FarmRoleStorageConfig::TYPE_GCE_EPHEMERAL) {
                 $ephemeralGce[$config->settings[FarmRoleStorageConfig::SETTING_GCE_EPHEMERAL_NAME]] = $config->id;
             }
         }
     }
     // validate ephemeral configs
     foreach (self::getConfigs() as $config) {
         if ($config->type == FarmRoleStorageConfig::TYPE_EC2_EPHEMERAL) {
             $name = $config->settings[FarmRoleStorageConfig::SETTING_EC2_EPHEMERAL_NAME];
             if (!isset($ephemeralEc2[$name]) || $ephemeralEc2[$name] != $config->id) {
                 $config->delete();
             }
         } else {
             if ($config->type == FarmRoleStorageConfig::TYPE_GCE_EPHEMERAL) {
                 $name = $config->settings[FarmRoleStorageConfig::SETTING_GCE_EPHEMERAL_NAME];
                 if (!isset($ephemeralGce[$name]) || $ephemeralGce[$name] != $config->id) {
                     $config->delete();
                 }
             }
         }
     }
 }