Search in sources :

Example 6 with Policy

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

the class FullTriggerHandlingTest method testSetup.

@Test
void testSetup() {
    Policy p = createPolicy();
    FullTrigger ft = new FullTrigger(p);
    assertFalse(ft.trigger.enabled);
    assertEquals("hula", ft.trigger.name);
    assertEquals(1, ft.conditions.size());
    assertEquals("bla", ft.conditions.get(0).expression);
    assertEquals(1, ft.trigger.actions.size());
    assertEquals("notification", ft.trigger.actions.iterator().next().actionPlugin);
    assertEquals("hula", ft.trigger.name);
    assertEquals("some text", ft.trigger.description);
}
Also used : Policy(com.redhat.cloud.policies.app.model.Policy) FullTrigger(com.redhat.cloud.policies.app.model.engine.FullTrigger) Test(org.junit.jupiter.api.Test)

Example 7 with Policy

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

the class FullTriggerHandlingTest method createPolicy.

@NotNull
private Policy createPolicy() {
    Policy p = new Policy();
    p.customerid = "1234";
    p.conditions = "bla";
    p.actions = "notification";
    p.name = "hula";
    p.description = "some text";
    return p;
}
Also used : Policy(com.redhat.cloud.policies.app.model.Policy) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with Policy

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

the class FullTriggerHandlingTest method testActionUpdate1.

@Test
void testActionUpdate1() {
    Policy p = createPolicy();
    FullTrigger ft = new FullTrigger(p);
    p.actions = "notification";
    ft.updateFromPolicy(p);
    assertEquals(1, ft.trigger.actions.size());
    assertEquals("notification", ft.trigger.actions.iterator().next().actionPlugin);
}
Also used : Policy(com.redhat.cloud.policies.app.model.Policy) FullTrigger(com.redhat.cloud.policies.app.model.engine.FullTrigger) Test(org.junit.jupiter.api.Test)

Example 9 with Policy

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

the class AdminService method syncToEngine.

@Path("/sync")
@POST
@Transactional
public Response syncToEngine(@QueryParam("token") String token) {
    boolean validToken = StuffHolder.getInstance().compareToken(token);
    if (!validToken) {
        return Response.status(Response.Status.FORBIDDEN).entity("You don't have permission for this").build();
    }
    final int[] count = { 0 };
    try (Stream<Policy> policies = Policy.streamAll()) {
        policies.forEach(p -> {
            FullTrigger fullTrigger;
            try {
                fullTrigger = engine.fetchTrigger(p.id, p.customerid);
            } catch (NotFoundException nfe) {
                fullTrigger = null;
            }
            if (fullTrigger == null) {
                // Engine does not have the trigger
                log.info("Trigger " + p.id + " not found, syncing");
                FullTrigger ft = new FullTrigger(p);
                engine.storeTrigger(ft, false, p.customerid);
                log.info("   done");
                count[0]++;
            } else {
                log.info("Trigger " + p.id + " already in engine, skipping");
            }
        });
    }
    String s = "Stored " + count[0] + " triggers";
    log.info(s);
    return Response.ok().entity(new Msg(s)).build();
}
Also used : Policy(com.redhat.cloud.policies.app.model.Policy) Msg(com.redhat.cloud.policies.app.model.Msg) NotFoundException(javax.ws.rs.NotFoundException) FullTrigger(com.redhat.cloud.policies.app.model.engine.FullTrigger) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Transactional(javax.transaction.Transactional)

Example 10 with Policy

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

the class AdminService method findOrphans.

@GET
@Path("/verify")
public Response findOrphans() {
    Map<String, List<TTT>> orphanedPolicies = new HashMap<>();
    List<TTT> orphanedInDB = new ArrayList<>();
    List<TTT> orphanedInEngine = new ArrayList<>();
    // Find active policies that do not have a trigger in engine
    try (Stream<Policy> policies = Policy.streamAll()) {
        policies.forEach(p -> {
            try {
                engine.fetchTrigger(p.id, p.customerid);
            } catch (NotFoundException nfe) {
                orphanedInDB.add(new TTT(p.customerid, p.id));
            }
        });
    }
    orphanedPolicies.put("orphanedInDB", orphanedInDB);
    // Find triggers in engine for accounts and check if
    // they have an active trigger in the DB
    List<String> customersInDb;
    try (Stream<Policy> policies = Policy.streamAll()) {
        customersInDb = policies.map(p -> p.customerid).distinct().collect(Collectors.toList());
    }
    customersInDb.forEach(cid -> {
        List<Trigger> triggers = engine.findTriggersForCustomer(cid);
        triggers.forEach(t -> {
            Policy pol = Policy.findById(cid, UUID.fromString(t.id));
            if (pol == null) {
                orphanedInEngine.add(new TTT(cid, t.id));
            }
        });
    });
    orphanedPolicies.put("orphanedInEngine", orphanedInEngine);
    return Response.ok().entity(orphanedPolicies).build();
}
Also used : Policy(com.redhat.cloud.policies.app.model.Policy) Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) StuffHolder(com.redhat.cloud.policies.app.StuffHolder) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) BigInteger(java.math.BigInteger) PolicyEngine(com.redhat.cloud.policies.app.PolicyEngine) ScheduledStatusProducer(com.redhat.cloud.policies.app.health.ScheduledStatusProducer) POST(javax.ws.rs.POST) RestClient(org.eclipse.microprofile.rest.client.inject.RestClient) Transactional(javax.transaction.Transactional) Set(java.util.Set) EntityManager(javax.persistence.EntityManager) UUID(java.util.UUID) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) List(java.util.List) Policy(com.redhat.cloud.policies.app.model.Policy) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) FullTrigger(com.redhat.cloud.policies.app.model.engine.FullTrigger) RequestScoped(javax.enterprise.context.RequestScoped) Trigger(com.redhat.cloud.policies.app.model.engine.Trigger) PostConstruct(javax.annotation.PostConstruct) Msg(com.redhat.cloud.policies.app.model.Msg) Optional(java.util.Optional) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NotFoundException(javax.ws.rs.NotFoundException) FullTrigger(com.redhat.cloud.policies.app.model.engine.FullTrigger) Trigger(com.redhat.cloud.policies.app.model.engine.Trigger) ArrayList(java.util.ArrayList) List(java.util.List) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

Policy (com.redhat.cloud.policies.app.model.Policy)21 Msg (com.redhat.cloud.policies.app.model.Msg)11 FullTrigger (com.redhat.cloud.policies.app.model.engine.FullTrigger)11 Path (javax.ws.rs.Path)11 Operation (org.eclipse.microprofile.openapi.annotations.Operation)9 APIResponse (org.eclipse.microprofile.openapi.annotations.responses.APIResponse)9 NotFoundException (javax.ws.rs.NotFoundException)8 Test (org.junit.jupiter.api.Test)8 Transactional (javax.transaction.Transactional)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 ConnectException (java.net.ConnectException)6 PersistenceException (javax.persistence.PersistenceException)6 SystemException (javax.transaction.SystemException)6 ProcessingException (javax.ws.rs.ProcessingException)6 WebApplicationException (javax.ws.rs.WebApplicationException)6 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)6 POST (javax.ws.rs.POST)5 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)5 Parameter (org.eclipse.microprofile.openapi.annotations.parameters.Parameter)5 GET (javax.ws.rs.GET)4