Search in sources :

Example 11 with SearchResultResourceRep

use of com.emc.storageos.model.search.SearchResultResourceRep in project coprhd-controller by CoprHD.

the class OrderService method getOtherSearchResults.

/**
 * parameter: 'orderStatus' The status for the order
 * parameter: 'startTime' Start time to search for orders
 * parameter: 'endTime' End time to search for orders
 *
 * @return Return a list of matching orders or an empty list if no match was found.
 */
@Override
protected SearchResults getOtherSearchResults(Map<String, List<String>> parameters, boolean authorized) {
    StorageOSUser user = getUserFromContext();
    String tenantId = user.getTenantId();
    if (parameters.containsKey(SearchConstants.TENANT_ID_PARAM)) {
        tenantId = parameters.get(SearchConstants.TENANT_ID_PARAM).get(0);
    }
    verifyAuthorizedInTenantOrg(uri(tenantId), user);
    if (!parameters.containsKey(SearchConstants.ORDER_STATUS_PARAM) && !parameters.containsKey(SearchConstants.START_TIME_PARAM) && !parameters.containsKey(SearchConstants.END_TIME_PARAM)) {
        throw APIException.badRequests.invalidParameterSearchMissingParameter(getResourceClass().getName(), SearchConstants.ORDER_STATUS_PARAM + " or " + SearchConstants.START_TIME_PARAM + " or " + SearchConstants.END_TIME_PARAM);
    }
    if (parameters.containsKey(SearchConstants.ORDER_STATUS_PARAM) && (parameters.containsKey(SearchConstants.START_TIME_PARAM) || parameters.containsKey(SearchConstants.END_TIME_PARAM))) {
        throw APIException.badRequests.parameterForSearchCouldNotBeCombinedWithAnyOtherParameter(getResourceClass().getName(), SearchConstants.ORDER_STATUS_PARAM, SearchConstants.START_TIME_PARAM + " or " + SearchConstants.END_TIME_PARAM);
    }
    List<Order> orders = Lists.newArrayList();
    if (parameters.containsKey(SearchConstants.ORDER_STATUS_PARAM)) {
        String orderStatus = parameters.get(SearchConstants.ORDER_STATUS_PARAM).get(0);
        ArgValidator.checkFieldNotEmpty(orderStatus, SearchConstants.ORDER_STATUS_PARAM);
        orders = orderManager.findOrdersByStatus(uri(tenantId), OrderStatus.valueOf(orderStatus));
    } else if (parameters.containsKey(SearchConstants.START_TIME_PARAM) || parameters.containsKey(SearchConstants.END_TIME_PARAM)) {
        Date startTime = null;
        if (parameters.containsKey(SearchConstants.START_TIME_PARAM)) {
            startTime = TimeUtils.getDateTimestamp(parameters.get(SearchConstants.START_TIME_PARAM).get(0));
        }
        Date endTime = null;
        if (parameters.containsKey(SearchConstants.END_TIME_PARAM)) {
            endTime = TimeUtils.getDateTimestamp(parameters.get(SearchConstants.END_TIME_PARAM).get(0));
        }
        if (startTime == null && endTime == null) {
            throw APIException.badRequests.invalidParameterSearchMissingParameter(getResourceClass().getName(), SearchConstants.ORDER_STATUS_PARAM + " or " + SearchConstants.START_TIME_PARAM + " or " + SearchConstants.END_TIME_PARAM);
        }
        if (startTime.after(endTime)) {
            throw APIException.badRequests.endTimeBeforeStartTime(startTime.toString(), endTime.toString());
        }
        int maxCount = -1;
        List<String> c = parameters.get(SearchConstants.ORDER_MAX_COUNT);
        if (c != null) {
            String maxCountParam = parameters.get(SearchConstants.ORDER_MAX_COUNT).get(0);
            maxCount = Integer.parseInt(maxCountParam);
        }
        orders = orderManager.findOrdersByTimeRange(uri(tenantId), startTime, endTime, maxCount);
    }
    ResRepFilter<SearchResultResourceRep> resRepFilter = (ResRepFilter<SearchResultResourceRep>) getPermissionFilter(getUserFromContext(), _permissionsHelper);
    List<SearchResultResourceRep> searchResultResourceReps = Lists.newArrayList();
    for (Order order : orders) {
        RestLinkRep selfLink = new RestLinkRep("self", RestLinkFactory.newLink(getResourceType(), order.getId()));
        SearchResultResourceRep searchResultResourceRep = new SearchResultResourceRep();
        searchResultResourceRep.setId(order.getId());
        searchResultResourceRep.setLink(selfLink);
        if (authorized || resRepFilter.isAccessible(searchResultResourceRep)) {
            searchResultResourceReps.add(searchResultResourceRep);
        }
    }
    SearchResults result = new SearchResults();
    result.setResource(searchResultResourceReps);
    return result;
}
Also used : Order(com.emc.storageos.db.client.model.uimodels.Order) StorageOSUser(com.emc.storageos.security.authentication.StorageOSUser) SearchResultResourceRep(com.emc.storageos.model.search.SearchResultResourceRep) RestLinkRep(com.emc.storageos.model.RestLinkRep) OrderMapper.toExecutionLogList(com.emc.sa.api.mapper.OrderMapper.toExecutionLogList) NamedElementQueryResultList(com.emc.storageos.db.client.constraint.NamedElementQueryResultList) ArrayList(java.util.ArrayList) OrderMapper.toOrderLogList(com.emc.sa.api.mapper.OrderMapper.toOrderLogList) OrderLogList(com.emc.vipr.model.catalog.OrderLogList) OrderList(com.emc.vipr.model.catalog.OrderList) List(java.util.List) ExecutionLogList(com.emc.vipr.model.catalog.ExecutionLogList) URIUtil.asString(com.emc.storageos.db.client.URIUtil.asString) ResRepFilter(com.emc.storageos.api.service.impl.response.ResRepFilter) SearchResults(com.emc.storageos.model.search.SearchResults) Date(java.util.Date)

