Skip to content

Commit

Permalink
Merge pull request #1 from apten-fors/apiurl
Browse files Browse the repository at this point in the history
Add opportunity to set GCORE_API_URL via ENV
  • Loading branch information
kokizzu authored Jul 29, 2024
2 parents ea63e2d + 746d34f commit 8971bce
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
29 changes: 27 additions & 2 deletions gcoreprovider/gcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package gcoreprovider
import (
"context"
"fmt"
"net/url"
"strings"
"time"

Expand All @@ -29,6 +30,7 @@ import (

const (
ProviderName = "gcore"
EnvAPIURL = "GCORE_API_URL"
EnvAPIToken = "GCORE_PERMANENT_API_TOKEN"
logDryRun = "[DryRun] "
maxTimeout = 60 * time.Second
Expand All @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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))
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const banner = `

var (
Version = "v0.0.1"
ApiUrl = ``
ApiKey = ``
ServerHost = ``
ServerPort = `8080`
Expand All @@ -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`)
Expand All @@ -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)
}
Expand Down

0 comments on commit 8971bce

Please sign in to comment.