Example usage for com.amazonaws.services.rds.model Tag Tag

List of usage examples for com.amazonaws.services.rds.model Tag Tag

Introduction

In this page you can find the example usage for com.amazonaws.services.rds.model Tag Tag.

Prototype

Tag

Source Link

Usage

From source file:com.cloudera.director.aws.rds.RDSProvider.java

License:Apache License

private Collection<Tag> convertToTags(Map<String, String> templateTags, RDSInstanceTemplate template,
        String instanceId) {//  w w  w  . ja  va  2  s  .  c o m
    Collection<Tag> tags = Lists.newArrayList();
    tags.add(new Tag().withKey(ResourceTags.RESOURCE_NAME.getTagKey())
            .withValue(String.format("%s-%s", template.getInstanceNamePrefix(), instanceId)));
    tags.add(new Tag().withKey(ResourceTags.CLOUDERA_DIRECTOR_ID.getTagKey()).withValue(instanceId));
    tags.add(new Tag().withKey(ResourceTags.CLOUDERA_DIRECTOR_TEMPLATE_NAME.getTagKey())
            .withValue(template.getName()));
    for (Map.Entry<String, String> e : templateTags.entrySet()) {
        tags.add(new Tag().withKey(e.getKey()).withValue(e.getValue()));
    }
    return tags;
}

From source file:com.github.blacklocus.rdsecho.AbstractEchoIntermediateStage.java

License:Open Source License

@Override
public Boolean call() throws Exception {

    // Validate state, make sure we're operating on what we expect to.

    String tagEchoManaged = echo.getTagEchoManaged();
    String tagEchoStage = echo.getTagEchoStage();

    LOG.info("Locating latest Echo managed instance (tagged with {}=true) in stage '{}' (tagged with {}={}).",
            tagEchoManaged, requisiteStage, tagEchoStage, requisiteStage);
    Optional<DBInstance> instanceOpt = echo.lastEchoInstance();
    if (!instanceOpt.isPresent()) {
        LOG.error("  Unable to locate Echo-managed instance. Is there one? Aborting.", tagEchoManaged);
        return false;
    }//  www. j av a 2s.  c o  m

    DBInstance instance = instanceOpt.get();
    Optional<Tag> stageOpt = echo.instanceStage(instance.getDBInstanceIdentifier());
    if (!stageOpt.isPresent()) {
        LOG.error("Unable to read Echo stage tag so cannot determine stage. To forcefully set the stage, edit "
                + "the instance's tags to add {}={} and run this modify operation again. "
                + "Cannot continue as it is so exiting.", tagEchoStage, requisiteStage);
        return false;
    }
    String instanceStage = stageOpt.get().getValue();
    if (!requisiteStage.equals(instanceStage)) {
        LOG.error("Current Echo stage on instance is {}={} but needs to be {}={}. To forcefully set the stage, "
                + "edit the instance's tags to set the required stage and run this modify operation again. "
                + "Cannot continue as it is so exiting.", tagEchoStage, instanceStage, tagEchoStage,
                requisiteStage);
        return false;
    }

    // Looks like we found a good echo instance, but is it available to us.

    String dbInstanceId = instance.getDBInstanceIdentifier();
    LOG.info("  Located echo-managed instance with identifier {}", dbInstanceId);
    if (!"available".equals(instance.getDBInstanceStatus())) {
        LOG.error("  Instance does not have status 'available' (saw {}) so aborting.",
                instance.getDBInstanceStatus());
        return false;
    }

    // Do the part special to traversing the this stage

    if (traverseStage(instance)) {

        // Advance. This replaces, same-named tags.
        rds.addTagsToResource(new AddTagsToResourceRequest()
                .withResourceName(RdsFind.instanceArn(cfg.region(), cfg.accountNumber(),
                        instance.getDBInstanceIdentifier()))
                .withTags(new Tag().withKey(tagEchoStage).withValue(resultantStage)));

        return true;

    } else {
        return false;
    }
}

From source file:com.github.blacklocus.rdsecho.EchoNew.java

License:Open Source License

