public async Task TestUpdateDnsRecordAsync() { var zone = ZoneTestData.Zones.First(); var record = DnsRecordTestData.DnsRecords.First(); var modified = new ModifiedDnsRecord { Name = "new.tothnet.hu", }; _wireMockServer .Given(Request.Create().WithPath($"/{ZoneEndpoints.Base}/{zone.Id}/{DnsRecordEndpoints.Base}/{record.Id}").UsingPut()) .RespondWith(Response.Create().WithStatusCode(200) .WithBody(x => { var body = JsonConvert.DeserializeObject <ModifiedDnsRecord>(x.Body); var response = DnsRecordTestData.DnsRecords.First(y => y.Id == x.PathSegments[3]).DeepClone(); response.Name = body.Name; return(WireMockResponseHelper.CreateTestResponse(response)); })); using var client = new CloudFlareClient(WireMockConnection.ApiKeyAuthentication, _connectionInfo); var update = await client.Zones.DnsRecords.UpdateAsync(zone.Id, record.Id, modified); update.Result.Should().BeEquivalentTo(record, opt => opt.Excluding(x => x.Name)); update.Result.Name.Should().BeEquivalentTo("new.tothnet.hu"); }
public async Task UpdateDnsAsync(CancellationToken cancellationToken) { try { using var client = new CloudFlareClient(_authentication); var externalIpAddress = await GetIpAddressAsync(cancellationToken); _logger.Debug("Got ip from external provider: {ip}", externalIpAddress.ToString()); if (externalIpAddress == null) { _logger.Error($"All external IP providers failed to resolve the ip"); return; } var zones = (await client.Zones.GetAsync(cancellationToken: cancellationToken)).Result; _logger.Debug("Found the following zones : {@zones}", zones); foreach (var zone in zones) { var records = (await client.Zones.DnsRecords.GetAsync(zone.Id, new DnsRecordFilter { Type = DnsRecordType.A }, null, cancellationToken)).Result; _logger.Debug("Found the following 'A' records in zone '{zone}': {@records}", zone.Name, records); foreach (var record in records) { if (record.Type == DnsRecordType.A && record.Content != externalIpAddress.ToString()) { var modified = new ModifiedDnsRecord { Type = DnsRecordType.A, Name = record.Name, Content = externalIpAddress.ToString(), }; var updateResult = (await client.Zones.DnsRecords.UpdateAsync(zone.Id, record.Id, modified, cancellationToken)); if (!updateResult.Success) { _logger.Error("The following errors happened during update of record '{record}' in zone '{zone}': {@error}", record.Name, zone.Name, updateResult.Errors); } else { _logger.Information("Successfully updated record '{record}' ip from '{previousIp}' to '{externalIpAddress}' in zone '{zone}'", record.Name, record.Content, externalIpAddress.ToString(), zone.Name); } } else { _logger.Debug("The IP for record '{record}' in zone '{zone}' is already '{externalIpAddress}'", record.Name, zone.Name, externalIpAddress.ToString()); } } } } catch (Exception ex) { _logger.Error(ex.Message); } }
public void TestSerialization() { var sut = new ModifiedDnsRecord(); JsonHelper.GetSerializedKeys(sut).Should().BeEquivalentTo(new SortedSet <string> { "name", "content", "ttl", "proxied", "type" }); }
private async Task UpdateCloudFlareDns(List<string> hostnames, IPAddress ip, CancellationToken ct) { _logger.LogInformation($"{DateTimeOffset.Now}: Executing CloudFlare Dns updates"); // Run Cloudflare DNS Update Procdure using var client = new CloudFlareClient(_authentication); var zones = await client.Zones.GetAsync(cancellationToken: ct); // TODO: Refactor foreach to be async? foreach (var zone in zones.Result) { var dnsRecords = await client.Zones.DnsRecords.GetAsync(zone.Id, cancellationToken: ct); var aRecords = dnsRecords.Result .Where(x => x.Type == Client.Enumerators.DnsRecordType.A) .Where(x => hostnames.Contains(x.Name)) .ToList(); foreach(var record in aRecords) { var modified = new ModifiedDnsRecord { Type = DnsRecordType.A, Name = record.Name, Content = ip.ToString(), }; var updateResult = await client.Zones.DnsRecords.UpdateAsync(zone.Id, record.Id, modified, ct); if (!updateResult.Success) { _logger.LogError($"{DateTimeOffset.Now}: The following errors happened during update of record {record.Name} in zone {zone.Name}: {updateResult.Errors}"); } else { _logger.LogInformation($"{DateTimeOffset.Now}: Successfully updated record {record.Name} ip from {record.Content} to {ip.ToString()} in zone {zone.Name}"); } } } }
/// <inheritdoc /> public async Task <CloudFlareResult <DnsRecord> > UpdateAsync(string zoneId, string identifier, ModifiedDnsRecord modifiedDnsRecord, CancellationToken cancellationToken = default) { var requestUri = $"{ZoneEndpoints.Base}/{zoneId}/{DnsRecordEndpoints.Base}/{identifier}"; return(await Connection.PutAsync <DnsRecord, ModifiedDnsRecord>(requestUri, modifiedDnsRecord, cancellationToken).ConfigureAwait(false)); }