-
-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Custom DNS Provider model (#285)
* Adding Custom DNS Provider model * Changed custom dns provider api spec * Streamlined HttpClient send methods * Fixed formatting * Update create record verb * Adding API Key auth * Make customizable API auth header name
- Loading branch information
Showing
9 changed files
with
127 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using Newtonsoft.Json; | ||
|
||
namespace KeyVault.Acmebot.Internal | ||
{ | ||
internal static class HttpClientExtensions | ||
{ | ||
public static Task<HttpResponseMessage> PostAsync<T>(this HttpClient client, string requestUri, T value) => client.PostAsync(requestUri, SerializeToJson(value)); | ||
|
||
public static Task<HttpResponseMessage> PutAsync<T>(this HttpClient client, string requestUri, T value) => client.PutAsync(requestUri, SerializeToJson(value)); | ||
|
||
public static Task<HttpResponseMessage> PatchAsync<T>(this HttpClient client, string requestUri, T value) => client.PatchAsync(requestUri, SerializeToJson(value)); | ||
|
||
public static Task<HttpResponseMessage> DeleteAsync<T>(this HttpClient client, string requestUri, T value) | ||
{ | ||
var request = new HttpRequestMessage(HttpMethod.Delete, requestUri) | ||
{ | ||
Content = SerializeToJson(value) | ||
}; | ||
|
||
return client.SendAsync(request); | ||
} | ||
|
||
private static HttpContent SerializeToJson<T>(T value) => new StringContent(JsonConvert.SerializeObject(value), Encoding.UTF8, "application/json"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace KeyVault.Acmebot.Options | ||
{ | ||
public class CustomDnsOptions | ||
{ | ||
public int PropagationSeconds { get; set; } = 180; | ||
|
||
public string Endpoint { get; set; } | ||
|
||
public string ApiKey { get; set; } | ||
|
||
public string ApiKeyHeaderName { get; set; } = "X-Api-Key"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
|
||
using KeyVault.Acmebot.Internal; | ||
using KeyVault.Acmebot.Options; | ||
|
||
namespace KeyVault.Acmebot.Providers | ||
{ | ||
public class CustomDnsProvider : IDnsProvider | ||
{ | ||
public CustomDnsProvider(CustomDnsOptions options) | ||
{ | ||
_httpClient = new HttpClient | ||
{ | ||
BaseAddress = new Uri(options.Endpoint) | ||
}; | ||
|
||
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation(options.ApiKeyHeaderName, options.ApiKey); | ||
|
||
PropagationSeconds = options.PropagationSeconds; | ||
} | ||
|
||
private readonly HttpClient _httpClient; | ||
|
||
public int PropagationSeconds { get; } | ||
|
||
public async Task<IReadOnlyList<DnsZone>> ListZonesAsync() | ||
{ | ||
var response = await _httpClient.GetAsync("zones"); | ||
|
||
response.EnsureSuccessStatusCode(); | ||
|
||
var zones = await response.Content.ReadAsAsync<DnsZone[]>(); | ||
|
||
return zones; | ||
} | ||
|
||
public async Task CreateTxtRecordAsync(DnsZone zone, string relativeRecordName, IEnumerable<string> values) | ||
{ | ||
var recordName = $"{relativeRecordName}.{zone.Name}"; | ||
|
||
var response = await _httpClient.PutAsync($"zones/{zone.Id}/records/{recordName}", new { type = "TXT", ttl = 60, values }); | ||
|
||
response.EnsureSuccessStatusCode(); | ||
} | ||
|
||
public async Task DeleteTxtRecordAsync(DnsZone zone, string relativeRecordName) | ||
{ | ||
var recordName = $"{relativeRecordName}.{zone.Name}"; | ||
|
||
var response = await _httpClient.DeleteAsync($"zones/{zone.Id}/records/{recordName}"); | ||
|
||
response.EnsureSuccessStatusCode(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters