Search in sources :

Example 26 with GetMultipleDataResult

use of alien4cloud.dao.model.GetMultipleDataResult in project alien4cloud by alien4cloud.

the class LocationSecurityController method getAuthorizedEnvironmentsAndEnvTypesPerApplicationPaginated.

/**
 * search applications,environments and environment types authorised to access the location.
 *
 * @return {@link RestResponse} that contains a {@link GetMultipleDataResult} of {@link GroupDTO}..
 */
@ApiOperation(value = "List all applications,environments and environment types authorized to access the location", notes = "Only user with ADMIN role can list authorized applications,environments and environment types to the location.")
@RequestMapping(value = "/applications/search", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('ADMIN')")
public RestResponse<GetMultipleDataResult<ApplicationEnvironmentAuthorizationDTO>> getAuthorizedEnvironmentsAndEnvTypesPerApplicationPaginated(@PathVariable String orchestratorId, @PathVariable String locationId, @ApiParam(value = "Text Query to search.") @RequestParam(required = false) String query, @ApiParam(value = "Query from the given index.") @RequestParam(required = false, defaultValue = "0") int from, @ApiParam(value = "Maximum number of results to retrieve.") @RequestParam(required = false, defaultValue = "20") int size) {
    Location location = locationService.getLocation(orchestratorId, locationId);
    List<Application> applicationsRelatedToEnvironment = Lists.newArrayList();
    List<Application> applicationsRelatedToEnvironmentType = Lists.newArrayList();
    List<ApplicationEnvironment> environments = Lists.newArrayList();
    List<String> environmentTypes = Lists.newArrayList();
    List<Application> applications = Lists.newArrayList();
    // we get all authorized applications and environment to not favor the one of them
    if (MapUtils.isNotEmpty(location.getEnvironmentPermissions())) {
        environments = alienDAO.findByIds(ApplicationEnvironment.class, location.getEnvironmentPermissions().keySet().toArray(new String[location.getEnvironmentPermissions().size()]));
        Set<String> environmentApplicationIds = environments.stream().map(ae -> new String(ae.getApplicationId())).collect(Collectors.toSet());
        applicationsRelatedToEnvironment = alienDAO.findByIds(Application.class, environmentApplicationIds.toArray(new String[environmentApplicationIds.size()]));
    }
    if (MapUtils.isNotEmpty(location.getEnvironmentTypePermissions())) {
        environmentTypes.addAll(location.getEnvironmentTypePermissions().keySet());
        Set<String> environmentTypeApplicationIds = Sets.newHashSet();
        for (String envType : safe(location.getEnvironmentTypePermissions()).keySet()) {
            environmentTypeApplicationIds.add(envType.split(":")[0]);
        }
        applicationsRelatedToEnvironmentType = alienDAO.findByIds(Application.class, environmentTypeApplicationIds.toArray(new String[environmentTypeApplicationIds.size()]));
    }
    if (MapUtils.isNotEmpty(location.getApplicationPermissions())) {
        applications = alienDAO.findByIds(Application.class, location.getApplicationPermissions().keySet().toArray(new String[location.getApplicationPermissions().size()]));
    }
    List<ApplicationEnvironmentAuthorizationDTO> allDTOs = ApplicationEnvironmentAuthorizationDTO.buildDTOs(applicationsRelatedToEnvironment, applicationsRelatedToEnvironmentType, environments, applications, environmentTypes);
    int to = (from + size < allDTOs.size()) ? from + size : allDTOs.size();
    allDTOs = IntStream.range(from, to).mapToObj(allDTOs::get).collect(Collectors.toList());
    List<String> ids = allDTOs.stream().map(appEnvDTO -> appEnvDTO.getApplication().getId()).collect(Collectors.toList());
    IdsFilterBuilder idFilters = FilterBuilders.idsFilter().ids(ids.toArray(new String[ids.size()]));
    GetMultipleDataResult<Application> tempResult = alienDAO.search(Application.class, query, null, idFilters, null, from, to, "id", false);
    return RestResponseBuilder.<GetMultipleDataResult<ApplicationEnvironmentAuthorizationDTO>>builder().data(ApplicationEnvironmentAuthorizationDTO.convert(tempResult, allDTOs)).build();
}
Also used : IntStream(java.util.stream.IntStream) PathVariable(org.springframework.web.bind.annotation.PathVariable) Lists(org.elasticsearch.common.collect.Lists) RequestParam(org.springframework.web.bind.annotation.RequestParam) Arrays(java.util.Arrays) ApplicationEnvironmentService(alien4cloud.application.ApplicationEnvironmentService) Subject(alien4cloud.security.Subject) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) LocationService(alien4cloud.orchestrators.locations.services.LocationService) ResourcePermissionService(alien4cloud.authorization.ResourcePermissionService) ApplicationEnvironmentAuthorizationDTO(alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationDTO) ApiParam(io.swagger.annotations.ApiParam) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) IdsFilterBuilder(org.elasticsearch.index.query.IdsFilterBuilder) User(alien4cloud.security.model.User) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) Location(alien4cloud.model.orchestrators.locations.Location) RequestBody(org.springframework.web.bind.annotation.RequestBody) ApiOperation(io.swagger.annotations.ApiOperation) Audit(alien4cloud.audit.annotation.Audit) RestResponseBuilder(alien4cloud.rest.model.RestResponseBuilder) RestResponse(alien4cloud.rest.model.RestResponse) Application(alien4cloud.model.application.Application) Api(io.swagger.annotations.Api) MapUtils(org.apache.commons.collections4.MapUtils) GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) FilterBuilders(org.elasticsearch.index.query.FilterBuilders) MediaType(org.springframework.http.MediaType) Resource(javax.annotation.Resource) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) IGenericSearchDAO(alien4cloud.dao.IGenericSearchDAO) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) IAlienGroupDao(alien4cloud.security.groups.IAlienGroupDao) IAlienUserDao(alien4cloud.security.users.IAlienUserDao) List(java.util.List) GroupDTO(alien4cloud.rest.orchestrator.model.GroupDTO) Group(alien4cloud.security.model.Group) UserDTO(alien4cloud.rest.orchestrator.model.UserDTO) ApplicationEnvironmentAuthorizationUpdateRequest(alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationUpdateRequest) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) IdsFilterBuilder(org.elasticsearch.index.query.IdsFilterBuilder) ApplicationEnvironmentAuthorizationDTO(alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationDTO) GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult) Application(alien4cloud.model.application.Application) Location(alien4cloud.model.orchestrators.locations.Location) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 27 with GetMultipleDataResult

