From 746d34fd06342360b1ecf82bbc31f40b879d3804 Mon Sep 17 00:00:00 2001 From: Aleksandr Zhitnikov Date: Mon, 29 Jul 2024 16:08:28 +0300 Subject: [PATCH] Add opportunity to set GCORE_API_URL via ENV --- gcoreprovider/gcore.go | 29 +++++++++++++++++++++++++++-- main.go | 4 +++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/gcoreprovider/gcore.go b/gcoreprovider/gcore.go index 8776cfd..ea1ce4c 100644 --- a/gcoreprovider/gcore.go +++ b/gcoreprovider/gcore.go @@ -16,6 +16,7 @@ package gcoreprovider import ( "context" "fmt" + "net/url" "strings" "time" @@ -29,6 +30,7 @@ import ( const ( ProviderName = "gcore" + EnvAPIURL = "GCORE_API_URL" EnvAPIToken = "GCORE_PERMANENT_API_TOKEN" logDryRun = "[DryRun] " maxTimeout = 60 * time.Second @@ -48,7 +50,22 @@ type DnsProvider struct { dryRun bool } -func NewProvider(domainFilter endpoint.DomainFilter, apiKey string, dryRun bool) (*DnsProvider, error) { +func setClientBaseURL(client interface{}, apiUrl string) (*gdns.Client, error) { + c, ok := client.(*gdns.Client) + if !ok { + return nil, fmt.Errorf("type assertion to *gdns.Client failed") + } + + var err error + c.BaseURL, err = url.Parse(apiUrl) + if err != nil { + return nil, fmt.Errorf("failed parsing %s url: %w", apiUrl, err) + } + + return c, nil +} + +func NewProvider(domainFilter endpoint.DomainFilter, apiUrl, apiKey string, dryRun bool) (*DnsProvider, error) { log.Infof("%s: starting init provider: filters=%+v , dryRun=%v", ProviderName, domainFilter.Filters, dryRun) defer log.Infof("%s: finishing init provider", ProviderName) @@ -60,6 +77,14 @@ func NewProvider(domainFilter endpoint.DomainFilter, apiKey string, dryRun bool) dryRun: dryRun, } + if apiUrl != "" { + newClient, err := setClientBaseURL(p.client, apiUrl) + if err != nil { + return nil, err + } + p.client = newClient + } + return p, nil } @@ -379,5 +404,5 @@ func unexistingTargets(existing *endpoint.Endpoint, type EnvError string func (e EnvError) Error() string { - return fmt.Sprintf("invalid evirement var: %s", string(e)) + return fmt.Sprintf("invalid environment var: %s", string(e)) } diff --git a/main.go b/main.go index 450e945..43a12d7 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,7 @@ const banner = ` var ( Version = "v0.0.1" + ApiUrl = `` ApiKey = `` ServerHost = `` ServerPort = `8080` @@ -45,6 +46,7 @@ var ( func main() { log.SetLevel(log.DebugLevel) fmt.Printf(banner, Version) + ApiUrl = os.Getenv(gcoreprovider.EnvAPIURL) ApiKey = os.Getenv(gcoreprovider.EnvAPIToken) ServerHost = os.Getenv(`SERVER_HOST`) ServerPort = os.Getenv(`SERVER_PORT`) @@ -53,7 +55,7 @@ func main() { } DryRun = os.Getenv(`DRY_RUN`) == `true` - provider, err := gcoreprovider.NewProvider(endpoint.DomainFilter{}, ApiKey, DryRun) + provider, err := gcoreprovider.NewProvider(endpoint.DomainFilter{}, ApiUrl, ApiKey, DryRun) if err != nil { log.Fatalf("Failed to initialize DNS provider: %v", err) }