Skip to content

Commit

Permalink
Refactored BaseInstanceOperation usage (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 bed9df4 commit 1fe1240
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.meridor.perspective.aws;

public interface Api {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.meridor.perspective.aws;

import com.amazonaws.regions.Regions;
import org.meridor.perspective.config.Cloud;

import java.util.function.BiConsumer;

public interface ApiProvider {

Api getApi(Cloud cloud, Regions region);

void forEachRegion(Cloud cloud, BiConsumer<String, Api> action) throws Exception;

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

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 org.meridor.perspective.config.Cloud;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.function.BiConsumer;

@Component
public class ApiProviderImpl implements ApiProvider {

@Override
public Api getApi(Cloud cloud, Regions region) {
return new ApiImpl(cloud, 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)));
}

private class ApiImpl implements Api {

private final AmazonEC2 client;

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

private AmazonEC2 createClient(Cloud cloud, Regions region) {
return AmazonEC2ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(
new BasicAWSCredentials(
cloud.getIdentity(),
cloud.getCredential()
)
))
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.meridor.perspective.aws;

import com.amazonaws.regions.Regions;
import org.meridor.perspective.beans.Instance;
import org.meridor.perspective.beans.MetadataKey;
import org.meridor.perspective.config.Cloud;
import org.meridor.perspective.worker.operation.AbstractInstanceOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public abstract class BaseInstanceOperation extends AbstractInstanceOperation<Api> {

@Autowired
private ApiProvider apiProvider;

@Override
protected Api getApi(Cloud cloud, Instance instance) {
Regions region = Regions.fromName(instance.getMetadata().get(MetadataKey.REGION));
return apiProvider.getApi(cloud, region);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,19 @@

import org.meridor.perspective.beans.Instance;
import org.meridor.perspective.config.Cloud;
import org.meridor.perspective.worker.operation.ConsumingOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.meridor.perspective.worker.operation.AbstractInstanceOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.function.BiFunction;
import java.util.function.Supplier;

@Component
public abstract class BaseInstanceOperation implements ConsumingOperation<Instance> {

private static final Logger LOG = LoggerFactory.getLogger(BaseInstanceOperation.class);
public abstract class BaseInstanceOperation extends AbstractInstanceOperation<Api> {

@Autowired
private ApiProvider apiProvider;

@Override
public boolean perform(Cloud cloud, Supplier<Instance> supplier) {
Instance instance = supplier.get();
try {
Api api = apiProvider.getApi(cloud);
boolean success = getAction().apply(api, instance);
if (success) {
LOG.debug(getSuccessMessage(instance));
return true;
} else {
LOG.error(getErrorMessage(instance));
return false;
}
} catch (Exception e) {
LOG.error(getErrorMessage(instance), e);
return false;
}
protected Api getApi(Cloud cloud, Instance instance) {
return apiProvider.getApi(cloud);
}

protected abstract BiFunction<Api, Instance, Boolean> getAction();

protected abstract String getSuccessMessage(Instance instance);

protected abstract String getErrorMessage(Instance instance);

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.meridor.perspective.openstack;

import org.meridor.perspective.backend.storage.ProjectsAware;
import org.meridor.perspective.beans.Instance;
import org.meridor.perspective.beans.MetadataKey;
import org.meridor.perspective.beans.Project;
import org.meridor.perspective.config.Cloud;
import org.meridor.perspective.backend.storage.ProjectsAware;
import org.meridor.perspective.worker.operation.ConsumingOperation;
import org.meridor.perspective.worker.operation.AbstractInstanceOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -15,7 +15,7 @@
import java.util.function.Supplier;

@Component
public abstract class BaseInstanceOperation implements ConsumingOperation<Instance> {
public abstract class BaseInstanceOperation extends AbstractInstanceOperation<Api> {

private static final Logger LOG = LoggerFactory.getLogger(BaseInstanceOperation.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.meridor.perspective.worker.operation;

import org.meridor.perspective.beans.Instance;
import org.meridor.perspective.config.Cloud;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.function.BiFunction;
import java.util.function.Supplier;

@Component
public abstract class AbstractInstanceOperation<T> implements ConsumingOperation<Instance> {

private static final Logger LOG = LoggerFactory.getLogger(AbstractInstanceOperation.class);

@Override
public boolean perform(Cloud cloud, Supplier<Instance> supplier) {
Instance instance = supplier.get();
try {
T api = getApi(cloud, instance);
boolean success = getAction().apply(api, instance);
if (success) {
LOG.debug(getSuccessMessage(instance));
return true;
} else {
LOG.error(getErrorMessage(instance));
return false;
}
} catch (Exception e) {
LOG.error(getErrorMessage(instance), e);
return false;
}
}

protected abstract T getApi(Cloud cloud, Instance instance);

protected abstract BiFunction<T, Instance, Boolean> getAction();

protected abstract String getSuccessMessage(Instance instance);

protected abstract String getErrorMessage(Instance instance);

}

0 comments on commit 1fe1240

Please sign in to comment.