use of alien4cloud.dao.model.GetMultipleDataResult in project alien4cloud by alien4cloud.

the class ApplicationEnvironmentAuthorizationDTO method convert.

public static GetMultipleDataResult<ApplicationEnvironmentAuthorizationDTO> convert(GetMultipleDataResult<Application> toConvert, List<ApplicationEnvironmentAuthorizationDTO> allDTOs) {
    if (toConvert == null) {
        return null;
    }
    GetMultipleDataResult<ApplicationEnvironmentAuthorizationDTO> converted = new GetMultipleDataResult<>();
    ReflectionUtil.mergeObject(toConvert, converted, "data");
    List<ApplicationEnvironmentAuthorizationDTO> data = Lists.newArrayList();
    for (Application app : toConvert.getData()) {
        for (ApplicationEnvironmentAuthorizationDTO appEnvDTO : allDTOs) {
            if (app.getId().equals(appEnvDTO.getApplication().getId())) {
                data.add(appEnvDTO);
            }
        }
    }
    converted.setData(data.toArray(new ApplicationEnvironmentAuthorizationDTO[toConvert.getData().length]));
    return converted;
}
Also used : GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult) Application(alien4cloud.model.application.Application)

Example 28 with GetMultipleDataResult

use of alien4cloud.dao.model.GetMultipleDataResult in project alien4cloud by alien4cloud.

the class QuickSearchController method searchByType.

private GetMultipleDataResult searchByType(BasicSearchRequest requestObject, Set<String> authoIndexes, Set<Class<?>> classes, Map<String, String[]> filters, FilterBuilder filterBuilder) {
    String[] indices = authoIndexes.toArray(new String[authoIndexes.size()]);
    if (indices.length == 0) {
        return new GetMultipleDataResult();
    }
    Class<?>[] classesArray = classes.toArray(new Class<?>[classes.size()]);
    GetMultipleDataResult searchResult = alienDAO.search(indices, classesArray, requestObject.getQuery(), filters, filterBuilder, FetchContext.QUICK_SEARCH, requestObject.getFrom(), requestObject.getSize());
    return searchResult;
}
Also used : GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult)

Example 29 with GetMultipleDataResult

use of alien4cloud.dao.model.GetMultipleDataResult in project alien4cloud by alien4cloud.

