use of com.amazonaws.services.ec2.model.StopInstancesResult in project solarnetwork-central by SolarNetwork.
the class AwsVirtualMachineBiz method changeVirtualMachinesState.
@Override
public void changeVirtualMachinesState(Set<String> machineIds, VirtualMachineState desiredState) {
AmazonEC2 client = getEc2Client();
List<InstanceStateChange> results = null;
if (desiredState == VirtualMachineState.Running) {
StartInstancesRequest req = new StartInstancesRequest().withInstanceIds(machineIds);
StartInstancesResult res = client.startInstances(req);
results = res.getStartingInstances();
} else if (desiredState == VirtualMachineState.Stopped) {
StopInstancesRequest req = new StopInstancesRequest().withInstanceIds(machineIds);
StopInstancesResult res = client.stopInstances(req);
results = res.getStoppingInstances();
} else {
throw new IllegalArgumentException("Desired state not supported: " + desiredState);
}
log.info("Changed EC2 instances {} desired state to {}: {}", machineIds, desiredState, results);
}
use of com.amazonaws.services.ec2.model.StopInstancesResult in project gocd-ecs-elastic-agent by gocd.
the class StopOperationTest method shouldStopInstance.
@Test
void shouldStopInstance() {
final ContainerInstance instanceToDeregister = containerInstance("i-abcde12", "container-instance-arn");
;
final ArgumentCaptor<StopInstancesRequest> stopInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(StopInstancesRequest.class);
when(ec2Client.stopInstances(stopInstancesRequestArgumentCaptor.capture())).thenReturn(new StopInstancesResult());
new StopOperation().execute(pluginSettings, instanceToDeregister);
final StopInstancesRequest stopInstancesRequest = stopInstancesRequestArgumentCaptor.getValue();
assertThat(stopInstancesRequest.getInstanceIds()).hasSize(1).contains("i-abcde12");
}
use of com.amazonaws.services.ec2.model.StopInstancesResult in project camel by apache.
the class AmazonEC2ClientMock method stopInstances.
@Override
public StopInstancesResult stopInstances(StopInstancesRequest stopInstancesRequest) {
StopInstancesResult result = new StopInstancesResult();
if (stopInstancesRequest.getInstanceIds().get(0).equals("test-1")) {
Collection<InstanceStateChange> coll = new ArrayList<InstanceStateChange>();
InstanceStateChange sc = new InstanceStateChange();
InstanceState previousState = new InstanceState();
previousState.setCode(80);
previousState.setName(InstanceStateName.Running);
InstanceState newState = new InstanceState();
newState.setCode(16);
newState.setName(InstanceStateName.Stopped);
sc.setPreviousState(previousState);
sc.setCurrentState(newState);
sc.setInstanceId("test-1");
coll.add(sc);
result.setStoppingInstances(coll);
} else {
throw new AmazonServiceException("The image-id doesn't exists");
}
return result;
}
use of com.amazonaws.services.ec2.model.StopInstancesResult in project photon-model by vmware.
the class AWSPowerService method powerOff.
private void powerOff(AmazonEC2AsyncClient client, ComputePowerRequest pr, DefaultAdapterContext c) {
OperationContext opContext = OperationContext.getOperationContext();
StopInstancesRequest request = new StopInstancesRequest();
request.withInstanceIds(c.child.id);
client.stopInstancesAsync(request, new AsyncHandler<StopInstancesRequest, StopInstancesResult>() {
@Override
public void onSuccess(StopInstancesRequest request, StopInstancesResult result) {
AWSUtils.waitForTransitionCompletion(getHost(), result.getStoppingInstances(), "stopped", client, (is, e) -> {
OperationContext.restoreOperationContext(opContext);
if (e != null) {
onError(e);
return;
}
updateComputeState(pr, c);
});
}
@Override
public void onError(Exception e) {
OperationContext.restoreOperationContext(opContext);
c.taskManager.patchTaskToFailure(e);
}
});
}
use of com.amazonaws.services.ec2.model.StopInstancesResult in project photon-model by vmware.
the class AWSResetService method reset.
private void reset(AmazonEC2AsyncClient client, ResourceOperationRequest pr, DefaultAdapterContext c) {
if (!c.child.powerState.equals(ComputeService.PowerState.ON)) {
logWarning(() -> String.format("Cannot perform a reset on this EC2 instance. " + "The machine should be in powered on state"));
c.taskManager.patchTaskToFailure(new IllegalStateException("Incorrect power state. Expected the machine " + "to be powered on "));
return;
}
// The stop action for reset is a force stop. So we use the withForce method to set the force parameter to TRUE
// This is similar to unplugging the machine from the power circuit.
// The OS and the applications are forcefully stopped.
StopInstancesRequest stopRequest = new StopInstancesRequest();
stopRequest.withInstanceIds(c.child.id).withForce(Boolean.TRUE);
client.stopInstancesAsync(stopRequest, new AWSAsyncHandler<StopInstancesRequest, StopInstancesResult>() {
@Override
protected void handleError(Exception e) {
c.taskManager.patchTaskToFailure(e);
}
@Override
protected void handleSuccess(StopInstancesRequest request, StopInstancesResult result) {
AWSUtils.waitForTransitionCompletion(getHost(), result.getStoppingInstances(), "stopped", client, (is, e) -> {
if (e != null) {
onError(e);
return;
}
// Instances will be started only if they're successfully stopped
startInstance(client, c);
});
}
});
}
Aggregations