FEAT: Refactor allowing multiple DNS Providers
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2024-03-15 23:16:23 +13:00
parent a977adf929
commit 17014eeae1
10 changed files with 56416 additions and 75 deletions

View File

@@ -9,12 +9,14 @@ import (
)
const (
defaultCheckInterval = 5 * 60
envKeyDnsToCheck = "CLOUDFLARE_DNS_TO_CHECK"
defaultCheckInterval = 5 * 60
envKeyDnsToCheck = "DNS_NAMES"
envKeyPublicIpResolverTag = "PUBLIC_IP_RESOLVER"
envKeyCloudflareApiKey = "CLOUDFLARE_API_KEY"
envKeyCloudflareZone = "CLOUDFLARE_ZONE"
envKeyDirectadminUser = "DA_USER"
envKeyDirectadminKey = "DA_LOGIN_KEY"
envKeyDirectadminUrl = "DA_LOGIN_URL"
envKeyOnChangeComment = "ON_CHANGE_COMMENT"
envKeyCheckIntervalSeconds = "CHECK_INTERVAL_SECONDS"
envKeyNotifiers = "NOTIFIERS"
@@ -23,6 +25,10 @@ const (
type Config struct {
DnsRecordsToCheck []string
PublicIpResolverTag string
DNSProviderTag string
DirectadminUsername string
DirectadminKey string
DirectadminUrl string
ApiToken string
WebhookToken string
CloudflareZone string
@@ -32,12 +38,28 @@ type Config struct {
}
func (c Config) Validate() error {
if c.ApiToken == "" {
return fmt.Errorf("empty api token env key %s", envKeyCloudflareApiKey)
}
switch c.DNSProviderTag {
case "cloudflare":
if c.ApiToken == "" {
return fmt.Errorf("empty api token env key %s", envKeyCloudflareApiKey)
}
if c.CloudflareZone == "" {
return fmt.Errorf("empty zone in env key %s", envKeyCloudflareZone)
// if c.CloudflareZone == "" {
// return fmt.Errorf("empty zone in env key %s", envKeyCloudflareZone)
// }
case "directadmin":
if c.DirectadminUrl == "" {
return fmt.Errorf("empty DirectAdmin URL env key %s", envKeyDirectadminUrl)
}
if c.DirectadminUsername == "" {
return fmt.Errorf("empty Username in env key %s", envKeyDirectadminUser)
}
if c.DirectadminKey == "" {
return fmt.Errorf("empty Login Key in env key %s", envKeyDirectadminKey)
}
}
if len(c.DnsRecordsToCheck) == 0 {
@@ -58,6 +80,9 @@ func NewConfig() Config {
DnsRecordsToCheck: parseCommaDelimited(os.Getenv(envKeyDnsToCheck)),
PublicIpResolverTag: os.Getenv(envKeyPublicIpResolverTag),
ApiToken: os.Getenv(envKeyCloudflareApiKey),
DirectadminUsername: os.Getenv(envKeyDirectadminUser),
DirectadminKey: os.Getenv(envKeyDirectadminKey),
DirectadminUrl: os.Getenv(envKeyDirectadminUrl),
CloudflareZone: os.Getenv(envKeyCloudflareZone),
OnChangeComment: os.Getenv(envKeyOnChangeComment),
Notifiers: parseCommaDelimited(os.Getenv(envKeyNotifiers)),