Java Code Examples for com.amazonaws.services.ec2.AmazonEC2Client#setEndpoint()

The following examples show how to use com.amazonaws.services.ec2.AmazonEC2Client#setEndpoint() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StartInstancesExample.java    From aws-mock with MIT License 6 votes vote down vote up
/**
 * Start specified instances (power-on the instances).
 *
 * @param instanceIDs
 *            IDs of the instances start
 * @return a list of state changes for the instances
 */
public static List<InstanceStateChange> startInstances(final List<String> instanceIDs) {
    // pass any credentials as aws-mock does not authenticate them at all
    AWSCredentials credentials = new BasicAWSCredentials("foo", "bar");
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);

    // the mock endpoint for ec2 which runs on your computer
    String ec2Endpoint = "http://localhost:8000/aws-mock/ec2-endpoint/";
    amazonEC2Client.setEndpoint(ec2Endpoint);

    // send the start request with args as instance IDs to start existing instances
    StartInstancesRequest request = new StartInstancesRequest();
    request.withInstanceIds(instanceIDs);
    StartInstancesResult result = amazonEC2Client.startInstances(request);

    return result.getStartingInstances();
}
 
Example 2
Source File: TerminateInstancesExample.java    From aws-mock with MIT License 6 votes vote down vote up
/**
 * Terminate specified instances (power-on the instances).
 *
 * @param instanceIDs
 *            IDs of the instances to terminate
 * @return a list of state changes for the instances
 */
public static List<InstanceStateChange> terminateInstances(final List<String> instanceIDs) {
    // pass any credentials as aws-mock does not authenticate them at all
    AWSCredentials credentials = new BasicAWSCredentials("foo", "bar");
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);

    // the mock endpoint for ec2 which runs on your computer
    String ec2Endpoint = "http://localhost:8000/aws-mock/ec2-endpoint/";
    amazonEC2Client.setEndpoint(ec2Endpoint);

    // send the terminate request with args as instance IDs
    TerminateInstancesRequest request = new TerminateInstancesRequest();
    request.withInstanceIds(instanceIDs);
    TerminateInstancesResult result = amazonEC2Client.terminateInstances(request);

    return result.getTerminatingInstances();
}
 
Example 3
Source File: StopInstancesExample.java    From aws-mock with MIT License 6 votes vote down vote up
/**
 * Stop specified instances (power-on the instances).
 *
 * @param instanceIDs
 *            IDs of the instances to stop
 * @return a list of state changes for the instances
 */
public static List<InstanceStateChange> stopInstances(final List<String> instanceIDs) {
    // pass any credentials as aws-mock does not authenticate them at all
    AWSCredentials credentials = new BasicAWSCredentials("foo", "bar");
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);

    // the mock endpoint for ec2 which runs on your computer
    String ec2Endpoint = "http://localhost:8000/aws-mock/ec2-endpoint/";
    amazonEC2Client.setEndpoint(ec2Endpoint);

    // send the stop request with args as instance IDs to stop running instances
    StopInstancesRequest request = new StopInstancesRequest();
    request.withInstanceIds(instanceIDs);
    StopInstancesResult result = amazonEC2Client.stopInstances(request);

    return result.getStoppingInstances();
}
 
Example 4
Source File: RunInstancesExample.java    From aws-mock with MIT License 6 votes vote down vote up
/**
 * Run some new ec2 instances.
 *
 * @param imageId
 *            AMI for running new instances from
 * @param runCount
 *            count of instances to run
 * @return a list of started instances
 */
public static List<Instance> runInstances(final String imageId, final int runCount) {
    // pass any credentials as aws-mock does not authenticate them at all
    AWSCredentials credentials = new BasicAWSCredentials("foo", "bar");
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);

    // the mock endpoint for ec2 which runs on your computer
    String ec2Endpoint = "http://localhost:8000/aws-mock/ec2-endpoint/";
    amazonEC2Client.setEndpoint(ec2Endpoint);

    // instance type
    String instanceType = "m1.large";

    // run 10 instances
    RunInstancesRequest request = new RunInstancesRequest();
    final int minRunCount = runCount;
    final int maxRunCount = runCount;
    request.withImageId(imageId).withInstanceType(instanceType)
            .withMinCount(minRunCount).withMaxCount(maxRunCount);
    RunInstancesResult result = amazonEC2Client.runInstances(request);

    return result.getReservation().getInstances();
}
 
Example 5
Source File: EC2Api.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
public AmazonEC2 connect(final String awsCredentialsId, final String regionName, final String endpoint) {
    final AmazonWebServicesCredentials credentials = AWSCredentialsHelper.getCredentials(awsCredentialsId, Jenkins.getInstance());
    final AmazonEC2Client client =
            credentials != null ?
                    new AmazonEC2Client(credentials) :
                    new AmazonEC2Client();

    final String effectiveEndpoint = getEndpoint(regionName, endpoint);
    if (effectiveEndpoint != null) client.setEndpoint(effectiveEndpoint);
    return client;
}
 
