Exemplo n.º 1
0
    public void DnsUpdaterEntry_Given_Constructed_ShouldDefault_UpdateIntervalSec()
    {
        // act
        var entry = new DnsUpdaterEntry();

        // assert
        Assert.AreEqual(43200, entry.UpdateIntervalSec);
    }
Exemplo n.º 2
0
    public void DnsUpdaterEntry_Given_Constructed_ShouldDefault_Name()
    {
        // act
        var entry = new DnsUpdaterEntry();

        // assert
        Assert.AreEqual(string.Empty, entry.Name);
    }
Exemplo n.º 3
0
    public void DnsUpdaterEntry_Given_Constructed_ShouldDefault_Type()
    {
        // act
        var entry = new DnsUpdaterEntry();

        // assert
        Assert.AreEqual(DnsType.Unspecified, entry.Type);
    }
Exemplo n.º 4
0
    public void DnsUpdaterEntry_Given_Constructed_ShouldDefault_NextUpdate()
    {
        // act
        var entry = new DnsUpdaterEntry();

        // assert
        Assert.IsNull(entry.NextUpdate);
    }
Exemplo n.º 5
0
    public void DnsUpdaterEntry_Given_Constructed_ShouldDefault_Enabled()
    {
        // act
        var entry = new DnsUpdaterEntry();

        // assert
        Assert.IsFalse(entry.Enabled);
    }
Exemplo n.º 6
0
    public static int GetIntConfig(this DnsUpdaterEntry config, string key, int fallback)
    {
        if (!config.Config.ContainsKey(key))
        {
            return(fallback);
        }

        return(int.TryParse(config.Config[key], out var parsed) ? parsed : fallback);
    }
Exemplo n.º 7
0
    public void DnsUpdaterEntry_Given_Constructed_ShouldDefault_Config()
    {
        // act
        var entry = new DnsUpdaterEntry();

        // assert
        Assert.IsNotNull(entry.Config);
        Assert.IsInstanceOf <Dictionary <string, string> >(entry.Config);
        Assert.AreEqual(0, entry.Config.Count);
    }
Exemplo n.º 8
0
    private static bool NeedsUpdating(DnsUpdaterEntry entry, DateTime now)
    {
        if (!entry.Enabled)
        {
            return(false);
        }

        if (!entry.NextUpdate.HasValue)
        {
            return(true);
        }

        return(!(entry.NextUpdate > now));
    }
Exemplo n.º 9
0
    private async Task UpdateFreeDnsEntry(DnsUpdaterEntry entry, CancellationToken stoppingToken)
    {
        var updateUrl = entry.GetConfig(ConfigKeys.Url);
        var timeoutMs = entry.GetIntConfig(ConfigKeys.TimeoutMs, _config.DefaultHttpTimeoutMs);
        var request   = new HttpRequestMessage(HttpMethod.Get, updateUrl);
        var response  = await _httpService.SendAsync(request, timeoutMs, stoppingToken);

        var responseBody = await response.Content.ReadAsStringAsync(stoppingToken);

        _logger.LogInformation("Update response for {entryName}: ({code}) {body}",
                               entry.Name,
                               (int)response.StatusCode,
                               responseBody);
    }
Exemplo n.º 10
0
    public async Task UpdateEntryAsync(DnsUpdaterEntry entry, CancellationToken stoppingToken)
    {
        // ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
        switch (entry.Type)
        {
        case DnsType.FreeDns:
            await UpdateFreeDnsEntry(entry, stoppingToken);

            break;

        default:
            HandleMissingUpdater(entry);
            break;
        }

        // Set the next update time
        entry.NextUpdate = _dateTime.Now.AddSeconds(entry.UpdateIntervalSec);
    }
Exemplo n.º 11
0
 public static string?GetConfig(this DnsUpdaterEntry config, string key, string?fallback = null) =>
 !config.Config.ContainsKey(key) ? fallback : config.Config[key];
Exemplo n.º 12
0
 private void HandleMissingUpdater(DnsUpdaterEntry entry)
 {
     _logger.LogError("No updater found for DnsEntry type '{type}'",
                      entry.Type.ToString("G")
                      );
 }