Example usage for com.amazonaws.services.ec2 AmazonEC2 setEndpoint

List of usage examples for com.amazonaws.services.ec2 AmazonEC2 setEndpoint

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2 AmazonEC2 setEndpoint.

Prototype

@Deprecated
void setEndpoint(String endpoint);

Source Link

Document

Overrides the default endpoint for this client ("https://ec2.us-east-1.amazonaws.com").

Usage

From source file:c3.ops.priam.aws.AWSMembership.java

License:Apache License

protected AmazonEC2 getEc2Client() {
    AmazonEC2 client = new AmazonEC2Client(provider.getAwsCredentialProvider());
    client.setEndpoint("ec2." + config.getDC() + ".amazonaws.com");
    return client;
}

From source file:c3.ops.priam.defaultimpl.PriamConfiguration.java

License:Apache License

/**
 * Get the fist 3 available zones in the region
 *//*from w w w.j  av a  2 s  .c o  m*/
public void setDefaultRACList(String region) {
    AmazonEC2 client = new AmazonEC2Client(provider.getAwsCredentialProvider());
    client.setEndpoint("ec2." + region + ".amazonaws.com");
    DescribeAvailabilityZonesResult res = client.describeAvailabilityZones();
    List<String> zone = Lists.newArrayList();
    for (AvailabilityZone reg : res.getAvailabilityZones()) {
        if (reg.getState().equals("available"))
            zone.add(reg.getZoneName());
        if (zone.size() == 3)
            break;
    }
    //        DEFAULT_AVAILABILITY_ZONES =  StringUtils.join(zone, ",");
    DEFAULT_AVAILABILITY_ZONES = ImmutableList.copyOf(zone);
}

From source file:com.appdynamics.connectors.AWSConnector.java

License:Apache License

private AmazonEC2 getConnector(IImage image, IProperty[] properties, IControllerServices controllerServices) {
    AmazonEC2 connector = ConnectorLocator.getInstance().getConnector(properties, controllerServices);

    if (image == null) {
        return connector;
    }//from w ww.  j  ava 2 s .co  m

    String region = Utils.getRegion(image.getProperties(), controllerServices);

    if (!Strings.isNullOrEmpty(region)) {
        region = region.toLowerCase();

        connector.setEndpoint(regionVsURLs.get(region));
    }

    return connector;
}

From source file:com.carrotgarden.maven.aws.ecc.CarrotElasticCompute.java

License:BSD License

private AmazonEC2 newClient() {

    final AmazonEC2 amazonClient = new AmazonEC2Client(credentials);

    amazonClient.setEndpoint(endpoint);

    return amazonClient;

}

From source file:com.dowdandassociates.gentoo.bootstrap.DefaultAmazonEC2Provider.java

License:Apache License

public AmazonEC2 get() {
    AmazonEC2 ec2Client = new AmazonEC2Client();
    ec2Client.setEndpoint(endpoint.get());
    return ec2Client;
}

From source file:com.ec2box.manage.action.EC2KeyAction.java

License:Apache License

/**
 * returns keypairs as a json string/*w ww .j  ava 2s.  c  o m*/
 */
@Action(value = "/manage/getKeyPairJSON")
public String getKeyPairJSON() {

    AWSCred awsCred = AWSCredDB.getAWSCred(ec2Key.getAwsCredId());

    //set  AWS credentials for service
    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(),
            awsCred.getSecretKey());
    AmazonEC2 service = new AmazonEC2Client(awsCredentials, AWSClientConfig.getClientConfig());

    service.setEndpoint(ec2Key.getEc2Region());

    DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();

    DescribeKeyPairsResult describeKeyPairsResult = service.describeKeyPairs(describeKeyPairsRequest);

    List<KeyPairInfo> keyPairInfoList = describeKeyPairsResult.getKeyPairs();
    String json = new Gson().toJson(keyPairInfoList);
    try {
        servletResponse.getOutputStream().write(json.getBytes());
    } catch (Exception ex) {
        log.error(ex.toString(), ex);
    }
    return null;
}

