Skip to content

Commit

Permalink
#1877 stop properly
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Feb 27, 2024
1 parent 4b430ae commit 2282ae7
Showing 1 changed file with 5 additions and 30 deletions.
35 changes: 5 additions & 30 deletions src/main/java/com/rultor/agents/aws/StopsInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
*/
package com.rultor.agents.aws;

import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.DryRunResult;
import com.amazonaws.services.ec2.model.DryRunSupportedRequest;
import com.amazonaws.services.ec2.model.StopInstancesRequest;
import com.jcabi.aspects.Immutable;
import com.jcabi.log.Logger;
Expand Down Expand Up @@ -71,33 +68,11 @@ public StopsInstance(final AwsEc2 api) {
@Override
public Iterable<Directive> process(final XML xml) throws IOException {
final String instance = xml.xpath("/talk/ec2/@id").get(0);
this.stop(instance);
return new Directives().xpath("/talk/ec2").strict(1).remove();
}

/**
* Stop instance.
* @param instance Instance ID
*/
private void stop(final String instance) {
final DryRunSupportedRequest<StopInstancesRequest> draft = () -> {
final StopInstancesRequest request = new StopInstancesRequest()
.withInstanceIds(instance);
return request.getDryRunRequest();
};
final AmazonEC2 client = this.api.aws();
final DryRunResult<StopInstancesRequest> response =
client.dryRun(draft);
if (!response.isSuccessful()) {
Logger.error(
this,
"Failed dry run to stop instance %s", instance
);
throw response.getDryRunResponse();
}
final StopInstancesRequest request = new StopInstancesRequest()
.withInstanceIds(instance);
client.stopInstances(request);
this.api.aws().stopInstances(
new StopInstancesRequest()
.withInstanceIds(instance)
);
Logger.info("Successfully stopped instance %s", instance);
return new Directives().xpath("/talk/ec2").strict(1).remove();
}
}

0 comments on commit 2282ae7

Please sign in to comment.