private async Task UpdateRoute53(string instanceId, Instance instance, List <Amazon.EC2.Model.Tag> tags, string action) { log.LogLine($"Checking whether to update, delete or ignore"); foreach (var tag in tags) { string key = tag.Key; if (key == PublicDNSTagKey || key == PrivateDNSTagKey) { log.LogLine($"Got one! {key}"); bool isPublicDNS = key == PublicDNSTagKey; UpdateDeleteIgnoreDecision decision = MakeDeleteUpdateOrIgnoreDecision(action, isPublicDNS); if (decision == UpdateDeleteIgnoreDecision.Ignore) { log.LogLine($"Ignoring {action} for {instanceId}"); continue; } string dnsName = tag.Value; string message = string.Empty; if (decision == UpdateDeleteIgnoreDecision.Update) { message = "Creating / updating"; } else if (decision == UpdateDeleteIgnoreDecision.Delete) { message = "Deleting"; } log.LogLine($"{message} record set for {instanceId} - {dnsName}"); string id = GetHostedZoneIdFor(dnsName); string ipAddress = isPublicDNS ? instance.PublicIpAddress : instance.PrivateIpAddress; ChangeResourceRecordSetsRequest rreq = await CreateChangeResourceRecordSetsRequest(id, dnsName, ipAddress, decision); if (rreq == null) { log.LogLine("Couldn't update. Skipping this one."); continue; } try { var rresp = await R53.ChangeResourceRecordSetsAsync(rreq); log.LogLine($"Result: {rresp.ChangeInfo.Status}"); } catch (AggregateException ex) { foreach (var exp in ex.InnerExceptions) { log.LogLine(exp.ToString()); } } } } }
private async Task <ChangeResourceRecordSetsRequest> CreateChangeResourceRecordSetsRequest(string id, string dnsName, string ipAddress, UpdateDeleteIgnoreDecision decision) { if (string.IsNullOrEmpty(ipAddress)) { log.LogLine($"No ip address provided. Looking up by hosted zone / dnsname."); ipAddress = await LookupIpAddressByZoneIdAndDNSName(id, dnsName); if (string.IsNullOrEmpty(ipAddress)) { log.LogLine($"No record set found for {id} - {dnsName}. Looks like it's been deleted already."); return(null); } } log.LogLine($"Creating {decision} change request for {dnsName} - {ipAddress} - {id}"); ChangeAction action = (decision == UpdateDeleteIgnoreDecision.Delete) ? ChangeAction.DELETE : ChangeAction.UPSERT; var result = new ChangeResourceRecordSetsRequest { HostedZoneId = id, ChangeBatch = new ChangeBatch { Changes = new List <Change> { new Change { Action = action, ResourceRecordSet = new ResourceRecordSet { Name = dnsName, ResourceRecords = new List <ResourceRecord> { new ResourceRecord { Value = ipAddress } }, Type = RRType.A, TTL = 300 } } } } }; log.LogLine("Returning:"); log.LogLine(JsonConvert.SerializeObject(result)); return(result); }