private List<AgentStatusRepresentation> waitForAgentsToStart(List<String> instanceIds) {
    for (int loop = 0; true; loop++) {
      try {
        URI uri = uriBuilderFrom(coordinatorUri).replacePath("v1/admin/agent").build();
        Request request = RequestBuilder.prepareGet().setUri(uri).build();
        List<AgentStatusRepresentation> agents =
            client.execute(request, createJsonResponseHandler(AGENTS_CODEC));

        Map<String, AgentStatusRepresentation> runningAgents = newHashMap();
        for (AgentStatusRepresentation agent : agents) {
          if (agent.getState() == AgentLifecycleState.ONLINE) {
            runningAgents.put(agent.getInstanceId(), agent);
          }
        }
        if (runningAgents.keySet().containsAll(instanceIds)) {
          WaitUtils.clearWaitMessage();
          runningAgents.keySet().retainAll(instanceIds);
          return ImmutableList.copyOf(runningAgents.values());
        }
      } catch (Exception ignored) {
      }

      WaitUtils.wait(loop);
    }
  }
  @Override
  public List<AgentStatusRepresentation> provisionAgents(
      String agentConfig,
      int agentCount,
      String instanceType,
      String availabilityZone,
      String ami,
      String keyPair,
      String securityGroup,
      boolean waitForStartup) {
    URI uri = uriBuilderFrom(coordinatorUri).replacePath("v1/admin/agent").build();

    AgentProvisioningRepresentation agentProvisioning =
        new AgentProvisioningRepresentation(
            agentConfig, agentCount, instanceType, availabilityZone, ami, keyPair, securityGroup);

    Request request =
        RequestBuilder.preparePost()
            .setUri(uri)
            .setHeader("Content-Type", "application/json")
            .setBodyGenerator(jsonBodyGenerator(AGENT_PROVISIONING_CODEC, agentProvisioning))
            .build();

    List<AgentStatusRepresentation> agents =
        client.execute(request, createJsonResponseHandler(AGENTS_CODEC));
    if (waitForStartup) {
      List<String> instanceIds = newArrayList();
      for (AgentStatusRepresentation agent : agents) {
        instanceIds.add(agent.getInstanceId());
      }
      agents = waitForAgentsToStart(instanceIds);
    }
    return agents;
  }