Example 12 with SearchResultResourceRep

use of com.emc.storageos.model.search.SearchResultResourceRep in project coprhd-controller by CoprHD.

the class ApprovalService method getOtherSearchResults.

/**
 * parameter: 'orderId' The id of the order to search for approvals
 * parameter: 'approvalStatus' The status for the approval.
 * parameter: 'tenantId' The id of the tenant (if not the current tenant)
 *
 * @return Return a list of matching approvals or an empty list if no match was found.
 */
@Override
protected SearchResults getOtherSearchResults(Map<String, List<String>> parameters, boolean authorized) {
    StorageOSUser user = getUserFromContext();
    String tenantId = user.getTenantId();
    if (parameters.containsKey(SearchConstants.TENANT_ID_PARAM)) {
        tenantId = parameters.get(SearchConstants.TENANT_ID_PARAM).get(0);
    }
    verifyAuthorizedInTenantOrg(uri(tenantId), user);
    if (!parameters.containsKey(SearchConstants.ORDER_ID_PARAM) && !parameters.containsKey(SearchConstants.APPROVAL_STATUS_PARAM)) {
        throw APIException.badRequests.invalidParameterSearchMissingParameter(getResourceClass().getName(), SearchConstants.ORDER_ID_PARAM + " or " + SearchConstants.APPROVAL_STATUS_PARAM);
    }
    if (parameters.containsKey(SearchConstants.ORDER_ID_PARAM) && parameters.containsKey(SearchConstants.APPROVAL_STATUS_PARAM)) {
        throw APIException.badRequests.parameterForSearchCouldNotBeCombinedWithAnyOtherParameter(getResourceClass().getName(), SearchConstants.ORDER_ID_PARAM, SearchConstants.APPROVAL_STATUS_PARAM);
    }
    List<ApprovalRequest> approvals = Lists.newArrayList();
    if (parameters.containsKey(SearchConstants.ORDER_ID_PARAM)) {
        String orderId = parameters.get(SearchConstants.ORDER_ID_PARAM).get(0);
        ArgValidator.checkFieldNotEmpty(orderId, SearchConstants.ORDER_ID_PARAM);
        approvals = approvalManager.findApprovalsByOrderId(uri(orderId));
    } else if (parameters.containsKey(SearchConstants.APPROVAL_STATUS_PARAM)) {
        String approvalStatus = parameters.get(SearchConstants.APPROVAL_STATUS_PARAM).get(0);
        ArgValidator.checkFieldNotEmpty(approvalStatus, SearchConstants.APPROVAL_STATUS_PARAM);
        approvals = approvalManager.findApprovalsByStatus(uri(tenantId), ApprovalStatus.valueOf(approvalStatus));
    }
    ResRepFilter<SearchResultResourceRep> resRepFilter = (ResRepFilter<SearchResultResourceRep>) getPermissionFilter(getUserFromContext(), _permissionsHelper);
    List<SearchResultResourceRep> searchResultResourceReps = Lists.newArrayList();
    for (ApprovalRequest approval : approvals) {
        RestLinkRep selfLink = new RestLinkRep("self", RestLinkFactory.newLink(getResourceType(), approval.getId()));
        SearchResultResourceRep searchResultResourceRep = new SearchResultResourceRep();
        searchResultResourceRep.setId(approval.getId());
        searchResultResourceRep.setLink(selfLink);
        if (authorized || resRepFilter.isAccessible(searchResultResourceRep)) {
            searchResultResourceReps.add(searchResultResourceRep);
        }
    }
    SearchResults result = new SearchResults();
    result.setResource(searchResultResourceReps);
    return result;
}
Also used : StorageOSUser(com.emc.storageos.security.authentication.StorageOSUser) SearchResultResourceRep(com.emc.storageos.model.search.SearchResultResourceRep) ApprovalRequest(com.emc.storageos.db.client.model.uimodels.ApprovalRequest) RestLinkRep(com.emc.storageos.model.RestLinkRep) ResRepFilter(com.emc.storageos.api.service.impl.response.ResRepFilter) SearchResults(com.emc.storageos.model.search.SearchResults)