@Override
public Boolean call() throws Exception {

    // Do some sanity checks to make sure we aren't generating a bunch of trouble in RDS

    String tagEchoManaged = echo.getTagEchoManaged();

    LOG.info("Checking to see if current echo-created instance (tagged {}) was created less than 24 hours ago. "
            + "If so this operation will not continue.", tagEchoManaged);
    Optional<DBInstance> newestInstanceOpt = echo.lastEchoInstance();
    if (newestInstanceOpt.isPresent()) {

        if (new DateTime(newestInstanceOpt.get().getInstanceCreateTime()).plusHours(24)
                .isAfter(DateTime.now())) {
            LOG.info("  Last echo-created RDS instance {} was created less than 24 hours ago. Aborting.",
                    tagEchoManaged);// www .  ja va  2s .  c o m
            return false;

        } else {
            LOG.info("  Last echo-created RDS instance {} was created more than 24 hours ago. Proceeding.",
                    tagEchoManaged);
        }

    } else {
        LOG.info("  No prior echo-created instance found with tag {}. Proceeding.", tagEchoManaged);
    }

    // Locate a suitable snapshot to be the basis of the new instance

    LOG.info("Locating latest snapshot from {}", cfg.snapshotDbInstanceIdentifier());
    Optional<DBSnapshot> dbSnapshotOpt = echo.latestSnapshot();
    if (dbSnapshotOpt.isPresent()) {
        DBSnapshot snapshot = dbSnapshotOpt.get();
        LOG.info("  Located snapshot {} completed on {}", snapshot.getDBSnapshotIdentifier(),
                new DateTime(snapshot.getSnapshotCreateTime()).toDateTimeISO().toString());

    } else {
        LOG.info("  Could not locate a suitable snapshot. Cannot continue.");
        return false;
    }

    // Info summary

    String dbSnapshotIdentifier = dbSnapshotOpt.get().getDBSnapshotIdentifier();
    String newDbInstanceIdentifier = cfg.name() + '-' + DateTime.now(DateTimeZone.UTC).toString("yyyy-MM-dd");
    LOG.info(
            "Proposed new db instance...\n" + "  engine           : {}\n" + "  license model    : {}\n"
                    + "  db instance class: {}\n" + "  multi az         : {}\n" + "  storage type     : {}\n"
                    + "  iops             : {}\n" + "  db snapshot id   : {}\n" + "  db instance id   : {}\n"
                    + "  port             : {}\n" + "  option group name: {}\n" + "  auto minor ver up: {}",
            cfg.newEngine(), cfg.newLicenseModel(), cfg.newDbInstanceClass(), cfg.newMultiAz(),
            cfg.newStorageType(), cfg.newIops(), dbSnapshotIdentifier, newDbInstanceIdentifier, cfg.newPort(),
            cfg.newOptionGroupName(), cfg.newAutoMinorVersionUpgrade());

    // Interactive user confirmation

    if (cfg.interactive()) {
        String format = "Proceed to create a new DB instance from this snapshot? Input %s to confirm.";
        if (!EchoUtil.prompt(newDbInstanceIdentifier, format, newDbInstanceIdentifier)) {
            LOG.info("User declined to proceed. Exiting.");
            return false;
        }
    }

    // Create the new database

    LOG.info("Creating new DB instance. Hold on to your butts.");
    RestoreDBInstanceFromDBSnapshotRequest request = new RestoreDBInstanceFromDBSnapshotRequest()
            .withEngine(cfg.newEngine()).withLicenseModel(cfg.newLicenseModel())
            .withDBInstanceClass(cfg.newDbInstanceClass()).withMultiAZ(cfg.newMultiAz())
            .withStorageType(cfg.newStorageType()).withIops(cfg.newIops())
            .withDBSnapshotIdentifier(dbSnapshotIdentifier).withDBInstanceIdentifier(newDbInstanceIdentifier)
            .withPort(cfg.newPort()).withOptionGroupName(cfg.newOptionGroupName())
            .withAutoMinorVersionUpgrade(cfg.newAutoMinorVersionUpgrade())
            .withTags(new Tag().withKey(echo.getTagEchoManaged()).withValue("true"),
                    new Tag().withKey(echo.getTagEchoStage()).withValue(EchoConst.STAGE_NEW));
    DBInstance restoredInstance = rds.restoreDBInstanceFromDBSnapshot(request);

    Optional<String[]> newTags = cfg.newTags();
    if (newTags.isPresent()) {
        List<Tag> tags = EchoUtil.parseTags(newTags.get());
        if (tags.size() > 0) {
            LOG.info("Applying tags on create new: {}", Arrays.asList(tags));
            AddTagsToResourceRequest tagsRequest = new AddTagsToResourceRequest()
                    .withResourceName(RdsFind.instanceArn(cfg.region(), cfg.accountNumber(),
                            restoredInstance.getDBInstanceIdentifier()));
            tagsRequest.setTags(tags);
            rds.addTagsToResource(tagsRequest);
        }
    }

    LOG.info("Created new DB instance. \n"
            + "  https://console.aws.amazon.com/rds/home?region={}#dbinstance:id={}\n"
            + "Additional preparation of the instance will continue once the instance becomes available.",
            cfg.region(), newDbInstanceIdentifier);

    return true;
}

From source file:com.github.blacklocus.rdsecho.utl.EchoUtil.java

License:Open Source License

public static List<Tag> parseTags(String[] rawTags) {
    List<Tag> tags = Lists.newArrayList();
    for (String rawTag : rawTags) {
        String[] splitTag = rawTag.split("=", 2);
        if (splitTag.length == 2) {
            tags.add(new Tag().withKey(splitTag[0]).withValue(splitTag[1]));
        }//from  ww  w .j  ava2 s .co m
    }
    return tags;
}

From source file:jp.classmethod.aws.gradle.rds.AmazonRDSRestoreDbClusterToPointInTimeTask.java

License:Apache License

@TaskAction
public void restoreDbClusterToPointInTime() { // NOPMD
    // to enable conventionMappings feature
    String dbClusterIdentifier = getDbClusterIdentifier();

    if (dbClusterIdentifier == null) {
        throw new GradleException("dbClusterIdentifier is not specified");
    }// w ww .  ja  v a  2 s. c  o  m

    AmazonRDSPluginExtension ext = getProject().getExtensions().getByType(AmazonRDSPluginExtension.class);
    AmazonRDS rds = ext.getClient();

    RestoreDBClusterToPointInTimeRequest request = new RestoreDBClusterToPointInTimeRequest()
            .withDBClusterIdentifier(dbClusterIdentifier).withDBSubnetGroupName(getDbSubnetGroupName())
            .withKmsKeyId(getKmsKeyId()).withRestoreType(getRestoreType()).withRestoreToTime(getRestoreToTime())
            .withSourceDBClusterIdentifier(getSourceDBClusterIdentifier())
            .withUseLatestRestorableTime(getUseLatestRestorableTime())
            .withVpcSecurityGroupIds(getVpcSecurityGroupIds())
            .withTags(getTags().entrySet().stream()
                    .map(it -> new Tag().withKey(it.getKey().toString()).withValue(it.getValue().toString()))
                    .collect(Collectors.toList()));
    if (getPort() != 0) {
        request.setPort(getPort());
    }
    dbCluster = rds.restoreDBClusterToPointInTime(request);
    getLogger().info("Restored an RDS cluster to point in time: {}", dbCluster.getDBClusterIdentifier());

}