Skip to content

Commit

Permalink
Merge pull request #6 from JSkimming/get-dnsrecords
Browse files Browse the repository at this point in the history
Added the Get DNS Records CloudFlare client method
  • Loading branch information
JSkimming committed Aug 5, 2015
2 parents 661601c + 9aa6673 commit e320f7a
Show file tree
Hide file tree
Showing 27 changed files with 599 additions and 84 deletions.
15 changes: 12 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,18 @@ after_build:
test_script: coverage.cmd

after_test: |
7z a CoverageResults.7z src\TestResults\
appveyor PushArtifact CoverageResults.7z -FileName "CoverageResults_%GitVersion_FullSemVer%.7z"
appveyor PushArtifact src\TestResults\Specifications.html -FileName "Specifications_%GitVersion_FullSemVer%.html"
FOR /r %%F IN (*coveralls.net.exe) DO SET coveralls_exe=%%F
%coveralls_exe% --opencover src\TestResults\Test.Coverage.xml
appveyor PushArtifact src\TestResults\Specifications.html -FileName "Specifications_%GitVersion_FullSemVer%.html"
#---------------------------------#
# global handlers #
#---------------------------------#

# after build failure or success
on_finish: |
7z a CoverageResults.7z src\TestResults\
appveyor PushArtifact CoverageResults.7z -FileName "CoverageResults_%GitVersion_FullSemVer%.7z"
#---------------------------------#
# notifications #
Expand All @@ -89,3 +96,5 @@ notifications:
auth_token:
secure: AkSNuuHmfzAFp2+Oq00NRQFOfrXl4ue08SHx0FC2WTeXSiZv8uVKI3ZBWns7HTcV
channel: cloudflare_net
on_build_status_changed: true

10 changes: 6 additions & 4 deletions coverage.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@
::@echo "%xunit_exe%" "%test_assemblies%" -noshadow -html "%xunit_results%"
::@"%xunit_exe%" "%test_assemblies%" -noshadow -html "%xunit_results%"

@echo "%cover_exe%" -register:user "-target:%mspec_exe%" "-targetargs:%test_assemblies% --timeinfo --silent --html %spec_results%" -filter:%coverage_filter% "-output:%coverage_results%"
@"%cover_exe%" -register:user "-target:%mspec_exe%" "-targetargs:%test_assemblies% --timeinfo --silent --html %spec_results%" -filter:%coverage_filter% "-output:%coverage_results%"
@echo "%cover_exe%" -register:user "-target:%mspec_exe%" "-targetargs:%test_assemblies% --timeinfo --silent --html %spec_results%" -returntargetcode -filter:%coverage_filter% "-output:%coverage_results%"
@"%cover_exe%" -register:user "-target:%mspec_exe%" "-targetargs:%test_assemblies% --timeinfo --silent --html %spec_results%" -returntargetcode -filter:%coverage_filter% "-output:%coverage_results%"
@IF ERRORLEVEL 1 GOTO :EOF

@echo "%cover_exe%" -register:user "-target:%xunit_exe%" "-targetargs:%test_assemblies% -noshadow -html %xunit_results%" -mergeoutput -filter:%coverage_filter% "-output:%coverage_results%"
@"%cover_exe%" -register:user "-target:%xunit_exe%" "-targetargs:%test_assemblies% -noshadow -html %xunit_results%" -mergeoutput -filter:%coverage_filter% "-output:%coverage_results%"
@echo "%cover_exe%" -register:user "-target:%xunit_exe%" "-targetargs:%test_assemblies% -noshadow -html %xunit_results%" -returntargetcode -mergeoutput -filter:%coverage_filter% "-output:%coverage_results%"
@"%cover_exe%" -register:user "-target:%xunit_exe%" "-targetargs:%test_assemblies% -noshadow -html %xunit_results%" -returntargetcode -mergeoutput -filter:%coverage_filter% "-output:%coverage_results%"
@IF ERRORLEVEL 1 GOTO :EOF