Example 13 with SearchResultResourceRep

use of com.emc.storageos.model.search.SearchResultResourceRep in project coprhd-controller by CoprHD.

the class AbstractResources method performSearch.

/**
 * Performs a search for resources matching the given parameters.
 *
 * @param params
 *            the search query parameters.
 * @return the list of resources.
 */
public List<SearchResultResourceRep> performSearch(Map<String, Object> params) {
    UriBuilder builder = client.uriBuilder(getSearchUrl());
    for (Map.Entry<String, Object> entry : params.entrySet()) {
        builder.queryParam(entry.getKey(), entry.getValue());
    }
    SearchResults searchResults = client.getURI(SearchResults.class, builder.build());
    List<SearchResultResourceRep> results = searchResults.getResource();
    if (results == null) {
        results = new ArrayList<SearchResultResourceRep>();
    }
    return results;
}
Also used : SearchResultResourceRep(com.emc.storageos.model.search.SearchResultResourceRep) UriBuilder(javax.ws.rs.core.UriBuilder) SearchResults(com.emc.storageos.model.search.SearchResults)

Example 14 with SearchResultResourceRep

use of com.emc.storageos.model.search.SearchResultResourceRep in project coprhd-controller by CoprHD.

the class ExportGroupService method getOtherSearchResults.

/**
 * Additional search criteria for a export group.
 *
 * If a matching export group is not found, an empty list is returned.
 *
 * Parameters - host String - URI of the host
 * - cluster String - URI of the cluster
 * - initiator String - URI of the initiator
 * - self_only - Optional parameter to allow for specific type search, [true or false]
 */
