public function createAction($farmId)
{
$object = $this->request->getJsonBody();
/* @var $farmRoleAdapter FarmRoleAdapter */
$farmRoleAdapter = $this->adapter('farmRole');
$this->getFarm($farmId, true);
//Pre validates the request object
$farmRoleAdapter->validateObject($object, Request::METHOD_POST);
$role = $farmRoleAdapter->toEntity($object);
$role->id = null;
$role->farmId = $farmId;
if (empty($object->scaling)) {
$role->settings[FarmRoleSetting::SCALING_ENABLED] = true;
$role->settings[FarmRoleSetting::SCALING_MIN_INSTANCES] = 1;
$role->settings[FarmRoleSetting::SCALING_MAX_INSTANCES] = 2;
} else {
if (empty($object->scaling->enabled) && !(empty($object->scaling->minInstances) || empty($object->scaling->maxInstances))) {
$role->settings[FarmRoleSetting::SCALING_ENABLED] = true;
}
}
$farmRoleAdapter->validateEntity($role);
//Saves entity
try {
$role->save();
} catch (InvalidEntityConfigurationException $e) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, $e->getMessage(), $e->getCode(), $e);
}
//Responds with 201 Created status
$this->response->setStatus(201);
return $this->result($farmRoleAdapter->toData($role));
}