From source file:com.ec2box.manage.action.EC2KeyAction.java

License:Apache License

@Action(value = "/manage/submitEC2Key", results = {
        @Result(name = "input", location = "/manage/view_ec2_keys.jsp"),
        @Result(name = "success", location = "/manage/viewEC2Keys.action", type = "redirect") })
public String submitEC2Key() {

    String retVal = SUCCESS;/* w ww  .j  av a2 s.c o  m*/

    try {

        //get AWS credentials from DB
        AWSCred awsCred = AWSCredDB.getAWSCred(ec2Key.getAwsCredId());

        //set  AWS credentials for service
        BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(),
                awsCred.getSecretKey());

        //create service
        AmazonEC2 service = new AmazonEC2Client(awsCredentials, AWSClientConfig.getClientConfig());
        service.setEndpoint(ec2Key.getEc2Region());

        //create key pair request
        CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest();
        createKeyPairRequest.withKeyName(ec2Key.getKeyNm());

        //call service
        CreateKeyPairResult createKeyPairResult = service.createKeyPair(createKeyPairRequest);
        //get key pair result
        KeyPair keyPair = createKeyPairResult.getKeyPair();

        //set private key
        String privateKey = keyPair.getKeyMaterial();
        ec2Key.setPrivateKey(privateKey);

        //add to db
        EC2KeyDB.saveEC2Key(ec2Key);

    } catch (AmazonServiceException ex) {
        addActionError(ex.getMessage());
        retVal = INPUT;
    }

    return retVal;

}

From source file:com.ec2box.manage.action.EC2KeyAction.java

License:Apache License

@Action(value = "/manage/importEC2Key", results = {
        @Result(name = "input", location = "/manage/view_ec2_keys.jsp"),
        @Result(name = "success", location = "/manage/viewEC2Keys.action", type = "redirect") })
public String importEC2Key() {

    String retVal = SUCCESS;/*from  ww w .  ja va  2  s  .co  m*/

    try {
        //get AWS credentials from DB
        AWSCred awsCred = AWSCredDB.getAWSCred(ec2Key.getAwsCredId());

        //set  AWS credentials for service
        BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(),
                awsCred.getSecretKey());

        //create service
        AmazonEC2 service = new AmazonEC2Client(awsCredentials, AWSClientConfig.getClientConfig());
        service.setEndpoint(ec2Key.getEc2Region());

        //describe key pair request
        DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
        describeKeyPairsRequest.setKeyNames(Arrays.asList(ec2Key.getKeyNm()));

        //call service
        DescribeKeyPairsResult describeKeyPairsResult = service.describeKeyPairs(describeKeyPairsRequest);

        if (describeKeyPairsResult != null && describeKeyPairsResult.getKeyPairs().size() > 0) {
            //add to db
            EC2KeyDB.saveEC2Key(ec2Key);
        } else {
            addActionError("Imported key does not exist on AWS");
            retVal = INPUT;
        }

    } catch (AmazonServiceException ex) {
        addActionError(ex.getMessage());
        retVal = INPUT;

    }

    return retVal;

}

From source file:com.ec2box.manage.action.SystemAction.java

License:Apache License

@Action(value = "/admin/viewSystems", results = {
        @Result(name = "success", location = "/admin/view_systems.jsp") })