@Override
protected SearchResults getOtherSearchResults(Map<String, List<String>> parameters, boolean authorized) {
    SearchResults result = new SearchResults();
    String[] searchCriteria = { SEARCH_HOST, SEARCH_CLUSTER, SEARCH_INITIATOR, SEARCH_LEVEL };
    validateSearchParameters(parameters, searchCriteria);
    boolean selfOnly = isSelfOnly(parameters, SEARCH_LEVEL);
    List<SearchResultResourceRep> resRepLists = new ArrayList<SearchResultResourceRep>();
    for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
        for (String searchValue : entry.getValue()) {
            if (entry.getKey().equals(SEARCH_HOST)) {
                // Search for host export
                searchForHostExport(searchValue, resRepLists, selfOnly, authorized);
            } else if (entry.getKey().equals(SEARCH_CLUSTER)) {
                // search for cluster export
                searchForClusterExport(searchValue, resRepLists, selfOnly, authorized);
            } else if (entry.getKey().equals(SEARCH_INITIATOR)) {
                // search for initiator export
                if (isInitiatorId(searchValue)) {
                    searchforInitiatorExport(searchValue, resRepLists, selfOnly, authorized);
                } else {
                    searchforInitiatorExportByWWN(searchValue, resRepLists, selfOnly, authorized);
                }
            }
            result.setResource(resRepLists);
        }
    }
    return result;
}
Also used : SearchResultResourceRep(com.emc.storageos.model.search.SearchResultResourceRep) ArrayList(java.util.ArrayList) ITLRestRepList(com.emc.storageos.model.block.export.ITLRestRepList) ArrayList(java.util.ArrayList) URIQueryResultList(com.emc.storageos.db.client.constraint.URIQueryResultList) List(java.util.List) BulkList(com.emc.storageos.api.service.impl.response.BulkList) SearchedResRepList(com.emc.storageos.api.service.impl.response.SearchedResRepList) SearchResults(com.emc.storageos.model.search.SearchResults) Map(java.util.Map) StringSetMap(com.emc.storageos.db.client.model.StringSetMap) OpStatusMap(com.emc.storageos.db.client.model.OpStatusMap) HashMap(java.util.HashMap) StringMap(com.emc.storageos.db.client.model.StringMap)

Example 15 with SearchResultResourceRep

use of com.emc.storageos.model.search.SearchResultResourceRep in project coprhd-controller by CoprHD.

the class TaggedResource method search.

/**
 * @brief search API
 *        Search resources by name, tag, project or additional parameters (for example, wwn or initiator_port etc.)
 *
 * @prereq none
 * @return search results
 */
/*
     * Parameters:
     * Common Parameters:
     * name: Name has to be a minimum of 2 characters. Could only be combined with project parameter
     * tag: Tag has to be a minimum of 2 characters. Could only be combined with tenant parameter
     * project: The full project URI needs to be provided.
     * 
     * NOTE: Name and tag are not case sensitive.
     * For Zone level resources, search by project is not allowed.
     * Special Parameters:
     * wwn: only used by block service
     * initiator_port: only used by virtual array service.
     */
