public Installation toInstallation() { Installation installation = new Installation(shortName, assignment.toAssignment(), URI.create(binaryFile), URI.create(configFile), resources); return installation; }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } InstallationRepresentation that = (InstallationRepresentation) o; if (assignment != null ? !assignment.equals(that.assignment) : that.assignment != null) { return false; } return true; }
@Override public int hashCode() { return assignment != null ? assignment.hashCode() : 0; }
public static InstallationRepresentation from(Installation installation) { Assignment assignment = installation.getAssignment(); return new InstallationRepresentation( installation.getShortName(), AssignmentRepresentation.from(assignment), installation.getBinaryFile().toString(), installation.getConfigFile().toString(), installation.getResources()); }
public static AssignmentRepresentation from(Assignment assignment) { return new AssignmentRepresentation(assignment.getBinary(), assignment.getConfig()); }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "No agents have the available resources to run the specified binary and configuration.") public void testInstallNotEnoughResources() { provisioner.addAgent(UUID.randomUUID().toString(), URI.create("fake://appleServer1/"), ImmutableMap.of("cpu", 0, "memory", 0)); coordinator.updateAllAgentsAndWait(); UriInfo uriInfo = MockUriInfo.from("http://localhost/v1/slot/assignment"); Response response = resource.install(AssignmentRepresentation.from(APPLE_ASSIGNMENT), 1, uriInfo, null); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); response.getEntity(); }
public static DeploymentRepresentation from(Deployment deployment) { return new DeploymentRepresentation( deployment.getNodeId(), AssignmentRepresentation.from(deployment.getAssignment()), deployment.getResources()); }
public Deployment toDeployment(File deploymentDir, File dataDir, String location) { return new Deployment(nodeId, location, deploymentDir, dataDir, assignment.toAssignment(), resources); }
@Override public int hashCode() { int result = (nodeId != null ? nodeId.hashCode() : 0); result = 31 * result + (assignment != null ? assignment.hashCode() : 0); result = 31 * result + (resources != null ? resources.hashCode() : 0); return result; }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } DeploymentRepresentation that = (DeploymentRepresentation) o; if (assignment != null ? !assignment.equals(that.assignment) : that.assignment != null) { return false; } if (nodeId != null ? !nodeId.equals(that.nodeId) : that.nodeId != null) { return false; } if (resources != null ? !resources.equals(that.resources) : that.resources != null) { return false; } return true; }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "No agents have the available resources to run the specified binary and configuration.") public void testInstallNotEnoughResources() { provisioner.addAgent(UUID.randomUUID().toString(), URI.create("fake://appleServer1/"), ImmutableMap.of("cpu", 0, "memory", 0)); coordinator.updateAllAgentsAndWait(); UriInfo uriInfo = MockUriInfo.from("http://localhost/v1/slot/assignment"); Response response = resource.install(AssignmentRepresentation.from(APPLE_ASSIGNMENT), 1, uriInfo, null); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); response.getEntity(); }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response install( AssignmentRepresentation assignmentRepresentation, @DefaultValue("1") @QueryParam("limit") int limit, @Context UriInfo uriInfo, @HeaderParam(AIRSHIP_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(AIRSHIP_SLOTS_VERSION_HEADER, createSlotsVersion(slots)) .build(); }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "No agents have the available resources to run the specified binary and configuration.") public void testInstallResourcesConsumed() { provisioner.addAgent(UUID.randomUUID().toString(), URI.create("fake://appleServer1/"), ImmutableMap.of("cpu", 1, "memory", 512)); coordinator.updateAllAgentsAndWait(); UriInfo uriInfo = MockUriInfo.from("http://localhost/v1/slot/assignment"); // install an apple server Response response = resource.install(AssignmentRepresentation.from(APPLE_ASSIGNMENT), 1, uriInfo, null); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Collection<SlotStatusRepresentation> slots = (Collection<SlotStatusRepresentation>) response.getEntity(); assertEquals(slots.size(), 1); assertAppleSlot(Iterables.get(slots, 0)); // try to install a banana server which will fail response = resource.install(AssignmentRepresentation.from(BANANA_ASSIGNMENT), 1, uriInfo, null); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); response.getEntity(); }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "No agents have the available resources to run the specified binary and configuration.") public void testInstallResourcesConsumed() { provisioner.addAgent(UUID.randomUUID().toString(), URI.create("fake://appleServer1/"), ImmutableMap.of("cpu", 1, "memory", 512)); coordinator.updateAllAgentsAndWait(); UriInfo uriInfo = MockUriInfo.from("http://localhost/v1/slot/assignment"); // install an apple server Response response = resource.install(AssignmentRepresentation.from(APPLE_ASSIGNMENT), 1, uriInfo, null); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Collection<SlotStatusRepresentation> slots = (Collection<SlotStatusRepresentation>) response.getEntity(); assertEquals(slots.size(), 1); assertAppleSlot(Iterables.get(slots, 0)); // try to install a banana server which will fail response = resource.install(AssignmentRepresentation.from(BANANA_ASSIGNMENT), 1, uriInfo, null); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); response.getEntity(); }
@Test public void testInstallWithinResourceLimit() { provisioner.addAgent(UUID.randomUUID().toString(), URI.create("fake://appleServer1/"), ImmutableMap.of("cpu", 1, "memory", 512)); coordinator.updateAllAgentsAndWait(); UriInfo uriInfo = MockUriInfo.from("http://localhost/v1/slot/assignment"); Response response = resource.install(AssignmentRepresentation.from(APPLE_ASSIGNMENT), 1, uriInfo, null); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Collection<SlotStatusRepresentation> slots = (Collection<SlotStatusRepresentation>) response.getEntity(); assertEquals(slots.size(), 1); for (SlotStatusRepresentation slotRepresentation : slots) { assertAppleSlot(slotRepresentation); } assertNull(response.getMetadata().get("Content-Type")); // content type is set by jersey based on @Produces }
@Test public void testInstallWithinResourceLimit() { provisioner.addAgent(UUID.randomUUID().toString(), URI.create("fake://appleServer1/"), ImmutableMap.of("cpu", 1, "memory", 512)); coordinator.updateAllAgentsAndWait(); UriInfo uriInfo = MockUriInfo.from("http://localhost/v1/slot/assignment"); Response response = resource.install(AssignmentRepresentation.from(APPLE_ASSIGNMENT), 1, uriInfo, null); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Collection<SlotStatusRepresentation> slots = (Collection<SlotStatusRepresentation>) response.getEntity(); assertEquals(slots.size(), 1); for (SlotStatusRepresentation slotRepresentation : slots) { assertAppleSlot(slotRepresentation); } assertNull(response.getMetadata().get("Content-Type")); // content type is set by jersey based on @Produces }
public void testInstall(int numberOfAgents, int limit, Assignment assignment) { for (int i = 0; i < numberOfAgents; i++) { provisioner.addAgent(UUID.randomUUID().toString(), URI.create("fake://appleServer1/"), ImmutableMap.of("cpu", 8, "memory", 1024)); } coordinator.updateAllAgentsAndWait(); UriInfo uriInfo = MockUriInfo.from("http://localhost/v1/slot/assignment?host=apple*"); Response response = resource.install(AssignmentRepresentation.from(assignment), limit, uriInfo, null); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Collection<SlotStatusRepresentation> slots = (Collection<SlotStatusRepresentation>) response.getEntity(); assertEquals(slots.size(), min(numberOfAgents, limit)); for (SlotStatusRepresentation slotRepresentation : slots) { SlotStatus slot = slotRepresentation.toSlotStatus("instance"); assertEquals(slot.getAssignment(), assignment); assertEquals(slot.getState(), STOPPED); } assertNull(response.getMetadata().get("Content-Type")); // content type is set by jersey based on @Produces }
public void testInstall(int numberOfAgents, int limit, Assignment assignment) { for (int i = 0; i < numberOfAgents; i++) { provisioner.addAgent(UUID.randomUUID().toString(), URI.create("fake://appleServer1/"), ImmutableMap.of("cpu", 8, "memory", 1024)); } coordinator.updateAllAgentsAndWait(); UriInfo uriInfo = MockUriInfo.from("http://localhost/v1/slot/assignment?host=apple*"); Response response = resource.install(AssignmentRepresentation.from(assignment), limit, uriInfo, null); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Collection<SlotStatusRepresentation> slots = (Collection<SlotStatusRepresentation>) response.getEntity(); assertEquals(slots.size(), min(numberOfAgents, limit)); for (SlotStatusRepresentation slotRepresentation : slots) { SlotStatus slot = slotRepresentation.toSlotStatus("instance"); assertEquals(slot.getAssignment(), assignment); assertEquals(slot.getState(), STOPPED); } assertNull(response.getMetadata().get("Content-Type")); // content type is set by jersey based on @Produces }
@Override public List<SlotStatusRepresentation> install(AgentFilter agentFilter, int count, Assignment assignment, String expectedVersion) { URI uri = agentFilter.toUri(uriBuilderFrom(coordinatorUri).replacePath("/v1/slot").addParameter("limit", String.valueOf(count))); Request.Builder requestBuilder = Request.Builder.preparePost() .setUri(uri) .setHeader("Content-Type", "application/json") .setBodyGenerator(jsonBodyGenerator(ASSIGNMENT_CODEC, AssignmentRepresentation.from(assignment))); if (expectedVersion != null) { requestBuilder.setHeader(AIRSHIP_SLOTS_VERSION_HEADER, expectedVersion); } List<SlotStatusRepresentation> slots = client.execute(requestBuilder.build(), createJsonResponseHandler(SLOTS_CODEC)); return slots; }
@Override public List<SlotStatusRepresentation> install(AgentFilter agentFilter, int count, Assignment assignment, String expectedVersion) { URI uri = agentFilter.toUri(uriBuilderFrom(coordinatorUri).replacePath("/v1/slot").addParameter("limit", String.valueOf(count))); Request.Builder requestBuilder = Request.Builder.preparePost() .setUri(uri) .setHeader("Content-Type", "application/json") .setBodyGenerator(jsonBodyGenerator(ASSIGNMENT_CODEC, AssignmentRepresentation.from(assignment))); if (expectedVersion != null) { requestBuilder.setHeader(AIRSHIP_SLOTS_VERSION_HEADER, expectedVersion); } List<SlotStatusRepresentation> slots = client.execute(requestBuilder.build(), createJsonResponseHandler(SLOTS_CODEC)); return slots; }