br.com.objectos.aws.rds.maven.plugin.CreateSnapshotMojo.java Source code

Java tutorial

Introduction

Here is the source code for br.com.objectos.aws.rds.maven.plugin.CreateSnapshotMojo.java

Source

/*
 * Copyright 2015 Objectos, Fbrica de Software LTDA.
 *
 * 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 br.com.objectos.aws.rds.maven.plugin;

import java.util.List;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.rds.AmazonRDSClient;
import com.amazonaws.services.rds.model.CreateDBSnapshotRequest;
import com.amazonaws.services.rds.model.DBSnapshot;
import com.amazonaws.services.rds.model.DescribeDBSnapshotsRequest;
import com.amazonaws.services.rds.model.DescribeDBSnapshotsResult;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
 * @author marcio.endo@objectos.com.br (Marcio Endo)
 */
@Mojo(name = "create-snapshot")
public class CreateSnapshotMojo extends AbstractMojo {

    @Parameter(required = true)
    private String accessKey;

    @Parameter(required = true)
    private String secretKey;

    @Parameter(required = true)
    private String dBInstanceIdentifier;

    @Parameter(required = true)
    private String dBSnapshotIdentifier;

    @Parameter
    private boolean skip;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        if (!skip) {
            execute0();
        }
    }

    void info(String template, Object... args) {
        String message = String.format(template, args);
        logger().info(message);
    }

    private DBSnapshot describeSnapshot(AmazonRDSClient client) {
        DBSnapshot snapshot = null;

        DescribeDBSnapshotsRequest describeDBSnapshotsRequest = new DescribeDBSnapshotsRequest()
                .withDBSnapshotIdentifier(dBSnapshotIdentifier);

        DescribeDBSnapshotsResult describeDBSnapshots = client.describeDBSnapshots(describeDBSnapshotsRequest);
        List<DBSnapshot> list = describeDBSnapshots.getDBSnapshots();

        if (!list.isEmpty()) {
            snapshot = list.get(0);
        }

        return snapshot;
    }

    private void execute0() throws MojoExecutionException {
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        AmazonRDSClient client = new AmazonRDSClient(credentials);

        CreateDBSnapshotRequest createDBSnapshotRequest = new CreateDBSnapshotRequest()
                .withDBInstanceIdentifier(dBInstanceIdentifier).withDBSnapshotIdentifier(dBSnapshotIdentifier);

        info("Starting RDS Snapshot '%s' at '%s'", dBSnapshotIdentifier, dBInstanceIdentifier);
        long startTime = System.currentTimeMillis();

        DBSnapshot snapshot = client.createDBSnapshot(createDBSnapshotRequest);
        info("Backing up... please wait.");

        while (!isDone(snapshot)) {
            try {
                Thread.sleep(5000);
                snapshot = describeSnapshot(client);

                if (snapshot == null) {
                    break;
                }
            } catch (InterruptedException e) {
                throw new MojoExecutionException("Interrupted while waiting", e);
            }
        }

        long endTime = System.currentTimeMillis();

        info("Snapshot took %d ms", endTime - startTime);
    }

    private boolean isDone(DBSnapshot snapshot) {
        String status = snapshot.getStatus();
        return "available".equalsIgnoreCase(status);
    }

    private Log logger() {
        return getLog();
    }

}