148 lines
4.5 KiB
Java
148 lines
4.5 KiB
Java
package fr.la_banquise.backend.rest;
|
|
|
|
import fr.la_banquise.backend.services.InstanceService;
|
|
import fr.la_banquise.backend.services.JiService;
|
|
import fr.la_banquise.backend.services.UserService;
|
|
import io.quarkus.security.Authenticated;
|
|
import io.quarkus.security.identity.SecurityIdentity;
|
|
import jakarta.annotation.security.RolesAllowed;
|
|
import jakarta.inject.Inject;
|
|
import jakarta.ws.rs.DELETE;
|
|
import jakarta.ws.rs.GET;
|
|
import jakarta.ws.rs.POST;
|
|
import jakarta.ws.rs.Path;
|
|
import jakarta.ws.rs.PathParam;
|
|
import jakarta.ws.rs.Produces;
|
|
import jakarta.ws.rs.QueryParam;
|
|
import jakarta.ws.rs.core.MediaType;
|
|
import jakarta.ws.rs.core.Response;
|
|
|
|
/**
|
|
* InstanceEndpoints
|
|
*/
|
|
@Path("/api/ji/")
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
public class InstanceEndpoints {
|
|
|
|
@Inject SecurityIdentity identity;
|
|
|
|
@Inject JiService jiService;
|
|
@Inject InstanceService instanceService;
|
|
@Inject UserService userService;
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
///
|
|
/// First, we create our Instance and basic functions
|
|
|
|
@POST
|
|
@RolesAllowed("ROOT")
|
|
@Path("/{id}/instance")
|
|
public Response createInstance(@PathParam("id") Long jiId,
|
|
@QueryParam("userId") Long userId) {
|
|
jiService.createInstance(jiId, userId);
|
|
return Response.ok().build();
|
|
}
|
|
|
|
@GET
|
|
@RolesAllowed("ROOT")
|
|
@Path("/instances")
|
|
public Response getAllInstances() {
|
|
return Response.ok(instanceService.getAllInstances()).build();
|
|
}
|
|
|
|
@GET
|
|
@RolesAllowed("ROOT")
|
|
@Path("/{id}/all-instances")
|
|
public Response getAllInstancesJi(@PathParam("id") Long jiId) {
|
|
return Response.ok(jiService.getAllInstancesJi(jiId)).build();
|
|
}
|
|
|
|
@GET
|
|
@Authenticated
|
|
@Path("/{id}/instances")
|
|
public Response getMyInstancesJi(@PathParam("id") Long jiId) {
|
|
String name = identity.getPrincipal().getName();
|
|
Long userId = userService.getId(name);
|
|
return Response.ok(jiService.getInstance(jiId, userId)).build();
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Now that the Instance is created, create their containers !
|
|
|
|
@POST
|
|
@RolesAllowed("ROOT")
|
|
@Path("/{id}/containers")
|
|
public Response createContainers(@PathParam("id") Long jiId) {
|
|
jiService.createContainers(jiId);
|
|
return Response.ok().build();
|
|
}
|
|
|
|
/// Finally, start/stop the containers
|
|
|
|
@POST
|
|
@RolesAllowed("ROOT")
|
|
@Path("/{id}/container/start")
|
|
public Response startContainers(@PathParam("id") Long jiId) {
|
|
jiService.startContainers(jiId);
|
|
return Response.ok().build();
|
|
}
|
|
|
|
@POST
|
|
@RolesAllowed("ROOT")
|
|
@Path("/{id}/container/stop")
|
|
public Response stopContainers(@PathParam("id") Long jiId) {
|
|
jiService.stopContainers(jiId);
|
|
return Response.ok().build();
|
|
}
|
|
|
|
@GET
|
|
@RolesAllowed("ROOT")
|
|
@Path("/{id}/containers")
|
|
public Response getStatusContainers(@PathParam("id") Long jiId) {
|
|
return Response.ok(jiService.getStatusContainers(jiId)).build();
|
|
}
|
|
|
|
@GET
|
|
@RolesAllowed("ROOT")
|
|
@Path("/{id}/container-admin")
|
|
public Response getStatusContainer(@PathParam("id") Long jiId,
|
|
@QueryParam("instId") Long instId) {
|
|
// TODO : add securtiy if needed ?
|
|
return Response.ok(jiService.getStatusContainerInst(jiId, instId)).build();
|
|
}
|
|
|
|
@GET
|
|
@RolesAllowed("ROOT")
|
|
@Path("/{id}/instance-owner")
|
|
public Response getOwnerInstance(@PathParam("id") Long jiId,
|
|
@QueryParam("instId") Long instId) {
|
|
// TODO : add securtiy if needed ?
|
|
return Response.ok(jiService.getOwnerInst(jiId, instId)).build();
|
|
}
|
|
|
|
@GET
|
|
@Authenticated
|
|
@Path("/{id}/container")
|
|
public Response getStatusMyContainer(@PathParam("id") Long jiId) {
|
|
String name = identity.getPrincipal().getName();
|
|
Long userId = userService.getId(name);
|
|
return Response.ok(jiService.getStatusContainer(jiId, userId)).build();
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Last but not least, be able do delete every container and instance
|
|
|
|
@DELETE
|
|
@RolesAllowed("ROOT")
|
|
@Path("/{id}/container/{instanceId}")
|
|
public Response
|
|
delContainer(@PathParam("id") Long jiId,
|
|
@PathParam("instanceId") Long instanceId) {
|
|
jiService.deleteContainer(instanceId);
|
|
// TODO: add filter by JI (but optionnal imo)
|
|
return Response.ok().build();
|
|
}
|
|
}
|