Search in sources :

Example 1 with Pager

use of com.redhat.cloud.policies.app.model.pager.Pager in project policies-ui-backend by RedHatInsights.

the class PolicyCrudService method getPoliciesForCustomer.

@Operation(summary = "Return all policies for a given account")
@GET
@Path("/")
@Parameters({ @Parameter(name = "offset", in = ParameterIn.QUERY, description = "Page number, starts 0, if not specified uses 0.", schema = @Schema(type = SchemaType.INTEGER)), @Parameter(name = "limit", in = ParameterIn.QUERY, description = "Number of items per page, if not specified uses 50. " + Pager.NO_LIMIT + " can be used to specify an unlimited page, when specified it ignores the offset", schema = @Schema(type = SchemaType.INTEGER)), @Parameter(name = "sortColumn", in = ParameterIn.QUERY, description = "Column to sort the results by", schema = @Schema(type = SchemaType.STRING, enumeration = { "name", "description", "is_enabled", "mtime" })), @Parameter(name = "sortDirection", in = ParameterIn.QUERY, description = "Sort direction used", schema = @Schema(type = SchemaType.STRING, enumeration = { "asc", "desc" })), @Parameter(name = "filter[name]", in = ParameterIn.QUERY, description = "Filtering policies by the name depending on the Filter operator used.", schema = @Schema(type = SchemaType.STRING)), @Parameter(name = "filter:op[name]", in = ParameterIn.QUERY, description = "Operations used with the filter", schema = @Schema(type = SchemaType.STRING, enumeration = { "equal", "like", "ilike", "not_equal" }, defaultValue = "equal")), @Parameter(name = "filter[description]", in = ParameterIn.QUERY, description = "Filtering policies by the description depending on the Filter operator used.", schema = @Schema(type = SchemaType.STRING)), @Parameter(name = "filter:op[description]", in = ParameterIn.QUERY, description = "Operations used with the filter", schema = @Schema(type = SchemaType.STRING, enumeration = { "equal", "like", "ilike", "not_equal" }, defaultValue = "equal")), @Parameter(name = "filter[is_enabled]", in = ParameterIn.QUERY, description = "Filtering policies by the is_enabled field." + "Defaults to true if no operand is given.", schema = @Schema(type = SchemaType.STRING, defaultValue = "true", enumeration = { "true", "false" })) })
@APIResponse(responseCode = "400", description = "Bad parameter for sorting was passed")
@APIResponse(responseCode = "404", description = "No policies found for customer")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@APIResponse(responseCode = "200", description = "Policies found", content = @Content(schema = @Schema(implementation = PagedResponseOfPolicy.class)), headers = @Header(name = "TotalCount", description = "Total number of items found", schema = @Schema(type = SchemaType.INTEGER)))
public Response getPoliciesForCustomer() {
    if (!user.canReadPolicies()) {
        return Response.status(Response.Status.FORBIDDEN).entity(new Msg(MISSING_PERMISSIONS_TO_RETRIEVE_POLICIES)).build();
    }
    Page<Policy> page;
    try {
        Pager pager = PagingUtils.extractPager(uriInfo);
        page = Policy.pagePoliciesForCustomer(entityManager, user.getAccount(), pager);
        for (Policy policy : page) {
            Long lastTriggerTime = policiesHistoryRepository.getLastTriggerTime(user.getAccount(), policy.id);
            if (lastTriggerTime != null) {
                policy.setLastTriggered(lastTriggerTime);
            }
        }
    } catch (IllegalArgumentException iae) {
        return Response.status(400, iae.getLocalizedMessage()).build();
    }
    return PagingUtils.responseBuilder(page).build();
}
Also used : Msg(com.redhat.cloud.policies.app.model.Msg) Policy(com.redhat.cloud.policies.app.model.Policy) Pager(com.redhat.cloud.policies.app.model.pager.Pager) Path(javax.ws.rs.Path) APIResponse(org.eclipse.microprofile.openapi.annotations.responses.APIResponse) Parameters(org.eclipse.microprofile.openapi.annotations.parameters.Parameters) GET(javax.ws.rs.GET) Operation(org.eclipse.microprofile.openapi.annotations.Operation)

Example 2 with Pager

use of com.redhat.cloud.policies.app.model.pager.Pager in project policies-ui-backend by RedHatInsights.

the class PolicyCrudService method getPolicyIdsForCustomer.

