Created
November 8, 2013 19:59
-
-
Save pherris/7376745 to your computer and use it in GitHub Desktop.
AWS Route 53 DNS manipulation with Gradle.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Updates the IP addresses | |
*/ | |
void assignInstancesToURL(url) { | |
initInstancesFromReservationId() | |
// all the changes to make | |
def changes = new ArrayList<com.amazonaws.services.route53.model.Change>() | |
def existingDNS = listRecordSets() | |
log.info "Existing DNS: \n $existingDNS" | |
// delete | |
def aRecordRemove = existingDNS.findResult { "${url}." == it.name && it.type == "A" ? it : null } | |
if (aRecordRemove) { | |
changes.add(new com.amazonaws.services.route53.model.Change("DELETE", aRecordRemove)) | |
} else { | |
log.info "no existing dns for $url" | |
} | |
//add new | |
def aRecord = new com.amazonaws.services.route53.model.ResourceRecordSet(url, "A") | |
aRecord.TTL = 1 | |
aRecord.resourceRecords = instances.collect { new com.amazonaws.services.route53.model.ResourceRecord(it.publicIpAddress) } | |
changes.add(new com.amazonaws.services.route53.model.Change("CREATE", aRecord)) | |
log.info "New IP addresses: \n" + instances.collect { it.publicIpAddress + "\n" } | |
def changeBatch = new com.amazonaws.services.route53.model.ChangeBatch(changes); | |
def changeRequest = new com.amazonaws.services.route53.model.ChangeResourceRecordSetsRequest(hostedZoneId, changeBatch); | |
// send the request | |
def result = route53Client.changeResourceRecordSets(changeRequest) | |
System.out.println(result.toString()) | |
} | |
/** | |
* Lists the record set for the specified hosted zone | |
*/ | |
List listRecordSets() { | |
def listRequest = new com.amazonaws.services.route53.model.ListResourceRecordSetsRequest(); | |
listRequest.setHostedZoneId(hostedZoneId); | |
def result = route53Client.listResourceRecordSets(listRequest); | |
return result.getResourceRecordSets(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment