Пример #1
0
 /**
  * @param  int  $roleId
  */
 public function xGetRoleChefSettingsAction($roleId)
 {
     /* @var $role Role */
     $role = Role::findPk($roleId);
     if (!$role) {
         $this->response->failure('Role not found');
         return;
     }
     $this->checkPermissions($role);
     $properties = [];
     foreach (RoleProperty::find([['roleId' => $role->id], ['name' => ['$like' => 'chef.%']]]) as $prop) {
         /* @var $prop RoleProperty */
         $properties[$prop->name] = $prop->value;
     }
     $this->response->data(['chef' => $properties]);
 }
Пример #2
0
 /**
  * @param   int         $roleId
  * @param   string      $name
  * @param   string      $description
  * @param   string      $osId
  * @param   int         $catId
  * @param   bool        $isQuickStart
  * @param   bool        $isDeprecated
  * @param   bool        $isScalarized
  * @param   JsonData    $behaviors
  * @param   JsonData    $images
  * @param   JsonData    $scripts
  * @param   JsonData    $variables
  * @param   JsonData    $chef
  * @param   JsonData    $environments
  * @throws  Exception
  * @throws  Scalr_Exception_Core
  * @throws  Scalr_Exception_InsufficientPermissions
  */
 public function xSaveAction($roleId = 0, $name, $description, $osId, $catId, $isQuickStart = false, $isDeprecated = false, $isScalarized = true, JsonData $behaviors, JsonData $images, JsonData $scripts, JsonData $variables, JsonData $chef, JsonData $environments)
 {
     $this->request->restrictAccess('ROLES', 'MANAGE');
     $accountId = $this->user->getAccountId() ?: NULL;
     if (!Role::isValidName($name)) {
         throw new Exception(_("Role name is incorrect"));
     }
     if (Role::isNameUsed($name, $accountId, $this->getEnvironmentId(true), $roleId)) {
         throw new Exception('Selected role name is already used. Please select another one.');
     }
     if (!$catId) {
         throw new Exception('Role category is required');
     }
     if ($roleId == 0) {
         if (!Os::findPk($osId)) {
             throw new Exception(sprintf('%s is not valid osId', $osId));
         }
         $role = new Role();
         $role->generation = 2;
         $role->origin = $this->user->isScalrAdmin() ? ROLE_TYPE::SHARED : ROLE_TYPE::CUSTOM;
         $role->accountId = $accountId;
         $role->envId = $this->getEnvironmentId(true);
         $role->name = $name;
         $role->catId = $catId;
         $role->osId = $osId;
         $role->isScalarized = $isScalarized ? 1 : 0;
         $role->addedByUserId = $this->user->getId();
         $role->addedByEmail = $this->user->getEmail();
         $role->setBehaviors((array) $behaviors);
         $role->save();
     } else {
         $role = Role::findPk($roleId);
         if (!$role) {
             throw new Scalr_Exception_Core(sprintf(_("Role ID#%s not found in database"), $roleId));
         }
         $this->request->checkPermissions($role, true);
         $role->name = $name;
         $role->catId = $catId;
     }
     $globalVariables = new Scalr_Scripting_GlobalVariables($this->user->getAccountId(), $this->getEnvironmentId(true), ScopeInterface::SCOPE_ROLE);
     $globalVariables->setValues($variables, $role->id);
     foreach (RoleProperty::find([['roleId' => $role->id], ['name' => ['$like' => ['chef.%']]]]) as $prop) {
         $prop->delete();
     }
     foreach ($chef as $name => $value) {
         $prop = new RoleProperty();
         $prop->roleId = $role->id;
         $prop->name = $name;
         $prop->value = $value;
         $prop->save();
     }
     $role->description = $description;
     $role->isQuickStart = $isQuickStart;
     $role->isDeprecated = $isDeprecated;
     foreach ($images as $i) {
         if (isset($i['platform']) && isset($i['cloudLocation']) && isset($i['imageId'])) {
             $role->setImage($i['platform'], $i['cloudLocation'], $i['imageId'], $this->user->getId(), $this->user->getEmail());
         }
     }
     $role->setScripts((array) $scripts);
     $role->save();
     if ($this->request->getScope() == ScopeInterface::SCOPE_ACCOUNT) {
         foreach (RoleEnvironment::find([['roleId' => $roleId]]) as $re) {
             $re->delete();
         }
         $accountEnvironments = [];
         $allowedEnvironments = [];
         foreach (Environment::find([['accountId' => $this->user->getAccountId()]]) as $env) {
             $accountEnvironments[] = $env->id;
         }
         foreach ($environments as $e) {
             if ($e['enabled'] == 1 && in_array($e['id'], $accountEnvironments)) {
                 $allowedEnvironments[] = $e['id'];
             }
         }
         if (count($allowedEnvironments) < count($accountEnvironments)) {
             foreach ($allowedEnvironments as $id) {
                 $re = new RoleEnvironment();
                 $re->roleId = $role->id;
                 $re->envId = $id;
                 $re->save();
             }
         }
     }
     $this->response->data(['role' => $this->getInfo($role->id, true), 'categories' => $this->listRoleCategories(true)]);
     $this->response->success('Role saved');
 }
Пример #3
0
 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')]];
     }
 }