/** * Creates a copy of this {@link Machine} with a different value for the * metadata field. * * @param metadata * Metadata to set for the {@link Machine} copy, or * <code>null</code> if no metadata is desired. * @return A copy */ public Machine withMetadata(JsonObject metadata) { return new Machine(this.id, this.machineState, this.membershipStatus, this.serviceState, this.cloudProvider, this.region, this.machineSize, this.requestTime, this.launchTime, this.publicIps, this.privateIps, metadata); }
@Override public boolean test(Machine machine) { return machine.getMachineState() == this.state; } }
@Override public boolean test(Machine machine) { return isAllocated().test(machine) && machine.getMembershipStatus().isActive(); } }
return Machine.builder().id(id).machineState(machineState).cloudProvider(cloudProvider).region(region) .machineSize(instance.getInstanceType()).membershipStatus(membershipStatus).serviceState(serviceState) .launchTime(launchtime).publicIps(publicIps).privateIps(privateIps).metadata(metadata).build();
/** * Parses a JSON representation of a {@link MachinePool} to its Java * counterpart. Any failure to parse the JSON representation into a valid * {@link MachinePool} instance results in an exception being thrown. * * @param machinePoolAsJson * @return * @throws IOException */ public static MachinePool fromJson(String machinePoolAsJson) throws IOException { MachinePool machinePool = JsonUtils.toObject(JsonUtils.parseJsonString(machinePoolAsJson), MachinePool.class); checkNotNull(machinePool.timestamp, "machine pool missing timestamp"); checkNotNull(machinePool.machines, "machine pool missing instances"); for (Machine machine : machinePool.machines) { checkNotNull(machine.getId(), "machine missing id"); checkNotNull(machine.getMachineState(), "machine missing state"); } return machinePool; }
/** * Retrieves a particular member machine from the cloud pool or throws an * exception if it could not be found. * * @param machineId * The id of the machine of interest. * @return * @throws NotFoundException */ private Machine getMachineOrFail(String machineId) throws NotFoundException { List<Machine> machines = listMachines(); for (Machine machine : machines) { if (machine.getId().equals(machineId)) { return machine; } } throw new NotFoundException(String.format("no machine with id '%s' found in cloud pool", machineId)); }
/** * Returns all <i>started</i> {@link Machine}s in the pool. See * {@link Machine#isStarted()}. * * @return */ public List<Machine> getStartedMachines() { return getMachines().stream().filter(Machine.isStarted()).collect(Collectors.toList()); }
@Override public boolean test(Machine machine) { return machine.getMembershipStatus().isEvictable(); } }
/** * Returns all active {@link Machine}s in the pool. See * {@link Machine#isActiveMember()}. * * @return */ public List<Machine> getActiveMachines() { return getMachines().stream().filter(Machine.isActiveMember()).collect(Collectors.toList()); }
/** * Returns all <i>allocated</i> {@link Machine}s in the pool. See * {@link Machine#isAllocated()}. * * @return */ public List<Machine> getAllocatedMachines() { return getMachines().stream().filter(Machine.isAllocated()).collect(Collectors.toList()); }
@Override public List<Machine> startMachines(int count) throws StartMachinesException, CloudPoolDriverException { checkState(isConfigured(), "attempt to use unconfigured driver"); List<Machine> startedMachines = new ArrayList<>(); try { for (int i = 0; i < count; i++) { // tag new server with cloud pool membership Map<String, String> tags = ImmutableMap.of(Constants.CLOUD_POOL_TAG, getPoolName()); Server newServer = this.client.launchServer(uniqueServerName(), provisioningTemplate(), tags); Machine machine = serverToMachine().apply(newServer); startedMachines.add(machine); if (provisioningTemplate().isAssignFloatingIp()) { String serverId = newServer.getId(); String floatingIp = this.client.assignFloatingIp(serverId); machine.getPublicIps().add(floatingIp); } } } catch (Exception e) { throw new StartMachinesException(count, startedMachines, e); } return startedMachines; }
Builder builder = Machine.builder(); builder.id(server.getId()); builder.cloudProvider(this.cloudProvider);
@Override public Machine apply(Instance instance) { Builder builder = Machine.builder();
@Override public boolean test(Machine machine) { return allocatedStates.contains(machine.getMachineState()); } }
/** * Creates a new {@link Machine} instance from the parameters passed * thus far to the {@link Builder}. * * @return */ public Machine build() { checkArgument(this.id != null, "machine: no id given"); checkArgument(this.machineState != null, "machine: no machineState given"); checkArgument(this.cloudProvider != null, "machine: no cloudProvider given"); checkArgument(this.region != null, "machine: no region given"); checkArgument(this.machineSize != null, "machine: no machineSize given"); // Note: we don't verify that launchTime is later than requestTime, // since this is typically meta data given by the cloud provider and // if it's wrong there is not much we can do about it. Furthermore, // from an operational standpoint, it shouldn't matter if the times // don't make sense. return new Machine(this.id, this.machineState, this.membershipStatus, this.serviceState, this.cloudProvider, this.region, this.machineSize, this.requestTime, this.launchTime, this.publicIps, this.privateIps, this.metadata); }
@Override public boolean test(Machine machine) { return startedStates.contains(machine.getMachineState()); } }