-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored BaseInstanceOperation usage (related to #191)
- Loading branch information
1 parent
9c0034c
commit 95596df
Showing
7 changed files
with
138 additions
and
36 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
perspective-aws/src/main/java/org/meridor/perspective/aws/Api.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package org.meridor.perspective.aws; | ||
|
||
public interface Api { | ||
} |
14 changes: 14 additions & 0 deletions
14
perspective-aws/src/main/java/org/meridor/perspective/aws/ApiProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
perspective-aws/src/main/java/org/meridor/perspective/aws/ApiProviderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
perspective-aws/src/main/java/org/meridor/perspective/aws/BaseInstanceOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ker/src/main/java/org/meridor/perspective/worker/operation/AbstractInstanceOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |