public static void checkSlotsVersion(String expectedSlotsVersion, Iterable<SlotStatus> slotStatuses) { Preconditions.checkNotNull(slotStatuses, "slotStatuses is null"); if (expectedSlotsVersion == null) { return; } String actualSlotsVersion = createSlotsVersion(slotStatuses); if (!expectedSlotsVersion.equals(actualSlotsVersion)) { throw new VersionConflictException(GALAXY_SLOTS_VERSION_HEADER, actualSlotsVersion); } }
@GET @Produces(MediaType.APPLICATION_JSON) public Response getAllSlots(@Context UriInfo uriInfo) { // build filter List<UUID> uuids = transform(coordinator.getAllSlotStatus(), uuidGetter()); Predicate<SlotStatus> slotFilter = SlotFilterBuilder.build(uriInfo, false, uuids); // select slots List<SlotStatus> slots = coordinator.getAllSlotsStatus(slotFilter); // build response return Response.ok(Iterables.transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository))) .header(GALAXY_SLOTS_VERSION_HEADER, createSlotsVersion(slots)) .build(); }
@DELETE @Produces(MediaType.APPLICATION_JSON) public Response terminateSlots(@Context UriInfo uriInfo, @HeaderParam(GALAXY_SLOTS_VERSION_HEADER) String expectedSlotsVersion) { // build filter List<UUID> uuids = Lists.transform(coordinator.getAllSlotStatus(), uuidGetter()); Predicate<SlotStatus> slotFilter = SlotFilterBuilder.build(uriInfo, true, uuids); // reset slots expected state List<SlotStatus> result = coordinator.resetExpectedState(slotFilter, expectedSlotsVersion); // build response return Response.ok(transform(result, fromSlotStatus(coordinator.getAllSlotStatus(), repository))) .header(GALAXY_SLOTS_VERSION_HEADER, createSlotsVersion(result)) .build(); } }
@DELETE @Produces(MediaType.APPLICATION_JSON) public Response terminateSlots(@Context UriInfo uriInfo, @HeaderParam(GALAXY_SLOTS_VERSION_HEADER) String expectedSlotsVersion) { // build filter List<UUID> uuids = transform(coordinator.getAllSlotStatus(), uuidGetter()); Predicate<SlotStatus> slotFilter = SlotFilterBuilder.build(uriInfo, true, uuids); // terminate slots List<SlotStatus> result = coordinator.terminate(slotFilter, expectedSlotsVersion); // build response return Response.ok(transform(result, fromSlotStatus(coordinator.getAllSlotStatus(), repository))) .header(GALAXY_SLOTS_VERSION_HEADER, createSlotsVersion(result)) .build(); } }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response upgrade(UpgradeVersions upgradeVersions, @Context UriInfo uriInfo, @HeaderParam(GALAXY_SLOTS_VERSION_HEADER) String expectedSlotsVersion) { Preconditions.checkNotNull(upgradeVersions, "upgradeRepresentation must not be null"); // build filter List<UUID> uuids = Lists.transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()); Predicate<SlotStatus> slotFilter = SlotFilterBuilder.build(uriInfo, true, uuids); // upgrade slots List<SlotStatus> results = coordinator.upgrade(slotFilter, upgradeVersions, expectedSlotsVersion); // build response return Response.ok(transform(results, fromSlotStatus(coordinator.getAllSlotStatus(), repository))) .header(GALAXY_SLOTS_VERSION_HEADER, createSlotsVersion(results)) .build(); } }
@PUT @Produces(MediaType.APPLICATION_JSON) public Response setState(String newState, @Context UriInfo uriInfo, @HeaderParam(GALAXY_SLOTS_VERSION_HEADER) String expectedSlotsVersion) { Preconditions.checkNotNull(newState, "newState must not be null"); SlotLifecycleState state = SlotLifecycleState.lookup(newState); if (state == null || state == UNKNOWN) { return Response.status(Response.Status.BAD_REQUEST).build(); } // build filter List<UUID> uuids = Lists.transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()); Predicate<SlotStatus> slotFilter = SlotFilterBuilder.build(uriInfo, true, uuids); // set slot state List<SlotStatus> results = coordinator.setState(state, slotFilter, expectedSlotsVersion); // build response return Response.ok(transform(results, fromSlotStatus(coordinator.getAllSlotStatus(), repository))) .header(GALAXY_SLOTS_VERSION_HEADER, createSlotsVersion(results)) .build(); } }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response install( AssignmentRepresentation assignmentRepresentation, @DefaultValue("1") @QueryParam("limit") int limit, @Context UriInfo uriInfo, @HeaderParam(GALAXY_AGENTS_VERSION_HEADER) String expectedAgentsVersion) { Preconditions.checkNotNull(assignmentRepresentation, "assignmentRepresentation must not be null"); Preconditions.checkArgument(limit > 0, "limit must be at least 1"); Assignment assignment = assignmentRepresentation.toAssignment(); // select the target agents Predicate<AgentStatus> agentFilter = AgentFilterBuilder.build(uriInfo, transform(coordinator.getAgents(), idGetter()), transform(coordinator.getAllSlotStatus(), uuidGetter()), false, repository); List<AgentStatus> agents = coordinator.getAgents(agentFilter); // verify the expected status of agents checkAgentsVersion(expectedAgentsVersion, agents); // install the software List<SlotStatus> slots = coordinator.install(agentFilter, limit, assignment); // calculate unique prefix size with the new slots included return Response.ok(transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository))) .header(GALAXY_SLOTS_VERSION_HEADER, createSlotsVersion(slots)) .build(); }
@Override public CommanderResponse<List<SlotStatusRepresentation>> show(SlotFilter slotFilter) { List<SlotStatus> allSlotStatus = coordinator.getAllSlotStatus(); List<UUID> uuids = transform(allSlotStatus, SlotStatus.uuidGetter()); Predicate<SlotStatus> slotPredicate = slotFilter.toSlotPredicate(false, uuids); List<SlotStatus> slots = coordinator.getAllSlotsStatus(slotPredicate); // update just in case something changed updateServiceInventory(); return createCommanderResponse(createSlotsVersion(slots), transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository))); }