/** * {@inheritdoc} * @see ApiEntityAdapter::validateEntity() * @param Entity\FarmRoleScalingMetric $entity scaling metric entity */ public function validateEntity($entity) { if (!$entity instanceof Entity\FarmRoleScalingMetric) { throw new InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\FarmRoleScalingMetric class")); } $farmRole = $entity->getFarmRole(); if (empty($farmRole)) { throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the Farm role with ID: %d", $entity->farmRoleId)); } $disabledScalingBehaviors = array_intersect(FarmRoleAdapter::$disableScalingBehaviors, $farmRole->getRole()->getBehaviors()); if (!empty($disabledScalingBehaviors)) { throw new ApiErrorException(409, ErrorMessage::ERR_CONFIGURATION_MISMATCH, sprintf('Can not add Scaling Metric to the Role with the following built-in automation types: %s.', implode(', ', RoleAdapter::behaviorsToData($disabledScalingBehaviors)))); } if (empty($entity->id)) { if (empty($entity->metricId)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property name"); } $criteria = [['farmRoleId' => $entity->farmRoleId], ['metricId' => $entity->metricId]]; if (!empty(Entity\FarmRoleScalingMetric::findOne($criteria))) { throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, 'This Scaling metric already added to the current Farm role.'); } } }
/** * Setups given scaling configuration to specified farm role * * @param FarmRole $farmRole Configurable farm role * @param object $scaling Scaling configuration * * @throws ApiErrorException */ public static function setupScalingConfiguration(FarmRole $farmRole, $scaling) { $disabledScalingBehaviors = array_intersect(static::$disableScalingBehaviors, $farmRole->getRole()->getBehaviors()); if (!empty($disabledScalingBehaviors)) { throw new ApiErrorException(409, ErrorMessage::ERR_CONFIGURATION_MISMATCH, sprintf('Can not add scaling configuration to the Role with the following built-in automation types: %s.', implode(', ', RoleAdapter::behaviorsToData($disabledScalingBehaviors)))); } if (isset($scaling->enabled)) { $farmRole->settings[FarmRoleSetting::SCALING_ENABLED] = intval($scaling->enabled); } if (isset($scaling->minInstances)) { if ($scaling->minInstances < 0) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Property scaling.minInstances must be greater than or equal to 0"); } $farmRole->settings[FarmRoleSetting::SCALING_MIN_INSTANCES] = intval($scaling->minInstances); } if (isset($scaling->maxInstances)) { if ($scaling->maxInstances > 400) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Property scaling.maxInstances must be less than or equal to 400"); } $farmRole->settings[FarmRoleSetting::SCALING_MAX_INSTANCES] = intval($scaling->maxInstances); } if ($farmRole->settings[FarmRoleSetting::SCALING_MAX_INSTANCES] < $farmRole->settings[FarmRoleSetting::SCALING_MIN_INSTANCES]) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Property scaling.maxInstances must be greater than or equal to scaling.minInstances"); } }