Skip to content

Commit

Permalink
Change to use DefaultAwsRegionProviderChain for region instead of CLI…
Browse files Browse the repository at this point in the history
… flag
  • Loading branch information
cartwrightian committed Oct 16, 2017
1 parent 908c632 commit 4c4225b
Show file tree
Hide file tree
Showing 43 changed files with 327 additions and 441 deletions.
13 changes: 5 additions & 8 deletions src/tw/com/AwsFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import tw.com.entity.*;
import tw.com.exceptions.*;
import tw.com.parameters.*;
import tw.com.providers.SavesFile;
import tw.com.providers.IdentityProvider;
import tw.com.providers.NotificationSender;
import tw.com.providers.ProvidesCurrentIp;
import tw.com.providers.SavesFile;
import tw.com.repository.CloudFormRepository;
import tw.com.repository.CloudRepository;
import tw.com.repository.ELBRepository;
Expand Down Expand Up @@ -62,19 +62,16 @@ public class AwsFacade implements ProvidesZones {
private MonitorStackEvents monitor;
private IdentityProvider identityProvider;

private String regionName;

public AwsFacade(MonitorStackEvents monitor, CloudFormRepository cfnRepository, VpcRepository vpcRepository,
ELBRepository elbRepository, CloudRepository cloudRepository, NotificationSender notificationSender,
IdentityProvider identityProvider, String regionName) {
public AwsFacade(MonitorStackEvents monitor, CloudFormRepository cfnRepository, VpcRepository vpcRepository,
ELBRepository elbRepository, CloudRepository cloudRepository, NotificationSender notificationSender,
IdentityProvider identityProvider) {
this.monitor = monitor;
this.cfnRepository = cfnRepository;
this.vpcRepository = vpcRepository;
this.elbRepository = elbRepository;
this.cloudRepository = cloudRepository;
this.notificationSender = notificationSender;
this.identityProvider = identityProvider;
this.regionName = regionName;
}

public List<TemplateParameter> validateTemplate(String templateBody) {
Expand Down Expand Up @@ -592,7 +589,7 @@ public List<InstanceSummary> listInstances(SearchCriteria searchCriteria) throws

@Override
public Map<String, AvailabilityZone> getZones() {
return cloudRepository.getZones(regionName);
return cloudRepository.getZones();
}

public KeyPair createKeyPair(ProjectAndEnv projAndEnv, SavesFile destination, String filename) throws CfnAssistException {
Expand Down
87 changes: 40 additions & 47 deletions src/tw/com/FacadeFactory.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package tw.com;

import com.amazonaws.auth.AWSCredentialsProviderChain;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.regions.Region;
import com.amazonaws.services.cloudformation.AmazonCloudFormationClient;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingClient;
import com.amazonaws.services.identitymanagement.AmazonIdentityManagementClient;
import com.amazonaws.services.rds.AmazonRDSClient;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.regions.AwsRegionProvider;
import com.amazonaws.regions.DefaultAwsRegionProviderChain;
import com.amazonaws.services.cloudformation.AmazonCloudFormation;
import com.amazonaws.services.cloudformation.AmazonCloudFormationClientBuilder;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancing;
import com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingClientBuilder;
import com.amazonaws.services.identitymanagement.AmazonIdentityManagement;
import com.amazonaws.services.identitymanagement.AmazonIdentityManagementClientBuilder;
import com.amazonaws.services.rds.AmazonRDS;
import com.amazonaws.services.rds.AmazonRDSClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClientBuilder;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
import org.apache.commons.cli.MissingArgumentException;
import tw.com.commandline.CommandExecutor;
import tw.com.entity.ProjectAndEnv;
Expand All @@ -25,21 +32,18 @@

public class FacadeFactory {
private boolean snsMonitoring = false;
private Region region;
private String project;

private boolean init;

// amazon apis
private AWSCredentialsProviderChain credentialsProvider;
private AmazonCloudFormationClient cfnClient;
private AmazonSQSClient sqsClient;
private AmazonSNSClient snsClient;
private AmazonS3Client s3Client;
private AmazonEC2Client ec2Client;
private AmazonElasticLoadBalancingClient elbClient;
private AmazonRDSClient rdsClient;
private AmazonIdentityManagementClient iamClient;
private AmazonCloudFormation cfnClient;
private AmazonSQS sqsClient;
private AmazonSNS snsClient;
private AmazonS3 s3Client;
private AmazonEC2 ec2Client;
private AmazonElasticLoadBalancing elbClient;
private AmazonRDS rdsClient;
private AmazonIdentityManagement iamClient;

// providers
private ArtifactUploader artifactUploader;
Expand All @@ -62,13 +66,8 @@ public class FacadeFactory {
private IdentityProvider identityProvider;

public FacadeFactory() {
credentialsProvider = new DefaultAWSCredentialsProviderChain();
}

public void setRegion(Region awsRegion) {
this.region = awsRegion;
}


public void setProject(String project) {
this.project = project;
}
Expand All @@ -88,7 +87,8 @@ private void init() {

private void createProviders() {
loadBalancerClient = new LoadBalancerClient(elbClient);
cloudClient = new CloudClient(ec2Client);
AwsRegionProvider regionProvider = new DefaultAwsRegionProviderChain();
cloudClient = new CloudClient(ec2Client, regionProvider);
formationClient = new CloudFormationClient(cfnClient);
datastoreClient = new RDSClient(rdsClient);
notificationSender = new SNSNotificationSender(snsClient);
Expand All @@ -103,22 +103,14 @@ private void createRepo() {
}

private void createAmazonAPIClients() {
cfnClient = new AmazonCloudFormationClient(credentialsProvider);
cfnClient.setRegion(region);
ec2Client = new AmazonEC2Client(credentialsProvider);
ec2Client.setRegion(region);
snsClient = new AmazonSNSClient(credentialsProvider);
snsClient.setRegion(region);
sqsClient = new AmazonSQSClient(credentialsProvider);
sqsClient.setRegion(region);
elbClient = new AmazonElasticLoadBalancingClient(credentialsProvider);
elbClient.setRegion(region);
s3Client = new AmazonS3Client(credentialsProvider);
s3Client.setRegion(region);
rdsClient = new AmazonRDSClient(credentialsProvider);
rdsClient.setRegion(region);
iamClient = new AmazonIdentityManagementClient(credentialsProvider);
iamClient.setRegion(region);
cfnClient = AmazonCloudFormationClientBuilder.defaultClient();
ec2Client = AmazonEC2ClientBuilder.defaultClient();
snsClient = AmazonSNSClientBuilder.defaultClient();
sqsClient = AmazonSQSClientBuilder.defaultClient();
elbClient = AmazonElasticLoadBalancingClientBuilder.defaultClient();
s3Client = AmazonS3ClientBuilder.defaultClient();
rdsClient = AmazonRDSClientBuilder.defaultClient();
iamClient = AmazonIdentityManagementClientBuilder.defaultClient();
}

public AwsFacade createFacade() throws MissingArgumentException, CfnAssistException, InterruptedException {
Expand All @@ -133,8 +125,9 @@ public AwsFacade createFacade() throws MissingArgumentException, CfnAssistExcept
}

monitor.init();
awsFacade = new AwsFacade(monitor, cfnRepository, vpcRepository, elbRepository,
cloudRepository, notificationSender, identityProvider, region.getName());
AwsRegionProvider regionProvider = new DefaultAwsRegionProviderChain();
awsFacade = new AwsFacade(monitor, cfnRepository, vpcRepository, elbRepository,
cloudRepository, notificationSender, identityProvider);
}
return awsFacade;
}
Expand Down
25 changes: 8 additions & 17 deletions src/tw/com/ant/CfnAssistAntTask.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package tw.com.ant;

import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.cloudformation.model.Parameter;
import org.apache.commons.cli.MissingArgumentException;
import org.apache.tools.ant.BuildException;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.RegionUtils;
import com.amazonaws.services.cloudformation.model.Parameter;

import tw.com.FacadeFactory;
import tw.com.commandline.CommandLineException;
import tw.com.entity.ProjectAndEnv;
import tw.com.exceptions.CfnAssistException;

import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class CfnAssistAntTask extends org.apache.tools.ant.Task {

private String awsRegion;
private String cfnProject;
private Integer cfnBuildNumber = null;
private String cfnEnv;
Expand All @@ -36,11 +33,7 @@ public CfnAssistAntTask() {
artifactParams = new LinkedList<>();
actionElements = new LinkedList<>();
}

public void setRegion(String awsRegion) {
this.awsRegion = awsRegion;
}


public void setProject(String cfnProject) {
this.cfnProject = cfnProject;
}
Expand Down Expand Up @@ -126,13 +119,11 @@ public void execute() {
if (capabilityIAM) {
projectAndEnv.setUseCapabilityIAM();
}
Region region = RegionUtils.getRegion(awsRegion);

Collection<Parameter> cfnParameters = createParameterList();
Collection<Parameter> artifacts = createArtifactList();

FacadeFactory factory = new FacadeFactory();
factory.setRegion(region);
factory.setProject(cfnProject);
if (snsMonitoring) {
factory.setSNSMonitoring();
Expand Down
9 changes: 0 additions & 9 deletions src/tw/com/commandline/CommandFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class CommandFlags {

private Option projectParam;
private Option envParam;
private Option regionParam;
private Option buildNumberParam;
private Option keysValuesParam;
private Option snsParam;
Expand All @@ -33,7 +32,6 @@ public class CommandFlags {

private String project;
private String env;
private String region;
private Integer buildNumber = null;
private Boolean sns;
private Boolean capabilityIAM;
Expand All @@ -52,7 +50,6 @@ public CommandFlags(String executableName, Options commandLineOptions) {
public void addOptions() {
commandLineOptions.addOption(projectParam);
commandLineOptions.addOption(envParam);
commandLineOptions.addOption(regionParam);
commandLineOptions.addOption(keysValuesParam);
commandLineOptions.addOption(buildNumberParam);
commandLineOptions.addOption(snsParam);
Expand All @@ -65,7 +62,6 @@ public void addOptions() {
public void populateFlags(CommandLine commandLine, HelpFormatter formatter) throws MissingArgumentException, InvalidStackParameterException {
project = checkForArgument(commandLine, formatter, projectParam, AwsFacade.PROJECT_TAG, false);
env = checkForArgument(commandLine, formatter, envParam, AwsFacade.ENVIRONMENT_TAG, false);
region = checkForArgument(commandLine, formatter, regionParam, Main.ENV_VAR_EC2_REGION, true);
String buildNumberAsString = checkForArgument(commandLine, formatter, buildNumberParam, AwsFacade.BUILD_TAG, false);
if (!buildNumberAsString.isEmpty()) {
buildNumber = Integer.parseInt(buildNumberAsString);
Expand All @@ -83,7 +79,6 @@ public void populateFlags(CommandLine commandLine, HelpFormatter formatter) thro
private void createOptions() {
projectParam = createParam("project", "Name of the cfnassist project, or use env var: " + AwsFacade.PROJECT_TAG);
envParam = createParam("env", "Name of cfnassit environment, or use env var: " + AwsFacade.ENVIRONMENT_TAG);
regionParam = createParam("region", "AWS Region name, or use env var: " + Main.ENV_VAR_EC2_REGION);

keysValuesParam = createParamMultiArgs("parameters",
"Provide paramters for cfn scripts, format as per cfn commandline tools");
Expand Down Expand Up @@ -197,10 +192,6 @@ public String getEnv() {
return env;
}

public String getRegion() {
return region;
}

public Integer getBuildNumber() {
return buildNumber;
}
Expand Down
31 changes: 15 additions & 16 deletions src/tw/com/commandline/Main.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package tw.com.commandline;

import com.amazonaws.regions.Region;
import com.amazonaws.regions.RegionUtils;
import com.amazonaws.regions.DefaultAwsRegionProviderChain;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.cloudformation.model.Parameter;
import org.apache.commons.cli.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tw.com.FacadeFactory;
import tw.com.entity.ProjectAndEnv;
import tw.com.exceptions.*;
import tw.com.exceptions.CfnAssistException;

import java.io.IOException;
import java.util.Collection;
Expand Down Expand Up @@ -54,10 +54,7 @@ public int parse(FacadeFactory factory, boolean act) {
flags.populateFlags(commandLine, formatter);
CommandLineAction action = commandActions.selectCorrectActionFromArgs(commandLine, formatter,
executableName, commandLineOptions );

Region awsRegion = populateRegion(flags.getRegion());
logger.info("Region set to " + awsRegion);


ProjectAndEnv projectAndEnv = new ProjectAndEnv(flags.getProject(), flags.getEnv());
if (flags.haveBuildNumber()) {
projectAndEnv.addBuildNumber(flags.getBuildNumber());
Expand All @@ -81,7 +78,6 @@ public int parse(FacadeFactory factory, boolean act) {
Collection<Parameter> artifacts = flags.getUploadParams();
action.validate(projectAndEnv, flags.getAdditionalParameters(), artifacts, argsForAction);

factory.setRegion(awsRegion);
setFactoryOptionsForAction(factory, action);

Collection<Parameter> additionalParams = flags.getAdditionalParameters();
Expand Down Expand Up @@ -119,15 +115,18 @@ private void setFactoryOptionsForAction(FacadeFactory factory, CommandLineAction
}
}

private Region populateRegion(String regionName) throws MissingArgumentException {
// TODO CHANGE TO USE DEFAULT REGION PROVIDERS BUILT INTO THE SDK
private Regions populateRegion(String regionName) throws MissingArgumentException {
logger.info("Check for region using name "+regionName);
Region result = RegionUtils.getRegion(regionName);
if (result==null) {
String msg = "Unable for find region for " + regionName;
logger.error(msg);
throw new MissingArgumentException(msg);
}
return result;
try {
Regions result = Regions.fromName(regionName);
return result;
}
catch(IllegalArgumentException noSuchRegion) {
String msg = "Unable for find region for " + regionName;
logger.error(msg);
throw new MissingArgumentException(msg);
}
}

}
2 changes: 2 additions & 0 deletions src/tw/com/parameters/ProvidesZones.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tw.com.parameters;

import com.amazonaws.regions.AwsRegionProvider;
import com.amazonaws.regions.DefaultAwsRegionProviderChain;
import com.amazonaws.services.ec2.model.AvailabilityZone;

import java.util.Map;
Expand Down
Loading

0 comments on commit 4c4225b

Please sign in to comment.