RequestResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus createResources(Request request) throws SystemException, UnsupportedPropertyException, NoSuchParentResourceException, ResourceAlreadyExistsException { if (request.getProperties().size() > 1) { throw new UnsupportedOperationException("Multiple actions/commands cannot be executed at the same time."); } final ExecuteActionRequest actionRequest = getActionRequest(request); final Map<String, String> requestInfoProperties = request.getRequestInfoProperties(); return getRequestStatus(createResources(new Command<RequestStatusResponse>() { @Override public RequestStatusResponse invoke() throws AmbariException, AuthorizationException { String clusterName = actionRequest.getClusterName(); ResourceType resourceType; Long resourceId; if (StringUtils.isEmpty(clusterName)) { resourceType = ResourceType.AMBARI; resourceId = null; } else { resourceType = ResourceType.CLUSTER; resourceId = getClusterResourceId(clusterName); } if (actionRequest.isCommand()) { String commandName = actionRequest.getCommandName(); if (StringUtils.isEmpty(commandName)) { commandName = "_unknown_command_"; } if (commandName.endsWith("_SERVICE_CHECK")) { if (!AuthorizationHelper.isAuthorized(resourceType, resourceId, RoleAuthorization.SERVICE_RUN_SERVICE_CHECK)) { throw new AuthorizationException("The authenticated user is not authorized to execute service checks."); } } else if (commandName.equals("DECOMMISSION")) { if (!AuthorizationHelper.isAuthorized(resourceType, resourceId, RoleAuthorization.SERVICE_DECOMMISSION_RECOMMISSION)) { throw new AuthorizationException("The authenticated user is not authorized to decommission services."); } } else { if (!AuthorizationHelper.isAuthorized(resourceType, resourceId, RoleAuthorization.SERVICE_RUN_CUSTOM_COMMAND)) { throw new AuthorizationException(String.format("The authenticated user is not authorized to execute the command, %s.", commandName)); } } } else { String actionName = actionRequest.getActionName(); if (StringUtils.isEmpty(actionName)) { actionName = "_unknown_action_"; } if (actionName.contains("SERVICE_CHECK")) { if (!AuthorizationHelper.isAuthorized(resourceType, resourceId, RoleAuthorization.SERVICE_RUN_SERVICE_CHECK)) { throw new AuthorizationException("The authenticated user is not authorized to execute service checks."); } } else { ActionDefinition actionDefinition = (actionName == null) ? null : getManagementController().getAmbariMetaInfo().getActionDefinition(actionName); Set<RoleAuthorization> permissions = (actionDefinition == null) ? null : actionDefinition.getPermissions(); if (!AuthorizationHelper.isAuthorized(resourceType, resourceId, permissions)) { throw new AuthorizationException(String.format("The authenticated user is not authorized to execute the action %s.", actionName)); } } } return getManagementController().createAction(actionRequest, requestInfoProperties); } })); } RequestResourceProvider(Set<String> propertyIds,
                          Map<Resource.Type, String> keyPropertyIds,
                          AmbariManagementController managementController); @Override RequestStatus createResources(Request request); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus updateResources(Request requestInfo, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate); static final String REQUEST_CLUSTER_NAME_PROPERTY_ID; static final String REQUEST_CLUSTER_ID_PROPERTY_ID; static final String REQUEST_ID_PROPERTY_ID; static final String REQUEST_SOURCE_SCHEDULE; static final String REQUEST_SOURCE_SCHEDULE_ID; static final String REQUEST_SOURCE_SCHEDULE_HREF; }
@Test public void testCreateResources() throws Exception { Resource.Type type = Resource.Type.Request; AmbariManagementController managementController = createMock(AmbariManagementController.class); RequestStatusResponse response = createNiceMock(RequestStatusResponse.class); replay(managementController, response); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String, Object>>(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put(RequestResourceProvider.REQUEST_ID_PROPERTY_ID, "Request100"); propertySet.add(properties); Request request = PropertyHelper.getCreateRequest(propertySet, null); try { provider.createResources(request); Assert.fail("Expected an UnsupportedOperationException"); } catch (UnsupportedOperationException e) { } verify(managementController, response); }