Search in sources :

Example 1 with UserGroupsEntity

use of org.apache.nifi.web.api.entity.UserGroupsEntity in project nifi by apache.

the class UserGroupsEndpointMerger method merge.

@Override
public final NodeResponse merge(final URI uri, final String method, final Set<NodeResponse> successfulResponses, final Set<NodeResponse> problematicResponses, final NodeResponse clientResponse) {
    if (!canHandle(uri, method)) {
        throw new IllegalArgumentException("Cannot use Endpoint Mapper of type " + getClass().getSimpleName() + " to map responses for URI " + uri + ", HTTP Method " + method);
    }
    final UserGroupsEntity responseEntity = clientResponse.getClientResponse().readEntity(UserGroupsEntity.class);
    final Collection<UserGroupEntity> userGroupEntities = responseEntity.getUserGroups();
    final Map<String, Map<NodeIdentifier, UserGroupEntity>> entityMap = new HashMap<>();
    for (final NodeResponse nodeResponse : successfulResponses) {
        final UserGroupsEntity nodeResponseEntity = nodeResponse == clientResponse ? responseEntity : nodeResponse.getClientResponse().readEntity(UserGroupsEntity.class);
        final Collection<UserGroupEntity> nodeUserGroupEntities = nodeResponseEntity.getUserGroups();
        // only retain user groups that all nodes agree on
        userGroupEntities.retainAll(nodeUserGroupEntities);
        for (final UserGroupEntity nodeUserGroupEntity : nodeUserGroupEntities) {
            final NodeIdentifier nodeId = nodeResponse.getNodeId();
            Map<NodeIdentifier, UserGroupEntity> innerMap = entityMap.get(nodeId);
            if (innerMap == null) {
                innerMap = new HashMap<>();
                entityMap.put(nodeUserGroupEntity.getId(), innerMap);
            }
            innerMap.put(nodeResponse.getNodeId(), nodeUserGroupEntity);
        }
    }
    UserGroupsEntityMerger.mergeUserGroups(userGroupEntities, entityMap);
    // create a new client response
    return new NodeResponse(clientResponse, responseEntity);
}
Also used : UserGroupsEntity(org.apache.nifi.web.api.entity.UserGroupsEntity) HashMap(java.util.HashMap) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) UserGroupEntity(org.apache.nifi.web.api.entity.UserGroupEntity) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with UserGroupsEntity

use of org.apache.nifi.web.api.entity.UserGroupsEntity in project nifi by apache.

the class TenantsResource method getUserGroups.

/**
 * Retrieves all the of user groups in this NiFi.
 *
 * @return A UserGroupsEntity.
 */
@GET
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.APPLICATION_JSON)
@Path("user-groups")
@ApiOperation(value = "Gets all user groups", notes = NON_GUARANTEED_ENDPOINT, response = UserGroupsEntity.class, authorizations = { @Authorization(value = "Read - /tenants") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response getUserGroups() {
    // ensure we're running with a configurable authorizer
    if (!AuthorizerCapabilityDetection.isManagedAuthorizer(authorizer)) {
        throw new IllegalStateException(AccessPolicyDAO.MSG_NON_MANAGED_AUTHORIZER);
    }
    if (isReplicateRequest()) {
        return replicate(HttpMethod.GET);
    }
    // authorize access
    serviceFacade.authorizeAccess(lookup -> {
        final Authorizable tenants = lookup.getTenant();
        tenants.authorize(authorizer, RequestAction.READ, NiFiUserUtils.getNiFiUser());
    });
    // get all the user groups
    final Set<UserGroupEntity> users = serviceFacade.getUserGroups();
    // create the response entity
    final UserGroupsEntity entity = new UserGroupsEntity();
    entity.setUserGroups(populateRemainingUserGroupEntitiesContent(users));
    // generate the response
    return generateOkResponse(entity).build();
}
Also used : UserGroupsEntity(org.apache.nifi.web.api.entity.UserGroupsEntity) Authorizable(org.apache.nifi.authorization.resource.Authorizable) UserGroupEntity(org.apache.nifi.web.api.entity.UserGroupEntity) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

UserGroupEntity (org.apache.nifi.web.api.entity.UserGroupEntity)2 UserGroupsEntity (org.apache.nifi.web.api.entity.UserGroupsEntity)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Authorizable (org.apache.nifi.authorization.resource.Authorizable)1 NodeResponse (org.apache.nifi.cluster.manager.NodeResponse)1 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)1