public AgentStatus toAgentStatus(String instanceId, String instanceType) { Builder<SlotStatus> builder = ImmutableList.builder(); for (SlotStatusRepresentation slot : slots) { builder.add(slot.toSlotStatus(instanceId)); } return new AgentStatus(agentId, AgentLifecycleState.ONLINE, instanceId, self, externalUri, location, instanceType, builder.build(), resources); }
private void assertAppleSlot(SlotStatusRepresentation slotRepresentation) { SlotStatus slot = slotRepresentation.toSlotStatus("instance"); assertEquals(slot.getAssignment(), APPLE_ASSIGNMENT); assertEquals(slot.getState(), STOPPED); assertEquals(slot.getResources(), ImmutableMap.of("cpu", 1, "memory", 512)); } }
private void assertAppleSlot(SlotStatusRepresentation slotRepresentation) { SlotStatus slot = slotRepresentation.toSlotStatus("instance"); assertEquals(slot.getAssignment(), APPLE_ASSIGNMENT); assertEquals(slot.getState(), STOPPED); assertEquals(slot.getResources(), ImmutableMap.of("cpu", 1, "memory", 512)); } }
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 }
private void assertUpgrade(SlotStatus slotStatus, String agentVersion, String slotVersion) { Response response = resource.assign(agentVersion, slotVersion, slotStatus.getId(), UPGRADE); assertEquals(response.getStatus(), Status.OK.getStatusCode()); SlotStatusRepresentation actualStatus = (SlotStatusRepresentation) response.getEntity(); SlotStatus expectedStatus = slotStatus.changeAssignment(slotStatus.getState(), APPLE_V2, slotStatus.getResources()); assertEquals(actualStatus, SlotStatusRepresentation.from(expectedStatus)); assertEquals(actualStatus.toSlotStatus(null), expectedStatus); assertEquals(response.getMetadata().get(AIRSHIP_AGENT_VERSION_HEADER).get(0), agent.getAgentStatus().getVersion()); assertEquals(response.getMetadata().get(AIRSHIP_SLOT_VERSION_HEADER).get(0), expectedStatus.getVersion()); assertNull(response.getMetadata().get("Content-Type")); // content type is set by jersey based on @Produces } }
private void assertUpgrade(SlotStatus slotStatus, String agentVersion, String slotVersion) { Response response = resource.assign(agentVersion, slotVersion, slotStatus.getId(), UPGRADE); assertEquals(response.getStatus(), Status.OK.getStatusCode()); SlotStatusRepresentation actualStatus = (SlotStatusRepresentation) response.getEntity(); SlotStatus expectedStatus = slotStatus.changeAssignment(slotStatus.getState(), APPLE_V2, slotStatus.getResources()); assertEquals(actualStatus, SlotStatusRepresentation.from(expectedStatus)); assertEquals(actualStatus.toSlotStatus(null), expectedStatus); assertEquals(response.getMetadata().get(AIRSHIP_AGENT_VERSION_HEADER).get(0), agent.getAgentStatus().getVersion()); assertEquals(response.getMetadata().get(AIRSHIP_SLOT_VERSION_HEADER).get(0), expectedStatus.getVersion()); assertNull(response.getMetadata().get("Content-Type")); // content type is set by jersey based on @Produces } }
@Override public SlotStatus terminate() { try { Request request = Request.Builder.prepareDelete() .setUri(slotStatus.getSelf()) .setHeader(AIRSHIP_AGENT_VERSION_HEADER, agent.status().getVersion()) .setHeader(AIRSHIP_SLOT_VERSION_HEADER, slotStatus.getVersion()) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.OK.getStatusCode())); updateStatus(slotStatusRepresentation.toSlotStatus(slotStatus.getInstanceId())); return slotStatus; } catch (Exception e) { log.error(e); return setErrorStatus(e.getMessage()); } }
@Override public SlotStatus kill() { try { Request request = Request.Builder.preparePut() .setUri(uriBuilderFrom(slotStatus.getSelf()).appendPath("lifecycle").build()) .setBodyGenerator(createStaticBodyGenerator("killing", UTF_8)) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.OK.getStatusCode())); updateStatus(slotStatusRepresentation.toSlotStatus(slotStatus.getInstanceId())); return slotStatus; } catch (Exception e) { log.error(e); return setErrorStatus(e.getMessage()); } } }
@Override public SlotStatus install(Installation installation) { Preconditions.checkNotNull(installation, "installation is null"); AgentStatus agentStatus = status(); URI internalUri = agentStatus.getInternalUri(); Preconditions.checkState(internalUri != null, "agent is down"); try { Request request = Request.Builder.preparePost() .setUri(uriBuilderFrom(internalUri).replacePath("/v1/agent/slot/").build()) .setHeader(CONTENT_TYPE, APPLICATION_JSON) .setHeader(AIRSHIP_AGENT_VERSION_HEADER, status().getVersion()) .setBodyGenerator(jsonBodyGenerator(installationCodec, InstallationRepresentation.from(installation))) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.CREATED.getStatusCode())); SlotStatus slotStatus = slotStatusRepresentation.toSlotStatus(agentStatus.getInstanceId()); setStatus(agentStatus.changeSlotStatus(slotStatus)); return slotStatus; } catch (Exception e) { throw Throwables.propagate(e); } } }
@Override public SlotStatus restart() { try { Request request = Request.Builder.preparePut() .setUri(uriBuilderFrom(slotStatus.getSelf()).appendPath("lifecycle").build()) .setHeader(AIRSHIP_AGENT_VERSION_HEADER, agent.status().getVersion()) .setHeader(AIRSHIP_SLOT_VERSION_HEADER, slotStatus.getVersion()) .setBodyGenerator(createStaticBodyGenerator("restarting", UTF_8)) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.OK.getStatusCode())); updateStatus(slotStatusRepresentation.toSlotStatus(slotStatus.getInstanceId())); return slotStatus; } catch (Exception e) { log.error(e); return setErrorStatus(e.getMessage()); } }
@Override public SlotStatus stop() { try { Request request = Request.Builder.preparePut() .setUri(uriBuilderFrom(slotStatus.getSelf()).appendPath("lifecycle").build()) .setHeader(AIRSHIP_AGENT_VERSION_HEADER, agent.status().getVersion()) .setHeader(AIRSHIP_SLOT_VERSION_HEADER, slotStatus.getVersion()) .setBodyGenerator(createStaticBodyGenerator("stopped", UTF_8)) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.OK.getStatusCode())); updateStatus(slotStatusRepresentation.toSlotStatus(slotStatus.getInstanceId())); return slotStatus; } catch (Exception e) { log.error(e); return setErrorStatus(e.getMessage()); } }
@Override public SlotStatus start() { try { Request request = Request.Builder.preparePut() .setUri(uriBuilderFrom(slotStatus.getSelf()).appendPath("lifecycle").build()) .setHeader(AIRSHIP_AGENT_VERSION_HEADER, agent.status().getVersion()) .setHeader(AIRSHIP_SLOT_VERSION_HEADER, slotStatus.getVersion()) .setBodyGenerator(createStaticBodyGenerator("running", UTF_8)) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.OK.getStatusCode())); updateStatus(slotStatusRepresentation.toSlotStatus(slotStatus.getInstanceId())); return slotStatus; } catch (Exception e) { log.error(e); return setErrorStatus(e.getMessage()); } }
@Override public SlotStatus assign(Installation installation) { try { Request request = Request.Builder.preparePut() .setUri(uriBuilderFrom(slotStatus.getSelf()).appendPath("assignment").build()) .setHeader(CONTENT_TYPE, APPLICATION_JSON) .setHeader(AIRSHIP_AGENT_VERSION_HEADER, agent.status().getVersion()) .setHeader(AIRSHIP_SLOT_VERSION_HEADER, slotStatus.getVersion()) .setBodyGenerator(jsonBodyGenerator(installationCodec, InstallationRepresentation.from(installation))) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.OK.getStatusCode())); updateStatus(slotStatusRepresentation.toSlotStatus(slotStatus.getInstanceId())); return slotStatus; } catch (Exception e) { log.error(e); return setErrorStatus(e.getMessage()); } }