Scalr\Api\Service\User\V1beta0\Adapter\FarmRoleAdapter::setupPlacementConfiguration PHP Method

setupPlacementConfiguration() public static method

Setups given placement configuration to specified farm role
public static setupPlacementConfiguration ( FarmRole $role, object $placement )
$role Scalr\Model\Entity\FarmRole Configurable farm role
$placement object Placement configuration
    public static function setupPlacementConfiguration(FarmRole $role, $placement)
    {
        if (empty($placement->placementConfigurationType)) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Missed property placement.placementConfigurationType');
        }
        if (empty($placement->region)) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property region");
        }
        $role->cloudLocation = $placement->region;
        switch ($placement->placementConfigurationType) {
            case FarmRoles::AWS_VPC_PLACEMENT_CONFIGURATION:
                if (empty($placement->subnets)) {
                    throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property subnets");
                }
                if (!is_array($placement->subnets)) {
                    throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Property subnets must be array");
                }
                $subnets = [];
                foreach ($placement->subnets as $subnet) {
                    $subnets[] = $subnet->id;
                }
                $role->settings[FarmRoleSetting::AWS_VPC_SUBNET_ID] = json_encode($subnets);
                if (isset($placement->router)) {
                    $role->settings[Scalr_Role_Behavior_Router::ROLE_VPC_SCALR_ROUTER_ID] = ApiController::getBareId($placement, 'router');
                }
                break;
            case FarmRoles::AWS_CLASSIC_PLACEMENT_CONFIGURATION:
                if (isset($placement->availabilityZones) && is_array($placement->availabilityZones)) {
                    if (empty($placement->availabilityZones)) {
                        $role->settings[FarmRoleSetting::AWS_AVAIL_ZONE] = "x-scalr-diff";
                    } else {
                        $role->settings[FarmRoleSetting::AWS_AVAIL_ZONE] = count($placement->availabilityZones) > 1 ? "x-scalr-custom=" . implode(':', array_unique($placement->availabilityZones)) : array_shift($placement->availabilityZones);
                    }
                } else {
                    if (empty($placement->availabilityZones)) {
                        $role->settings[FarmRoleSetting::AWS_AVAIL_ZONE] = '';
                    } else {
                        throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Property availabilityZones must be array or NULL");
                    }
                }
                break;
            case FarmRoles::OPEN_STACK_PLACEMENT_CONFIGURATION:
            case FarmRoles::CLOUD_STACK_PLACEMENT_CONFIGURATION:
            case FarmRoles::GCE_PLACEMENT_CONFIGURATION:
                throw new ApiErrorException(501, ErrorMessage::ERR_NOT_IMPLEMENTED, 'Unsupported placementConfigurationType');
            default:
                throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, 'Unknown placementConfigurationType');
        }
    }

Usage Example

Example #1
0
 /**
  * Change placement configuration
  *
  * @param   int $farmRoleId Unique farm-role identifier
  *
  * @return  ResultEnvelope
  * @throws  ApiErrorException
  */
 public function modifyPlacementAction($farmRoleId)
 {
     $role = $this->getFarmRole($farmRoleId, null, true);
     FarmRoleAdapter::setupPlacementConfiguration($role, $this->request->getJsonBody());
     /* @var $farmRoleAdapter FarmRoleAdapter */
     $farmRoleAdapter = $this->adapter('farmRole');
     $farmRoleAdapter->validateEntity($role);
     $role->save();
     return $this->result(FarmRoleAdapter::getPlacementConfiguration($role));
 }