Skip to content

Commit e33c5c8

Browse files
authored
Merge pull request #46 from libdns/opts
fix: use environment variables to initialize provider
2 parents 339420f + 930887d commit e33c5c8

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

client.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,42 @@ func (p *Provider) init(ctx context.Context) {
2626
if p.MaxRetries == 0 {
2727
p.MaxRetries = 5
2828
}
29-
if p.Region == "" {
30-
p.Region = "us-east-1"
31-
}
3229
if p.MaxWaitDur == 0 {
3330
p.MaxWaitDur = time.Minute
3431
}
3532

3633
opts := make([]func(*config.LoadOptions) error, 0)
3734
opts = append(opts,
38-
config.WithSharedConfigProfile(p.AWSProfile),
39-
config.WithRegion(p.Region),
4035
config.WithRetryer(func() aws.Retryer {
4136
return retry.AddWithMaxAttempts(retry.NewStandard(), p.MaxRetries)
4237
}),
4338
)
39+
40+
profile := p.Profile
41+
if profile == "" {
42+
profile = p.AWSProfile
43+
}
44+
45+
if profile != "" {
46+
opts = append(opts, config.WithSharedConfigProfile(profile))
47+
}
48+
49+
if p.Region != "" {
50+
opts = append(opts, config.WithRegion(p.Region))
51+
}
52+
4453
if p.AccessKeyId != "" && p.SecretAccessKey != "" {
54+
token := p.SessionToken
55+
if token == "" {
56+
token = p.Token
57+
}
58+
4559
opts = append(opts,
46-
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(p.AccessKeyId, p.SecretAccessKey, p.Token)),
60+
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(p.AccessKeyId, p.SecretAccessKey, token)),
4761
)
4862
}
49-
cfg, err := config.LoadDefaultConfig(ctx, opts...)
5063

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

151-
func (p *Provider) getRecords(ctx context.Context, zoneID string, zone string) ([]libdns.Record, error) {
165+
func (p *Provider) getRecords(ctx context.Context, zoneID string, _ string) ([]libdns.Record, error) {
152166
getRecordsInput := &r53.ListResourceRecordSetsInput{
153167
HostedZoneId: aws.String(zoneID),
154168
MaxItems: aws.Int32(1000),

provider.go

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,56 @@ import (
88
"github.com/libdns/libdns"
99
)
1010

11-
// Provider implements the libdns interfaces for Route53
11+
// Provider implements the libdns interfaces for Route53.
12+
//
13+
// By default, the provider loads the AWS configuration from the environment.
14+
// To override these values, set the fields in the Provider struct.
1215
type Provider struct {
13-
MaxRetries int `json:"max_retries,omitempty"`
14-
MaxWaitDur time.Duration `json:"max_wait_dur,omitempty"`
15-
WaitForPropagation bool `json:"wait_for_propagation,omitempty"`
16-
Region string `json:"region,omitempty"`
17-
AWSProfile string `json:"aws_profile,omitempty"`
18-
AccessKeyId string `json:"access_key_id,omitempty"`
19-
SecretAccessKey string `json:"secret_access_key,omitempty"`
20-
Token string `json:"token,omitempty"`
21-
client *r53.Client
16+
client *r53.Client
17+
18+
// Region is the AWS Region to use. If not set, it will use AWS_REGION
19+
// environment variable.
20+
Region string `json:"region,omitempty"`
21+
22+
// AWSProfile is the AWS Profile to use. If not set, it will use
23+
// AWS_PROFILE environment variable.
24+
//
25+
// Deprecated: Use Profile instead
26+
AWSProfile string `json:"aws_profile,omitempty"`
27+
28+
// AWSProfile is the AWS Profile to use. If not set, it will use
29+
// AWS_PROFILE environment variable.
30+
Profile string `json:"profile,omitempty"`
31+
32+
// AccessKeyId is the AWS Access Key ID to use. If not set, it will use
33+
// AWS_ACCESS_KEY_ID
34+
AccessKeyId string `json:"access_key_id,omitempty"`
35+
36+
// SecretAccessKey is the AWS Secret Access Key to use. If not set, it will use
37+
// AWS_SECRET_ACCESS_KEY environment variable.
38+
SecretAccessKey string `json:"secret_access_key,omitempty"`
39+
40+
// Token is the AWS Session Token to use. If not set, it will use
41+
// AWS_SESSION_TOKEN environment variable.
42+
//
43+
// Deprecated: Use SessionToken instead.
44+
Token string `json:"token,omitempty"`
45+
46+
// SessionToken is the AWS Session Token to use. If not set, it will use
47+
// AWS_SESSION_TOKEN environment variable.
48+
SessionToken string `json:"session_token,omitempty"`
49+
50+
// MaxRetries is the maximum number of retries to make when a request
51+
// fails. If not set, it will use 5 retries.
52+
MaxRetries int `json:"max_retries,omitempty"`
53+
54+
// MaxWaitDur is the maximum amount of time to wait for a record to be
55+
// propagated. If not set, it will use 1 minutes.
56+
MaxWaitDur time.Duration `json:"max_wait_dur,omitempty"`
57+
58+
// WaitForPropagation if set to true, it will wait for the record to be
59+
// propagated before returning.
60+
WaitForPropagation bool `json:"wait_for_propagation,omitempty"`
2261
}
2362

2463
// GetRecords lists all the records in the zone.

0 commit comments

Comments
 (0)