diff --git a/perspective-aws/src/main/java/org/meridor/perspective/aws/Api.java b/perspective-aws/src/main/java/org/meridor/perspective/aws/Api.java new file mode 100644 index 00000000..84e523d1 --- /dev/null +++ b/perspective-aws/src/main/java/org/meridor/perspective/aws/Api.java @@ -0,0 +1,4 @@ +package org.meridor.perspective.aws; + +public interface Api { +} diff --git a/perspective-aws/src/main/java/org/meridor/perspective/aws/ApiProvider.java b/perspective-aws/src/main/java/org/meridor/perspective/aws/ApiProvider.java new file mode 100644 index 00000000..6a4a5690 --- /dev/null +++ b/perspective-aws/src/main/java/org/meridor/perspective/aws/ApiProvider.java @@ -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 action) throws Exception; + +} diff --git a/perspective-aws/src/main/java/org/meridor/perspective/aws/ApiProviderImpl.java b/perspective-aws/src/main/java/org/meridor/perspective/aws/ApiProviderImpl.java new file mode 100644 index 00000000..5761e4f8 --- /dev/null +++ b/perspective-aws/src/main/java/org/meridor/perspective/aws/ApiProviderImpl.java @@ -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 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(); + } + } +} diff --git a/perspective-aws/src/main/java/org/meridor/perspective/aws/BaseInstanceOperation.java b/perspective-aws/src/main/java/org/meridor/perspective/aws/BaseInstanceOperation.java new file mode 100644 index 00000000..a5aab99a --- /dev/null +++ b/perspective-aws/src/main/java/org/meridor/perspective/aws/BaseInstanceOperation.java @@ -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 { + + @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); + } + +} diff --git a/perspective-digital-ocean/src/main/java/org/meridor/perspective/digitalocean/BaseInstanceOperation.java b/perspective-digital-ocean/src/main/java/org/meridor/perspective/digitalocean/BaseInstanceOperation.java index 9fea722d..7a059e77 100644 --- a/perspective-digital-ocean/src/main/java/org/meridor/perspective/digitalocean/BaseInstanceOperation.java +++ b/perspective-digital-ocean/src/main/java/org/meridor/perspective/digitalocean/BaseInstanceOperation.java @@ -2,49 +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 { - - private static final Logger LOG = LoggerFactory.getLogger(BaseInstanceOperation.class); +public abstract class BaseInstanceOperation extends AbstractInstanceOperation { @Autowired private ApiProvider apiProvider; @Override - public boolean perform(Cloud cloud, Supplier supplier) { - Instance instance = supplier.get(); - if (instance.getRealId() == null) { - return false; - } - 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 getAction(); - - protected abstract String getSuccessMessage(Instance instance); - - protected abstract String getErrorMessage(Instance instance); - } diff --git a/perspective-openstack/src/main/java/org/meridor/perspective/openstack/BaseInstanceOperation.java b/perspective-openstack/src/main/java/org/meridor/perspective/openstack/BaseInstanceOperation.java index 663ff502..7e1a0b68 100644 --- a/perspective-openstack/src/main/java/org/meridor/perspective/openstack/BaseInstanceOperation.java +++ b/perspective-openstack/src/main/java/org/meridor/perspective/openstack/BaseInstanceOperation.java @@ -5,7 +5,7 @@ import org.meridor.perspective.beans.MetadataKey; import org.meridor.perspective.beans.Project; import org.meridor.perspective.config.Cloud; -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; @@ -15,7 +15,7 @@ import java.util.function.Supplier; @Component -public abstract class BaseInstanceOperation implements ConsumingOperation { +public abstract class BaseInstanceOperation extends AbstractInstanceOperation { private static final Logger LOG = LoggerFactory.getLogger(BaseInstanceOperation.class); diff --git a/perspective-worker/src/main/java/org/meridor/perspective/worker/operation/AbstractInstanceOperation.java b/perspective-worker/src/main/java/org/meridor/perspective/worker/operation/AbstractInstanceOperation.java new file mode 100644 index 00000000..ee42d07f --- /dev/null +++ b/perspective-worker/src/main/java/org/meridor/perspective/worker/operation/AbstractInstanceOperation.java @@ -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 implements ConsumingOperation { + + private static final Logger LOG = LoggerFactory.getLogger(AbstractInstanceOperation.class); + + @Override + public boolean perform(Cloud cloud, Supplier 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 getAction(); + + protected abstract String getSuccessMessage(Instance instance); + + protected abstract String getErrorMessage(Instance instance); + +}