@GET
@Path("/search")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public SearchResults search() {
    // 1. Figure out user privilege
    boolean bAuthorized = false;
    StorageOSUser user = getUserFromContext();
    if (_permissionsHelper.userHasGivenRole(user, null, Role.SYSTEM_MONITOR)) {
        bAuthorized = true;
    } else if ((isZoneLevelResource() || isSysAdminReadableResource()) && (_permissionsHelper.userHasGivenRole(user, null, Role.SYSTEM_ADMIN))) {
        bAuthorized = true;
    }
    // 2. Parameter check and early detection for unsupported search
    String name = null, tag = null;
    URI projectId = null, tenant = null;
    Map<String, List<String>> parameters = uriInfo.getQueryParameters();
    // remove non-search related common parameters
    parameters.remove(RequestProcessingUtils.REQUESTING_COOKIES);
    if (parameters.containsKey("name")) {
        name = parameters.get("name").get(0);
        checkSearchParameterLength("name", name, getResourceClass());
        checkParameterCombination(parameters, getResourceClass(), "name", "project");
    }
    if (parameters.containsKey("tag")) {
        tag = parameters.get("tag").get(0);
        checkSearchParameterLength("tag", tag, getResourceClass());
        checkParameterCombination(parameters, getResourceClass(), "tag", "tenant");
        // set tenant scope for non-zone resources
        if (!isZoneLevelResource()) {
            if (parameters.containsKey("tenant")) {
                tenant = URI.create(parameters.get("tenant").get(0));
            }
        }
    }
    if (parameters.containsKey("project")) {
        checkParameterCombination(parameters, getResourceClass(), "project", "name");
        projectId = URI.create(parameters.get("project").get(0));
        if (isZoneLevelResource()) {
            throw APIException.badRequests.invalidParameterSearchProjectNotSupported(getResourceClass().getName());
        }
        // check if user is authorized for the project
        if (!bAuthorized) {
            if (!isAuthorized(projectId)) {
                throw APIException.forbidden.insufficientPermissionsForUser(getUserFromContext().toString());
            }
            bAuthorized = true;
        }
    }
    // 3. Search from db and response permission-eligible results
    SearchResults result = new SearchResults();
    if (name != null || tag != null) {
        SearchedResRepList resRepList = null;
        if (name != null) {
            // search named resources
            resRepList = getNamedSearchResults(name, projectId);
        } else {
            // search tagged resources
            resRepList = getTagSearchResults(tag, tenant);
        }
        if (!bAuthorized) {
            SearchedResRepList filteredResRepList = new SearchedResRepList();
            Iterator<SearchResultResourceRep> _queryResultIterator = resRepList.iterator();
            ResRepFilter<SearchResultResourceRep> resrepFilter = null;
            resrepFilter = (ResRepFilter<SearchResultResourceRep>) getPermissionFilter(getUserFromContext(), _permissionsHelper);
            filteredResRepList.setResult(new FilterIterator<SearchResultResourceRep>(_queryResultIterator, resrepFilter));
            result.setResource(filteredResRepList);
        } else {
            result.setResource(resRepList);
        }
        return result;
    }
    if (projectId != null) {
        // start resource search within project
        // note: for project resources search, the permission check
        // has been addressed in parameter checke period.
        result.setResource(getProjectSearchResults(projectId));
        return result;
    }
    // some other resource specific search
    return getOtherSearchResults(parameters, bAuthorized);
}
Also used : StorageOSUser(com.emc.storageos.security.authentication.StorageOSUser) SearchResultResourceRep(com.emc.storageos.model.search.SearchResultResourceRep) SearchedResRepList(com.emc.storageos.api.service.impl.response.SearchedResRepList) SearchedResRepList(com.emc.storageos.api.service.impl.response.SearchedResRepList) List(java.util.List) SearchResults(com.emc.storageos.model.search.SearchResults) URI(java.net.URI) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

SearchResultResourceRep (com.emc.storageos.model.search.SearchResultResourceRep)19 SearchResults (com.emc.storageos.model.search.SearchResults)11 SearchedResRepList (com.emc.storageos.api.service.impl.response.SearchedResRepList)10 RestLinkRep (com.emc.storageos.model.RestLinkRep)8 ArrayList (java.util.ArrayList)8 List (java.util.List)8 URI (java.net.URI)7 BulkList (com.emc.storageos.api.service.impl.response.BulkList)6 ResRepFilter (com.emc.storageos.api.service.impl.response.ResRepFilter)5 URIQueryResultList (com.emc.storageos.db.client.constraint.URIQueryResultList)5 Map (java.util.Map)5 StringMap (com.emc.storageos.db.client.model.StringMap)3 ITLRestRepList (com.emc.storageos.model.block.export.ITLRestRepList)3 StorageOSUser (com.emc.storageos.security.authentication.StorageOSUser)3 NamedElementQueryResultList (com.emc.storageos.db.client.constraint.NamedElementQueryResultList)2 ExportGroup (com.emc.storageos.db.client.model.ExportGroup)2 Host (com.emc.storageos.db.client.model.Host)2 HashMap (java.util.HashMap)2 OrderMapper.toExecutionLogList (com.emc.sa.api.mapper.OrderMapper.toExecutionLogList)1 OrderMapper.toOrderLogList (com.emc.sa.api.mapper.OrderMapper.toOrderLogList)1