-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(DNSimple): User API tokens #4274
Changes from 12 commits
9b3c285
ea2d259
26694e4
d0c121b
d475eb6
3babcbc
acf188f
eb59b9b
c54a9a8
9c24ecc
660b20b
3eb852f
487501d
ed3efdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -99,7 +99,14 @@ const ( | |||||
|
||||||
// NewDnsimpleProvider initializes a new Dnsimple based provider | ||||||
func NewDnsimpleProvider(domainFilter endpoint.DomainFilter, zoneIDFilter provider.ZoneIDFilter, dryRun bool) (provider.Provider, error) { | ||||||
return BuildDnsimpleProvider(domainFilter, zoneIDFilter, dryRun) | ||||||
} | ||||||
|
||||||
// Create a new Dnsimple based provider returning a *dnsimpleProvider. The *dnsimpleProvider return type is needed for testing purposes | ||||||
// therefore this method, and not NewDnsimpleProvider, must be the one used by dnsimple_test.go | ||||||
func BuildDnsimpleProvider(domainFilter endpoint.DomainFilter, zoneIDFilter provider.ZoneIDFilter, dryRun bool) (*dnsimpleProvider, error) { | ||||||
oauthToken := os.Getenv("DNSIMPLE_OAUTH") | ||||||
dnsimpleAccountId := os.Getenv("DNSIMPLE_ACCOUNT_ID") | ||||||
if len(oauthToken) == 0 { | ||||||
return nil, fmt.Errorf("no dnsimple oauth token provided") | ||||||
} | ||||||
|
@@ -118,11 +125,14 @@ func NewDnsimpleProvider(domainFilter endpoint.DomainFilter, zoneIDFilter provid | |||||
dryRun: dryRun, | ||||||
} | ||||||
|
||||||
whoamiResponse, err := provider.identity.Whoami(context.Background()) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
provider.accountID = dnsimpleAccountId | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in 487501d |
||||||
if provider.accountID == "" { | ||||||
whoamiResponse, err := provider.identity.Whoami(context.Background()) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
provider.accountID = int64ToString(whoamiResponse.Data.Account.ID) | ||||||
} | ||||||
provider.accountID = int64ToString(whoamiResponse.Data.Account.ID) | ||||||
return provider, nil | ||||||
} | ||||||
|
||||||
|
@@ -136,9 +146,29 @@ func (p *dnsimpleProvider) GetAccountID(ctx context.Context) (accountID string, | |||||
return int64ToString(whoamiResponse.Data.Account.ID), nil | ||||||
} | ||||||
|
||||||
func ZonesFromZoneString(zonestring string) map[string]dnsimple.Zone { | ||||||
zones := make(map[string]dnsimple.Zone) | ||||||
zoneNames := strings.Split(zonestring, ",") | ||||||
for indexId, zoneName := range zoneNames { | ||||||
zone := dnsimple.Zone{Name: zoneName, ID: int64(indexId)} | ||||||
zones[int64ToString(zone.ID)] = zone | ||||||
} | ||||||
return zones | ||||||
} | ||||||
|
||||||
// Returns a list of filtered Zones | ||||||
func (p *dnsimpleProvider) Zones(ctx context.Context) (map[string]dnsimple.Zone, error) { | ||||||
zones := make(map[string]dnsimple.Zone) | ||||||
|
||||||
// If the DNSIMPLE_ZONES environment variable is specified, generate a list of Zones from it | ||||||
// This is useful for when the DNSIMPLE_OAUTH environment variable is a User API token and | ||||||
// not an Account API token as the User API token will not have permissions to list Zones | ||||||
// belong to another account which the User has access permissions for. | ||||||
envZonesStr := os.Getenv("DNSIMPLE_ZONES") | ||||||
if envZonesStr != "" { | ||||||
return ZonesFromZoneString(envZonesStr), nil | ||||||
} | ||||||
|
||||||
page := 1 | ||||||
listOptions := &dnsimple.ZoneListOptions{} | ||||||
for { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,9 +33,10 @@ import ( | |
) | ||
|
||
var ( | ||
mockProvider dnsimpleProvider | ||
dnsimpleListRecordsResponse dnsimple.ZoneRecordsResponse | ||
dnsimpleListZonesResponse dnsimple.ZonesResponse | ||
mockProvider dnsimpleProvider | ||
dnsimpleListRecordsResponse dnsimple.ZoneRecordsResponse | ||
dnsimpleListZonesResponse dnsimple.ZonesResponse | ||
dnsimpleListZonesFromEnvResponse dnsimple.ZonesResponse | ||
) | ||
|
||
func TestDnsimpleServices(t *testing.T) { | ||
|
@@ -55,6 +56,16 @@ func TestDnsimpleServices(t *testing.T) { | |
Response: dnsimple.Response{Pagination: &dnsimple.Pagination{}}, | ||
Data: zones, | ||
} | ||
firstEnvDefinedZone := dnsimple.Zone{ | ||
ID: 0, | ||
AccountID: 12345, | ||
Name: "example-from-env.com", | ||
} | ||
envDefinedZones := []dnsimple.Zone{firstEnvDefinedZone} | ||
dnsimpleListZonesFromEnvResponse = dnsimple.ZonesResponse{ | ||
Response: dnsimple.Response{Pagination: &dnsimple.Pagination{}}, | ||
Data: envDefinedZones, | ||
} | ||
firstRecord := dnsimple.ZoneRecord{ | ||
ID: 2, | ||
ZoneID: "example.com", | ||
|
@@ -151,6 +162,15 @@ func testDnsimpleProviderZones(t *testing.T) { | |
mockProvider.accountID = "2" | ||
_, err = mockProvider.Zones(ctx) | ||
assert.NotNil(t, err) | ||
|
||
mockProvider.accountID = "3" | ||
os.Setenv("DNSIMPLE_ZONES", "example-from-env.com") | ||
result, err = mockProvider.Zones(ctx) | ||
assert.Nil(t, err) | ||
validateDnsimpleZones(t, result, dnsimpleListZonesFromEnvResponse.Data) | ||
|
||
mockProvider.accountID = "2" | ||
os.Unsetenv("DNSIMPLE_ZONES") | ||
} | ||
|
||
func testDnsimpleProviderRecords(t *testing.T) { | ||
|
@@ -207,6 +227,17 @@ func testDnsimpleSuitableZone(t *testing.T) { | |
|
||
zone := dnsimpleSuitableZone("example-beta.example.com", zones) | ||
assert.Equal(t, zone.Name, "example.com") | ||
|
||
os.Setenv("DNSIMPLE_ZONES", "environment-example.com,example.environment-example.com") | ||
mockProvider.accountID = "3" | ||
zones, err = mockProvider.Zones(ctx) | ||
assert.Nil(t, err) | ||
|
||
zone = dnsimpleSuitableZone("hello.example.environment-example.com", zones) | ||
assert.Equal(t, zone.Name, "example.environment-example.com") | ||
|
||
os.Unsetenv("DNSIMPLE_ZONES") | ||
mockProvider.accountID = "1" | ||
} | ||
|
||
func TestNewDnsimpleProvider(t *testing.T) { | ||
|
@@ -215,10 +246,22 @@ func TestNewDnsimpleProvider(t *testing.T) { | |
if err == nil { | ||
t.Errorf("Expected to fail new provider on bad token") | ||
} | ||
|
||
os.Unsetenv("DNSIMPLE_OAUTH") | ||
_, err = NewDnsimpleProvider(endpoint.NewDomainFilter([]string{"example.com"}), provider.NewZoneIDFilter([]string{""}), true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change wasn't really within the scope of this PR but this should be added in any case as without it, |
||
if err == nil { | ||
t.Errorf("Expected to fail new provider on empty token") | ||
} | ||
|
||
os.Setenv("DNSIMPLE_OAUTH", "xxxxxxxxxxxxxxxxxxxxxxxxxx") | ||
os.Setenv("DNSIMPLE_ACCOUNT_ID", "12345678") | ||
builtProvider, err := BuildDnsimpleProvider(endpoint.NewDomainFilter([]string{"example.com"}), provider.NewZoneIDFilter([]string{""}), true) | ||
if err != nil { | ||
t.Errorf("Unexpected error thrown when testing BuildDnsimpleProvider with the DNSIMPLE_ACCOUNT_ID environment variable set") | ||
} | ||
assert.Equal(t, builtProvider.accountID, "12345678") | ||
os.Unsetenv("DNSIMPLE_OAUTH") | ||
os.Unsetenv("DNSIMPLE_ACCOUNT_ID") | ||
} | ||
|
||
func testDnsimpleGetRecordID(t *testing.T) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 487501d