Scalr\Api\Service\User\V1beta0\Adapter\ScalingRuleAdapter::validateEntity PHP Method

validateEntity() public method

See also: ApiEntityAdapter::validateEntity()
public validateEntity ( FarmRoleScalingMetric $entity )
$entity Scalr\Model\Entity\FarmRoleScalingMetric 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.');
            }
        }
    }

Usage Example

Example #1
0
 /**
  * {@inheritdoc}
  * @see ScalingRuleAdapter::validateEntity()
  */
 public function validateEntity($entity)
 {
     parent::validateEntity($entity);
     if (!$entity->getFarmRole()->getRole()->isScalarized) {
         throw new ApiErrorException(409, ErrorMessage::ERR_CONFIGURATION_MISMATCH, sprintf('Can not add %s Scaling metric to the agentless Role', ScalingMetricAdapter::metricNameToData($entity->metric->name)));
     }
     $criteria[] = ['farmRoleId' => $entity->farmRoleId];
     $criteria[] = ['metricId' => ScalingMetric::METRIC_DATE_AND_TIME_ID];
     if (!empty(FarmRoleScalingMetric::findOne($criteria))) {
         throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, 'DateAndTime metric cannot be used with others');
     }
     $settings = $entity->settings;
     if ($entity->metric->isInvert) {
         $this->validateNumericSetting($settings, FarmRoleScalingMetric::MIN, 'scaleUp');
         $this->validateNumericSetting($settings, FarmRoleScalingMetric::MAX, 'scaleDown');
     } else {
         $this->validateNumericSetting($settings, FarmRoleScalingMetric::MIN, 'scaleDown');
         $this->validateNumericSetting($settings, FarmRoleScalingMetric::MAX, 'scaleUp');
     }
     if ($entity->metricId != ScalingMetric::METRIC_SQS_QUEUE_SIZE_ID && $entity->settings[FarmRoleScalingMetric::MIN] >= $entity->settings[FarmRoleScalingMetric::MAX]) {
         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, sprintf('Scale up value must be %s than Scale down value', $entity->metric->isInvert ? 'less' : 'greater'));
     }
 }
All Usage Examples Of Scalr\Api\Service\User\V1beta0\Adapter\ScalingRuleAdapter::validateEntity