Example 6
Source File: InfrastructureConfiguration.java    From chaos-lemur with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnProperty("aws.accessKeyId")
AmazonEC2Client amazonEC2(@Value("${aws.accessKeyId}") String accessKeyId,
                          @Value("${aws.secretAccessKey}") String secretAccessKey,
                          @Value("${aws.region:us-east-1}") String regionName) {

    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(new BasicAWSCredentials(accessKeyId, secretAccessKey));
    Region region = Region.getRegion(Regions.fromName(regionName));
    amazonEC2Client.setEndpoint(region.getServiceEndpoint("ec2"));

    return amazonEC2Client;
}
 
Example 7
Source File: RaigadConfiguration.java    From Raigad with Apache License 2.0 5 votes vote down vote up
public GetASGName(String region, String instanceId) {
    super(NUMBER_OF_RETRIES, WAIT_TIME);
    this.region = region;
    this.instanceId = instanceId;
    client = new AmazonEC2Client(provider.getAwsCredentialProvider());
    client.setEndpoint("ec2." + region + ".amazonaws.com");
}
 
Example 8
Source File: BeanstalkConnector.java    From cloudml with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Collection<String> getEnvIPs(String envName, int timeout) {
    DescribeEnvironmentResourcesRequest request = new DescribeEnvironmentResourcesRequest()
            .withEnvironmentName(envName);
    List<Instance> instances = null;
    System.out.print("Waiting for environment ips");
    while(timeout-->0){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(BeanstalkConnector.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.print("-");
        DescribeEnvironmentResourcesResult res = beanstalkClient.describeEnvironmentResources(request);
        instances = res.getEnvironmentResources().getInstances();
        if(instances.size()==0)
            continue;
        AmazonEC2Client ec2 = new AmazonEC2Client(awsCredentials);
        ec2.setEndpoint(beanstalkEndpoint.replace("elasticbeanstalk", "ec2"));
        List<String> instanceIds = new ArrayList<String>();
        for (Instance instance : instances) {
            instanceIds.add(instance.getId());
        }
        List<String> ips = new ArrayList<String>();
        DescribeInstancesRequest desins = new DescribeInstancesRequest().withInstanceIds(instanceIds);
        DescribeInstancesResult desinres = ec2.describeInstances(desins);
        for (Reservation reservation : desinres.getReservations()) {
            for (com.amazonaws.services.ec2.model.Instance ins : reservation.getInstances()) {
                String ip = ins.getPublicIpAddress();
                if(ip!=null && ip.length()>0)
                    ips.add(ip);
            }
        }
        if(ips.size()>0)
            return ips;

    }
    return Collections.EMPTY_LIST;
}
 
Example 9
Source File: DescribeImagesExample.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Describe all available AMIs within aws-mock.
 *
 * @return a list of AMIs
 */
public static List<Image> describeAllImages() {
    // pass any credentials as aws-mock does not authenticate them at all
    AWSCredentials credentials = new BasicAWSCredentials("foo", "bar");
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);

    // the mock endpoint for ec2 which runs on your computer
    String ec2Endpoint = "http://localhost:8000/aws-mock/ec2-endpoint/";
    amazonEC2Client.setEndpoint(ec2Endpoint);

    // describe all AMIs in aws-mock.
    DescribeImagesResult result = amazonEC2Client.describeImages();

    return result.getImages();
}
 
Example 10
Source File: DescribeInstancesExample.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Describe all mock instances within aws-mock.
 *
 * @return a list of all instances
 */
public static List<Instance> describeAllInstances() {
    // pass any credentials as aws-mock does not authenticate them at all
    AWSCredentials credentials = new BasicAWSCredentials("foo", "bar");
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);

    // the mock endpoint for ec2 which runs on your computer
    String ec2Endpoint = "http://localhost:8000/aws-mock/ec2-endpoint/";
    amazonEC2Client.setEndpoint(ec2Endpoint);

    DescribeInstancesResult response = amazonEC2Client.describeInstances();
    List<Reservation> reservations = response.getReservations();

    List<Instance> ret = new ArrayList<Instance>();

    for (Reservation reservation : reservations) {
        List<Instance> instances = reservation.getInstances();

        if (null != instances) {

            for (Instance i : instances) {
                ret.add(i);
            }
        }
    }

    return ret;
}
 
Example 11
Source File: DescribeInstancesExample.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Describe specified instances within aws-mock.
 *
 * @param instanceIDs
 *            a list of instance IDs to describe
 * @return a list of specified instances
 */