@echo "%report_exe%" -verbosity:Error "-reports:%coverage_results%" "-targetdir:%~dp0src\TestResults" -reporttypes:XmlSummary
@"%report_exe%" -verbosity:Error "-reports:%coverage_results%" "-targetdir:%~dp0src\TestResults" -reporttypes:XmlSummary
Expand Down
6 changes: 6 additions & 0 deletions src/CloudFlare.NET/CloudFlare.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,16 @@
<Compile Include="CloudFlareResponse.cs" />
<Compile Include="CloudFlareResponseBase.cs" />
<Compile Include="CloudFlareResultInfo.cs" />
<Compile Include="DnsRecord.cs" />
<Compile Include="DnsRecordClientExtensions.cs" />
<Compile Include="DnsRecordType.cs" />
<Compile Include="HttpClientDnsRecordExtensions.cs" />
<Compile Include="HttpClientExtensions.cs" />
<Compile Include="HttpClientZoneExtensions.cs" />
<Compile Include="ICloudFlareClient.cs" />
<Compile Include="IdentifierTag.cs" />
<Compile Include="IdentifierTag.IEquatable.cs" />
<Compile Include="IDnsRecordClient.cs" />
<Compile Include="IZoneClient.cs" />
<Compile Include="Zone.cs" />
<Compile Include="ZoneClientExtensions.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/CloudFlare.NET/CloudFlare.NET.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>$id$</id>
<version>0.0.1</version>
<version>0.0.2</version>
<title>$title$</title>
<authors>James Skimming</authors>
<owners>James Skimming</owners>
Expand Down
11 changes: 10 additions & 1 deletion src/CloudFlare.NET/CloudFlareClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ public class CloudFlareClient : ICloudFlareClient
/// <inheritdoc/>
public Task<IReadOnlyList<Zone>> GetZonesAsync(CancellationToken cancellationToken, CloudFlareAuth auth = null)
{
return Client.GetZonesAsync(auth, cancellationToken);
return Client.GetZonesAsync(cancellationToken, auth);
}

