public function validateEntity($entity)
{
if (!$entity instanceof Farm) {
throw new InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\Farm class"));
}
if ($entity->id !== null) {
if (!Farm::findPk($entity->id)) {
throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the Farm with ID: %d", $entity->id));
}
}
$entity->name = trim($entity->name);
if (empty($entity->name)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property name");
}
$strippedName = str_replace(array('>', '<'), '', strip_tags($entity->name));
if ($strippedName != $entity->name) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid Farm name");
}
$criteria = $this->controller->getScopeCriteria();
$criteria[] = ['name' => $entity->name];
if (isset($entity->id)) {
$criteria[] = ['id' => ['$ne' => $entity->id]];
}
if (count(Farm::find($criteria))) {
throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, "Farm with name '{$entity->name}' already exists");
}
if (!empty($entity->settings[FarmSetting::EC2_VPC_REGION])) {
$region = $entity->settings[FarmSetting::EC2_VPC_REGION];
$vpcId = $entity->settings[FarmSetting::EC2_VPC_ID];
if (!in_array($region, Aws::getCloudLocations())) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Unknown VPC region");
}
$gov = new Scalr_Governance($this->controller->getEnvironment()->id);
$vpcGovernanceRegions = $gov->getValue(SERVER_PLATFORMS::EC2, Scalr_Governance::AWS_VPC, 'regions');
if (isset($vpcGovernanceRegions)) {
if (!array_key_exists($region, $vpcGovernanceRegions)) {
$regions = array_keys($vpcGovernanceRegions);
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, sprintf("Only %s %s allowed according to governance settings", ...count($regions) > 1 ? [implode(', ', $regions), 'regions are'] : [array_shift($regions), 'region is']));
}
$vpcGovernanceIds = $vpcGovernanceRegions[$region]['ids'];
if (!empty($vpcGovernanceIds) && !in_array($vpcId, $vpcGovernanceIds)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, sprintf("Only %s %s allowed according to governance settings", ...count($vpcGovernanceIds) > 1 ? [implode(', ', $vpcGovernanceIds), 'vpcs are'] : [array_shift($vpcGovernanceIds), 'vpc is']));
}
}
$found = null;
/* @var $vpc VpcData */
//TODO rewrite aws service usage
foreach ($this->controller->getContainer()->aws($region, $this->controller->getEnvironment())->ec2->vpc->describe() as $vpc) {
if ($vpcId == $vpc->vpcId) {
$found = $vpc;
break;
}
}
if (empty($found)) {
throw new ApiErrorException(400, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Could not find out the VPC with ID '{$vpcId}' in region '{$region}'");
}
} else {
if (!empty($entity->settings[FarmSetting::EC2_VPC_ID])) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property vpc.region");
}
}
if (\Scalr::config('scalr.analytics.enabled')) {
if (isset($entity->settings[FarmSetting::PROJECT_ID])) {
if (!$this->controller->getContainer()->analytics->projects->get($entity->settings[FarmSetting::PROJECT_ID], true, ['accountId' => $this->controller->getUser()->getAccountId()])) {
throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "The project is not allowed for you");
}
if (isset($entity->id) && Farm::findPk($entity->id)->settings[FarmSetting::PROJECT_ID] != $entity->settings[FarmSetting::PROJECT_ID]) {
$this->controller->checkPermissions($entity, Acl::PERM_FARMS_PROJECTS);
}
} else {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property project");
}
}
if (isset($entity->id) && $entity->isTeamsChanged() && $entity->ownerId != $this->controller->getUser()->id) {
$this->controller->checkPermissions($entity, Acl::PERM_FARMS_CHANGE_OWNERSHIP);
}
}