Пример #1
0
        private string GetHostedZoneIdFor(string dnsName)
        {
            log.LogLine($"Getting hosted zone id for {dnsName}");
            var zoneName = string.Join(".", dnsName.Split('.').Skip(1));
            var req      = new ListHostedZonesByNameRequest
            {
                DNSName = zoneName
            };

            var response = R53.ListHostedZonesByNameAsync(req);

            try
            {
                response.Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var exp in ex.InnerExceptions)
                {
                    log.LogLine(exp.ToString());
                }
            }
            string id     = response.Result.HostedZones[0].Id;
            string result = id.Split('/')[2];

            log.LogLine($"Returning {result}");
            return(result);
        }
Пример #2
0
        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());
                        }
                    }
                }
            }
        }
Пример #3
0
        private async Task <string> LookupIpAddressByZoneIdAndDNSName(string id, string dnsName)
        {
            var request = new ListResourceRecordSetsRequest
            {
                HostedZoneId    = id,
                StartRecordName = dnsName
            };
            var response = await R53.ListResourceRecordSetsAsync(request);

            if (response.ResourceRecordSets.Count == 0 || response.ResourceRecordSets[0].ResourceRecords.Count == 0)
            {
                return(string.Empty);
            }
            return(response.ResourceRecordSets[0].ResourceRecords[0].Value);
        }