Skip to content

Commit

Permalink
Added first draft of basic instance operations (related to #191)
Browse files Browse the repository at this point in the history
  • Loading branch information
vania-pooh committed Nov 24, 2016
1 parent 1fe1240 commit 640db12
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 3 deletions.
13 changes: 13 additions & 0 deletions perspective-aws/src/main/java/org/meridor/perspective/aws/Api.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
package org.meridor.perspective.aws;

public interface Api {

boolean rebootInstance(String instanceId);

boolean hardRebootInstance(String instanceId);

boolean startInstance(String instanceId);

boolean shutdownInstance(String instanceId);

boolean deleteInstance(String instanceId);

void close();

}
Original file line number Diff line number Diff line change
@@ -1,34 +1,55 @@
package org.meridor.perspective.aws;

import com.amazonaws.AmazonWebServiceResult;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.RebootInstancesRequest;
import com.amazonaws.services.ec2.model.StartInstancesRequest;
import com.amazonaws.services.ec2.model.StopInstancesRequest;
import com.amazonaws.services.ec2.model.TerminateInstancesRequest;
import org.meridor.perspective.config.Cloud;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

@Component
public class ApiProviderImpl implements ApiProvider {

private final Map<Cloud, Map<Regions, Api>> apiCache = new HashMap<>();

@Override
public Api getApi(Cloud cloud, Regions region) {
return new ApiImpl(cloud, region);
apiCache.putIfAbsent(cloud, new HashMap<>());
apiCache.get(cloud).putIfAbsent(region, new ApiImpl(cloud, region));
return apiCache.get(cloud).get(region);
}

@Override
public void forEachRegion(Cloud cloud, BiConsumer<String, Api> action) throws Exception {
Arrays.stream(Regions.values()).forEach(r -> action.accept(r.getName(), new ApiImpl(cloud, r)));
Arrays.stream(Regions.values()).forEach(r -> action.accept(r.getName(), getApi(cloud, r)));
}

@PreDestroy
public void onDestroy() {
apiCache.keySet().forEach(c -> {
Map<Regions, Api> regionsMap = apiCache.get(c);
regionsMap.keySet().forEach(r -> regionsMap.get(r).close());
});
}

private class ApiImpl implements Api {

private final AmazonEC2 client;

public ApiImpl(Cloud cloud, Regions region) {
ApiImpl(Cloud cloud, Regions region) {
this.client = createClient(cloud, region);
}

Expand All @@ -43,5 +64,47 @@ private AmazonEC2 createClient(Cloud cloud, Regions region) {
))
.build();
}

@Override
public boolean rebootInstance(String instanceId) {
RebootInstancesRequest request = new RebootInstancesRequest(Collections.singletonList(instanceId));
return isResponseSuccessful(client.rebootInstances(request));
}

@Override
public boolean hardRebootInstance(String instanceId) {
//TODO: to be implemented!!!
throw new UnsupportedOperationException();
}

@Override
public boolean startInstance(String instanceId) {
StartInstancesRequest request = new StartInstancesRequest(Collections.singletonList(instanceId));
return isResponseSuccessful(client.startInstances(request));
}

@Override
public boolean shutdownInstance(String instanceId) {
StopInstancesRequest request = new StopInstancesRequest(Collections.singletonList(instanceId));
return isResponseSuccessful(client.stopInstances(request));
}

@Override
public boolean deleteInstance(String instanceId) {
TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest(Collections.singletonList(instanceId));
return isResponseSuccessful(client.terminateInstances(terminateInstancesRequest));
}

private boolean isResponseSuccessful(AmazonWebServiceResult<?> response) {
//TODO: to be implemented!
return true;
}

@Override
public void close() {
client.shutdown();
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.meridor.perspective.aws;

import org.meridor.perspective.beans.Instance;
import org.meridor.perspective.config.OperationType;
import org.springframework.stereotype.Component;

import java.util.function.BiFunction;

@Component
public class DeleteInstanceOperation extends BaseInstanceOperation {

@Override
protected BiFunction<Api, Instance, Boolean> getAction() {
return (api, instance) -> {
try {
return api.deleteInstance(instance.getRealId());
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}

@Override
protected String getSuccessMessage(Instance instance) {
return String.format("Deleted instance %s (%s)", instance.getName(), instance.getId());
}

@Override
protected String getErrorMessage(Instance instance) {
return String.format("Failed to delete instance %s (%s)", instance.getName(), instance.getId());
}

@Override
public OperationType[] getTypes() {
return new OperationType[]{OperationType.DELETE_INSTANCE};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.meridor.perspective.aws;

import org.meridor.perspective.beans.Instance;
import org.meridor.perspective.config.OperationType;
import org.springframework.stereotype.Component;

import java.util.function.BiFunction;

import static org.meridor.perspective.config.OperationType.REBOOT_INSTANCE;

@Component
public class RebootInstanceOperation extends BaseInstanceOperation {

@Override
protected BiFunction<Api, Instance, Boolean> getAction() {
return (api, instance) -> {
try {
return api.rebootInstance(instance.getRealId());
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}

@Override
protected String getSuccessMessage(Instance instance) {
return String.format("Rebooted instance %s (%s)", instance.getName(), instance.getId());
}

@Override
protected String getErrorMessage(Instance instance) {
return String.format("Failed to reboot instance %s (%s)", instance.getName(), instance.getId());
}

@Override
public OperationType[] getTypes() {
return new OperationType[]{REBOOT_INSTANCE};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.meridor.perspective.aws;

import org.meridor.perspective.beans.Instance;
import org.meridor.perspective.config.OperationType;
import org.springframework.stereotype.Component;

import java.util.function.BiFunction;

import static org.meridor.perspective.config.OperationType.SHUTDOWN_INSTANCE;

@Component
public class ShutdownInstanceOperation extends BaseInstanceOperation {

@Override
protected BiFunction<Api, Instance, Boolean> getAction() {
return (api, instance) -> {
try {
return api.shutdownInstance(instance.getRealId());
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}

@Override
protected String getSuccessMessage(Instance instance) {
return String.format("Shut down instance %s (%s)", instance.getName(), instance.getId());
}

@Override
protected String getErrorMessage(Instance instance) {
return String.format("Failed to shut down instance %s (%s)", instance.getName(), instance.getId());
}

@Override
public OperationType[] getTypes() {
return new OperationType[]{SHUTDOWN_INSTANCE};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.meridor.perspective.aws;

import org.meridor.perspective.beans.Instance;
import org.meridor.perspective.config.OperationType;
import org.springframework.stereotype.Component;

import java.util.function.BiFunction;

import static org.meridor.perspective.config.OperationType.START_INSTANCE;

@Component
public class StartInstanceOperation extends BaseInstanceOperation {

@Override
protected BiFunction<Api, Instance, Boolean> getAction() {
return (api, instance) -> {
try {
return api.startInstance(instance.getRealId());
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}

@Override
protected String getSuccessMessage(Instance instance) {
return String.format("Started instance %s (%s)", instance.getName(), instance.getId());
}

@Override
protected String getErrorMessage(Instance instance) {
return String.format("Failed to start instance %s (%s)", instance.getName(), instance.getId());
}

@Override
public OperationType[] getTypes() {
return new OperationType[]{START_INSTANCE};
}

}

0 comments on commit 640db12

Please sign in to comment.