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

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

Introduction

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

Prototype

AddTagsToResourceRequest

Source Link

Usage

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;
    }/*from w ww. j  av  a2s. 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);/* ww w .j a v  a 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.EchoPromote.java

License:Open Source License

@Override
boolean traverseStage(DBInstance instance) {

    LOG.info("Reading current DNS records");
    String tld = EchoUtil.getTLD(cfg.promoteCname()) + '.';
    HostedZone hostedZone = route53Find.hostedZone(nameEquals(tld)).get();
    LOG.info("  Found corresponding HostedZone. name: {} id: {}", hostedZone.getName(), hostedZone.getId());

    ResourceRecordSet resourceRecordSet = route53Find
            .resourceRecordSet(hostedZone.getId(), cnameEquals(cfg.promoteCname())).get();
    ResourceRecord resourceRecord = getOnlyElement(resourceRecordSet.getResourceRecords());
    LOG.info("  Found CNAME {} with current value {}", resourceRecordSet.getName(), resourceRecord.getValue());

    Endpoint endpoint = instance.getEndpoint();
    String tagEchoManaged = echo.getTagEchoManaged();
    String dbInstanceId = instance.getDBInstanceIdentifier();
    if (null == endpoint) {
        LOG.info("Echo DB instance {} (id: {}) has no address. Is it still initializing?", tagEchoManaged,
                dbInstanceId);/*from  w  w w.  j  a  va2s  .c om*/
        return false;
    }
    String instanceAddr = endpoint.getAddress();
    if (resourceRecord.getValue().equals(instanceAddr)) {
        LOG.info("  Echo DB instance {} ({}) lines up with CNAME {}. Nothing to do.", tagEchoManaged,
                instanceAddr, resourceRecordSet.getName());
        return false;
    } else {
        LOG.info("  Echo DB instance {} ({}) differs from CNAME {}.", tagEchoManaged, instanceAddr,
                resourceRecordSet.getName());
    }

    if (cfg.interactive()) {
        String format = "Are you sure you want to promote %s to be the new target of %s? Input %s to confirm.";
        if (!EchoUtil.prompt(dbInstanceId, format, dbInstanceId, cfg.promoteCname(), dbInstanceId)) {
            LOG.info("User declined to proceed. Exiting.");
            return false;
        }
    }

    LOG.info("Updating CNAME {} from {} to {}", cfg.name(), resourceRecord.getValue(), instanceAddr);
    ChangeResourceRecordSetsRequest request = new ChangeResourceRecordSetsRequest()
            .withHostedZoneId(hostedZone.getId())
            .withChangeBatch(new ChangeBatch().withChanges(
                    new Change(ChangeAction.UPSERT, new ResourceRecordSet(cfg.promoteCname(), RRType.CNAME)
                            .withResourceRecords(new ResourceRecord(instanceAddr)).withTTL(cfg.promoteTtl()))));
    route53.changeResourceRecordSets(request);

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

    LOG.info("Searching for any existing promoted instance to demote.");

    return true;
}