@Operation(summary = "Return all policy ids for a given account after applying the filters")
@GET
@Path("/ids")
@Parameters({ @Parameter(name = "filter[name]", in = ParameterIn.QUERY, description = "Filtering policies by the name depending on the Filter operator used.", schema = @Schema(type = SchemaType.STRING)), @Parameter(name = "filter:op[name]", in = ParameterIn.QUERY, description = "Operations used with the filter", schema = @Schema(type = SchemaType.STRING, enumeration = { "equal", "like", "ilike", "not_equal" }, defaultValue = "equal")), @Parameter(name = "filter[description]", in = ParameterIn.QUERY, description = "Filtering policies by the description depending on the Filter operator used.", schema = @Schema(type = SchemaType.STRING)), @Parameter(name = "filter:op[description]", in = ParameterIn.QUERY, description = "Operations used with the filter", schema = @Schema(type = SchemaType.STRING, enumeration = { "equal", "like", "ilike", "not_equal" }, defaultValue = "equal")), @Parameter(name = "filter[is_enabled]", in = ParameterIn.QUERY, description = "Filtering policies by the is_enabled field." + "Defaults to true if no operand is given.", schema = @Schema(type = SchemaType.STRING, defaultValue = "true", enumeration = { "true", "false" })) })
@APIResponse(responseCode = "400", description = "Bad parameter for sorting was passed")
@APIResponse(responseCode = "404", description = "No policies found for customer")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@APIResponse(responseCode = "200", description = "PolicyIds found", content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = UUID.class)))
public Response getPolicyIdsForCustomer() {
    if (!user.canReadPolicies()) {
        return Response.status(Response.Status.FORBIDDEN).entity(new Msg(MISSING_PERMISSIONS_TO_RETRIEVE_POLICIES)).build();
    }
    List<UUID> uuids;
    try {
        Pager pager = PagingUtils.extractPager(uriInfo);
        uuids = Policy.getPolicyIdsForCustomer(entityManager, user.getAccount(), pager);
    } catch (IllegalArgumentException iae) {
        return Response.status(400, iae.getLocalizedMessage()).build();
    }
    return Response.ok(uuids).build();
}
Also used : Msg(com.redhat.cloud.policies.app.model.Msg) Pager(com.redhat.cloud.policies.app.model.pager.Pager) UUID(java.util.UUID) Path(javax.ws.rs.Path) APIResponse(org.eclipse.microprofile.openapi.annotations.responses.APIResponse) Parameters(org.eclipse.microprofile.openapi.annotations.parameters.Parameters) GET(javax.ws.rs.GET) Operation(org.eclipse.microprofile.openapi.annotations.Operation)

Example 3 with Pager

use of com.redhat.cloud.policies.app.model.pager.Pager in project policies-ui-backend by RedHatInsights.

the class TagsFilterTest method filter3.

@Test
void filter3() throws URISyntaxException {
    UriInfo info = new ResteasyUriInfo(new URI("https://foo?filter[name]=VM&filter:op[name]=like"));
    Pager pager = PagingUtils.extractPager(info);
    String query = PolicyHistoryTagFilterHelper.getTagsFilterFromPager(pager);
    assertEquals("tags.display_name MATCHES '*vm*'", query);
}
Also used : ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) Pager(com.redhat.cloud.policies.app.model.pager.Pager) URI(java.net.URI) ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.jupiter.api.Test)

Example 4 with Pager

use of com.redhat.cloud.policies.app.model.pager.Pager in project policies-ui-backend by RedHatInsights.

the class TagsFilterTest method filter1.

@Test
void filter1() throws URISyntaxException {
    UriInfo info = new ResteasyUriInfo(new URI("https://foo?filter[name]=VM"));
    Pager pager = PagingUtils.extractPager(info);
    String query = PolicyHistoryTagFilterHelper.getTagsFilterFromPager(pager);
    assertEquals("tags.display_name = 'vm'", query);
}
Also used : ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) Pager(com.redhat.cloud.policies.app.model.pager.Pager) URI(java.net.URI) ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.jupiter.api.Test)

Example 5 with Pager

use of com.redhat.cloud.policies.app.model.pager.Pager in project policies-ui-backend by RedHatInsights.

the class TagsFilterTest method filter6.

@Test
void filter6() throws URISyntaxException {
    UriInfo info = new ResteasyUriInfo(new URI("https://foo?filter[name]=VM&filter:op[id]=not_equal"));
    Pager pager = PagingUtils.extractPager(info);
    String query = PolicyHistoryTagFilterHelper.getTagsFilterFromPager(pager);
    assertEquals("tags.display_name = 'vm'", query);
}
Also used : ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) Pager(com.redhat.cloud.policies.app.model.pager.Pager) URI(java.net.URI) ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.jupiter.api.Test)

Aggregations

Pager (com.redhat.cloud.policies.app.model.pager.Pager)26 Test (org.junit.jupiter.api.Test)21 URI (java.net.URI)8 UriInfo (javax.ws.rs.core.UriInfo)8 ResteasyUriInfo (org.jboss.resteasy.specimpl.ResteasyUriInfo)8 Sort (io.quarkus.panache.common.Sort)4 Msg (com.redhat.cloud.policies.app.model.Msg)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 Policy (com.redhat.cloud.policies.app.model.Policy)2 Filter (com.redhat.cloud.policies.app.model.filter.Filter)2 UUID (java.util.UUID)2 Operation (org.eclipse.microprofile.openapi.annotations.Operation)2 Parameters (org.eclipse.microprofile.openapi.annotations.parameters.Parameters)2 APIResponse (org.eclipse.microprofile.openapi.annotations.responses.APIResponse)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Page (com.redhat.cloud.policies.app.model.pager.Page)1 ValidActionS (com.redhat.cloud.policies.app.model.validation.ValidActionS)1 PanacheEntityBase (io.quarkus.hibernate.orm.panache.PanacheEntityBase)1 PanacheQuery (io.quarkus.hibernate.orm.panache.PanacheQuery)1