}
public void ensureCNAME(final String source, final String target)
throws Exception {
final HostedZone zone = findZone(source);
Util.assertNotNull(zone, "missing zone for " + source);
final String zoneId = zone.getId();
final boolean isPresent;
final ResourceRecordSet recordOld;
{
final ResourceRecordSet recordFound = findRecord(zoneId, source);
if (recordFound == null) {
isPresent = false;
recordOld = makeRecordCNAME(source, target);
} else {
isPresent = true;
recordOld = recordFound;
}
}
final ResourceRecordSet recordNew = makeRecordCNAME(source, target);
recordNew.setTTL(recordOld.getTTL());
//
final Collection<Change> changeList = new LinkedList<Change>();
if (isPresent) {
changeList.add(new Change(ChangeAction.DELETE, recordOld));
changeList.add(new Change(ChangeAction.CREATE, recordNew));
} else {
changeList.add(new Change(ChangeAction.CREATE, recordNew));
}
final ChangeBatch changeRequest = new ChangeBatch();
changeRequest.setComment("updated : " + new Date());
changeRequest.setChanges(changeList);
final ChangeResourceRecordSetsRequest request = new ChangeResourceRecordSetsRequest();
request.setHostedZoneId(zone.getId());
request.setChangeBatch(changeRequest);
final ChangeResourceRecordSetsResult result = amazonClient
.changeResourceRecordSets(request);