use of com.redhat.cloud.policies.app.model.Msg in project policies-ui-backend by RedHatInsights.
the class PolicyCrudService method deletePolicy.
@Operation(summary = "Delete a single policy for a customer by its id")
@DELETE
@Path("/{id}")
@APIResponse(responseCode = "200", description = "Policy deleted")
@APIResponse(responseCode = "404", description = "Policy not found")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@Parameter(name = "id", description = "UUID of the policy")
@Transactional
public Response deletePolicy(@PathParam("id") UUID policyId) {
if (!user.canWritePolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg("Missing permissions to delete policy")).build();
}
Policy policy = Policy.findById(user.getAccount(), policyId);
ResponseBuilder builder = Response.ok();
if (policy == null) {
builder = Response.status(Response.Status.NOT_FOUND);
} else {
boolean deletedOnEngine = false;
try {
engine.deleteTrigger(policy.id, user.getAccount());
deletedOnEngine = true;
} catch (NotFoundException nfe) {
// Engine does not have it - we can delete anyway
deletedOnEngine = true;
} catch (Exception e) {
log.warning("Deletion on engine failed because of " + e.getMessage());
builder = Response.serverError().entity(new Msg(e.getMessage()));
}
if (deletedOnEngine) {
policy.delete(policy);
builder = Response.ok(policy);
}
}
return builder.build();
}
use of com.redhat.cloud.policies.app.model.Msg in project policies-ui-backend by RedHatInsights.
the class PolicyCrudService method setEnabledStateForPolicy.
@Operation(summary = "Enable/disable a policy")
@Parameter(name = "id", description = "ID of the Policy")
@Parameter(name = "enabled", schema = @Schema(type = SchemaType.BOOLEAN, defaultValue = "false"), description = "Should the policy be enabled (true) or disabled (false, default)")
@APIResponse(responseCode = "200", description = "Policy updated")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@APIResponse(responseCode = "404", description = "Policy not found")
@APIResponse(responseCode = "500", description = "Updating failed")
@POST
@Path("/{id:[0-9a-fA-F-]+}/enabled")
@Transactional
public Response setEnabledStateForPolicy(@PathParam("id") UUID policyId, @QueryParam("enabled") boolean shouldBeEnabled) {
if (!user.canWritePolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg(MISSING_PERMISSIONS_TO_UPDATE_POLICY)).build();
}
Policy storedPolicy = Policy.findById(user.getAccount(), policyId);
ResponseBuilder builder;
if (storedPolicy == null) {
builder = Response.status(404, "Original policy not found");
} else {
try {
if (shouldBeEnabled) {
engine.enableTrigger(storedPolicy.id, user.getAccount());
} else {
engine.disableTrigger(storedPolicy.id, user.getAccount());
}
storedPolicy.isEnabled = shouldBeEnabled;
storedPolicy.setMtimeToNow();
storedPolicy.persist();
builder = Response.ok();
} catch (NotFoundException nfe) {
builder = Response.status(404, "Policy not found in engine");
log.warning("Enable/Disable failed, policy [" + storedPolicy.id + "] not found in engine");
} catch (Exception e) {
builder = Response.status(500, "Update failed: " + e.getMessage());
}
}
return builder.build();
}
use of com.redhat.cloud.policies.app.model.Msg in project policies-ui-backend by RedHatInsights.
the class PolicyCrudService method deletePolicies.
@Operation(summary = "Delete policies for a customer by the ids passed in the body. Result will be a list of deleted UUIDs")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@APIResponse(responseCode = "200", description = "Policies deleted", content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = UUID.class)))
@DELETE
@Path("/ids")
@Transactional
public Response deletePolicies(List<UUID> uuids) {
if (!user.canWritePolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg("Missing permissions to delete policy")).build();
}
List<UUID> deleted = new ArrayList<>(uuids.size());
for (UUID uuid : uuids) {
Policy policy = Policy.findById(user.getAccount(), uuid);
if (policy == null) {
// Nothing to do for us
deleted.add(uuid);
} else {
boolean deletedOnEngine = false;
try {
engine.deleteTrigger(policy.id, user.getAccount());
deletedOnEngine = true;
} catch (NotFoundException nfe) {
// Engine does not have it - we can delete anyway
deletedOnEngine = true;
} catch (Exception e) {
log.warning("Deletion on engine failed because of " + e.getMessage());
}
if (deletedOnEngine) {
policy.delete();
deleted.add(uuid);
}
}
}
return Response.ok(deleted).build();
}
use of com.redhat.cloud.policies.app.model.Msg in project policies-ui-backend by RedHatInsights.
the class PolicyCrudService method updatePolicy.
@Operation(summary = "Update a single policy for a customer by its id")
@PUT
@Path("/{policyId}")
@APIResponse(responseCode = "200", description = "Policy updated or policy validated", content = @Content(schema = @Schema(implementation = Policy.class)))
@APIResponse(responseCode = "400", description = "Invalid or no policy provided")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@APIResponse(responseCode = "404", description = "Policy did not exist - did you store it before?")
@APIResponse(responseCode = "409", description = "Persisting failed", content = @Content(schema = @Schema(implementation = Msg.class)))
@Transactional
public Response updatePolicy(@QueryParam("dry") boolean dryRun, @PathParam("policyId") UUID policyId, @NotNull @Valid Policy policy) {
if (!user.canWritePolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg(MISSING_PERMISSIONS_TO_UPDATE_POLICY)).build();
}
Policy storedPolicy = Policy.findById(user.getAccount(), policyId);
ResponseBuilder builder;
if (storedPolicy == null) {
builder = Response.status(404, "Original policy not found");
} else {
if (!policy.id.equals(policyId)) {
builder = Response.status(400, "Invalid policy");
} else {
Response invalidNameResponse = isNameUnique(policy);
if (invalidNameResponse != null) {
return invalidNameResponse;
}
try {
FullTrigger trigger = new FullTrigger(policy);
engine.updateTrigger(policy.id, trigger, true, user.getAccount());
} catch (Exception e) {
return Response.status(400, e.getMessage()).entity(getEngineExceptionMsg(e)).build();
}
if (dryRun) {
return Response.status(200).entity(new Msg("Policy validated")).build();
}
// so we need to first poll from it.
try {
FullTrigger existingTrigger;
try {
existingTrigger = engine.fetchTrigger(storedPolicy.id, user.getAccount());
} catch (Exception e) {
return Response.status(400, e.getMessage()).entity(getEngineExceptionMsg(e)).build();
}
storedPolicy.populateFrom(policy);
storedPolicy.customerid = user.getAccount();
storedPolicy.setMtimeToNow();
existingTrigger.updateFromPolicy(storedPolicy);
try {
engine.updateTrigger(storedPolicy.id, existingTrigger, false, user.getAccount());
} catch (Exception e) {
transactionManager.setRollbackOnly();
return Response.status(400, e.getMessage()).entity(getEngineExceptionMsg(e)).build();
}
} catch (Throwable t) {
try {
transactionManager.setRollbackOnly();
} catch (SystemException ex) {
throw new RuntimeException(ex);
}
return getResponseSavingPolicyThrowable(t);
}
builder = Response.ok(storedPolicy);
}
}
return builder.build();
}
Aggregations