Skip to content

Commit

Permalink
Merge pull request #46 from libdns/opts
Browse files Browse the repository at this point in the history
fix: use environment variables to initialize provider
  • Loading branch information
aymanbagabas authored Jun 24, 2024
2 parents 339420f + 930887d commit e33c5c8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 18 deletions.
30 changes: 22 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,42 @@ func (p *Provider) init(ctx context.Context) {
if p.MaxRetries == 0 {
p.MaxRetries = 5
}
if p.Region == "" {
p.Region = "us-east-1"
}
if p.MaxWaitDur == 0 {
p.MaxWaitDur = time.Minute
}

opts := make([]func(*config.LoadOptions) error, 0)
opts = append(opts,
config.WithSharedConfigProfile(p.AWSProfile),
config.WithRegion(p.Region),
config.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), p.MaxRetries)
}),
)

profile := p.Profile
if profile == "" {
profile = p.AWSProfile
}

if profile != "" {
opts = append(opts, config.WithSharedConfigProfile(profile))
}

if p.Region != "" {
opts = append(opts, config.WithRegion(p.Region))
}

if p.AccessKeyId != "" && p.SecretAccessKey != "" {
token := p.SessionToken
if token == "" {
token = p.Token
}

opts = append(opts,
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(p.AccessKeyId, p.SecretAccessKey, p.Token)),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(p.AccessKeyId, p.SecretAccessKey, token)),
)
}
cfg, err := config.LoadDefaultConfig(ctx, opts...)

cfg, err := config.LoadDefaultConfig(ctx, opts...)
if err != nil {
log.Fatalf("route53: unable to load AWS SDK config, %v", err)
}
Expand Down Expand Up @@ -148,7 +162,7 @@ func marshalRecord(record libdns.Record) []types.ResourceRecord {
return resourceRecords
}

func (p *Provider) getRecords(ctx context.Context, zoneID string, zone string) ([]libdns.Record, error) {
func (p *Provider) getRecords(ctx context.Context, zoneID string, _ string) ([]libdns.Record, error) {
getRecordsInput := &r53.ListResourceRecordSetsInput{
HostedZoneId: aws.String(zoneID),
MaxItems: aws.Int32(1000),
Expand Down
59 changes: 49 additions & 10 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,56 @@ import (
"github.com/libdns/libdns"
)

// Provider implements the libdns interfaces for Route53
// Provider implements the libdns interfaces for Route53.
//
// By default, the provider loads the AWS configuration from the environment.
// To override these values, set the fields in the Provider struct.
type Provider struct {
MaxRetries int `json:"max_retries,omitempty"`
MaxWaitDur time.Duration `json:"max_wait_dur,omitempty"`
WaitForPropagation bool `json:"wait_for_propagation,omitempty"`
Region string `json:"region,omitempty"`
AWSProfile string `json:"aws_profile,omitempty"`
AccessKeyId string `json:"access_key_id,omitempty"`
SecretAccessKey string `json:"secret_access_key,omitempty"`
Token string `json:"token,omitempty"`
client *r53.Client
client *r53.Client

// Region is the AWS Region to use. If not set, it will use AWS_REGION
// environment variable.
Region string `json:"region,omitempty"`

// AWSProfile is the AWS Profile to use. If not set, it will use
// AWS_PROFILE environment variable.
//
// Deprecated: Use Profile instead
AWSProfile string `json:"aws_profile,omitempty"`

// AWSProfile is the AWS Profile to use. If not set, it will use
// AWS_PROFILE environment variable.
Profile string `json:"profile,omitempty"`

// AccessKeyId is the AWS Access Key ID to use. If not set, it will use
// AWS_ACCESS_KEY_ID
AccessKeyId string `json:"access_key_id,omitempty"`

// SecretAccessKey is the AWS Secret Access Key to use. If not set, it will use
// AWS_SECRET_ACCESS_KEY environment variable.
SecretAccessKey string `json:"secret_access_key,omitempty"`

// Token is the AWS Session Token to use. If not set, it will use
// AWS_SESSION_TOKEN environment variable.
//
// Deprecated: Use SessionToken instead.
Token string `json:"token,omitempty"`

// SessionToken is the AWS Session Token to use. If not set, it will use
// AWS_SESSION_TOKEN environment variable.
SessionToken string `json:"session_token,omitempty"`

// MaxRetries is the maximum number of retries to make when a request
// fails. If not set, it will use 5 retries.
MaxRetries int `json:"max_retries,omitempty"`

// MaxWaitDur is the maximum amount of time to wait for a record to be
// propagated. If not set, it will use 1 minutes.
MaxWaitDur time.Duration `json:"max_wait_dur,omitempty"`

// WaitForPropagation if set to true, it will wait for the record to be
// propagated before returning.
WaitForPropagation bool `json:"wait_for_propagation,omitempty"`
}

// GetRecords lists all the records in the zone.
Expand Down

0 comments on commit e33c5c8

Please sign in to comment.