/// <inheritdoc/>
public Task<IReadOnlyList<DnsRecord>> GetDnsRecordsAsync(
IdentifierTag zoneId,
CancellationToken cancellationToken,
CloudFlareAuth auth = null)
{
return Client.GetDnsRecordsAsync(zoneId, cancellationToken, auth);
}
}
}
149 changes: 149 additions & 0 deletions src/CloudFlare.NET/DnsRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
namespace CloudFlare.NET
{
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

/// <summary>
/// Represents a DNS record for a <see cref="Zone"/>.
/// </summary>
/// <seealso href="https://api.cloudflare.com/#dns-records-for-a-zone-properties"/>
public class DnsRecord
{
/// <summary>
/// Initializes a new instance of the <see cref="DnsRecord"/> class.
/// </summary>
public DnsRecord(
IdentifierTag id,
DnsRecordType type,
string name,
bool proxiable,
bool proxied,
int ttl,
bool locked,
IdentifierTag zoneId,
DateTimeOffset createdOn,
DateTimeOffset modifiedOn,
int priority,
string content = null,
string zoneName = null,
JObject data = null,
JObject meta = null)
{
if (id == null)
throw new ArgumentNullException(nameof(id));
if (name == null)
throw new ArgumentNullException(nameof(name));
if (zoneId == null)
throw new ArgumentNullException(nameof(zoneId));

Id = id;
Type = type;
Name = name;
Content = content ?? string.Empty;
Proxiable = proxiable;
Proxied = proxied;
Ttl = ttl;
Locked = locked;
ZoneId = zoneId;
ZoneName = zoneName ?? string.Empty;
CreatedOn = createdOn;
ModifiedOn = modifiedOn;
Data = data ?? new JObject();
Meta = meta ?? new JObject();
Priority = priority;
}

/// <summary>
/// API item identifier tag.
/// </summary>
[JsonProperty("id")]
public IdentifierTag Id { get; }

/// <summary>
/// Record type.
/// </summary>
[JsonProperty("type")]
public DnsRecordType Type { get; }

/// <summary>
/// A valid host name.
/// </summary>
[JsonProperty("name")]
public string Name { get; }

/// <summary>
/// Valid content for the given <see cref="Type"/>.
/// </summary>
[JsonProperty("content")]
public string Content { get; }

/// <summary>
/// Whether the record can be proxied by CloudFlare or not.
/// </summary>
[JsonProperty("proxiable")]
public bool Proxiable { get; }

/// <summary>
/// Whether the record is receiving the performance and security benefits of CloudFlare
/// </summary>
[JsonProperty("proxied")]
public bool Proxied { get; }

/// <summary>
/// Time to live for DNS record. Value of 1 is 'automatic'.
/// </summary>
[JsonProperty("ttl")]
public int Ttl { get; }

/// <summary>
/// Whether this record can be modified/deleted (<see langword="true"/> means it's managed by CloudFlare).
/// </summary>
[JsonProperty("locked")]
public bool Locked { get; }

/// <summary>
/// API item identifier tag.
/// </summary>
[JsonProperty("zone_id")]
public IdentifierTag ZoneId { get; }

/// <summary>
/// The domain of the record.
/// </summary>
[JsonProperty("zone_name")]
public string ZoneName { get; }

/// <summary>
/// When the record was created.
/// </summary>
[JsonProperty("created_on")]
public DateTimeOffset CreatedOn { get; }

/// <summary>
/// When the record was last modified.
/// </summary>
[JsonProperty("modified_on")]
public DateTimeOffset ModifiedOn { get; }

/// <summary>
/// Metadata about the record.
/// </summary>
[JsonProperty("data")]
public JObject Data { get; }

/// <summary>
/// Extra CloudFlare-specific information about the record.
/// </summary>
[JsonProperty("meta")]
public JObject Meta { get; }

/// <summary>
/// The priority of this is a <see cref="DnsRecordType.MX"/> record.
/// </summary>
[JsonProperty("priority")]
public int Priority { get; }
}
}
32 changes: 32 additions & 0 deletions src/CloudFlare.NET/DnsRecordClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace CloudFlare.NET
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
/// Helper extension methods on <see cref="IDnsRecordClient"/>.
/// </summary>
public static class DnsRecordClientExtensions
{
/// <summary>
/// Gets the zones for the subscription.
/// </summary>
/// <returns>The zones for the subscription.</returns>
public static Task<IReadOnlyList<DnsRecord>> GetDnsRecordsAsync(
this IDnsRecordClient client,
IdentifierTag zoneId,
CloudFlareAuth auth = null)
{
if (client == null)
throw new ArgumentNullException(nameof(client));
if (zoneId == null)
throw new ArgumentNullException(nameof(zoneId));

return client.GetDnsRecordsAsync(zoneId, CancellationToken.None, auth);
}
}
}
25 changes: 25 additions & 0 deletions src/CloudFlare.NET/DnsRecordType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace CloudFlare.NET
{
using System.Diagnostics.CodeAnalysis;

// ReSharper disable InconsistentNaming
#pragma warning disable 1591

/// <summary>
/// The type of a <see cref="DnsRecord"/>.
/// </summary>
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:EnumerationItemsMustBeDocumented",
Justification = "Names a self-explanatory.")]
public enum DnsRecordType
{
A,
AAAA,
CNAME,
TXT,
SRV,
LOC,
MX,
NS,
SPF,
}
}
46 changes: 46 additions & 0 deletions src/CloudFlare.NET/HttpClientDnsRecordExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace CloudFlare.NET
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
/// Extension methods on <see cref="HttpClient"/> to wrap the Zone APIs
/// </summary>
/// <seealso href="https://api.cloudflare.com/#dns-records-for-a-zone"/>
public static class HttpClientDnsRecordExtensions
{
/// <summary>
/// Gets the zones for the account specified by the <paramref name="auth"/> details.
/// </summary>
public static async Task<IReadOnlyList<DnsRecord>> GetDnsRecordsAsync(
this HttpClient client,
IdentifierTag zoneId,
CancellationToken cancellationToken,
CloudFlareAuth auth)
{
if (client == null)
throw new ArgumentNullException(nameof(client));
if (zoneId == null)
throw new ArgumentNullException(nameof(zoneId));
if (auth == null)
throw new ArgumentNullException(nameof(auth));

Uri uri = new Uri(CloudFlareConstants.BaseUri, $"zones/{zoneId}/dns_records");
var request = new HttpRequestMessage(HttpMethod.Get, uri);
request.AddAuth(auth);

HttpResponseMessage response =
await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
.ConfigureAwait(false);

return (await response
.GetResultAsync<IReadOnlyList<DnsRecord>>(cancellationToken)
.ConfigureAwait(false))
.Result;
}
}
}
Loading

0 comments on commit e320f7a

Please sign in to comment.