Example #1
0
        public GandiService(GandiConfig setup)
        {
            Logger.Log("Starting GandiService (constructor)");
            _apiKey = setup.ApiKey;
            _zoneName = setup.ZoneName;
            _simulate = setup.Simulate;

            if (setup.UseTest) { _proxy.Url = testApiUrl; }
            else { _proxy.Url = prodApiUrl; }
            Logger.Log("Using proxy address " + _proxy.Url);

            Logger.Log("Retrieving zone list...");
            try
            {
                ZoneListReturn[] list = _proxy.ZoneList(_apiKey);
                _zone = list.FirstOrDefault<ZoneListReturn>(zr => zr.name.Equals(setup.ZoneName, StringComparison.InvariantCultureIgnoreCase));
                foreach (ZoneListReturn zl in list)
                {
                    Logger.Log(String.Format("Found Zone: [{1}]{0}", zl.name, zl.id));
                }
                _ready = true;
            }
            catch (Exception e)
            {
                Logger.Log(Logger.LogLevel.Error, "EXCEPTION: " + e.Message);
                _ready = false;
            }
            if (_zone.id == 0)
            {
                Logger.Log("Zone {0} not found.", setup.ZoneName);
                _ready = false;
            }
        }
Example #2
0
 private ZoneRecordReturn GetHostRecord(string hostName, ZoneListReturn hostZone)
 {
     if (!_ready) { return new ZoneRecordReturn(); }
     ZoneRecordReturn[] records = _proxy.RecordList(_apiKey, hostZone.id, hostZone.version);
     return records.FirstOrDefault<ZoneRecordReturn>(r => r.name.Equals(hostName, StringComparison.InvariantCultureIgnoreCase));
 }
Example #3
0
        public bool UpdateHost(string hostName, string ipAddress, int defaultTTL)
        {
            if (_simulate || !_ready) { return false; }
            int newZoneVersion = _proxy.ZoneNewVersion(_apiKey, _zone.id);
            Logger.Log(String.Format("Created zone version {0}.", newZoneVersion));

            ZoneListReturn newZone = new ZoneListReturn();
            newZone.id = _zone.id;
            newZone.version = newZoneVersion;

            ZoneRecordReturn record = GetHostRecord(hostName, newZone);

            if (record.id == 0) { return false; }

            Logger.Log("Zone Record Found -");
            Logger.Log("ID: {0}", record.id);
            Logger.Log("Name: {0}", record.name);
            Logger.Log("Type: {0}", record.type);
            Logger.Log("Value: '{0}'", record.value);
            Logger.Log("newIP: '{0}'", ipAddress);
            Logger.Log("TTL: {0}", record.ttl);
            if (record.type != "A")
            {
                Logger.Log("Unable to update record. Not an A record.");
                return false;
            }
            ZoneRecord newRecord;
            RecordUpdateOptions recordToUpdate;
            recordToUpdate.id = record.id;
            newRecord.name = record.name;
            newRecord.type = "A";
            newRecord.ttl = defaultTTL;
            newRecord.value = ipAddress;
            ZoneRecordReturn[] updateResults = _proxy.RecordUpdate(_apiKey, newZone.id, newZoneVersion, recordToUpdate, newRecord);
            //RecordDeleteOptions recordToDelete;
            //recordToDelete.id = record.id;
            //int numDel = proxy.RecordDelete(_gandiApiKey, test.id, newZoneVersion, recordToDelete);
            //StaticLogger.Log("NumDel: {0}", numDel);
            if (updateResults.Length > 0)
            {
                // ZoneRecordReturn result = updateResults[0];
                // StaticLogger.Log("Record inserted. New record ID: {0}", result.id);
                bool zoneUpdated = _proxy.ZoneSetActiveVersion(_apiKey, newZone.id, newZoneVersion);
                if (zoneUpdated)
                {
                    Logger.Log("Zone updated to version {0}. Deleting previous...", newZoneVersion);
                    zoneUpdated = _proxy.ZoneDeleteVersion(_apiKey, _zone.id, _zone.version);
                    _zone = newZone;
                }
                else
                {
                    Logger.Log("Zone update failed.");
                }
                return zoneUpdated;
            }
            else
            {
                Logger.Log("Unable to update record. Deleting unused zone version {0}.", newZoneVersion);
                _proxy.ZoneDeleteVersion(_apiKey, newZone.id, newZoneVersion);
                return false;
            }
        }