public function _role($from, $to, $action)
{
switch ($action) {
case static::ACT_CONVERT_TO_OBJECT:
/* @var $from FarmRole */
$to->role = ['id' => $from->roleId];
break;
case static::ACT_CONVERT_TO_ENTITY:
/* @var $to FarmRole */
$roleId = ApiController::getBareId($from, 'role');
$role = $this->controller->getRole($roleId);
if ($to->roleId != $roleId) {
$envs = $role->getAllowedEnvironments();
if (!empty($envs) && !in_array($this->controller->getEnvironment()->id, $envs)) {
throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the Role with ID: %d", $role->id));
}
if ($role->isDeprecated) {
throw new ApiErrorException(409, ErrorMessage::ERR_DEPRECATED, "Role '{$roleId}' is deprecated");
}
$destinationBehaviors = $role->getBehaviors();
if ($role->isScalarized) {
$unsupportedBehaviors = array_diff($destinationBehaviors, static::$farmRoleSupportedBehaviors);
if (!empty($unsupportedBehaviors)) {
throw new ApiNotImplementedErrorException(sprintf('For now Scalr API supports only following built-in automation types: %s.', implode(', ', RoleAdapter::behaviorsToData(static::$farmRoleSupportedBehaviors))));
}
}
/* @var $sourceRole Role */
$sourceRole = $to->getRole();
if (!empty($sourceRole)) {
if ($sourceRole->getOs()->family !== $role->getOs()->family) {
throw new ApiErrorException(409, ErrorMessage::ERR_OS_MISMATCH, sprintf('Operating System of the Role "%s" must match to the OS of the replacement Role "%s"', $sourceRole->getOs()->family, $role->getOs()->family));
}
if ($sourceRole->isScalarized) {
if (!$role->isScalarized) {
throw new ApiErrorException(409, ErrorMessage::ERR_CONFIGURATION_MISMATCH, 'Can not replace the Role that has Scalr Agent to the agentless Role');
}
$sourceBehaviors = $sourceRole->getBehaviors();
if ($sourceRole->hasBehavior(ROLE_BEHAVIORS::CHEF) && empty($to->settings[Scalr_Role_Behavior_Chef::ROLE_CHEF_BOOTSTRAP]) && empty(RoleProperty::findOne([['name' => Scalr_Role_Behavior_Chef::ROLE_CHEF_BOOTSTRAP], ['roleId' => $sourceRole->id], ['value' => 1]]))) {
$sourceBehaviors = array_diff($sourceBehaviors, [ROLE_BEHAVIORS::CHEF]);
}
$compareBehaviors = array_diff($sourceBehaviors, $destinationBehaviors);
if (!empty($compareBehaviors)) {
throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, sprintf('The replacement role does not include the necessary builtinAutomation %s', implode(', ', RoleAdapter::behaviorsToData($compareBehaviors))));
}
}
}
}
$to->roleId = $roleId;
break;
case static::ACT_GET_FILTER_CRITERIA:
return [['roleId' => ApiController::getBareId($from, 'role')]];
}
}