public function _name($from, $to, $action) { switch ($action) { case static::ACT_CONVERT_TO_OBJECT: /* @var $from Entity\FarmRoleScalingMetric */ $to->name = ScalingMetricAdapter::metricNameToData($from->metric->name); break; case static::ACT_CONVERT_TO_ENTITY: $this->validateString($from->name, 'Property name contains invalid characters'); /* @var $metric Entity\ScalingMetric */ $metric = Entity\ScalingMetric::findOne([['name' => ScalingMetricAdapter::metricNameToEntity($from->name)]]); if (empty($metric)) { throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf('Scaling metric with name %s does not exist', $from->name)); } /* @var $to Entity\FarmRoleScalingMetric */ $to->metricId = $metric->id; $to->metric = $metric; break; case static::ACT_GET_FILTER_CRITERIA: return [[]]; } }
/** * Remove metrics. * * @param JsonData $metrics json array of metricId to remove * @throws Scalr_Exception_Core * @throws Scalr_Exception_InsufficientPermissions * @throws \Scalr\Exception\ModelException */ public function xRemoveAction(JsonData $metrics) { $this->request->restrictAccess(Acl::RESOURCE_GENERAL_CUSTOM_SCALING_METRICS, Acl::PERM_GENERAL_CUSTOM_SCALING_METRICS_MANAGE); $processed = []; $err = []; foreach ($metrics as $metricId) { try { if (!$this->db->GetOne("SELECT id FROM farm_role_scaling_metrics WHERE metric_id=? LIMIT 1", [$metricId])) { /* @var $metric Entity\ScalingMetric */ $metric = Entity\ScalingMetric::findOne([['id' => $metricId], ['envId' => $this->getEnvironmentId()]]); if (!$metric) { throw new Scalr_UI_Exception_NotFound(); } $metric->delete(); $processed[] = $metricId; } else { $err[] = sprintf(_('Metric #%s is used and cannot be removed'), $metricId); } } catch (Exception $e) { $err[] = $e->getMessage(); } } if (!count($err)) { $this->response->success('Selected metric(s) successfully removed'); } else { $this->response->warning(implode("\n", $err)); } $this->response->data(['processed' => $processed]); }
/** * {@inheritdoc} * @see ApiEntityAdapter::validateEntity() */ public function validateEntity($entity) { if (!$entity instanceof Entity\ScalingMetric) { throw new InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\ScalingMetric class")); } if (empty($entity->name)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Missed property name'); } if (!preg_match('/^' . Entity\ScalingMetric::NAME_REGEXP . '$/', $entity->name)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, 'Scaling metric name should be both alphanumeric and greater than 5 chars'); } $criteria = $this->controller->getScopeCriteria(); $criteria[] = ['name' => $entity->name]; $criteria[] = ['id' => ['$ne' => $entity->id]]; if (!empty(Entity\ScalingMetric::findOne($criteria))) { throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, sprintf('Scaling metric with name %s already exists', $entity->name)); } if (empty($entity->retrieveMethod)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Missed property retrieveMethod'); } if (!in_array($entity->retrieveMethod, [Entity\ScalingMetric::RETRIEVE_METHOD_EXECUTE, Entity\ScalingMetric::RETRIEVE_METHOD_READ, Entity\ScalingMetric::RETRIEVE_METHOD_URL_REQUEST])) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, 'Unexpected retrieveMethod value'); } $uriParts = parse_url($entity->filePath); if (!$uriParts) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, 'Property filePath is invalid'); } if ($entity->retrieveMethod == Entity\ScalingMetric::RETRIEVE_METHOD_URL_REQUEST && (empty($uriParts['scheme']) || !in_array($uriParts['scheme'], ['http', 'https']))) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, 'Invalid path for request.'); } if (empty($entity->calcFunction)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Missed property function'); } if (empty($entity->filePath)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Missed property filePath'); } }
/** * Get ScalingMetric entity * * @param string $criteria search criteria * @return ScalingMetric */ protected function getScalingMetric($criteria) { /* @var $metric ScalingMetric */ $metric = ScalingMetric::findOne([['name' => $criteria]]); static::toDelete(ScalingMetric::class, [$metric->id]); return $metric; }