Пример #1
0
        public static Task <IEnumerable <DnsimpleRecord> > LoadDnsimpleRecords(Config config, string fqDomainName)
        {
            return(Task.Run(() =>
            {
                var domainName = DeriveBaseDomainName(fqDomainName);
                Log.Trace("Loading DNS records for '{0}' (for '{1}').", domainName, fqDomainName);
                var token = UnprotectSecret(config.dnsimpleApiToken, config.randomString);
                var client = new DNSimpleRestClient(config.dnsimpleUsername, token: token);

                var records = client.ListRecords(domainName);
                int recordCount;
                try
                {
                    recordCount = (int)records.Length;
                }
                catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)
                {
                    // Length property does not exist: probably because of a failure.
                    throw new Exception("Error when loading Dnsimple records: " + records.message);
                }
                Log.Debug("Loaded {0:N0} DNS record(s) for '{1}' (for '{2}').", recordCount, domainName, fqDomainName);
                return ((ExpandoObject[])records).Select(x => new DnsimpleRecord(x)).ToList().AsEnumerable();
            }));
        }
        public override bool UpdateHostname(string hostname, string ipAddress)
        {
            try
            {
                if (HasIpAddresssChanged(hostname, ipAddress) == false)
                {
                    return(true);                                                                    // No change, no need to update
                }
                if (IsValidIpAddress(ipAddress) == false)
                {
                    logger.Error(string.Format("Invalid IP Address provided: {0}", ipAddress));
                    return(false);
                }

                var domainName = DomainName.Parse(hostname);

                dynamic records = client.ListRecords(domainName.Domain);

                var recordsResponse = records as IDictionary <String, object>;
                if (recordsResponse != null && recordsResponse.ContainsKey("error"))
                {
                    logger.Error(records.error);
                    return(false);
                }

                if (records != null)
                {
                    for (var i = 0; i < records.Length; i++)
                    {
                        var hostForRecord = records[i].record.name;

                        if (domainName.Host == hostForRecord)
                        {
                            var ipAddressForRecord = records[i].record.content;

                            if (ipAddress == ipAddressForRecord)
                            {
                                return(true);                                //No need to update.
                            }
                            else
                            {
                                dynamic record = client.UpdateRecord(domainName.Domain, records[i].record.id, hostForRecord, ipAddress);

                                var recordResponse = record as IDictionary <String, object>;
                                if (recordResponse != null && recordResponse.ContainsKey("error"))
                                {
                                    logger.Error(record.error);
                                    return(false);
                                }
                                else
                                {
                                    LastUpdateIpAddresses[hostname] = ipAddress;
                                    logger.Info(string.Format("Updated DNS Record: {0} to IP Address: {1}", hostname, ipAddress));
                                    return(true);
                                }
                            }
                        }
                    }
                }

                //At this point we have checked all of the existing records and have not found the host name,
                //so we will try to create one.

                client.AddRecord(domainName.Domain, domainName.Host, DnsRecordType.A, ipAddress);

                LastUpdateIpAddresses[hostname] = ipAddress;
                return(true);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return(false);
            }
        }