the class ComponentTest method recommendForCapabilityWhenAlreadyRecommendedTest.

@Test
public void recommendForCapabilityWhenAlreadyRecommendedTest() {
    RestResponse<NodeType> response = null;
    RecommendationRequest recRequest = new RecommendationRequest();
    recRequest.setComponentId(indexedNodeType.getId());
    recRequest.setCapability("jdni");
    response = componentController.recommendComponentForCapability(recRequest);
    assertNull(response.getError());
    assertNotNull(response.getData());
    assertTrue(response.getData().getDefaultCapabilities().contains("jdni"));
    Map<String, String[]> filters = new HashMap<>();
    filters.put(AlienConstants.DEFAULT_CAPABILITY_FIELD_NAME, new String[] { "jdni" });
    GetMultipleDataResult result = dao.find(NodeType.class, filters, 1);
    NodeType component;
    if (result == null || result.getData() == null || result.getData().length == 0) {
        component = null;
    } else {
        component = (NodeType) result.getData()[0];
    }
    assertNotNull(component);
    assertNotNull(component.getDefaultCapabilities());
    assertTrue(component.getId().equals(recRequest.getComponentId()));
    assertTrue(component.getDefaultCapabilities().contains("jdni"));
}
Also used : HashMap(java.util.HashMap) NodeType(org.alien4cloud.tosca.model.types.NodeType) GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult) Test(org.junit.Test)

Example 30 with GetMultipleDataResult

use of alien4cloud.dao.model.GetMultipleDataResult in project alien4cloud by alien4cloud.

the class SuggestionController method tagSuggest.

/**
 * Get suggestion for tags based on current tags defined on the components.
 *
 * @param tagName The name of the tag for which to get suggestion.
 * @param searchPrefix The current prefix for the tag suggestion.
 * @return A {@link RestResponse} that contains a list of suggestions for the tag key.
 */
@ApiIgnore
@RequestMapping(value = "/tag/{tagName}/{searchPrefix}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
public RestResponse<String[]> tagSuggest(@PathVariable String tagName, @PathVariable String searchPrefix) {
    String suggestFieldPath = TAG_FIELD.concat(".").concat(tagName);
    GetMultipleDataResult searchResult = dao.suggestSearch(INDEXES, CLASSES, suggestFieldPath, searchPrefix, FetchContext.TAG_SUGGESTION, 0, SUGGESTION_COUNT);
    String[] types = searchResult.getTypes();
    Set<String> tagsSuggestions = Sets.newHashSet();
    for (int i = 0; i < types.length; i++) {
        List<Tag> tags;
        if (types[i].equals(MappingBuilder.indexTypeFromClass(Application.class))) {
            Application app = (Application) searchResult.getData()[i];
            tags = app.getTags();
        } else {
            AbstractToscaType indexedToscaElement = (AbstractToscaType) searchResult.getData()[i];
            tags = indexedToscaElement.getTags();
        }
        addSuggestedTag(tags, tagName, searchPrefix, tagsSuggestions);
    }
    return RestResponseBuilder.<String[]>builder().data(tagsSuggestions.toArray(new String[tagsSuggestions.size()])).build();
}
Also used : AbstractToscaType(org.alien4cloud.tosca.model.types.AbstractToscaType) Tag(alien4cloud.model.common.Tag) GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult) Application(alien4cloud.model.application.Application) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ApiIgnore(springfox.documentation.annotations.ApiIgnore) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

GetMultipleDataResult (alien4cloud.dao.model.GetMultipleDataResult)32 Test (org.junit.Test)9 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 ApiOperation (io.swagger.annotations.ApiOperation)7 HashMap (java.util.HashMap)7 NodeType (org.alien4cloud.tosca.model.types.NodeType)7 Application (alien4cloud.model.application.Application)4 User (alien4cloud.security.model.User)4 Then (cucumber.api.java.en.Then)4 Location (alien4cloud.model.orchestrators.locations.Location)3 IdsFilterBuilder (org.elasticsearch.index.query.IdsFilterBuilder)3 ApplicationEnvironmentService (alien4cloud.application.ApplicationEnvironmentService)2 IGenericSearchDAO (alien4cloud.dao.IGenericSearchDAO)2 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)2 Orchestrator (alien4cloud.model.orchestrators.Orchestrator)2 FilteredSearchRequest (alien4cloud.rest.model.FilteredSearchRequest)2 Group (alien4cloud.security.model.Group)2 Arrays (java.util.Arrays)2 Resource (javax.annotation.Resource)2