com.agilegroups.aws.services.AmazonEC2ServicesImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.agilegroups.aws.services.AmazonEC2ServicesImpl.java

Source

/*
 * Copyright 2012-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.agilegroups.aws.services;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import com.agilegroups.aws.ApplicationConfig;
import com.agilegroups.aws.ApplicationSetup;
import com.agilegroups.aws.model.InstanceModel;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.AssociateAddressRequest;
import com.amazonaws.services.ec2.model.AssociateAddressResult;
import com.amazonaws.services.ec2.model.StartInstancesRequest;
import com.amazonaws.services.ec2.model.StartInstancesResult;

@Component
public class AmazonEC2ServicesImpl implements AmazonEC2Services {

    private static final Logger LOG = LoggerFactory.getLogger(AmazonEC2ServicesImpl.class);

    private List<String> instanceIds = new ArrayList<String>();

    //@Autowired
    //ApplicationConfig config;

    @Value("${accessKey:NA}")
    private String accessKey;

    @Value("${secretKey:NA}")
    private String secretKey;

    private AWSCredentials credentials;
    private AmazonEC2Client client;

    public List<String> getInstanceIds() {
        return this.instanceIds;
    }

    @PostConstruct
    public void init() {

        this.credentials = new BasicAWSCredentials(accessKey, secretKey);
        client = new AmazonEC2Client(this.credentials);

        //System.out.println("Here are the instanceIds: " + getInstanceIds());
        //LOG.info("Initialized [{}] [{}] [{}] [{}] [{}]", username, remoteAddress, path, instanceIds, foo);      

        LOG.info("Here are the properties: ");
        LOG.info("  accessKey: " + accessKey);
        if (secretKey == null) {
            LOG.warn("  secretKey was null");
        }

    }

    public StartInstancesResult startInstances(List<String> instanceIds) {

        StartInstancesRequest request = new StartInstancesRequest(instanceIds);

        return this.client.startInstances(request);
    }

    @Override
    public AssociateAddressResult associateAddress(InstanceModel model) {

        AssociateAddressRequest request = new AssociateAddressRequest(model.getInstanceId(), model.getIpAddress());

        return this.client.associateAddress(request);
    }

}