Skip to content

Commit

Permalink
Improvement GoDaddy provider (#376)
Browse files Browse the repository at this point in the history
* Fixed godaddy http request error

* Adding name server validation
  • Loading branch information
shibayan authored Sep 30, 2021
1 parent b5e395d commit 47e884b
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions KeyVault.Acmebot/Providers/GoDaddyProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
Expand All @@ -20,14 +22,15 @@ public GoDaddyProvider(GoDaddyOptions options)
}

private readonly GoDaddyClient _client;
private readonly IdnMapping _idnMapping = new IdnMapping();

public int PropagationSeconds => 600;

public async Task<IReadOnlyList<DnsZone>> ListZonesAsync()
{
var zones = await _client.ListZonesAsync();

return zones.Select(x => new DnsZone { Id = x.Domain, Name = x.Domain }).ToArray();
return zones.Select(x => new DnsZone { Id = x.DomainId, Name = _idnMapping.GetAscii(x.Domain), NameServers = x.NameServers }).ToArray();
}

public async Task CreateTxtRecordAsync(DnsZone zone, string relativeRecordName, IEnumerable<string> values)
Expand All @@ -45,19 +48,12 @@ public async Task CreateTxtRecordAsync(DnsZone zone, string relativeRecordName,
});
}

await _client.AddRecordAsync(zone.Id, entries);
await _client.AddRecordAsync(zone.Name, entries);
}

public async Task DeleteTxtRecordAsync(DnsZone zone, string relativeRecordName)
{
var records = await _client.ListRecordsAsync(zone.Id);

var recordsToDelete = records.Where(r => r.Name == relativeRecordName && r.Type == "TXT");

foreach (var record in recordsToDelete)
{
await _client.DeleteRecordAsync(zone.Id, record);
}
await _client.DeleteRecordAsync(zone.Name, "TXT", relativeRecordName);
}

private class GoDaddyClient
Expand Down Expand Up @@ -87,7 +83,7 @@ public GoDaddyClient(string apiKey, string apiSecret)

public async Task<IReadOnlyList<ZoneDomain>> ListZonesAsync()
{
var response = await _httpClient.GetAsync("v1/domains?statuses=ACTIVE");
var response = await _httpClient.GetAsync("v1/domains?statuses=ACTIVE&includes=nameServers");

response.EnsureSuccessStatusCode();

Expand All @@ -96,27 +92,19 @@ public async Task<IReadOnlyList<ZoneDomain>> ListZonesAsync()
return domains;
}

public async Task<IReadOnlyList<DnsEntry>> ListRecordsAsync(string zoneId)
public async Task DeleteRecordAsync(string domain, string type, string name)
{
var response = await _httpClient.GetAsync($"v1/domains/{zoneId}/records");
var response = await _httpClient.DeleteAsync($"v1/domains/{domain}/records/{type}/{name}");

response.EnsureSuccessStatusCode();

var entries = await response.Content.ReadAsAsync<DnsEntry[]>();

return entries;
}

public async Task DeleteRecordAsync(string zoneId, DnsEntry entry)
{
var response = await _httpClient.DeleteAsync($"v1/domains/{zoneId}/records/{entry.Type}/{entry.Name}");

response.EnsureSuccessStatusCode();
if (response.StatusCode != HttpStatusCode.NotFound)
{
response.EnsureSuccessStatusCode();
}
}

public async Task AddRecordAsync(string zoneId, IReadOnlyList<DnsEntry> entries)
public async Task AddRecordAsync(string domain, IReadOnlyList<DnsEntry> entries)
{
var response = await _httpClient.PatchAsync($"v1/domains/{zoneId}/records", entries);
var response = await _httpClient.PatchAsync($"v1/domains/{domain}/records", entries);

response.EnsureSuccessStatusCode();
}
Expand All @@ -129,6 +117,9 @@ public class ZoneDomain

[JsonProperty("domainId")]
public string DomainId { get; set; }

[JsonProperty("nameServers")]
public string[] NameServers { get; set; }
}

public class DnsEntry
Expand Down

0 comments on commit 47e884b

Please sign in to comment.