public String viewSystems() {

    Long userId = AuthUtil.getUserId(servletRequest.getSession());
    String userType = AuthUtil.getUserType(servletRequest.getSession());

    List<String> ec2RegionList = EC2KeyDB.getEC2Regions();
    List<String> instanceIdList = new ArrayList<String>();

    //default instance state
    if (sortedSet.getFilterMap().get(FILTER_BY_INSTANCE_STATE) == null) {
        sortedSet.getFilterMap().put(FILTER_BY_INSTANCE_STATE, AppConfig.getProperty("defaultInstanceState"));
    }//from  w  w  w .  j a  v  a  2s  .com

    try {
        Map<String, HostSystem> hostSystemList = new HashMap<String, HostSystem>();

        //if user profile has been set or user is a manager
        List<Profile> profileList = UserProfileDB.getProfilesByUser(userId);
        if (profileList.size() > 0 || Auth.MANAGER.equals(userType)) {
            //set tags for profile
            List<String> profileTags = new ArrayList<>();
            for (Profile profile : profileList) {
                profileTags.add(profile.getTag());
            }
            Map<String, List<String>> profileTagMap = parseTags(profileTags);

            //set tags from input filters
            Map<String, List<String>> filterTags = fetchInputFilterTags(userType, profileTagMap);

            //parse out security group list in format group[,group]
            List<String> securityGroupList = new ArrayList<>();
            if (StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_SECURITY_GROUP))) {
                securityGroupList = Arrays
                        .asList(sortedSet.getFilterMap().get(FILTER_BY_SECURITY_GROUP).split(","));
            }

            //get AWS credentials from DB
            for (AWSCred awsCred : AWSCredDB.getAWSCredList()) {

                if (awsCred != null) {
                    //set  AWS credentials for service
                    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(),
                            awsCred.getSecretKey());

                    for (String ec2Region : ec2RegionList) {
                        //create service

                        AmazonEC2 service = new AmazonEC2Client(awsCredentials,
                                AWSClientConfig.getClientConfig());
                        service.setEndpoint(ec2Region);

                        //only return systems that have keys set
                        List<String> keyValueList = new ArrayList<String>();
                        for (EC2Key ec2Key : EC2KeyDB.getEC2KeyByRegion(ec2Region, awsCred.getId())) {
                            keyValueList.add(ec2Key.getKeyNm());
                        }

                        DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();

                        Filter keyNmFilter = new Filter("key-name", keyValueList);
                        describeInstancesRequest.withFilters(keyNmFilter);

                        //instance state filter
                        if (StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_INSTANCE_STATE))) {
                            List<String> instanceStateList = new ArrayList<String>();
                            instanceStateList.add(sortedSet.getFilterMap().get(FILTER_BY_INSTANCE_STATE));
                            Filter instanceStateFilter = new Filter("instance-state-name", instanceStateList);
                            describeInstancesRequest.withFilters(instanceStateFilter);
                        }

                        if (securityGroupList.size() > 0) {
                            Filter groupFilter = new Filter("group-name", securityGroupList);
                            describeInstancesRequest.withFilters(groupFilter);
                        }
                        //set name value pair for tag filter
                        List<String> tagList = new ArrayList<String>();

                        //always add all profile tags to filter list
                        addTagsToDescribeInstanceRequest(profileTagMap, describeInstancesRequest, tagList);

                        //add all additional filter tags provided by the user
                        addTagsToDescribeInstanceRequest(filterTags, describeInstancesRequest, tagList);

                        if (tagList.size() > 0) {
                            Filter tagFilter = new Filter("tag-key", tagList);
                            describeInstancesRequest.withFilters(tagFilter);
                        }

                        DescribeInstancesResult describeInstancesResult = service
                                .describeInstances(describeInstancesRequest);

                        for (Reservation res : describeInstancesResult.getReservations()) {
                            for (Instance instance : res.getInstances()) {

                                HostSystem hostSystem = new HostSystem();
                                hostSystem.setInstance(instance.getInstanceId());

                                //check for public dns if doesn't exist set to ip or pvt dns
                                if (!"true".equals(AppConfig.getProperty("useEC2PvtDNS"))
                                        && StringUtils.isNotEmpty(instance.getPublicDnsName())) {
                                    hostSystem.setHost(instance.getPublicDnsName());
                                } else if (!"true".equals(AppConfig.getProperty("useEC2PvtDNS"))
                                        && StringUtils.isNotEmpty(instance.getPublicIpAddress())) {
                                    hostSystem.setHost(instance.getPublicIpAddress());
                                } else if (StringUtils.isNotEmpty(instance.getPrivateDnsName())) {
                                    hostSystem.setHost(instance.getPrivateDnsName());
                                } else {
                                    hostSystem.setHost(instance.getPrivateIpAddress());
                                }

                                hostSystem.setKeyId(EC2KeyDB
                                        .getEC2KeyByNmRegion(instance.getKeyName(), ec2Region, awsCred.getId())
                                        .getId());
                                hostSystem.setEc2Region(ec2Region);
                                hostSystem.setState(instance.getState().getName());
                                for (Tag tag : instance.getTags()) {
                                    if ("Name".equals(tag.getKey())) {
                                        hostSystem.setDisplayNm(tag.getValue());
                                    }
                                }
                                instanceIdList.add(hostSystem.getInstance());
                                hostSystemList.put(hostSystem.getInstance(), hostSystem);
                            }
                        }

                        if (instanceIdList.size() > 0) {
                            //set instance id list to check permissions when creating sessions
                            servletRequest.getSession().setAttribute("instanceIdList",
                                    new ArrayList<String>(instanceIdList));
                            if (showStatus) {
                                //make service call 100 instances at a time b/c of AWS limitation
                                int i = 0;
                                List<String> idCallList = new ArrayList<String>();
                                while (!instanceIdList.isEmpty()) {
                                    idCallList.add(instanceIdList.remove(0));
                                    i++;
                                    //when i eq 100 make call
                                    if (i >= 100 || instanceIdList.isEmpty()) {

                                        //get status for host systems
                                        DescribeInstanceStatusRequest describeInstanceStatusRequest = new DescribeInstanceStatusRequest();
                                        describeInstanceStatusRequest.withInstanceIds(idCallList);
                                        DescribeInstanceStatusResult describeInstanceStatusResult = service
                                                .describeInstanceStatus(describeInstanceStatusRequest);

                                        for (InstanceStatus instanceStatus : describeInstanceStatusResult
                                                .getInstanceStatuses()) {

                                            HostSystem hostSystem = hostSystemList
                                                    .remove(instanceStatus.getInstanceId());
                                            hostSystem.setSystemStatus(
                                                    instanceStatus.getSystemStatus().getStatus());
                                            hostSystem.setInstanceStatus(
                                                    instanceStatus.getInstanceStatus().getStatus());

                                            //check and filter by instance or system status
                                            if ((StringUtils.isEmpty(
                                                    sortedSet.getFilterMap().get(FILTER_BY_INSTANCE_STATUS))
                                                    && StringUtils.isEmpty(sortedSet.getFilterMap()
                                                            .get(FILTER_BY_SYSTEM_STATUS)))
                                                    || (hostSystem.getInstanceStatus()
                                                            .equals(sortedSet.getFilterMap()
                                                                    .get(FILTER_BY_INSTANCE_STATUS))
                                                            && StringUtils.isEmpty(sortedSet.getFilterMap()
                                                                    .get(FILTER_BY_SYSTEM_STATUS)))
                                                    || (hostSystem.getInstanceStatus()
                                                            .equals(sortedSet.getFilterMap()
                                                                    .get(FILTER_BY_SYSTEM_STATUS))
                                                            && StringUtils.isEmpty(sortedSet.getFilterMap()
                                                                    .get(FILTER_BY_INSTANCE_STATUS)))
                                                    || (hostSystem.getInstanceStatus()
                                                            .equals(sortedSet.getFilterMap()
                                                                    .get(FILTER_BY_SYSTEM_STATUS))
                                                            && hostSystem.getInstanceStatus()
                                                                    .equals(sortedSet.getFilterMap()
                                                                            .get(FILTER_BY_INSTANCE_STATUS)))) {
                                                hostSystemList.put(hostSystem.getInstance(), hostSystem);
                                            }
                                        }

                                        //start over
                                        i = 0;
                                        //clear list
                                        idCallList.clear();
                                    }

                                }

                                //check alarms for ec2 instances
                                AmazonCloudWatchClient cloudWatchClient = new AmazonCloudWatchClient(
                                        awsCredentials, AWSClientConfig.getClientConfig());
                                cloudWatchClient.setEndpoint(ec2Region.replace("ec2", "monitoring"));

                                DescribeAlarmsResult describeAlarmsResult = cloudWatchClient.describeAlarms();

                                for (MetricAlarm metricAlarm : describeAlarmsResult.getMetricAlarms()) {

                                    for (Dimension dim : metricAlarm.getDimensions()) {

                                        if (dim.getName().equals("InstanceId")) {
                                            HostSystem hostSystem = hostSystemList.remove(dim.getValue());
                                            if (hostSystem != null) {
                                                if ("ALARM".equals(metricAlarm.getStateValue())) {
                                                    hostSystem
                                                            .setMonitorAlarm(hostSystem.getMonitorAlarm() + 1);
                                                } else if ("INSUFFICIENT_DATA"
                                                        .equals(metricAlarm.getStateValue())) {
                                                    hostSystem.setMonitorInsufficientData(
                                                            hostSystem.getMonitorInsufficientData() + 1);
                                                } else {
                                                    hostSystem.setMonitorOk(hostSystem.getMonitorOk() + 1);
                                                }
                                                //check and filter by alarm state
                                                if (StringUtils.isEmpty(
                                                        sortedSet.getFilterMap().get(FILTER_BY_ALARM_STATE))) {
                                                    hostSystemList.put(hostSystem.getInstance(), hostSystem);
                                                } else if ("ALARM".equals(
                                                        sortedSet.getFilterMap().get(FILTER_BY_ALARM_STATE))
                                                        && hostSystem.getMonitorAlarm() > 0) {
                                                    hostSystemList.put(hostSystem.getInstance(), hostSystem);
                                                } else if ("INSUFFICIENT_DATA".equals(
                                                        sortedSet.getFilterMap().get(FILTER_BY_ALARM_STATE))
                                                        && hostSystem.getMonitorInsufficientData() > 0) {
                                                    hostSystemList.put(hostSystem.getInstance(), hostSystem);
                                                } else if ("OK".equals(
                                                        sortedSet.getFilterMap().get(FILTER_BY_ALARM_STATE))
                                                        && hostSystem.getMonitorOk() > 0
                                                        && hostSystem.getMonitorInsufficientData() <= 0
                                                        && hostSystem.getMonitorAlarm() <= 0) {
                                                    hostSystemList.put(hostSystem.getInstance(), hostSystem);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //set ec2 systems
            SystemDB.setSystems(hostSystemList.values());
            sortedSet = SystemDB.getSystemSet(sortedSet, new ArrayList<String>(hostSystemList.keySet()));

        }
    } catch (AmazonServiceException ex) {
        log.error(ex.toString(), ex);
    }

    if (script != null && script.getId() != null) {
        script = ScriptDB.getScript(script.getId(), userId);
    }

    return SUCCESS;
}

From source file:com.keybox.manage.action.ApplicationKeysAction.java

License:Apache License

/**
  * returns keypairs as a json string/*  w  ww  . j a v a  2s  . c o  m*/
  */
@Action(value = "/manage/getKeyPairJSON")
public String getKeyPairJSON() {

    AWSCred awsCred = AWSCredDB.getAWSCred(ec2Key.getAwsCredentials().getId());

    //set  AWS credentials for service
    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(),
            awsCred.getSecretKey());
    AmazonEC2 service = new AmazonEC2Client(awsCredentials, AWSClientConfig.getClientConfig());

    service.setEndpoint(ec2Key.getEc2Region());

    DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();

    DescribeKeyPairsResult describeKeyPairsResult = service.describeKeyPairs(describeKeyPairsRequest);

    List<KeyPairInfo> keyPairInfoList = describeKeyPairsResult.getKeyPairs();
    String json = new Gson().toJson(keyPairInfoList);
    try {
        servletResponse.getOutputStream().write(json.getBytes());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}