public static List<Instance> describeInstances(final List<String> instanceIDs) {
    // pass any credentials as aws-mock does not authenticate them at all
    AWSCredentials credentials = new BasicAWSCredentials("foo", "bar");
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);

    // the mock endpoint for ec2 which runs on your computer
    String ec2Endpoint = "http://localhost:8000/aws-mock/ec2-endpoint/";
    amazonEC2Client.setEndpoint(ec2Endpoint);

    DescribeInstancesRequest request = new DescribeInstancesRequest();
    request.withInstanceIds(instanceIDs);

    DescribeInstancesResult response = amazonEC2Client.describeInstances(request);
    List<Reservation> reservations = response.getReservations();

    List<Instance> ret = new ArrayList<Instance>();

    for (Reservation reservation : reservations) {
        List<Instance> instances = reservation.getInstances();

        if (null != instances) {

            for (Instance i : instances) {
                ret.add(i);
            }
        }
    }

    return ret;
}
 
Example 12
Source File: AWSDatabaseHolder.java    From billow with Apache License 2.0 4 votes vote down vote up
public AWSDatabaseHolder(Config config) {
    maxAgeInMs = config.getDuration("maxAge", TimeUnit.MILLISECONDS);

    final DefaultAWSCredentialsProviderChain awsCredentialsProviderChain = new DefaultAWSCredentialsProviderChain();

    final ClientConfiguration clientConfig = new ClientConfiguration();
    clientConfig.setRetryPolicy(new RetryPolicy(null, null, config.getInt("maxErrorRetry"), true));
    clientConfig.setSocketTimeout(config.getInt("socketTimeout") * 1000);

    final AmazonEC2 bootstrapEC2Client = AmazonEC2ClientBuilder.standard().withCredentials(awsCredentialsProviderChain).build();

    ec2Clients = Maps.newHashMap();
    rdsClients = Maps.newHashMap();
    sqsClients = Maps.newHashMap();
    dynamoDBClients = Maps.newHashMap();
    elasticacheClients = Maps.newHashMap();
    elasticsearchClients = Maps.newHashMap();

    final List<Region> ec2Regions = bootstrapEC2Client.describeRegions().getRegions();
    for (Region region : ec2Regions) {
        final String regionName = region.getRegionName();
        final String endpoint = region.getEndpoint();
        log.debug("Adding ec2 region {}", region);

        if (config.getBoolean("ec2Enabled")) {
            final AmazonEC2Client ec2Client = new AmazonEC2Client(awsCredentialsProviderChain, clientConfig);
            ec2Client.setEndpoint(endpoint);
            ec2Clients.put(regionName, ec2Client);
        }

        if (config.getBoolean("rdsEnabled")) {
            final AmazonRDSClient rdsClient = new AmazonRDSClient(awsCredentialsProviderChain, clientConfig);
            rdsClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "rds."));
            rdsClients.put(regionName, rdsClient);
        }

        if (config.getBoolean("dynamodbEnabled")) {
            final AmazonDynamoDBClient dynamoDBClient =
                new AmazonDynamoDBClient(awsCredentialsProviderChain, clientConfig);
            dynamoDBClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "dynamodb."));
            dynamoDBClients.put(regionName, dynamoDBClient);
        }

        if (config.getBoolean("sqsEnabled")) {
            final AmazonSQSClient sqsClient = new AmazonSQSClient(awsCredentialsProviderChain, clientConfig);
            sqsClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "sqs."));
            sqsClients.put(regionName, sqsClient);
        }

        if (config.getBoolean("elasticacheEnabled")) {
            final AmazonElastiCacheClient elastiCacheClient = new AmazonElastiCacheClient
                (awsCredentialsProviderChain, clientConfig);
            elastiCacheClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "elasticache."));
            elasticacheClients.put(regionName, elastiCacheClient);
        }

        if (config.getBoolean("elasticsearchEnabled")) {
            final AWSElasticsearchClient elasticsearchClient = new AWSElasticsearchClient
                (awsCredentialsProviderChain, clientConfig);
            elasticsearchClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "es."));
            elasticsearchClients.put(regionName, elasticsearchClient);
        }
    }

    this.iamClient = AmazonIdentityManagementClientBuilder.standard()
        .withCredentials(awsCredentialsProviderChain)
        .withClientConfiguration(clientConfig)
        .build();

    if (config.hasPath("accountNumber")) {
        this.awsAccountNumber = config.getString("accountNumber");
    } else {
        this.awsAccountNumber = null;
    }

    if (config.hasPath("arnPartition")) {
        this.awsARNPartition = config.getString("arnPartition");
    } else {
        this.awsARNPartition = "aws";
    }

    rebuild();
}