diff --git a/mockns1/example_test.go b/mockns1/example_test.go index 369bc1b..d38d06f 100644 --- a/mockns1/example_test.go +++ b/mockns1/example_test.go @@ -1,6 +1,7 @@ package mockns1_test import ( + "context" "net/http" "net/url" "testing" @@ -29,7 +30,7 @@ func Example() { []*dns.Zone{{Zone: "foo.bar"}})) // Perform your tests - zones, _, err := ns1.Zones.List() + zones, _, err := ns1.Zones.List(context.Background()) require.Nil(t, err) require.Equal(t, 1, len(zones)) require.Equal(t, "foo.bar", zones[0].Zone) diff --git a/mockns1/network_test.go b/mockns1/network_test.go index 9b029ee..33a4b27 100644 --- a/mockns1/network_test.go +++ b/mockns1/network_test.go @@ -1,6 +1,7 @@ package mockns1_test import ( + "context" "testing" "github.com/stretchr/testify/require" @@ -37,7 +38,7 @@ func TestNetwork(t *testing.T) { require.Nil(t, mock.NetworkGetTestCase(nil, nil, networks)) - resp, _, err := client.Network.Get() + resp, _, err := client.Network.Get(context.Background()) require.Nil(t, err) require.NotNil(t, resp) require.Equal(t, len(networks), len(resp)) diff --git a/mockns1/zone_test.go b/mockns1/zone_test.go index 5279b05..10f1c59 100644 --- a/mockns1/zone_test.go +++ b/mockns1/zone_test.go @@ -1,6 +1,7 @@ package mockns1_test import ( + "context" "encoding/json" "testing" @@ -27,7 +28,7 @@ func TestZone(t *testing.T) { require.Nil(t, mock.AddZoneListTestCase(nil, nil, zones)) - resp, _, err := client.Zones.List() + resp, _, err := client.Zones.List(context.Background()) require.Nil(t, err) require.NotNil(t, resp) require.Equal(t, len(zones), len(resp)) @@ -48,7 +49,7 @@ func TestZone(t *testing.T) { require.Nil(t, mock.AddZoneGetTestCase(zone.Zone, nil, nil, zone)) - resp, _, err := client.Zones.Get(zone.Zone) + resp, _, err := client.Zones.Get(context.Background(), zone.Zone) require.Nil(t, err) require.NotNil(t, resp) require.Equal(t, zone.Zone, resp.Zone) @@ -71,7 +72,7 @@ func TestZone(t *testing.T) { require.Nil(t, mock.AddZoneCreateTestCase(nil, nil, zone, resp)) require.Zero(t, zone.TTL) - _, err := client.Zones.Create(zone) + _, err := client.Zones.Create(context.Background(), zone) require.Nil(t, err) require.Equal(t, zone.TTL, resp.TTL) }) @@ -84,14 +85,14 @@ func TestZone(t *testing.T) { require.Nil(t, mock.AddZoneUpdateTestCase(nil, nil, zone, zone)) - _, err := client.Zones.Update(zone) + _, err := client.Zones.Update(context.Background(), zone) require.Nil(t, err) }) t.Run("AddZoneDeleteTestCase", func(t *testing.T) { require.Nil(t, mock.AddZoneDeleteTestCase("delete.zone", nil, nil)) - _, err := client.Zones.Delete("delete.zone") + _, err := client.Zones.Delete(context.Background(), "delete.zone") require.Nil(t, err) }) } diff --git a/rest/account_apikey.go b/rest/account_apikey.go index dbfd339..a24c7ef 100644 --- a/rest/account_apikey.go +++ b/rest/account_apikey.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -14,8 +15,8 @@ type APIKeysService service // List returns all api keys in the account. // // NS1 API docs: https://ns1.com/api/#apikeys-get -func (s *APIKeysService) List() ([]*account.APIKey, *http.Response, error) { - req, err := s.client.NewRequest("GET", "account/apikeys", nil) +func (s *APIKeysService) List(ctx context.Context) ([]*account.APIKey, *http.Response, error) { + req, err := s.client.NewRequest(ctx, "GET", "account/apikeys", nil) if err != nil { return nil, nil, err } @@ -33,10 +34,10 @@ func (s *APIKeysService) List() ([]*account.APIKey, *http.Response, error) { // Note: do not use the API Key itself as the keyid in the URL — use the id of the key. // // NS1 API docs: https://ns1.com/api/#apikeys-id-get -func (s *APIKeysService) Get(keyID string) (*account.APIKey, *http.Response, error) { +func (s *APIKeysService) Get(ctx context.Context, keyID string) (*account.APIKey, *http.Response, error) { path := fmt.Sprintf("account/apikeys/%s", keyID) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -60,7 +61,7 @@ func (s *APIKeysService) Get(keyID string) (*account.APIKey, *http.Response, err // Create takes a *APIKey and creates a new account apikey. // // NS1 API docs: https://ns1.com/api/#apikeys-put -func (s *APIKeysService) Create(a *account.APIKey) (*http.Response, error) { +func (s *APIKeysService) Create(ctx context.Context, a *account.APIKey) (*http.Response, error) { var ( req *http.Request err error @@ -69,12 +70,12 @@ func (s *APIKeysService) Create(a *account.APIKey) (*http.Response, error) { // If this is DDI then the permissions need to be transformed to DDI-compatible permissions. if s.client.DDI && a != nil { ddiAPIKey := apiKeyToDDIAPIKey(a) - req, err = s.client.NewRequest("PUT", "account/apikeys", ddiAPIKey) + req, err = s.client.NewRequest(ctx, "PUT", "account/apikeys", ddiAPIKey) if err != nil { return nil, err } } else { - req, err = s.client.NewRequest("PUT", "account/apikeys", a) + req, err = s.client.NewRequest(ctx, "PUT", "account/apikeys", a) if err != nil { return nil, err } @@ -98,7 +99,7 @@ func (s *APIKeysService) Create(a *account.APIKey) (*http.Response, error) { // Update changes the name or access rights for an API Key. // // NS1 API docs: https://ns1.com/api/#apikeys-id-post -func (s *APIKeysService) Update(a *account.APIKey) (*http.Response, error) { +func (s *APIKeysService) Update(ctx context.Context, a *account.APIKey) (*http.Response, error) { path := fmt.Sprintf("account/apikeys/%s", a.ID) var ( @@ -109,12 +110,12 @@ func (s *APIKeysService) Update(a *account.APIKey) (*http.Response, error) { // If this is DDI then the permissions need to be transformed to DDI-compatible permissions. if s.client.DDI && a != nil { ddiAPIKey := apiKeyToDDIAPIKey(a) - req, err = s.client.NewRequest("POST", path, ddiAPIKey) + req, err = s.client.NewRequest(ctx, "POST", path, ddiAPIKey) if err != nil { return nil, err } } else { - req, err = s.client.NewRequest("POST", path, a) + req, err = s.client.NewRequest(ctx, "POST", path, a) if err != nil { return nil, err } @@ -138,10 +139,10 @@ func (s *APIKeysService) Update(a *account.APIKey) (*http.Response, error) { // Delete deletes an apikey. // // NS1 API docs: https://ns1.com/api/#apikeys-id-delete -func (s *APIKeysService) Delete(keyID string) (*http.Response, error) { +func (s *APIKeysService) Delete(ctx context.Context, keyID string) (*http.Response, error) { path := fmt.Sprintf("account/apikeys/%s", keyID) - req, err := s.client.NewRequest("DELETE", path, nil) + req, err := s.client.NewRequest(ctx, "DELETE", path, nil) if err != nil { return nil, err } diff --git a/rest/account_apikey_test.go b/rest/account_apikey_test.go index 386a3bf..464d034 100644 --- a/rest/account_apikey_test.go +++ b/rest/account_apikey_test.go @@ -1,6 +1,7 @@ package rest import ( + "context" "encoding/json" "io/ioutil" "net/http" @@ -36,7 +37,7 @@ func TestCreateAPIKey(t *testing.T) { Permissions: account.PermissionsMap{}, } - _, err := c.APIKeys.Create(k) + _, err := c.APIKeys.Create(context.Background(), k) require.NoError(t, err) } @@ -95,7 +96,7 @@ func TestCreateDDIAPIKey(t *testing.T) { Permissions: account.PermissionsMap{}, } - _, err := c.APIKeys.Create(k) + _, err := c.APIKeys.Create(context.Background(), k) require.NoError(t, err) // Create a key with auth tags k = &account.APIKey{ @@ -150,6 +151,6 @@ func TestCreateDDIAPIKey(t *testing.T) { }, } - _, err = c.APIKeys.Create(k) + _, err = c.APIKeys.Create(context.Background(), k) require.NoError(t, err) } diff --git a/rest/account_setting.go b/rest/account_setting.go index 6211f13..469685b 100644 --- a/rest/account_setting.go +++ b/rest/account_setting.go @@ -1,6 +1,7 @@ package rest import ( + "context" "net/http" "gopkg.in/ns1/ns1-go.v2/rest/model/account" @@ -12,8 +13,8 @@ type SettingsService service // Get returns the basic contact details associated with the account. // // NS1 API docs: https://ns1.com/api/#settings-get -func (s *SettingsService) Get() (*account.Setting, *http.Response, error) { - req, err := s.client.NewRequest("GET", "account/settings", nil) +func (s *SettingsService) Get(ctx context.Context) (*account.Setting, *http.Response, error) { + req, err := s.client.NewRequest(ctx, "GET", "account/settings", nil) if err != nil { return nil, nil, err } @@ -30,8 +31,8 @@ func (s *SettingsService) Get() (*account.Setting, *http.Response, error) { // Update changes most of the basic contact details, except customerid. // // NS1 API docs: https://ns1.com/api/#settings-post -func (s *SettingsService) Update(us *account.Setting) (*http.Response, error) { - req, err := s.client.NewRequest("POST", "account/settings", &us) +func (s *SettingsService) Update(ctx context.Context, us *account.Setting) (*http.Response, error) { + req, err := s.client.NewRequest(ctx, "POST", "account/settings", &us) if err != nil { return nil, err } diff --git a/rest/account_team.go b/rest/account_team.go index 1ff8dd9..5078851 100644 --- a/rest/account_team.go +++ b/rest/account_team.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -14,8 +15,8 @@ type TeamsService service // List returns all teams in the account. // // NS1 API docs: https://ns1.com/api/#teams-get -func (s *TeamsService) List() ([]*account.Team, *http.Response, error) { - req, err := s.client.NewRequest("GET", "account/teams", nil) +func (s *TeamsService) List(ctx context.Context) ([]*account.Team, *http.Response, error) { + req, err := s.client.NewRequest(ctx, "GET", "account/teams", nil) if err != nil { return nil, nil, err } @@ -32,10 +33,10 @@ func (s *TeamsService) List() ([]*account.Team, *http.Response, error) { // Get returns details of a single team. // // NS1 API docs: https://ns1.com/api/#teams-id-get -func (s *TeamsService) Get(id string) (*account.Team, *http.Response, error) { +func (s *TeamsService) Get(ctx context.Context, id string) (*account.Team, *http.Response, error) { path := fmt.Sprintf("account/teams/%s", id) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -58,7 +59,7 @@ func (s *TeamsService) Get(id string) (*account.Team, *http.Response, error) { // Create takes a *Team and creates a new account team. // // NS1 API docs: https://ns1.com/api/#teams-put -func (s *TeamsService) Create(t *account.Team) (*http.Response, error) { +func (s *TeamsService) Create(ctx context.Context, t *account.Team) (*http.Response, error) { var ( req *http.Request err error @@ -67,12 +68,12 @@ func (s *TeamsService) Create(t *account.Team) (*http.Response, error) { // If this is DDI then the permissions need to be transformed to DDI-compatible permissions. if s.client.DDI && t != nil { ddiTeam := teamToDDITeam(t) - req, err = s.client.NewRequest("PUT", "account/teams", ddiTeam) + req, err = s.client.NewRequest(ctx, "PUT", "account/teams", ddiTeam) if err != nil { return nil, err } } else { - req, err = s.client.NewRequest("PUT", "account/teams", t) + req, err = s.client.NewRequest(ctx, "PUT", "account/teams", t) if err != nil { return nil, err } @@ -96,7 +97,7 @@ func (s *TeamsService) Create(t *account.Team) (*http.Response, error) { // Update changes the name or access rights for a team. // // NS1 API docs: https://ns1.com/api/#teams-id-post -func (s *TeamsService) Update(t *account.Team) (*http.Response, error) { +func (s *TeamsService) Update(ctx context.Context, t *account.Team) (*http.Response, error) { path := fmt.Sprintf("account/teams/%s", t.ID) var ( @@ -107,12 +108,12 @@ func (s *TeamsService) Update(t *account.Team) (*http.Response, error) { // If this is DDI then the permissions need to be transformed to DDI-compatible permissions. if s.client.DDI && t != nil { ddiTeam := teamToDDITeam(t) - req, err = s.client.NewRequest("POST", path, ddiTeam) + req, err = s.client.NewRequest(ctx, "POST", path, ddiTeam) if err != nil { return nil, err } } else { - req, err = s.client.NewRequest("POST", path, t) + req, err = s.client.NewRequest(ctx, "POST", path, t) if err != nil { return nil, err } @@ -136,10 +137,10 @@ func (s *TeamsService) Update(t *account.Team) (*http.Response, error) { // Delete deletes a team. // // NS1 API docs: https://ns1.com/api/#teams-id-delete -func (s *TeamsService) Delete(id string) (*http.Response, error) { +func (s *TeamsService) Delete(ctx context.Context, id string) (*http.Response, error) { path := fmt.Sprintf("account/teams/%s", id) - req, err := s.client.NewRequest("DELETE", path, nil) + req, err := s.client.NewRequest(ctx, "DELETE", path, nil) if err != nil { return nil, err } diff --git a/rest/account_team_test.go b/rest/account_team_test.go index 306b83a..47a5fce 100644 --- a/rest/account_team_test.go +++ b/rest/account_team_test.go @@ -1,6 +1,7 @@ package rest import ( + "context" "encoding/json" "io/ioutil" "net/http" @@ -34,7 +35,7 @@ func TestCreateTeam(t *testing.T) { Permissions: account.PermissionsMap{}, } - _, err := c.Teams.Create(tm) + _, err := c.Teams.Create(context.Background(), tm) require.NoError(t, err) } @@ -64,6 +65,6 @@ func TestCreateDDITeam(t *testing.T) { Permissions: account.PermissionsMap{}, } - _, err := c.Teams.Create(tm) + _, err := c.Teams.Create(context.Background(), tm) require.NoError(t, err) } diff --git a/rest/account_user.go b/rest/account_user.go index 82bc7cf..babe83b 100644 --- a/rest/account_user.go +++ b/rest/account_user.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -14,8 +15,8 @@ type UsersService service // List returns all users in the account. // // NS1 API docs: https://ns1.com/api/#users-get -func (s *UsersService) List() ([]*account.User, *http.Response, error) { - req, err := s.client.NewRequest("GET", "account/users", nil) +func (s *UsersService) List(ctx context.Context) ([]*account.User, *http.Response, error) { + req, err := s.client.NewRequest(ctx, "GET", "account/users", nil) if err != nil { return nil, nil, err } @@ -32,10 +33,10 @@ func (s *UsersService) List() ([]*account.User, *http.Response, error) { // Get returns details of a single user. // // NS1 API docs: https://ns1.com/api/#users-user-get -func (s *UsersService) Get(username string) (*account.User, *http.Response, error) { +func (s *UsersService) Get(ctx context.Context, username string) (*account.User, *http.Response, error) { path := fmt.Sprintf("account/users/%s", username) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -58,7 +59,7 @@ func (s *UsersService) Get(username string) (*account.User, *http.Response, erro // Create takes a *User and creates a new account user. // // NS1 API docs: https://ns1.com/api/#users-put -func (s *UsersService) Create(u *account.User) (*http.Response, error) { +func (s *UsersService) Create(ctx context.Context, u *account.User) (*http.Response, error) { var ( req *http.Request err error @@ -67,12 +68,12 @@ func (s *UsersService) Create(u *account.User) (*http.Response, error) { // If this is DDI then the permissions need to be transformed to DDI-compatible permissions. if s.client.DDI && u != nil { ddiUser := userToDDIUser(u) - req, err = s.client.NewRequest("PUT", "account/users", ddiUser) + req, err = s.client.NewRequest(ctx, "PUT", "account/users", ddiUser) if err != nil { return nil, err } } else { - req, err = s.client.NewRequest("PUT", "account/users", u) + req, err = s.client.NewRequest(ctx, "PUT", "account/users", u) if err != nil { return nil, err } @@ -96,7 +97,7 @@ func (s *UsersService) Create(u *account.User) (*http.Response, error) { // Update change contact details, notification settings, or access rights for a user. // // NS1 API docs: https://ns1.com/api/#users-user-post -func (s *UsersService) Update(u *account.User) (*http.Response, error) { +func (s *UsersService) Update(ctx context.Context, u *account.User) (*http.Response, error) { path := fmt.Sprintf("account/users/%s", u.Username) var ( @@ -107,12 +108,12 @@ func (s *UsersService) Update(u *account.User) (*http.Response, error) { // If this is DDI then the permissions need to be transformed to DDI-compatible permissions. if s.client.DDI && u != nil { ddiUser := userToDDIUser(u) - req, err = s.client.NewRequest("POST", path, ddiUser) + req, err = s.client.NewRequest(ctx, "POST", path, ddiUser) if err != nil { return nil, err } } else { - req, err = s.client.NewRequest("POST", path, u) + req, err = s.client.NewRequest(ctx, "POST", path, u) if err != nil { return nil, err } @@ -136,10 +137,10 @@ func (s *UsersService) Update(u *account.User) (*http.Response, error) { // Delete deletes a user. // // NS1 API docs: https://ns1.com/api/#users-user-delete -func (s *UsersService) Delete(username string) (*http.Response, error) { +func (s *UsersService) Delete(ctx context.Context, username string) (*http.Response, error) { path := fmt.Sprintf("account/users/%s", username) - req, err := s.client.NewRequest("DELETE", path, nil) + req, err := s.client.NewRequest(ctx, "DELETE", path, nil) if err != nil { return nil, err } diff --git a/rest/account_user_test.go b/rest/account_user_test.go index e6590de..e1b38ea 100644 --- a/rest/account_user_test.go +++ b/rest/account_user_test.go @@ -1,6 +1,7 @@ package rest import ( + "context" "encoding/json" "io/ioutil" "net/http" @@ -35,7 +36,7 @@ func TestCreateUser(t *testing.T) { Permissions: account.PermissionsMap{}, } - _, err := c.Users.Create(u) + _, err := c.Users.Create(context.Background(), u) require.NoError(t, err) } @@ -66,6 +67,6 @@ func TestCreateDDIUser(t *testing.T) { Permissions: account.PermissionsMap{}, } - _, err := c.Users.Create(u) + _, err := c.Users.Create(context.Background(), u) require.NoError(t, err) } diff --git a/rest/account_warning.go b/rest/account_warning.go index 78d4588..7c9a4b4 100644 --- a/rest/account_warning.go +++ b/rest/account_warning.go @@ -1,6 +1,7 @@ package rest import ( + "context" "net/http" "gopkg.in/ns1/ns1-go.v2/rest/model/account" @@ -13,8 +14,8 @@ type WarningsService service // alert messages to users with billing notifications enabled. // // NS1 API docs: https://ns1.com/api/#usagewarnings-get -func (s *WarningsService) Get() (*account.UsageWarning, *http.Response, error) { - req, err := s.client.NewRequest("GET", "account/usagewarnings", nil) +func (s *WarningsService) Get(ctx context.Context) (*account.UsageWarning, *http.Response, error) { + req, err := s.client.NewRequest(ctx, "GET", "account/usagewarnings", nil) if err != nil { return nil, nil, err } @@ -31,8 +32,8 @@ func (s *WarningsService) Get() (*account.UsageWarning, *http.Response, error) { // Update changes alerting toggles and thresholds for overage warning alert messages. // // NS1 API docs: https://ns1.com/api/#usagewarnings-post -func (s *WarningsService) Update(uw *account.UsageWarning) (*http.Response, error) { - req, err := s.client.NewRequest("POST", "account/usagewarnings", &uw) +func (s *WarningsService) Update(ctx context.Context, uw *account.UsageWarning) (*http.Response, error) { + req, err := s.client.NewRequest(ctx, "POST", "account/usagewarnings", &uw) if err != nil { return nil, err } diff --git a/rest/application.go b/rest/application.go index 0aa4eb2..48e38b6 100644 --- a/rest/application.go +++ b/rest/application.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -14,8 +15,8 @@ type ApplicationsService service // List returns all pulsar Applications // // NS1 API docs: https://ns1.com/api#get-list-pulsar-applications -func (s *ApplicationsService) List() ([]*pulsar.Application, *http.Response, error) { - req, err := s.client.NewRequest("GET", "pulsar/apps", nil) +func (s *ApplicationsService) List(ctx context.Context) ([]*pulsar.Application, *http.Response, error) { + req, err := s.client.NewRequest(ctx, "GET", "pulsar/apps", nil) if err != nil { return nil, nil, err } @@ -32,10 +33,10 @@ func (s *ApplicationsService) List() ([]*pulsar.Application, *http.Response, err // Get takes a application id and returns application struct. // // NS1 API docs: https://ns1.com/api#get-list-pulsar-applications -func (s *ApplicationsService) Get(id string) (*pulsar.Application, *http.Response, error) { +func (s *ApplicationsService) Get(ctx context.Context, id string) (*pulsar.Application, *http.Response, error) { path := fmt.Sprintf("pulsar/apps/%s", id) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -59,8 +60,8 @@ func (s *ApplicationsService) Get(id string) (*pulsar.Application, *http.Respons // // The given application must have at least the name // NS1 API docs: https://ns1.com/api#put-create-a-pulsar-application -func (s *ApplicationsService) Create(a *pulsar.Application) (*http.Response, error) { - req, err := s.client.NewRequest("PUT", "pulsar/apps", a) +func (s *ApplicationsService) Create(ctx context.Context, a *pulsar.Application) (*http.Response, error) { + req, err := s.client.NewRequest(ctx, "PUT", "pulsar/apps", a) if err != nil { return nil, err } @@ -71,10 +72,10 @@ func (s *ApplicationsService) Create(a *pulsar.Application) (*http.Response, err // Update takes a *pulsar.Application and updates the application with same id on Ns1. // // NS1 API docs: https://ns1.com/api#post-modify-an-application -func (s *ApplicationsService) Update(a *pulsar.Application) (*http.Response, error) { +func (s *ApplicationsService) Update(ctx context.Context, a *pulsar.Application) (*http.Response, error) { path := fmt.Sprintf("pulsar/apps/%s", a.ID) - req, err := s.client.NewRequest("POST", path, &a) + req, err := s.client.NewRequest(ctx, "POST", path, &a) if err != nil { return nil, err } @@ -96,10 +97,10 @@ func (s *ApplicationsService) Update(a *pulsar.Application) (*http.Response, err // Delete takes a application Id, and removes an existing application // // NS1 API docs: https://ns1.com/api#delete-delete-a-pulsar-application -func (s *ApplicationsService) Delete(id string) (*http.Response, error) { +func (s *ApplicationsService) Delete(ctx context.Context, id string) (*http.Response, error) { path := fmt.Sprintf("pulsar/apps/%s", id) - req, err := s.client.NewRequest("DELETE", path, nil) + req, err := s.client.NewRequest(ctx, "DELETE", path, nil) if err != nil { return nil, err } diff --git a/rest/application_test.go b/rest/application_test.go index 422818a..698c1ea 100644 --- a/rest/application_test.go +++ b/rest/application_test.go @@ -1,6 +1,7 @@ package rest_test import ( + "context" "net/http" "testing" @@ -33,7 +34,7 @@ func TestApplication(t *testing.T) { require.Nil(t, mock.AddApplicationTestCase(nil, header, applications)) - respApplications, resp, err := client.Applications.List() + respApplications, resp, err := client.Applications.List(context.Background()) require.Nil(t, err) require.NotNil(t, respApplications) require.Equal(t, len(applications), len(respApplications)) @@ -53,7 +54,7 @@ func TestApplication(t *testing.T) { nil, nil, "", `{"message": "test error"}`, )) - applications, resp, err := client.Applications.List() + applications, resp, err := client.Applications.List(context.Background()) require.Nil(t, applications) require.NotNil(t, err) require.Contains(t, err.Error(), "test error") @@ -62,7 +63,7 @@ func TestApplication(t *testing.T) { t.Run("Other", func(t *testing.T) { c := api.NewClient(errorClient{}, api.SetEndpoint("")) - applications, resp, err := c.Applications.List() + applications, resp, err := c.Applications.List(context.Background()) require.Nil(t, resp) require.Error(t, err) require.Nil(t, applications) @@ -86,7 +87,7 @@ func TestApplication(t *testing.T) { header, pulsar.NewApplication(name))) - respApplication, resp, err := client.Applications.Get(id) + respApplication, resp, err := client.Applications.Get(context.Background(), id) require.Nil(t, err) require.NotNil(t, respApplication) require.Equal(t, name, respApplication.Name) @@ -102,7 +103,7 @@ func TestApplication(t *testing.T) { nil, nil, "", `{"message": "error text that will be ignored by API"}`, )) - respApplications, resp, err := client.Applications.Get(id) + respApplications, resp, err := client.Applications.Get(context.Background(), id) require.Nil(t, respApplications) require.NotNil(t, err) require.Contains(t, err.Error(), "does not exist") @@ -111,7 +112,7 @@ func TestApplication(t *testing.T) { t.Run("Other", func(t *testing.T) { c := api.NewClient(errorClient{}, api.SetEndpoint("")) - applications, resp, err := c.Applications.Get(id) + applications, resp, err := c.Applications.Get(context.Background(), id) require.Nil(t, resp) require.Error(t, err) require.Nil(t, applications) @@ -127,7 +128,7 @@ func TestApplication(t *testing.T) { require.Nil(t, mock.AddApplicationCreateTestCase(nil, nil, application, application)) - _, err := client.Applications.Create(application) + _, err := client.Applications.Create(context.Background(), application) require.Nil(t, err) require.Equal(t, application.Name, "App_test") }) @@ -140,7 +141,7 @@ func TestApplication(t *testing.T) { nil, nil, application, `{"Message": "test error"}`, )) - _, err := client.Applications.Create(application) + _, err := client.Applications.Create(context.Background(), application) require.Contains(t, err.Error(), "test error") }) }) @@ -157,7 +158,7 @@ func TestApplication(t *testing.T) { application, application)) - _, err := client.Applications.Update(application) + _, err := client.Applications.Update(context.Background(), application) require.Nil(t, err) }) @@ -169,7 +170,7 @@ func TestApplication(t *testing.T) { nil, nil, application, `{"message": "pulsar app not found"}`, )) - _, err := client.Applications.Update(application) + _, err := client.Applications.Update(context.Background(), application) require.Equal(t, api.ErrApplicationMissing, err) }) }) @@ -181,7 +182,7 @@ func TestApplication(t *testing.T) { require.Nil(t, mock.AddApplicationDeleteTestCase(id, nil, nil)) - _, err := client.Applications.Delete(id) + _, err := client.Applications.Delete(context.Background(), id) require.Nil(t, err) }) @@ -193,7 +194,7 @@ func TestApplication(t *testing.T) { nil, nil, "", `{"message": "pulsar app not found"}`, )) - _, err := client.Applications.Delete(id) + _, err := client.Applications.Delete(context.Background(), id) require.Equal(t, api.ErrApplicationMissing, err) }) }) diff --git a/rest/client.go b/rest/client.go index ad11684..dfa1587 100644 --- a/rest/client.go +++ b/rest/client.go @@ -2,6 +2,7 @@ package rest import ( "bytes" + "context" "encoding/json" "fmt" "io/ioutil" @@ -201,7 +202,7 @@ func (c Client) Do(req *http.Request, v interface{}) (*http.Response, error) { } // NextFunc knows how to get and parse additional info from uri into v. -type NextFunc func(v *interface{}, uri string) (*http.Response, error) +type NextFunc func(ctx context.Context, v *interface{}, uri string) (*http.Response, error) // DoWithPagination Does, and follows Link headers for pagination. The returned // Response is from the last URI visited - either the last page, or one that @@ -218,7 +219,7 @@ func (c Client) DoWithPagination(req *http.Request, v interface{}, f NextFunc) ( nextURI := ParseLink(resp.Header.Get("Link"), forceHTTPS).Next() for nextURI != "" { - resp, err = f(&v, nextURI) + resp, err = f(req.Context(), &v, nextURI) if err != nil { return resp, err } @@ -228,7 +229,7 @@ func (c Client) DoWithPagination(req *http.Request, v interface{}, f NextFunc) ( } // NewRequest constructs and returns a http.Request. -func (c *Client) NewRequest(method, path string, body interface{}) (*http.Request, error) { +func (c *Client) NewRequest(ctx context.Context, method, path string, body interface{}) (*http.Request, error) { rel, err := url.Parse(path) if err != nil { return nil, err @@ -250,6 +251,8 @@ func (c *Client) NewRequest(method, path string, body interface{}) (*http.Reques return nil, err } + req = req.WithContext(ctx) + req.Header.Add(headerAuth, c.APIKey) req.Header.Add("User-Agent", c.UserAgent) return req, nil @@ -391,8 +394,8 @@ func SetIntParam(key string, val int) func(*url.Values) { return func(v *url.Values) { v.Set(key, strconv.Itoa(val)) } } -func (c *Client) getURI(v interface{}, uri string) (*http.Response, error) { - req, err := c.NewRequest("GET", uri, nil) +func (c *Client) getURI(ctx context.Context, v interface{}, uri string) (*http.Response, error) { + req, err := c.NewRequest(ctx, "GET", uri, nil) if err != nil { return nil, err } diff --git a/rest/client_test.go b/rest/client_test.go index 5d603fe..db37d60 100644 --- a/rest/client_test.go +++ b/rest/client_test.go @@ -2,6 +2,7 @@ package rest import ( "bytes" + "context" "encoding/json" "errors" "io/ioutil" @@ -238,7 +239,7 @@ func TestClient_getURI(t *testing.T) { httpClient.On("Do", mock.Anything).Return(&mockResp, nil) var v interface{} - resp, err := client.getURI(v, "http://example.com") + resp, err := client.getURI(context.Background(), v, "http://example.com") assert.Equal(t, &mockResp, resp) assert.Nil(t, err) @@ -257,7 +258,7 @@ func TestClient_getURIWithNon2XXResponse(t *testing.T) { httpClient.On("Do", mock.Anything).Return(&mockResp, nil) var v interface{} - resp, err := client.getURI(v, "http://example.com") + resp, err := client.getURI(context.Background(), v, "http://example.com") assert.Equal(t, &mockResp, resp) assert.Equal(t, &Error{Resp: &mockResp}, err) @@ -276,7 +277,7 @@ func (c *mockHTTPClient) Do(req *http.Request) (*http.Response, error) { } // Hanging this off of mockHTTPClient for convent access to mocking stuff -func (c *mockHTTPClient) nextFunc(v *interface{}, uri string) (*http.Response, error) { +func (c *mockHTTPClient) nextFunc(ctx context.Context, v *interface{}, uri string) (*http.Response, error) { args := c.Called(v, uri) return args.Get(0).(*http.Response), args.Error(1) } diff --git a/rest/data_feed.go b/rest/data_feed.go index 7f149e3..2c2cf33 100644 --- a/rest/data_feed.go +++ b/rest/data_feed.go @@ -1,6 +1,7 @@ package rest import ( + "context" "fmt" "net/http" @@ -13,10 +14,10 @@ type DataFeedsService service // List returns all data feeds connected to a given data source. // // NS1 API docs: https://ns1.com/api/#feeds-get -func (s *DataFeedsService) List(sourceID string) ([]*data.Feed, *http.Response, error) { +func (s *DataFeedsService) List(ctx context.Context, sourceID string) ([]*data.Feed, *http.Response, error) { path := fmt.Sprintf("data/feeds/%s", sourceID) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -33,10 +34,10 @@ func (s *DataFeedsService) List(sourceID string) ([]*data.Feed, *http.Response, // Get takes a data source ID and a data feed ID and returns the details of a single data feed // // NS1 API docs: https://ns1.com/api/#feeds-feed-get -func (s *DataFeedsService) Get(sourceID string, feedID string) (*data.Feed, *http.Response, error) { +func (s *DataFeedsService) Get(ctx context.Context, sourceID string, feedID string) (*data.Feed, *http.Response, error) { path := fmt.Sprintf("data/feeds/%s/%s", sourceID, feedID) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -53,10 +54,10 @@ func (s *DataFeedsService) Get(sourceID string, feedID string) (*data.Feed, *htt // Create takes a *DataFeed and connects a new data feed to an existing data source. // // NS1 API docs: https://ns1.com/api/#feeds-put -func (s *DataFeedsService) Create(sourceID string, df *data.Feed) (*http.Response, error) { +func (s *DataFeedsService) Create(ctx context.Context, sourceID string, df *data.Feed) (*http.Response, error) { path := fmt.Sprintf("data/feeds/%s", sourceID) - req, err := s.client.NewRequest("PUT", path, &df) + req, err := s.client.NewRequest(ctx, "PUT", path, &df) if err != nil { return nil, err } @@ -79,10 +80,10 @@ func (s *DataFeedsService) Create(sourceID string, df *data.Feed) (*http.Respons // not updated during a POST. // // NS1 API docs: https://ns1.com/api/#feeds-post -func (s *DataFeedsService) Update(sourceID string, df *data.Feed) (*http.Response, error) { +func (s *DataFeedsService) Update(ctx context.Context, sourceID string, df *data.Feed) (*http.Response, error) { path := fmt.Sprintf("data/feeds/%s/%s", sourceID, df.ID) - req, err := s.client.NewRequest("POST", path, &df) + req, err := s.client.NewRequest(ctx, "POST", path, &df) if err != nil { return nil, err } @@ -99,10 +100,10 @@ func (s *DataFeedsService) Update(sourceID string, df *data.Feed) (*http.Respons // Delete takes a data source ID and a data feed ID and disconnects the feed from the data source and all attached destination metadata tables. // // NS1 API docs: https://ns1.com/api/#feeds-delete -func (s *DataFeedsService) Delete(sourceID string, feedID string) (*http.Response, error) { +func (s *DataFeedsService) Delete(ctx context.Context, sourceID string, feedID string) (*http.Response, error) { path := fmt.Sprintf("data/feeds/%s/%s", sourceID, feedID) - req, err := s.client.NewRequest("DELETE", path, nil) + req, err := s.client.NewRequest(ctx, "DELETE", path, nil) if err != nil { return nil, err } diff --git a/rest/data_source.go b/rest/data_source.go index 6c36ede..228cfaa 100644 --- a/rest/data_source.go +++ b/rest/data_source.go @@ -1,6 +1,7 @@ package rest import ( + "context" "fmt" "net/http" @@ -13,8 +14,8 @@ type DataSourcesService service // List returns all connected data sources. // // NS1 API docs: https://ns1.com/api/#sources-get -func (s *DataSourcesService) List() ([]*data.Source, *http.Response, error) { - req, err := s.client.NewRequest("GET", "data/sources", nil) +func (s *DataSourcesService) List(ctx context.Context) ([]*data.Source, *http.Response, error) { + req, err := s.client.NewRequest(ctx, "GET", "data/sources", nil) if err != nil { return nil, nil, err } @@ -31,10 +32,10 @@ func (s *DataSourcesService) List() ([]*data.Source, *http.Response, error) { // Get takes an ID returns the details for a single data source. // // NS1 API docs: https://ns1.com/api/#sources-source-get -func (s *DataSourcesService) Get(id string) (*data.Source, *http.Response, error) { +func (s *DataSourcesService) Get(ctx context.Context, id string) (*data.Source, *http.Response, error) { path := fmt.Sprintf("data/sources/%s", id) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -51,8 +52,8 @@ func (s *DataSourcesService) Get(id string) (*data.Source, *http.Response, error // Create takes a *DataSource and creates a new data source. // // NS1 API docs: https://ns1.com/api/#sources-put -func (s *DataSourcesService) Create(ds *data.Source) (*http.Response, error) { - req, err := s.client.NewRequest("PUT", "data/sources", &ds) +func (s *DataSourcesService) Create(ctx context.Context, ds *data.Source) (*http.Response, error) { + req, err := s.client.NewRequest(ctx, "PUT", "data/sources", &ds) if err != nil { return nil, err } @@ -70,10 +71,10 @@ func (s *DataSourcesService) Create(ds *data.Source) (*http.Response, error) { // NOTE: This does not 'publish' data. See the Publish method. // // NS1 API docs: https://ns1.com/api/#sources-post -func (s *DataSourcesService) Update(ds *data.Source) (*http.Response, error) { +func (s *DataSourcesService) Update(ctx context.Context, ds *data.Source) (*http.Response, error) { path := fmt.Sprintf("data/sources/%s", ds.ID) - req, err := s.client.NewRequest("POST", path, &ds) + req, err := s.client.NewRequest(ctx, "POST", path, &ds) if err != nil { return nil, err } @@ -90,10 +91,10 @@ func (s *DataSourcesService) Update(ds *data.Source) (*http.Response, error) { // Delete takes an ID and removes an existing data source and all connected feeds from the source. // // NS1 API docs: https://ns1.com/api/#sources-delete -func (s *DataSourcesService) Delete(id string) (*http.Response, error) { +func (s *DataSourcesService) Delete(ctx context.Context, id string) (*http.Response, error) { path := fmt.Sprintf("data/sources/%s", id) - req, err := s.client.NewRequest("DELETE", path, nil) + req, err := s.client.NewRequest(ctx, "DELETE", path, nil) if err != nil { return nil, err } @@ -109,10 +110,10 @@ func (s *DataSourcesService) Delete(id string) (*http.Response, error) { // Publish takes a datasources' id and data to publish. // // NS1 API docs: https://ns1.com/api/#feed-post -func (s *DataSourcesService) Publish(dsID string, data interface{}) (*http.Response, error) { +func (s *DataSourcesService) Publish(ctx context.Context, dsID string, data interface{}) (*http.Response, error) { path := fmt.Sprintf("feed/%s", dsID) - req, err := s.client.NewRequest("POST", path, &data) + req, err := s.client.NewRequest(ctx, "POST", path, &data) if err != nil { return nil, err } diff --git a/rest/dns_view.go b/rest/dns_view.go index 2b68ff4..d82547d 100644 --- a/rest/dns_view.go +++ b/rest/dns_view.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -14,8 +15,8 @@ type DNSViewService service // List returns all DNS Views // // NS1 API docs: https://ns1.com/api#getlist-all-dns-views -func (s *DNSViewService) List() ([]*dns.DNSView, *http.Response, error) { - req, err := s.client.NewRequest("GET", "views", nil) +func (s *DNSViewService) List(ctx context.Context) ([]*dns.DNSView, *http.Response, error) { + req, err := s.client.NewRequest(ctx, "GET", "views", nil) if err != nil { return nil, nil, err } @@ -33,8 +34,8 @@ func (s *DNSViewService) List() ([]*dns.DNSView, *http.Response, error) { // // The given DNSView must have at least the name // NS1 API docs: https://ns1.com/api#putcreate-a-dns-view -func (s *DNSViewService) Create(v *dns.DNSView) (*http.Response, error) { - req, err := s.client.NewRequest("PUT", fmt.Sprintf("/v1/views/%s", v.Name), v) +func (s *DNSViewService) Create(ctx context.Context, v *dns.DNSView) (*http.Response, error) { + req, err := s.client.NewRequest(ctx, "PUT", fmt.Sprintf("/v1/views/%s", v.Name), v) if err != nil { return nil, err } @@ -57,10 +58,10 @@ func (s *DNSViewService) Create(v *dns.DNSView) (*http.Response, error) { // Get takes a DNS view name and returns DNSView struct. // // NS1 API docs: https://ns1.com/api#getview-dns-view-details -func (s *DNSViewService) Get(viewName string) (*dns.DNSView, *http.Response, error) { +func (s *DNSViewService) Get(ctx context.Context, viewName string) (*dns.DNSView, *http.Response, error) { path := fmt.Sprintf("views/%s", viewName) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -83,10 +84,10 @@ func (s *DNSViewService) Get(viewName string) (*dns.DNSView, *http.Response, err // Update takes a *dns.DNSView and updates the DNS view with same name on NS1. // // NS1 API docs: https://ns1.com/api#postedit-a-dns-view -func (s *DNSViewService) Update(v *dns.DNSView) (*http.Response, error) { +func (s *DNSViewService) Update(ctx context.Context, v *dns.DNSView) (*http.Response, error) { path := fmt.Sprintf("views/%s", v.Name) - req, err := s.client.NewRequest("POST", path, &v) + req, err := s.client.NewRequest(ctx, "POST", path, &v) if err != nil { return nil, err } @@ -108,10 +109,10 @@ func (s *DNSViewService) Update(v *dns.DNSView) (*http.Response, error) { // Delete takes a DNS view name, and removes an existing DNS view // // NS1 API docs: https://ns1.com/api#deletedelete-a-dns-view -func (s *DNSViewService) Delete(viewName string) (*http.Response, error) { +func (s *DNSViewService) Delete(ctx context.Context, viewName string) (*http.Response, error) { path := fmt.Sprintf("views/%s", viewName) - req, err := s.client.NewRequest("DELETE", path, nil) + req, err := s.client.NewRequest(ctx, "DELETE", path, nil) if err != nil { return nil, err } @@ -133,10 +134,10 @@ func (s *DNSViewService) Delete(viewName string) (*http.Response, error) { // GetPreference returns a map[string]int of preferences. // // NS1 API docs: https://ns1.com/api#getget-dns-view-preference -func (s *DNSViewService) GetPreferences() (map[string]int, *http.Response, error) { +func (s *DNSViewService) GetPreferences(ctx context.Context) (map[string]int, *http.Response, error) { path := "config/views/preference" - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -153,10 +154,10 @@ func (s *DNSViewService) GetPreferences() (map[string]int, *http.Response, error // UpdatePreference takes a map[string]int and returns a map[string]int of preferences. // // NS1 API docs: https://ns1.com/api#postedit-dns-view-preference -func (s *DNSViewService) UpdatePreferences(m map[string]int) (map[string]int, *http.Response, error) { +func (s *DNSViewService) UpdatePreferences(ctx context.Context, m map[string]int) (map[string]int, *http.Response, error) { path := "config/views/preference" - req, err := s.client.NewRequest("POST", path, m) + req, err := s.client.NewRequest(ctx, "POST", path, m) if err != nil { return nil, nil, err } diff --git a/rest/dns_view_test.go b/rest/dns_view_test.go index a61e2c1..3922b2f 100644 --- a/rest/dns_view_test.go +++ b/rest/dns_view_test.go @@ -1,6 +1,7 @@ package rest_test import ( + "context" "fmt" "net/http" "reflect" @@ -39,7 +40,7 @@ func TestDNSView(t *testing.T) { require.Nil(t, mock.AddDNSViewListTestCase(nil, nil, views)) - respDNSViews, _, err := client.View.List() + respDNSViews, _, err := client.View.List(context.Background()) require.Nil(t, err) require.NotNil(t, respDNSViews) require.Equal(t, len(views), len(respDNSViews)) @@ -60,7 +61,7 @@ func TestDNSView(t *testing.T) { nil, nil, "", `{"message": "test error"}`, )) - views, resp, err := client.View.List() + views, resp, err := client.View.List(context.Background()) require.Nil(t, views) require.NotNil(t, err) require.Contains(t, err.Error(), "test error") @@ -78,7 +79,7 @@ func TestDNSView(t *testing.T) { require.Nil(t, mock.AddDNSViewGetTestCase(myView.Name, nil, nil, &dnsView)) - respDNSView, _, err := client.View.Get(myView.Name) + respDNSView, _, err := client.View.Get(context.Background(), myView.Name) require.Nil(t, err) require.True(t, reflect.DeepEqual(&dnsView, respDNSView)) @@ -93,7 +94,7 @@ func TestDNSView(t *testing.T) { http.MethodGet, "views/myView", http.StatusNotFound, nil, nil, "", `{"message": "Resource not found"}`, )) - dnsView, resp, err := client.View.Get("myView") + dnsView, resp, err := client.View.Get(context.Background(), "myView") require.Nil(t, dnsView) require.NotNil(t, err) require.Equal(t, api.ErrViewMissing.Error(), err.Error()) @@ -108,7 +109,7 @@ func TestDNSView(t *testing.T) { http.MethodGet, "views/myView", http.StatusBadRequest, nil, nil, "", `{"message": "test error"}`, )) - dnsView, resp, err := client.View.Get("myView") + dnsView, resp, err := client.View.Get(context.Background(), "myView") require.Nil(t, dnsView) require.NotNil(t, err) require.Contains(t, err.Error(), "test error") @@ -124,7 +125,7 @@ func TestDNSView(t *testing.T) { require.Nil(t, mock.AddDNSViewCreateTestCase(nil, nil, &myView, &myView)) - _, err := client.View.Create(&myView) + _, err := client.View.Create(context.Background(), &myView) require.Nil(t, err) }) @@ -139,7 +140,7 @@ func TestDNSView(t *testing.T) { nil, nil, dnsView, `{"message": "conflicts with existing resource"}`, )) - _, err = client.View.Create(&dnsView) + _, err = client.View.Create(context.Background(), &dnsView) require.Equal(t, api.ErrViewExists.Error(), err.Error()) }) @@ -154,7 +155,7 @@ func TestDNSView(t *testing.T) { nil, nil, dnsView, `{"message": "test error"}`, )) - _, err = client.View.Create(&dnsView) + _, err = client.View.Create(context.Background(), &dnsView) require.Contains(t, err.Error(), "test error") }) @@ -170,7 +171,7 @@ func TestDNSView(t *testing.T) { require.Nil(t, mock.AddDNSViewUpdateTestCase(nil, nil, &dnsView, &dnsView)) - _, err := client.View.Update(&dnsView) + _, err := client.View.Update(context.Background(), &dnsView) require.Nil(t, err) }) @@ -183,7 +184,7 @@ func TestDNSView(t *testing.T) { http.MethodPost, fmt.Sprintf("views/%s", dnsView.Name), http.StatusNotFound, nil, nil, dnsView, `{"message": "Resource not found"}`, )) - resp, err := client.View.Update(&dnsView) + resp, err := client.View.Update(context.Background(), &dnsView) require.NotNil(t, err) require.Equal(t, api.ErrViewMissing.Error(), err.Error()) require.Equal(t, http.StatusNotFound, resp.StatusCode) @@ -197,7 +198,7 @@ func TestDNSView(t *testing.T) { http.MethodPost, fmt.Sprintf("views/%s", dnsView.Name), http.StatusBadGateway, nil, nil, dnsView, `{"message": "test error"}`, )) - resp, err := client.View.Update(&dnsView) + resp, err := client.View.Update(context.Background(), &dnsView) require.NotNil(t, err) require.Contains(t, err.Error(), "test error") @@ -211,7 +212,7 @@ func TestDNSView(t *testing.T) { t.Run("Success", func(t *testing.T) { defer mock.ClearTestCases() require.Nil(t, mock.AddDNSViewGetPreferencesTestCase(nil, nil, myMap)) - respMap, _, err := client.View.GetPreferences() + respMap, _, err := client.View.GetPreferences(context.Background()) require.Nil(t, err) require.True(t, reflect.DeepEqual(myMap, respMap)) }) @@ -224,7 +225,7 @@ func TestDNSView(t *testing.T) { http.MethodGet, "config/views/preference", http.StatusBadGateway, nil, nil, "", `{"message": "test error"}`, )) - m, resp, err := client.View.GetPreferences() + m, resp, err := client.View.GetPreferences(context.Background()) require.Nil(t, m) require.NotNil(t, err) require.Contains(t, err.Error(), "test error") @@ -238,7 +239,7 @@ func TestDNSView(t *testing.T) { t.Run("Success", func(t *testing.T) { defer mock.ClearTestCases() require.Nil(t, mock.AddDNSViewUpdatePreferencesTestCase(nil, nil, myMap, myMap)) - respMap, _, err := client.View.UpdatePreferences(myMap) + respMap, _, err := client.View.UpdatePreferences(context.Background(), myMap) require.Nil(t, err) require.True(t, reflect.DeepEqual(myMap, respMap)) }) @@ -251,7 +252,7 @@ func TestDNSView(t *testing.T) { http.MethodPost, "config/views/preference", http.StatusNotFound, nil, nil, myMap, `{"message": "Resource not found"}`, )) - m, resp, err := client.View.UpdatePreferences(myMap) + m, resp, err := client.View.UpdatePreferences(context.Background(), myMap) require.Nil(t, m) require.NotNil(t, err) require.Equal(t, api.ErrViewMissing.Error(), err.Error()) @@ -265,7 +266,7 @@ func TestDNSView(t *testing.T) { http.MethodPost, "config/views/preference", http.StatusBadGateway, nil, nil, myMap, `{"message": "test error"}`, )) - m, resp, err := client.View.UpdatePreferences(myMap) + m, resp, err := client.View.UpdatePreferences(context.Background(), myMap) require.Nil(t, m) require.NotNil(t, err) require.Contains(t, err.Error(), "test error") diff --git a/rest/dnssec.go b/rest/dnssec.go index 89ff970..784b842 100644 --- a/rest/dnssec.go +++ b/rest/dnssec.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -14,10 +15,10 @@ type DNSSECService service // Get takes a zone, and returns DNSSEC information. // // NS1 API docs: https://ns1.com/api#get-get-dnssec-details-for-a-zone -func (s *DNSSECService) Get(zone string) (*dns.ZoneDNSSEC, *http.Response, error) { +func (s *DNSSECService) Get(ctx context.Context, zone string) (*dns.ZoneDNSSEC, *http.Response, error) { path := fmt.Sprintf("zones/%s/dnssec", zone) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } diff --git a/rest/ipam.go b/rest/ipam.go index eab5927..bd6af7b 100644 --- a/rest/ipam.go +++ b/rest/ipam.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -15,8 +16,8 @@ type IPAMService service // networks. // // NS1 API docs: https://ns1.com/api#getview-a-list-of-root-addresses -func (s *IPAMService) ListAddrs() ([]ipam.Address, *http.Response, error) { - req, err := s.client.NewRequest(http.MethodGet, "ipam/address", nil) +func (s *IPAMService) ListAddrs(ctx context.Context) ([]ipam.Address, *http.Response, error) { + req, err := s.client.NewRequest(ctx, http.MethodGet, "ipam/address", nil) if err != nil { return nil, nil, err } @@ -38,9 +39,9 @@ func (s *IPAMService) ListAddrs() ([]ipam.Address, *http.Response, error) { // GetSubnet returns the subnet corresponding to the provided address ID. // // NS1 API docs: https://ns1.com/api#getview-a-subnet -func (s *IPAMService) GetSubnet(addrID int) (*ipam.Address, *http.Response, error) { +func (s *IPAMService) GetSubnet(ctx context.Context, addrID int) (*ipam.Address, *http.Response, error) { reqPath := fmt.Sprintf("ipam/address/%d", addrID) - req, err := s.client.NewRequest(http.MethodGet, reqPath, nil) + req, err := s.client.NewRequest(ctx, http.MethodGet, reqPath, nil) if err != nil { return nil, nil, err } @@ -59,9 +60,9 @@ func (s *IPAMService) GetSubnet(addrID int) (*ipam.Address, *http.Response, erro // specified IP address. // // NS1 API docs: https://ns1.com/api#getview-address-children -func (s *IPAMService) GetChildren(addrID int) ([]*ipam.Address, *http.Response, error) { +func (s *IPAMService) GetChildren(ctx context.Context, addrID int) ([]*ipam.Address, *http.Response, error) { reqPath := fmt.Sprintf("ipam/address/%d/children", addrID) - req, err := s.client.NewRequest(http.MethodGet, reqPath, nil) + req, err := s.client.NewRequest(ctx, http.MethodGet, reqPath, nil) if err != nil { return nil, nil, err } @@ -83,9 +84,9 @@ func (s *IPAMService) GetChildren(addrID int) ([]*ipam.Address, *http.Response, // GetParent fetches the addresses parent. // // NS1 API docs: https://ns1.com/api#getview-address-parent -func (s *IPAMService) GetParent(addrID int) (*ipam.Address, *http.Response, error) { +func (s *IPAMService) GetParent(ctx context.Context, addrID int) (*ipam.Address, *http.Response, error) { reqPath := fmt.Sprintf("ipam/address/%d/parent", addrID) - req, err := s.client.NewRequest(http.MethodGet, reqPath, nil) + req, err := s.client.NewRequest(ctx, http.MethodGet, reqPath, nil) if err != nil { return nil, nil, err } @@ -104,7 +105,7 @@ func (s *IPAMService) GetParent(addrID int) (*ipam.Address, *http.Response, erro // The Prefix and Network fields are required. // // NS1 API docs: https://ns1.com/api#putcreate-a-subnet -func (s *IPAMService) CreateSubnet(addr *ipam.Address) (*ipam.Address, *http.Response, error) { +func (s *IPAMService) CreateSubnet(ctx context.Context, addr *ipam.Address) (*ipam.Address, *http.Response, error) { switch { case addr.Prefix == "": return nil, nil, errors.New("the Prefix field is required") @@ -112,7 +113,7 @@ func (s *IPAMService) CreateSubnet(addr *ipam.Address) (*ipam.Address, *http.Res return nil, nil, errors.New("the Network field is required") } - req, err := s.client.NewRequest(http.MethodPut, "ipam/address", addr) + req, err := s.client.NewRequest(ctx, http.MethodPut, "ipam/address", addr) if err != nil { return nil, nil, err } @@ -132,13 +133,13 @@ func (s *IPAMService) CreateSubnet(addr *ipam.Address) (*ipam.Address, *http.Res // Parent is whether or not to include the parent in the parent field. // // NS1 API docs: https://ns1.com/api#postedit-a-subnet -func (s *IPAMService) EditSubnet(addr *ipam.Address, parent bool) (newAddr, parentAddr *ipam.Address, resp *http.Response, err error) { +func (s *IPAMService) EditSubnet(ctx context.Context, addr *ipam.Address, parent bool) (newAddr, parentAddr *ipam.Address, resp *http.Response, err error) { if addr.ID == 0 { return nil, nil, nil, errors.New("the ID field is required") } reqPath := fmt.Sprintf("ipam/address/%d", addr.ID) - req, err := s.client.NewRequest(http.MethodPost, reqPath, addr) + req, err := s.client.NewRequest(ctx, http.MethodPost, reqPath, addr) if err != nil { return nil, nil, nil, err } @@ -173,9 +174,9 @@ func (s *IPAMService) EditSubnet(addr *ipam.Address, parent bool) (newAddr, pare // - KVPS and options will be copied; tags will be inherited // // NS1 API docs: https://ns1.com/api#postsplit-a-subnet -func (s *IPAMService) SplitSubnet(id, prefix int) (rootAddr int, prefixIDs []int, resp *http.Response, err error) { +func (s *IPAMService) SplitSubnet(ctx context.Context, id, prefix int) (rootAddr int, prefixIDs []int, resp *http.Response, err error) { reqPath := fmt.Sprintf("ipam/address/%d/split", id) - req, err := s.client.NewRequest(http.MethodPost, reqPath, struct { + req, err := s.client.NewRequest(ctx, http.MethodPost, reqPath, struct { Prefix int `json:"prefix"` }{ Prefix: prefix, @@ -195,8 +196,8 @@ func (s *IPAMService) SplitSubnet(id, prefix int) (rootAddr int, prefixIDs []int // MergeSubnet merges several subnets together. // // NS1 API docs: https://ns1.com/api#postmerge-a-subnet -func (s *IPAMService) MergeSubnet(rootID, mergeID int) (*ipam.Address, *http.Response, error) { - req, err := s.client.NewRequest(http.MethodPost, "ipam/address/merge", struct { +func (s *IPAMService) MergeSubnet(ctx context.Context, rootID, mergeID int) (*ipam.Address, *http.Response, error) { + req, err := s.client.NewRequest(ctx, http.MethodPost, "ipam/address/merge", struct { Root int `json:"root_address_id"` Merge int `json:"merged_address_id"` }{ @@ -215,9 +216,9 @@ func (s *IPAMService) MergeSubnet(rootID, mergeID int) (*ipam.Address, *http.Res // DeleteSubnet removes a subnet entirely. // // NS1 API docs: https://ns1.com/api#deletedelete-a-subnet -func (s *IPAMService) DeleteSubnet(id int) (*http.Response, error) { +func (s *IPAMService) DeleteSubnet(ctx context.Context, id int) (*http.Response, error) { reqPath := fmt.Sprintf("ipam/address/%d", id) - req, err := s.client.NewRequest(http.MethodDelete, reqPath, nil) + req, err := s.client.NewRequest(ctx, http.MethodDelete, reqPath, nil) if err != nil { return nil, err } @@ -227,9 +228,9 @@ func (s *IPAMService) DeleteSubnet(id int) (*http.Response, error) { // nextAddrs is a pagination helper than gets and appends another list of // addresses to the passed list. -func (s *IPAMService) nextAddrs(v *interface{}, uri string) (*http.Response, error) { +func (s *IPAMService) nextAddrs(ctx context.Context, v *interface{}, uri string) (*http.Response, error) { addrs := []*ipam.Address{} - resp, err := s.client.getURI(&addrs, uri) + resp, err := s.client.getURI(ctx, &addrs, uri) if err != nil { return resp, err } diff --git a/rest/ipam_test.go b/rest/ipam_test.go index 658c0f2..4875b22 100644 --- a/rest/ipam_test.go +++ b/rest/ipam_test.go @@ -1,6 +1,7 @@ package rest_test import ( + "context" "net/http" "testing" @@ -34,7 +35,7 @@ func TestIPAMAddrs(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respAddrs, _, err := client.IPAM.ListAddrs() + respAddrs, _, err := client.IPAM.ListAddrs(context.Background()) if err != nil { t.Fatalf("error listing IPAM addresses: %v", err) } @@ -66,7 +67,7 @@ func TestIPAMAddrs(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respAddrs, resp, err := client.IPAM.ListAddrs() + respAddrs, resp, err := client.IPAM.ListAddrs(context.Background()) if err != nil { t.Fatalf("error listing IPAM addresses: %v", err) } @@ -96,7 +97,7 @@ func TestIPAMAddrs(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respAddr, _, err := client.IPAM.GetSubnet(1) + respAddr, _, err := client.IPAM.GetSubnet(context.Background(), 1) if err != nil { t.Fatalf("error getting subnet: %v", err) } @@ -121,7 +122,7 @@ func TestIPAMAddrs(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respAddrs, _, err := client.IPAM.GetChildren(1) + respAddrs, _, err := client.IPAM.GetChildren(context.Background(), 1) if err != nil { t.Fatalf("error listing child subnets: %v", err) } @@ -153,7 +154,7 @@ func TestIPAMAddrs(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respAddrs, resp, err := client.IPAM.GetChildren(1) + respAddrs, resp, err := client.IPAM.GetChildren(context.Background(), 1) if err != nil { t.Fatalf("error listing child subnets %v", err) } @@ -182,7 +183,7 @@ func TestIPAMAddrs(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respAddr, _, err := client.IPAM.GetParent(1) + respAddr, _, err := client.IPAM.GetParent(context.Background(), 1) if err != nil { t.Fatalf("error getting subnet: %v", err) } @@ -196,12 +197,12 @@ func TestIPAMAddrs(t *testing.T) { t.Run("RequiredParams", func(t *testing.T) { addr := &ipam.Address{Name: "a", Network: 1} - _, _, err = client.IPAM.CreateSubnet(addr) + _, _, err = client.IPAM.CreateSubnet(context.Background(), addr) if err == nil { t.Errorf("expected a missing prefix to result in an error") } addr = &ipam.Address{Name: "a", Prefix: "127.0.1.0/24"} - _, _, err = client.IPAM.CreateSubnet(addr) + _, _, err = client.IPAM.CreateSubnet(context.Background(), addr) if err == nil { t.Errorf("expected a missing network to result in an error") } @@ -212,7 +213,7 @@ func TestIPAMAddrs(t *testing.T) { if err != nil { t.Fatalf("error adding test case: %v", err) } - respAddr, _, err := client.IPAM.CreateSubnet(addr) + respAddr, _, err := client.IPAM.CreateSubnet(context.Background(), addr) if err != nil { t.Fatalf("error creating subnet: %v", err) } @@ -224,7 +225,7 @@ func TestIPAMAddrs(t *testing.T) { t.Run("EditSubnet", func(t *testing.T) { t.Run("RequiredParams", func(t *testing.T) { addr := &ipam.Address{Name: "a"} - _, _, _, err = client.IPAM.EditSubnet(addr, true) + _, _, _, err = client.IPAM.EditSubnet(context.Background(), addr, true) if err == nil { t.Errorf("expected a missing ID to result in an error") } @@ -238,7 +239,7 @@ func TestIPAMAddrs(t *testing.T) { if err != nil { t.Fatalf("error adding test case: %v", err) } - respAddr, parent, _, err := client.IPAM.EditSubnet(addr, false) + respAddr, parent, _, err := client.IPAM.EditSubnet(context.Background(), addr, false) if err != nil { t.Fatalf("error editing subnet: %v", err) } @@ -257,7 +258,7 @@ func TestIPAMAddrs(t *testing.T) { if err != nil { t.Fatalf("error adding test case: %v", err) } - respAddr, parent, _, err := client.IPAM.EditSubnet(addr, true) + respAddr, parent, _, err := client.IPAM.EditSubnet(context.Background(), addr, true) if err != nil { t.Fatalf("error editing subnet: %v", err) } @@ -292,7 +293,7 @@ func TestIPAMAddrs(t *testing.T) { if err != nil { t.Fatalf("error adding test case: %v", err) } - rootAddr, prefixIDs, _, err := client.IPAM.SplitSubnet(1, prefix) + rootAddr, prefixIDs, _, err := client.IPAM.SplitSubnet(context.Background(), 1, prefix) if err != nil { t.Fatalf("error creating subnet: %v", err) } @@ -321,7 +322,7 @@ func TestIPAMAddrs(t *testing.T) { if err != nil { t.Fatalf("error adding test case: %v", err) } - newAddr, _, err := client.IPAM.MergeSubnet(1, 2) + newAddr, _, err := client.IPAM.MergeSubnet(context.Background(), 1, 2) if err != nil { t.Fatalf("error creating subnet: %v", err) } @@ -337,7 +338,7 @@ func TestIPAMAddrs(t *testing.T) { if err != nil { t.Fatalf("error adding test case: %v", err) } - _, err = client.IPAM.DeleteSubnet(1) + _, err = client.IPAM.DeleteSubnet(context.Background(), 1) if err != nil { t.Fatalf("error deleting subnet: %v", err) } diff --git a/rest/monitor_job.go b/rest/monitor_job.go index 9ff927e..ca19834 100644 --- a/rest/monitor_job.go +++ b/rest/monitor_job.go @@ -1,6 +1,7 @@ package rest import ( + "context" "fmt" "net/http" "net/url" @@ -14,8 +15,8 @@ type JobsService service // List returns all monitoring jobs for the account. // // NS1 API docs: https://ns1.com/api/#jobs-get -func (s *JobsService) List() ([]*monitor.Job, *http.Response, error) { - req, err := s.client.NewRequest("GET", "monitoring/jobs", nil) +func (s *JobsService) List(ctx context.Context) ([]*monitor.Job, *http.Response, error) { + req, err := s.client.NewRequest(ctx, "GET", "monitoring/jobs", nil) if err != nil { return nil, nil, err } @@ -32,10 +33,10 @@ func (s *JobsService) List() ([]*monitor.Job, *http.Response, error) { // Get takes an ID and returns details for a specific monitoring job. // // NS1 API docs: https://ns1.com/api/#jobs-jobid-get -func (s *JobsService) Get(id string) (*monitor.Job, *http.Response, error) { +func (s *JobsService) Get(ctx context.Context, id string) (*monitor.Job, *http.Response, error) { path := fmt.Sprintf("%s/%s", "monitoring/jobs", id) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -52,10 +53,10 @@ func (s *JobsService) Get(id string) (*monitor.Job, *http.Response, error) { // Create takes a *MonitoringJob and creates a new monitoring job. // // NS1 API docs: https://ns1.com/api/#jobs-put -func (s *JobsService) Create(mj *monitor.Job) (*http.Response, error) { +func (s *JobsService) Create(ctx context.Context, mj *monitor.Job) (*http.Response, error) { path := fmt.Sprintf("%s/%s", "monitoring/jobs", mj.ID) - req, err := s.client.NewRequest("PUT", path, &mj) + req, err := s.client.NewRequest(ctx, "PUT", path, &mj) if err != nil { return nil, err } @@ -72,10 +73,10 @@ func (s *JobsService) Create(mj *monitor.Job) (*http.Response, error) { // Update takes a *MonitoringJob and change the configuration details of an existing monitoring job. // // NS1 API docs: https://ns1.com/api/#jobs-jobid-post -func (s *JobsService) Update(mj *monitor.Job) (*http.Response, error) { +func (s *JobsService) Update(ctx context.Context, mj *monitor.Job) (*http.Response, error) { path := fmt.Sprintf("%s/%s", "monitoring/jobs", mj.ID) - req, err := s.client.NewRequest("POST", path, &mj) + req, err := s.client.NewRequest(ctx, "POST", path, &mj) if err != nil { return nil, err } @@ -92,10 +93,10 @@ func (s *JobsService) Update(mj *monitor.Job) (*http.Response, error) { // Delete takes an ID and immediately terminates and deletes and existing monitoring job. // // NS1 API docs: https://ns1.com/api/#jobs-jobid-delete -func (s *JobsService) Delete(id string) (*http.Response, error) { +func (s *JobsService) Delete(ctx context.Context, id string) (*http.Response, error) { path := fmt.Sprintf("%s/%s", "monitoring/jobs", id) - req, err := s.client.NewRequest("DELETE", path, nil) + req, err := s.client.NewRequest(ctx, "DELETE", path, nil) if err != nil { return nil, err } @@ -111,7 +112,7 @@ func (s *JobsService) Delete(id string) (*http.Response, error) { // History takes an ID and returns status log history for a specific monitoring job. // // NS1 API docs: https://ns1.com/api/#history-get -func (s *JobsService) History(id string, opts ...func(*url.Values)) ([]*monitor.StatusLog, *http.Response, error) { +func (s *JobsService) History(ctx context.Context, id string, opts ...func(*url.Values)) ([]*monitor.StatusLog, *http.Response, error) { v := url.Values{} for _, opt := range opts { opt(&v) @@ -119,7 +120,7 @@ func (s *JobsService) History(id string, opts ...func(*url.Values)) ([]*monitor. path := fmt.Sprintf("%s/%s?%s", "monitoring/history", id, v.Encode()) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } diff --git a/rest/monitor_notify.go b/rest/monitor_notify.go index 4d79551..031afd0 100644 --- a/rest/monitor_notify.go +++ b/rest/monitor_notify.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -14,8 +15,8 @@ type NotificationsService service // List returns all configured notification lists. // // NS1 API docs: https://ns1.com/api/#lists-get -func (s *NotificationsService) List() ([]*monitor.NotifyList, *http.Response, error) { - req, err := s.client.NewRequest("GET", "lists", nil) +func (s *NotificationsService) List(ctx context.Context) ([]*monitor.NotifyList, *http.Response, error) { + req, err := s.client.NewRequest(ctx, "GET", "lists", nil) if err != nil { return nil, nil, err } @@ -32,10 +33,10 @@ func (s *NotificationsService) List() ([]*monitor.NotifyList, *http.Response, er // Get returns the details and notifiers associated with a specific notification list. // // NS1 API docs: https://ns1.com/api/#lists-listid-get -func (s *NotificationsService) Get(listID string) (*monitor.NotifyList, *http.Response, error) { +func (s *NotificationsService) Get(ctx context.Context, listID string) (*monitor.NotifyList, *http.Response, error) { path := fmt.Sprintf("%s/%s", "lists", listID) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -58,8 +59,8 @@ func (s *NotificationsService) Get(listID string) (*monitor.NotifyList, *http.Re // Create takes a *NotifyList and creates a new notify list. // // NS1 API docs: https://ns1.com/api/#lists-put -func (s *NotificationsService) Create(nl *monitor.NotifyList) (*http.Response, error) { - req, err := s.client.NewRequest("PUT", "lists", &nl) +func (s *NotificationsService) Create(ctx context.Context, nl *monitor.NotifyList) (*http.Response, error) { + req, err := s.client.NewRequest(ctx, "PUT", "lists", &nl) if err != nil { return nil, err } @@ -82,10 +83,10 @@ func (s *NotificationsService) Create(nl *monitor.NotifyList) (*http.Response, e // Update adds or removes entries or otherwise update a notification list. // // NS1 API docs: https://ns1.com/api/#list-listid-post -func (s *NotificationsService) Update(nl *monitor.NotifyList) (*http.Response, error) { +func (s *NotificationsService) Update(ctx context.Context, nl *monitor.NotifyList) (*http.Response, error) { path := fmt.Sprintf("%s/%s", "lists", nl.ID) - req, err := s.client.NewRequest("POST", path, &nl) + req, err := s.client.NewRequest(ctx, "POST", path, &nl) if err != nil { return nil, err } @@ -102,10 +103,10 @@ func (s *NotificationsService) Update(nl *monitor.NotifyList) (*http.Response, e // Delete immediately deletes an existing notification list. // // NS1 API docs: https://ns1.com/api/#lists-listid-delete -func (s *NotificationsService) Delete(listID string) (*http.Response, error) { +func (s *NotificationsService) Delete(ctx context.Context, listID string) (*http.Response, error) { path := fmt.Sprintf("%s/%s", "lists", listID) - req, err := s.client.NewRequest("DELETE", path, nil) + req, err := s.client.NewRequest(ctx, "DELETE", path, nil) if err != nil { return nil, err } diff --git a/rest/network.go b/rest/network.go index 6c3a3e7..5d2a79f 100644 --- a/rest/network.go +++ b/rest/network.go @@ -1,6 +1,7 @@ package rest import ( + "context" "net/http" "gopkg.in/ns1/ns1-go.v2/rest/model/dns" @@ -12,8 +13,8 @@ type NetworkService service // GetNetworks returns a list of all available NS1 DNS networks associated // with your account. // NS1 API docs: https://ns1.com/api?docId=403388 -func (s *NetworkService) Get() ([]*dns.Network, *http.Response, error) { - req, err := s.client.NewRequest(http.MethodGet, "networks", nil) +func (s *NetworkService) Get(ctx context.Context) ([]*dns.Network, *http.Response, error) { + req, err := s.client.NewRequest(ctx, http.MethodGet, "networks", nil) if err != nil { return nil, nil, err } diff --git a/rest/network_test.go b/rest/network_test.go index 386f508..aff9472 100644 --- a/rest/network_test.go +++ b/rest/network_test.go @@ -1,6 +1,7 @@ package rest_test import ( + "context" "net/http" "testing" @@ -43,7 +44,7 @@ func TestNetwork(t *testing.T) { require.Nil(t, mock.NetworkGetTestCase(nil, nil, networks)) - respNetworks, _, err := client.Network.Get() + respNetworks, _, err := client.Network.Get(context.Background()) require.Nil(t, err) require.NotNil(t, respNetworks) require.Equal(t, len(networks), len(respNetworks)) @@ -62,7 +63,7 @@ func TestNetwork(t *testing.T) { require.Nil(t, mock.NetworkGetTestCase(nil, nil, networks)) - respNetworks, _, err := client.Network.Get() + respNetworks, _, err := client.Network.Get(context.Background()) require.Nil(t, err) require.Equal(t, networks, respNetworks) @@ -77,7 +78,7 @@ func TestNetwork(t *testing.T) { nil, nil, "", `{"message": "test error"}`, )) - networks, resp, err := client.Network.Get() + networks, resp, err := client.Network.Get(context.Background()) require.Nil(t, networks) require.NotNil(t, err) require.Contains(t, err.Error(), "test error") @@ -86,7 +87,7 @@ func TestNetwork(t *testing.T) { t.Run("Other", func(t *testing.T) { c := api.NewClient(errorClient{}, api.SetEndpoint("")) - networks, resp, err := c.Network.Get() + networks, resp, err := c.Network.Get(context.Background()) require.Nil(t, resp) require.Error(t, err) require.Nil(t, networks) diff --git a/rest/optiondef.go b/rest/optiondef.go index a3ad0c0..dbe2135 100644 --- a/rest/optiondef.go +++ b/rest/optiondef.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "gopkg.in/ns1/ns1-go.v2/rest/model/dhcp" @@ -13,8 +14,8 @@ type OptionDefService service // List returns a list of all option definitions. // // NS1 API docs: https://ns1.com/api#getlist-dhcp-option-definitions -func (s *OptionDefService) List() ([]dhcp.OptionDef, *http.Response, error) { - req, err := s.client.NewRequest(http.MethodGet, "dhcp/optiondef", nil) +func (s *OptionDefService) List(ctx context.Context) ([]dhcp.OptionDef, *http.Response, error) { + req, err := s.client.NewRequest(ctx, http.MethodGet, "dhcp/optiondef", nil) if err != nil { return nil, nil, err } @@ -27,9 +28,9 @@ func (s *OptionDefService) List() ([]dhcp.OptionDef, *http.Response, error) { // Get returns the option definition corresponding to the provided ID. // // NS1 API docs: https://ns1.com/api#getview-dhcp-option-definition -func (s *OptionDefService) Get(odSpace, odKey string) (*dhcp.OptionDef, *http.Response, error) { +func (s *OptionDefService) Get(ctx context.Context, odSpace, odKey string) (*dhcp.OptionDef, *http.Response, error) { reqPath := fmt.Sprintf("dhcp/optiondef/%s/%s", odSpace, odKey) - req, err := s.client.NewRequest(http.MethodGet, reqPath, nil) + req, err := s.client.NewRequest(ctx, http.MethodGet, reqPath, nil) if err != nil { return nil, nil, err } @@ -48,7 +49,7 @@ func (s *OptionDefService) Get(odSpace, odKey string) (*dhcp.OptionDef, *http.Re // The FriendlyName, Description, Code, Schema.Type fields are required. // // NS1 API docs: https://ns1.com/api#putcreate-an-custom-dhcp-option-definition -func (s *OptionDefService) Create(od *dhcp.OptionDef, odSpace, odKey string) (*dhcp.OptionDef, *http.Response, error) { +func (s *OptionDefService) Create(ctx context.Context, od *dhcp.OptionDef, odSpace, odKey string) (*dhcp.OptionDef, *http.Response, error) { switch { case od.FriendlyName == "": return nil, nil, errors.New("the FriendlyName field is required") @@ -61,7 +62,7 @@ func (s *OptionDefService) Create(od *dhcp.OptionDef, odSpace, odKey string) (*d } reqPath := fmt.Sprintf("dhcp/optiondef/%s/%s", odSpace, odKey) - req, err := s.client.NewRequest(http.MethodPut, reqPath, od) + req, err := s.client.NewRequest(ctx, http.MethodPut, reqPath, od) if err != nil { return nil, nil, err } @@ -79,9 +80,9 @@ func (s *OptionDefService) Create(od *dhcp.OptionDef, odSpace, odKey string) (*d // Delete removes a option definition entirely. // // NS1 API docs: https://ns1.com/api#deletedelete-a-custom-dhcp-option-definition -func (s *OptionDefService) Delete(odSpace, odKey string) (*http.Response, error) { +func (s *OptionDefService) Delete(ctx context.Context, odSpace, odKey string) (*http.Response, error) { reqPath := fmt.Sprintf("dhcp/optiondef/%s/%s", odSpace, odKey) - req, err := s.client.NewRequest(http.MethodDelete, reqPath, nil) + req, err := s.client.NewRequest(ctx, http.MethodDelete, reqPath, nil) if err != nil { return nil, err } diff --git a/rest/optiondef_test.go b/rest/optiondef_test.go index f24c1b0..bb5ac02 100644 --- a/rest/optiondef_test.go +++ b/rest/optiondef_test.go @@ -1,6 +1,7 @@ package rest_test import ( + "context" "gopkg.in/ns1/ns1-go.v2/rest/model/dhcp" "net/http" "testing" @@ -45,7 +46,7 @@ func TestDHCPOptionDef(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respSgs, _, err := client.OptionDef.List() + respSgs, _, err := client.OptionDef.List(context.Background()) if err != nil { t.Fatalf("error listing DHCP option definitions: %v", err) } @@ -79,7 +80,7 @@ func TestDHCPOptionDef(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respOD, _, err := client.OptionDef.Get("space", "key") + respOD, _, err := client.OptionDef.Get(context.Background(), "space", "key") if err != nil { t.Fatalf("error getting scpe group: %v", err) } @@ -94,7 +95,7 @@ func TestDHCPOptionDef(t *testing.T) { t.Run("RequiredParams", func(t *testing.T) { od := &dhcp.OptionDef{} - _, _, err = client.OptionDef.Create(od, "space", "key") + _, _, err = client.OptionDef.Create(context.Background(), od, "space", "key") if err == nil { t.Errorf("expected a missing code to result in an error") } @@ -102,7 +103,7 @@ func TestDHCPOptionDef(t *testing.T) { od = &dhcp.OptionDef{ Code: 1, } - _, _, err = client.OptionDef.Create(od, "space", "key") + _, _, err = client.OptionDef.Create(context.Background(), od, "space", "key") if err == nil { t.Errorf("expected a missing friendly name to result in an error") } @@ -111,7 +112,7 @@ func TestDHCPOptionDef(t *testing.T) { Code: 1, Description: "a", } - _, _, err = client.OptionDef.Create(od, "space", "key") + _, _, err = client.OptionDef.Create(context.Background(), od, "space", "key") if err == nil { t.Errorf("expected a missing description to result in an error") } @@ -121,7 +122,7 @@ func TestDHCPOptionDef(t *testing.T) { Description: "a", FriendlyName: "a", } - _, _, err = client.OptionDef.Create(od, "space", "key") + _, _, err = client.OptionDef.Create(context.Background(), od, "space", "key") if err == nil { t.Errorf("expected a missing schema type to result in an error") } @@ -139,7 +140,7 @@ func TestDHCPOptionDef(t *testing.T) { if err != nil { t.Fatalf("error adding test case: %v", err) } - respOD, _, err := client.OptionDef.Create(od, "space", "key") + respOD, _, err := client.OptionDef.Create(context.Background(), od, "space", "key") if err != nil { t.Fatalf("error creating option definition: %v", err) } @@ -156,7 +157,7 @@ func TestDHCPOptionDef(t *testing.T) { if err != nil { t.Fatalf("error adding test case: %v", err) } - _, err = client.OptionDef.Delete("space", "key") + _, err = client.OptionDef.Delete(context.Background(), "space", "key") if err != nil { t.Fatalf("error deleting option definition: %v", err) } diff --git a/rest/pulsar_job.go b/rest/pulsar_job.go index c65cf47..d887b1f 100644 --- a/rest/pulsar_job.go +++ b/rest/pulsar_job.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -14,9 +15,9 @@ type PulsarJobsService service // List takes an Application ID and returns all Jobs inside said Application. // // NS1 API docs: https://ns1.com/api/#getlist-jobs-within-an-app -func (s *PulsarJobsService) List(appId string) ([]*pulsar.PulsarJob, *http.Response, error) { +func (s *PulsarJobsService) List(ctx context.Context, appId string) ([]*pulsar.PulsarJob, *http.Response, error) { path := fmt.Sprintf("pulsar/apps/%s/jobs", appId) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -40,10 +41,10 @@ func (s *PulsarJobsService) List(appId string) ([]*pulsar.PulsarJob, *http.Respo // Get takes an Application ID and Job Id and returns full configuration for a pulsar Job. // // NS1 API docs: https://ns1.com/api/#getview-job-details -func (s *PulsarJobsService) Get(appId string, jobId string) (*pulsar.PulsarJob, *http.Response, error) { +func (s *PulsarJobsService) Get(ctx context.Context, appId string, jobId string) (*pulsar.PulsarJob, *http.Response, error) { path := fmt.Sprintf("pulsar/apps/%s/jobs/%s", appId, jobId) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -72,10 +73,10 @@ func (s *PulsarJobsService) Get(appId string, jobId string) (*pulsar.PulsarJob, // Create takes a *PulsarJob and an AppId and creates a new Pulsar Job in the specified Application with the specific name, typeid, host and url_path. // // NS1 API docs: https://ns1.com/api/#putcreate-a-pulsar-job -func (s *PulsarJobsService) Create(j *pulsar.PulsarJob) (*http.Response, error) { +func (s *PulsarJobsService) Create(ctx context.Context, j *pulsar.PulsarJob) (*http.Response, error) { path := fmt.Sprintf("pulsar/apps/%s/jobs", j.AppID) - req, err := s.client.NewRequest("PUT", path, j) + req, err := s.client.NewRequest(ctx, "PUT", path, j) if err != nil { return nil, err } @@ -99,10 +100,10 @@ func (s *PulsarJobsService) Create(j *pulsar.PulsarJob) (*http.Response, error) // // Only the fields to be updated are required in the given job. // NS1 API docs: https://ns1.com/api/#postmodify-a-pulsar-job -func (s *PulsarJobsService) Update(j *pulsar.PulsarJob) (*http.Response, error) { +func (s *PulsarJobsService) Update(ctx context.Context, j *pulsar.PulsarJob) (*http.Response, error) { path := fmt.Sprintf("pulsar/apps/%s/jobs/%s", j.AppID, j.JobID) - req, err := s.client.NewRequest("POST", path, j) + req, err := s.client.NewRequest(ctx, "POST", path, j) if err != nil { return nil, err } @@ -129,10 +130,10 @@ func (s *PulsarJobsService) Update(j *pulsar.PulsarJob) (*http.Response, error) // Delete takes a appId and jobId and removes an existing Pulsar job . // // NS1 API docs: https://ns1.com/api/#deletedelete-a-pulsar-job -func (s *PulsarJobsService) Delete(pulsarJob *pulsar.PulsarJob) (*http.Response, error) { +func (s *PulsarJobsService) Delete(ctx context.Context, pulsarJob *pulsar.PulsarJob) (*http.Response, error) { path := fmt.Sprintf("pulsar/apps/%s/jobs/%s", pulsarJob.AppID, pulsarJob.JobID) - req, err := s.client.NewRequest("DELETE", path, nil) + req, err := s.client.NewRequest(ctx, "DELETE", path, nil) if err != nil { return nil, err } diff --git a/rest/pulsar_job_test.go b/rest/pulsar_job_test.go index 31f3dbe..37f0a06 100644 --- a/rest/pulsar_job_test.go +++ b/rest/pulsar_job_test.go @@ -1,6 +1,7 @@ package rest_test import ( + "context" "fmt" "net/http" "reflect" @@ -48,7 +49,7 @@ func TestPulsarJob(t *testing.T) { require.Nil(t, mock.AddPulsarJobListTestCase(myAppID, nil, nil, pulsarJobs)) - respPulsarJobs, _, err := client.PulsarJobs.List(myAppID) + respPulsarJobs, _, err := client.PulsarJobs.List(context.Background(), myAppID) require.Nil(t, err) require.NotNil(t, respPulsarJobs) require.Equal(t, len(pulsarJobs), len(respPulsarJobs)) @@ -73,7 +74,7 @@ func TestPulsarJob(t *testing.T) { nil, nil, "", `{"message": "pulsar app not found"}`, )) - pulsarJobs, resp, err := client.PulsarJobs.List(myAppID) + pulsarJobs, resp, err := client.PulsarJobs.List(context.Background(), myAppID) require.Nil(t, pulsarJobs) require.NotNil(t, err) require.Equal(t, api.ErrAppMissing.Error(), err.Error()) @@ -89,7 +90,7 @@ func TestPulsarJob(t *testing.T) { nil, nil, "", `{"message": "error text that will be ignored by API"}`, )) - pulsarJobs, resp, err := client.PulsarJobs.List(myAppID) + pulsarJobs, resp, err := client.PulsarJobs.List(context.Background(), myAppID) require.Nil(t, pulsarJobs) require.NotNil(t, err) require.Contains(t, err.Error(), "does not exist") @@ -154,7 +155,7 @@ func TestPulsarJob(t *testing.T) { require.Nil(t, mock.AddPulsarJobGetTestCase(myAppID, myJobID, nil, nil, pulsarJob)) - respPulsarJob, _, err := client.PulsarJobs.Get(myAppID, myJobID) + respPulsarJob, _, err := client.PulsarJobs.Get(context.Background(), myAppID, myJobID) require.Nil(t, err) require.True(t, reflect.DeepEqual(pulsarJob, respPulsarJob)) @@ -169,7 +170,7 @@ func TestPulsarJob(t *testing.T) { http.MethodGet, fmt.Sprintf("/pulsar/apps/%s/jobs/%s", myAppID, myJobID), http.StatusNotFound, nil, nil, "", fmt.Sprintf(`{"message": "pulsar job %s not found for appid %s"}`, myJobID, myAppID), )) - pulsarJob, resp, err := client.PulsarJobs.Get(myAppID, myJobID) + pulsarJob, resp, err := client.PulsarJobs.Get(context.Background(), myAppID, myJobID) require.Nil(t, pulsarJob) require.NotNil(t, err) require.Equal(t, api.ErrJobMissing.Error(), err.Error()) @@ -184,7 +185,7 @@ func TestPulsarJob(t *testing.T) { http.MethodGet, fmt.Sprintf("/pulsar/apps/%s/jobs/%s", myAppID, myJobID), http.StatusNotFound, nil, nil, "", `{"message": "pulsar app not found"}`, )) - pulsarJob, resp, err := client.PulsarJobs.Get(myAppID, myJobID) + pulsarJob, resp, err := client.PulsarJobs.Get(context.Background(), myAppID, myJobID) require.Nil(t, pulsarJob) require.NotNil(t, err) require.Equal(t, api.ErrAppMissing.Error(), err.Error()) @@ -200,7 +201,7 @@ func TestPulsarJob(t *testing.T) { nil, nil, "", `{"message": "test error"}`, )) - pulsarJob, resp, err := client.PulsarJobs.Get(myAppID, myJobID) + pulsarJob, resp, err := client.PulsarJobs.Get(context.Background(), myAppID, myJobID) require.Nil(t, pulsarJob) require.NotNil(t, err) require.Contains(t, err.Error(), "test error") @@ -221,7 +222,7 @@ func TestPulsarJob(t *testing.T) { require.Nil(t, mock.AddPulsarJobCreateTestCase(nil, nil, pulsarJob, pulsarJob)) - _, err := client.PulsarJobs.Create(pulsarJob) + _, err := client.PulsarJobs.Create(context.Background(), pulsarJob) require.Nil(t, err) }) @@ -235,7 +236,7 @@ func TestPulsarJob(t *testing.T) { nil, nil, pulsarJob, `{"message": "pulsar app not found"}`, )) - _, err = client.PulsarJobs.Create(pulsarJob) + _, err = client.PulsarJobs.Create(context.Background(), pulsarJob) require.Equal(t, api.ErrAppMissing.Error(), err.Error()) }) @@ -249,7 +250,7 @@ func TestPulsarJob(t *testing.T) { nil, nil, pulsarJob, `{"message": "test error"}`, )) - _, err = client.PulsarJobs.Create(pulsarJob) + _, err = client.PulsarJobs.Create(context.Background(), pulsarJob) require.Contains(t, err.Error(), "test error") }) @@ -269,7 +270,7 @@ func TestPulsarJob(t *testing.T) { require.Nil(t, mock.AddPulsarJobUpdateTestCase(nil, nil, pulsarJob, pulsarJob)) - _, err := client.PulsarJobs.Update(pulsarJob) + _, err := client.PulsarJobs.Update(context.Background(), pulsarJob) require.Nil(t, err) }) @@ -282,7 +283,7 @@ func TestPulsarJob(t *testing.T) { http.MethodPost, fmt.Sprintf("/pulsar/apps/%s/jobs/%s", pulsarJob.AppID, pulsarJob.JobID), http.StatusNotFound, nil, nil, pulsarJob, fmt.Sprintf(`{"message": "pulsar job %s not found for appid %s"}`, myJobID, myAppID), )) - resp, err := client.PulsarJobs.Update(pulsarJob) + resp, err := client.PulsarJobs.Update(context.Background(), pulsarJob) require.NotNil(t, err) require.Equal(t, api.ErrJobMissing.Error(), err.Error()) require.Equal(t, http.StatusNotFound, resp.StatusCode) @@ -296,7 +297,7 @@ func TestPulsarJob(t *testing.T) { http.MethodPost, fmt.Sprintf("/pulsar/apps/%s/jobs/%s", pulsarJob.AppID, pulsarJob.JobID), http.StatusNotFound, nil, nil, pulsarJob, `{"message": "pulsar app not found"}`, )) - resp, err := client.PulsarJobs.Update(pulsarJob) + resp, err := client.PulsarJobs.Update(context.Background(), pulsarJob) require.NotNil(t, err) require.Equal(t, api.ErrAppMissing.Error(), err.Error()) @@ -311,7 +312,7 @@ func TestPulsarJob(t *testing.T) { http.MethodPost, fmt.Sprintf("/pulsar/apps/%s/jobs/%s", pulsarJob.AppID, pulsarJob.JobID), http.StatusNotFound, nil, nil, pulsarJob, `{"message": "test error"}`, )) - resp, err := client.PulsarJobs.Update(pulsarJob) + resp, err := client.PulsarJobs.Update(context.Background(), pulsarJob) require.NotNil(t, err) require.Contains(t, err.Error(), "test error") @@ -333,7 +334,7 @@ func TestPulsarJob(t *testing.T) { require.Nil(t, mock.AddPulsarJobDeleteTestCase(nil, nil, pulsarJob, nil)) - _, err := client.PulsarJobs.Delete(pulsarJob) + _, err := client.PulsarJobs.Delete(context.Background(), pulsarJob) require.Nil(t, err) }) @@ -347,7 +348,7 @@ func TestPulsarJob(t *testing.T) { http.MethodDelete, fmt.Sprintf("/pulsar/apps/%s/jobs/%s", pulsarJob.AppID, pulsarJob.JobID), http.StatusNotFound, nil, nil, "", fmt.Sprintf(`{"message": "pulsar job %s not found for appid %s"}`, pulsarJob.JobID, pulsarJob.AppID), )) - resp, err := client.PulsarJobs.Delete(pulsarJob) + resp, err := client.PulsarJobs.Delete(context.Background(), pulsarJob) require.NotNil(t, err) require.Equal(t, api.ErrJobMissing.Error(), err.Error()) require.Equal(t, http.StatusNotFound, resp.StatusCode) @@ -361,7 +362,7 @@ func TestPulsarJob(t *testing.T) { http.MethodDelete, fmt.Sprintf("/pulsar/apps/%s/jobs/%s", pulsarJob.AppID, pulsarJob.JobID), http.StatusNotFound, nil, nil, "", `{"message": "pulsar app not found"}`, )) - resp, err := client.PulsarJobs.Delete(pulsarJob) + resp, err := client.PulsarJobs.Delete(context.Background(), pulsarJob) require.NotNil(t, err) require.Equal(t, api.ErrAppMissing.Error(), err.Error()) @@ -376,7 +377,7 @@ func TestPulsarJob(t *testing.T) { http.MethodDelete, fmt.Sprintf("/pulsar/apps/%s/jobs/%s", pulsarJob.AppID, pulsarJob.JobID), http.StatusNotFound, nil, nil, "", `{"message": "test error"}`, )) - resp, err := client.PulsarJobs.Delete(pulsarJob) + resp, err := client.PulsarJobs.Delete(context.Background(), pulsarJob) require.NotNil(t, err) require.Contains(t, err.Error(), "test error") diff --git a/rest/record.go b/rest/record.go index 4aed429..b58f7af 100644 --- a/rest/record.go +++ b/rest/record.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -14,10 +15,10 @@ type RecordsService service // Get takes a zone, domain and record type t and returns full configuration for a DNS record. // // NS1 API docs: https://ns1.com/api/#record-get -func (s *RecordsService) Get(zone, domain, t string) (*dns.Record, *http.Response, error) { +func (s *RecordsService) Get(ctx context.Context, zone, domain, t string) (*dns.Record, *http.Response, error) { path := fmt.Sprintf("zones/%s/%s/%s", zone, domain, t) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -41,10 +42,10 @@ func (s *RecordsService) Get(zone, domain, t string) (*dns.Record, *http.Respons // // The given record must have at least one answer. // NS1 API docs: https://ns1.com/api/#record-put -func (s *RecordsService) Create(r *dns.Record) (*http.Response, error) { +func (s *RecordsService) Create(ctx context.Context, r *dns.Record) (*http.Response, error) { path := fmt.Sprintf("zones/%s/%s/%s", r.Zone, r.Domain, r.Type) - req, err := s.client.NewRequest("PUT", path, &r) + req, err := s.client.NewRequest(ctx, "PUT", path, &r) if err != nil { return nil, err } @@ -71,10 +72,10 @@ func (s *RecordsService) Create(r *dns.Record) (*http.Response, error) { // // Only the fields to be updated are required in the given record. // NS1 API docs: https://ns1.com/api/#record-post -func (s *RecordsService) Update(r *dns.Record) (*http.Response, error) { +func (s *RecordsService) Update(ctx context.Context, r *dns.Record) (*http.Response, error) { path := fmt.Sprintf("zones/%s/%s/%s", r.Zone, r.Domain, r.Type) - req, err := s.client.NewRequest("POST", path, &r) + req, err := s.client.NewRequest(ctx, "POST", path, &r) if err != nil { return nil, err } @@ -102,10 +103,10 @@ func (s *RecordsService) Update(r *dns.Record) (*http.Response, error) { // Delete takes a zone, domain and record type t and removes an existing record and all associated answers and configuration details. // // NS1 API docs: https://ns1.com/api/#record-delete -func (s *RecordsService) Delete(zone string, domain string, t string) (*http.Response, error) { +func (s *RecordsService) Delete(ctx context.Context, zone string, domain string, t string) (*http.Response, error) { path := fmt.Sprintf("zones/%s/%s/%s", zone, domain, t) - req, err := s.client.NewRequest("DELETE", path, nil) + req, err := s.client.NewRequest(ctx, "DELETE", path, nil) if err != nil { return nil, err } diff --git a/rest/reservation.go b/rest/reservation.go index a3747d0..9ed6758 100644 --- a/rest/reservation.go +++ b/rest/reservation.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -14,8 +15,8 @@ type ReservationService service // List returns a list of all reservations. // // NS1 API docs: https://ns1.com/api#getlist-reservations -func (s *ReservationService) List() ([]dhcp.Reservation, *http.Response, error) { - req, err := s.client.NewRequest(http.MethodGet, "dhcp/reservation", nil) +func (s *ReservationService) List(ctx context.Context) ([]dhcp.Reservation, *http.Response, error) { + req, err := s.client.NewRequest(ctx, http.MethodGet, "dhcp/reservation", nil) if err != nil { return nil, nil, err } @@ -32,9 +33,9 @@ func (s *ReservationService) List() ([]dhcp.Reservation, *http.Response, error) // Get returns the reservation corresponding to the provided reservation ID. // // NS1 API docs: https://ns1.com/api#getview-a-reservations-details -func (s *ReservationService) Get(scID int) (*dhcp.Reservation, *http.Response, error) { +func (s *ReservationService) Get(ctx context.Context, scID int) (*dhcp.Reservation, *http.Response, error) { reqPath := fmt.Sprintf("dhcp/reservation/%d", scID) - req, err := s.client.NewRequest(http.MethodGet, reqPath, nil) + req, err := s.client.NewRequest(ctx, http.MethodGet, reqPath, nil) if err != nil { return nil, nil, err } @@ -53,13 +54,13 @@ func (s *ReservationService) Get(scID int) (*dhcp.Reservation, *http.Response, e // The Options field is required. // // NS1 API docs: https://ns1.com/api#putcreate-a-reservation -func (s *ReservationService) Create(sc *dhcp.Reservation) (*dhcp.Reservation, *http.Response, error) { +func (s *ReservationService) Create(ctx context.Context, sc *dhcp.Reservation) (*dhcp.Reservation, *http.Response, error) { switch { case sc.Options == nil: return nil, nil, errors.New("the Options field is required") } - req, err := s.client.NewRequest(http.MethodPut, "dhcp/reservation", sc) + req, err := s.client.NewRequest(ctx, http.MethodPut, "dhcp/reservation", sc) if err != nil { return nil, nil, err } @@ -78,7 +79,7 @@ func (s *ReservationService) Create(sc *dhcp.Reservation) (*dhcp.Reservation, *h // The ID, Options fields are required. // // NS1 API docs: https://ns1.com/api#postmodify-a-reservation -func (s *ReservationService) Edit(sc *dhcp.Reservation) (*dhcp.Reservation, *http.Response, error) { +func (s *ReservationService) Edit(ctx context.Context, sc *dhcp.Reservation) (*dhcp.Reservation, *http.Response, error) { switch { case sc.ID == nil: return nil, nil, errors.New("the ID field is required") @@ -87,7 +88,7 @@ func (s *ReservationService) Edit(sc *dhcp.Reservation) (*dhcp.Reservation, *htt } reqPath := fmt.Sprintf("dhcp/reservation/%d", *sc.ID) - req, err := s.client.NewRequest(http.MethodPost, reqPath, sc) + req, err := s.client.NewRequest(ctx, http.MethodPost, reqPath, sc) if err != nil { return nil, nil, err } @@ -103,9 +104,9 @@ func (s *ReservationService) Edit(sc *dhcp.Reservation) (*dhcp.Reservation, *htt // Delete removes a reservation entirely. // // NS1 API docs: https://ns1.com/api#deletedelete-a-reservation -func (s *ReservationService) Delete(id int) (*http.Response, error) { +func (s *ReservationService) Delete(ctx context.Context, id int) (*http.Response, error) { reqPath := fmt.Sprintf("dhcp/reservation/%d", id) - req, err := s.client.NewRequest(http.MethodDelete, reqPath, nil) + req, err := s.client.NewRequest(ctx, http.MethodDelete, reqPath, nil) if err != nil { return nil, err } diff --git a/rest/reservation_test.go b/rest/reservation_test.go index 18c12a6..c11df2d 100644 --- a/rest/reservation_test.go +++ b/rest/reservation_test.go @@ -1,6 +1,7 @@ package rest_test import ( + "context" "gopkg.in/ns1/ns1-go.v2/rest/model/dhcp" "net/http" "testing" @@ -34,7 +35,7 @@ func TestDHCPReservation(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respSgs, _, err := client.Reservation.List() + respSgs, _, err := client.Reservation.List(context.Background()) if err != nil { t.Fatalf("error listing reservations: %v", err) } @@ -60,7 +61,7 @@ func TestDHCPReservation(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respAddr, _, err := client.Reservation.Get(1) + respAddr, _, err := client.Reservation.Get(context.Background(), 1) if err != nil { t.Fatalf("error getting reservation: %v", err) } @@ -74,7 +75,7 @@ func TestDHCPReservation(t *testing.T) { t.Run("RequiredParams", func(t *testing.T) { sg := &dhcp.Reservation{} - _, _, err = client.Reservation.Create(sg) + _, _, err = client.Reservation.Create(context.Background(), sg) if err == nil { t.Errorf("expected a missing address id to result in an error") } @@ -89,7 +90,7 @@ func TestDHCPReservation(t *testing.T) { if err != nil { t.Fatalf("error adding test case: %v", err) } - respSG, _, err := client.Reservation.Create(sg) + respSG, _, err := client.Reservation.Create(context.Background(), sg) if err != nil { t.Fatalf("error creating reservation: %v", err) } @@ -105,13 +106,13 @@ func TestDHCPReservation(t *testing.T) { IDAddress: &idAddr, Options: make([]dhcp.Option, 0), } - _, _, err = client.Reservation.Edit(sg) + _, _, err = client.Reservation.Edit(context.Background(), sg) if err == nil { t.Errorf("expected a missing ID to result in an error") } sg = &dhcp.Reservation{} - _, _, err = client.Reservation.Edit(sg) + _, _, err = client.Reservation.Edit(context.Background(), sg) if err == nil { t.Errorf("expected a missing reservation ID to result in an error") } @@ -131,7 +132,7 @@ func TestDHCPReservation(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respSG, _, err := client.Reservation.Edit(sg) + respSG, _, err := client.Reservation.Edit(context.Background(), sg) if err != nil { t.Fatalf("error editing reservation: %v", err) } @@ -147,7 +148,7 @@ func TestDHCPReservation(t *testing.T) { if err != nil { t.Fatalf("error adding test case: %v", err) } - _, err = client.Reservation.Delete(1) + _, err = client.Reservation.Delete(context.Background(), 1) if err != nil { t.Fatalf("error deleting reservation: %v", err) } diff --git a/rest/scope.go b/rest/scope.go index 7c70b65..5750e7e 100644 --- a/rest/scope.go +++ b/rest/scope.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -14,8 +15,8 @@ type ScopeService service // List returns a list of all scopes. // // NS1 API docs: https://ns1.com/api#getlist-scopes -func (s *ScopeService) List() ([]dhcp.Scope, *http.Response, error) { - req, err := s.client.NewRequest(http.MethodGet, "dhcp/scope", nil) +func (s *ScopeService) List(ctx context.Context) ([]dhcp.Scope, *http.Response, error) { + req, err := s.client.NewRequest(ctx, http.MethodGet, "dhcp/scope", nil) if err != nil { return nil, nil, err } @@ -32,9 +33,9 @@ func (s *ScopeService) List() ([]dhcp.Scope, *http.Response, error) { // Get returns the scope corresponding to the provided scope ID. // // NS1 API docs: https://ns1.com/api#getview-scope-details -func (s *ScopeService) Get(scID int) (*dhcp.Scope, *http.Response, error) { +func (s *ScopeService) Get(ctx context.Context, scID int) (*dhcp.Scope, *http.Response, error) { reqPath := fmt.Sprintf("dhcp/scope/%d", scID) - req, err := s.client.NewRequest(http.MethodGet, reqPath, nil) + req, err := s.client.NewRequest(ctx, http.MethodGet, reqPath, nil) if err != nil { return nil, nil, err } @@ -53,13 +54,13 @@ func (s *ScopeService) Get(scID int) (*dhcp.Scope, *http.Response, error) { // The IDAddress field is required. // // NS1 API docs: https://ns1.com/api#putcreate-a-scope -func (s *ScopeService) Create(sc *dhcp.Scope) (*dhcp.Scope, *http.Response, error) { +func (s *ScopeService) Create(ctx context.Context, sc *dhcp.Scope) (*dhcp.Scope, *http.Response, error) { switch { case sc.IDAddress == nil: return nil, nil, errors.New("the IDAddress field is required") } - req, err := s.client.NewRequest(http.MethodPut, "dhcp/scope", sc) + req, err := s.client.NewRequest(ctx, http.MethodPut, "dhcp/scope", sc) if err != nil { return nil, nil, err } @@ -78,14 +79,14 @@ func (s *ScopeService) Create(sc *dhcp.Scope) (*dhcp.Scope, *http.Response, erro // The IDAddress field is required. // // NS1 API docs: https://ns1.com/api#postmodify-a-scope -func (s *ScopeService) Edit(sc *dhcp.Scope) (*dhcp.Scope, *http.Response, error) { +func (s *ScopeService) Edit(ctx context.Context, sc *dhcp.Scope) (*dhcp.Scope, *http.Response, error) { switch { case sc.IDAddress == nil: return nil, nil, errors.New("the IDAddress field is required") } reqPath := fmt.Sprintf("dhcp/scope/%d", sc.ID) - req, err := s.client.NewRequest(http.MethodPost, reqPath, sc) + req, err := s.client.NewRequest(ctx, http.MethodPost, reqPath, sc) if err != nil { return nil, nil, err } @@ -101,9 +102,9 @@ func (s *ScopeService) Edit(sc *dhcp.Scope) (*dhcp.Scope, *http.Response, error) // Delete removes a scope entirely. // // NS1 API docs: https://ns1.com/api#deleteremove-a-scope -func (s *ScopeService) Delete(id int) (*http.Response, error) { +func (s *ScopeService) Delete(ctx context.Context, id int) (*http.Response, error) { reqPath := fmt.Sprintf("dhcp/scope/%d", id) - req, err := s.client.NewRequest(http.MethodDelete, reqPath, nil) + req, err := s.client.NewRequest(ctx, http.MethodDelete, reqPath, nil) if err != nil { return nil, err } diff --git a/rest/scope_test.go b/rest/scope_test.go index 778f1df..94c7182 100644 --- a/rest/scope_test.go +++ b/rest/scope_test.go @@ -1,6 +1,7 @@ package rest_test import ( + "context" "gopkg.in/ns1/ns1-go.v2/rest/model/dhcp" "net/http" "testing" @@ -34,7 +35,7 @@ func TestDHCPScope(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respSgs, _, err := client.Scope.List() + respSgs, _, err := client.Scope.List(context.Background()) if err != nil { t.Fatalf("error listing scopes: %v", err) } @@ -60,7 +61,7 @@ func TestDHCPScope(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respAddr, _, err := client.Scope.Get(1) + respAddr, _, err := client.Scope.Get(context.Background(), 1) if err != nil { t.Fatalf("error getting scope: %v", err) } @@ -74,7 +75,7 @@ func TestDHCPScope(t *testing.T) { t.Run("RequiredParams", func(t *testing.T) { sg := &dhcp.Scope{} - _, _, err = client.Scope.Create(sg) + _, _, err = client.Scope.Create(context.Background(), sg) if err == nil { t.Errorf("expected a missing address id to result in an error") } @@ -88,7 +89,7 @@ func TestDHCPScope(t *testing.T) { if err != nil { t.Fatalf("error adding test case: %v", err) } - respSG, _, err := client.Scope.Create(sg) + respSG, _, err := client.Scope.Create(context.Background(), sg) if err != nil { t.Fatalf("error creating scope: %v", err) } @@ -101,13 +102,13 @@ func TestDHCPScope(t *testing.T) { t.Run("RequiredParams", func(t *testing.T) { idAddr := 123 sg := &dhcp.Scope{IDAddress: &idAddr} - _, _, err = client.Scope.Edit(sg) + _, _, err = client.Scope.Edit(context.Background(), sg) if err == nil { t.Errorf("expected a missing ID to result in an error") } sg = &dhcp.Scope{} - _, _, err = client.Scope.Edit(sg) + _, _, err = client.Scope.Edit(context.Background(), sg) if err == nil { t.Errorf("expected a missing address ID to result in an error") } @@ -125,7 +126,7 @@ func TestDHCPScope(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respSG, _, err := client.Scope.Edit(sg) + respSG, _, err := client.Scope.Edit(context.Background(), sg) if err != nil { t.Fatalf("error editing scope: %v", err) } @@ -141,7 +142,7 @@ func TestDHCPScope(t *testing.T) { if err != nil { t.Fatalf("error adding test case: %v", err) } - _, err = client.Scope.Delete(1) + _, err = client.Scope.Delete(context.Background(), 1) if err != nil { t.Fatalf("error deleting scope: %v", err) } diff --git a/rest/scopegroup.go b/rest/scopegroup.go index 0315006..e2b570a 100644 --- a/rest/scopegroup.go +++ b/rest/scopegroup.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "gopkg.in/ns1/ns1-go.v2/rest/model/dhcp" @@ -13,8 +14,8 @@ type ScopeGroupService service // List returns a list of all scope groups. // // NS1 API docs: https://ns1.com/api#getlist-scope-groups -func (s *ScopeGroupService) List() ([]dhcp.ScopeGroup, *http.Response, error) { - req, err := s.client.NewRequest(http.MethodGet, "dhcp/scopegroup", nil) +func (s *ScopeGroupService) List(ctx context.Context) ([]dhcp.ScopeGroup, *http.Response, error) { + req, err := s.client.NewRequest(ctx, http.MethodGet, "dhcp/scopegroup", nil) if err != nil { return nil, nil, err } @@ -27,9 +28,9 @@ func (s *ScopeGroupService) List() ([]dhcp.ScopeGroup, *http.Response, error) { // Get returns the Scope Group corresponding to the provided scope group ID. // // NS1 API docs: https://ns1.com/api#getview-scope-group -func (s *ScopeGroupService) Get(sgID int) (*dhcp.ScopeGroup, *http.Response, error) { +func (s *ScopeGroupService) Get(ctx context.Context, sgID int) (*dhcp.ScopeGroup, *http.Response, error) { reqPath := fmt.Sprintf("dhcp/scopegroup/%d", sgID) - req, err := s.client.NewRequest(http.MethodGet, reqPath, nil) + req, err := s.client.NewRequest(ctx, http.MethodGet, reqPath, nil) if err != nil { return nil, nil, err } @@ -48,13 +49,13 @@ func (s *ScopeGroupService) Get(sgID int) (*dhcp.ScopeGroup, *http.Response, err // The Name field is required. // // NS1 API docs: https://ns1.com/api#putcreate-a-scope-group -func (s *ScopeGroupService) Create(sg *dhcp.ScopeGroup) (*dhcp.ScopeGroup, *http.Response, error) { +func (s *ScopeGroupService) Create(ctx context.Context, sg *dhcp.ScopeGroup) (*dhcp.ScopeGroup, *http.Response, error) { switch { case sg.Name == "": return nil, nil, errors.New("the Name field is required") } - req, err := s.client.NewRequest(http.MethodPut, "dhcp/scopegroup", sg) + req, err := s.client.NewRequest(ctx, http.MethodPut, "dhcp/scopegroup", sg) if err != nil { return nil, nil, err } @@ -73,13 +74,13 @@ func (s *ScopeGroupService) Create(sg *dhcp.ScopeGroup) (*dhcp.ScopeGroup, *http // The ID field is required. // // NS1 API docs: https://ns1.com/api#postedit-scope-group -func (s *ScopeGroupService) Edit(sg *dhcp.ScopeGroup) (*dhcp.ScopeGroup, *http.Response, error) { +func (s *ScopeGroupService) Edit(ctx context.Context, sg *dhcp.ScopeGroup) (*dhcp.ScopeGroup, *http.Response, error) { if sg.ID == nil { return nil, nil, errors.New("the ID field is required") } reqPath := fmt.Sprintf("dhcp/scopegroup/%d", *sg.ID) - req, err := s.client.NewRequest(http.MethodPost, reqPath, sg) + req, err := s.client.NewRequest(ctx, http.MethodPost, reqPath, sg) if err != nil { return nil, nil, err } @@ -95,9 +96,9 @@ func (s *ScopeGroupService) Edit(sg *dhcp.ScopeGroup) (*dhcp.ScopeGroup, *http.R // Delete removes a Scope Group entirely. // // NS1 API docs: https://ns1.com/api#deleteremove-scope-group-by-id -func (s *ScopeGroupService) Delete(id int) (*http.Response, error) { +func (s *ScopeGroupService) Delete(ctx context.Context, id int) (*http.Response, error) { reqPath := fmt.Sprintf("dhcp/scopegroup/%d", id) - req, err := s.client.NewRequest(http.MethodDelete, reqPath, nil) + req, err := s.client.NewRequest(ctx, http.MethodDelete, reqPath, nil) if err != nil { return nil, err } diff --git a/rest/scopegroup_test.go b/rest/scopegroup_test.go index 34c85f2..aa8e6b4 100644 --- a/rest/scopegroup_test.go +++ b/rest/scopegroup_test.go @@ -1,6 +1,7 @@ package rest_test import ( + "context" "gopkg.in/ns1/ns1-go.v2/rest/model/dhcp" "net/http" "testing" @@ -33,7 +34,7 @@ func TestDHCPScopeGroup(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respSgs, _, err := client.ScopeGroup.List() + respSgs, _, err := client.ScopeGroup.List(context.Background()) if err != nil { t.Fatalf("error listing IPAM addresses: %v", err) } @@ -59,7 +60,7 @@ func TestDHCPScopeGroup(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respAddr, _, err := client.ScopeGroup.Get(1) + respAddr, _, err := client.ScopeGroup.Get(context.Background(), 1) if err != nil { t.Fatalf("error getting scpe group: %v", err) } @@ -73,7 +74,7 @@ func TestDHCPScopeGroup(t *testing.T) { t.Run("RequiredParams", func(t *testing.T) { sg := &dhcp.ScopeGroup{} - _, _, err = client.ScopeGroup.Create(sg) + _, _, err = client.ScopeGroup.Create(context.Background(), sg) if err == nil { t.Errorf("expected a missing name to result in an error") } @@ -94,7 +95,7 @@ func TestDHCPScopeGroup(t *testing.T) { if err != nil { t.Fatalf("error adding test case: %v", err) } - respSG, _, err := client.ScopeGroup.Create(sg) + respSG, _, err := client.ScopeGroup.Create(context.Background(), sg) if err != nil { t.Fatalf("error creating scope group: %v", err) } @@ -106,7 +107,7 @@ func TestDHCPScopeGroup(t *testing.T) { t.Run("Edit", func(t *testing.T) { t.Run("RequiredParams", func(t *testing.T) { sg := &dhcp.ScopeGroup{Name: "a"} - _, _, err = client.ScopeGroup.Edit(sg) + _, _, err = client.ScopeGroup.Edit(context.Background(), sg) if err == nil { t.Errorf("expected a missing ID to result in an error") } @@ -132,7 +133,7 @@ func TestDHCPScopeGroup(t *testing.T) { t.Fatalf("error adding test case: %v", err) } - respSG, _, err := client.ScopeGroup.Edit(sg) + respSG, _, err := client.ScopeGroup.Edit(context.Background(), sg) if err != nil { t.Fatalf("error editing scope group: %v", err) } @@ -148,7 +149,7 @@ func TestDHCPScopeGroup(t *testing.T) { if err != nil { t.Fatalf("error adding test case: %v", err) } - _, err = client.ScopeGroup.Delete(1) + _, err = client.ScopeGroup.Delete(context.Background(), 1) if err != nil { t.Fatalf("error deleting scope group: %v", err) } diff --git a/rest/stat.go b/rest/stat.go index 57877e8..ed8ac2b 100644 --- a/rest/stat.go +++ b/rest/stat.go @@ -1,6 +1,7 @@ package rest import ( + "context" "fmt" "net/http" ) @@ -13,28 +14,28 @@ type StatsService service // GetQPS returns current queries per second (QPS) for the account. // The QPS number is lagged by approximately 30 seconds for statistics collection; // and the rate is computed over the preceding minute. -func (s *StatsService) GetQPS() (float32, *http.Response, error) { - return s.getQPS(statsQPSEndpoint) +func (s *StatsService) GetQPS(ctx context.Context) (float32, *http.Response, error) { + return s.getQPS(ctx, statsQPSEndpoint) } // GetZoneQPS returns current queries per second (QPS) for a specific zone. // The QPS number is lagged by approximately 30 seconds for statistics collection; // and the rate is computed over the preceding minute. -func (s *StatsService) GetZoneQPS(zone string) (float32, *http.Response, error) { +func (s *StatsService) GetZoneQPS(ctx context.Context, zone string) (float32, *http.Response, error) { path := fmt.Sprintf("%s/%s", statsQPSEndpoint, zone) - return s.getQPS(path) + return s.getQPS(ctx, path) } // GetRecordQPS returns current queries per second (QPS) for a specific record. // The QPS number is lagged by approximately 30 seconds for statistics collection; // and the rate is computed over the preceding minute. -func (s *StatsService) GetRecordQPS(zone, record, t string) (float32, *http.Response, error) { +func (s *StatsService) GetRecordQPS(ctx context.Context, zone, record, t string) (float32, *http.Response, error) { path := fmt.Sprintf("%s/%s/%s/%s", statsQPSEndpoint, zone, record, t) - return s.getQPS(path) + return s.getQPS(ctx, path) } -func (s *StatsService) getQPS(path string) (float32, *http.Response, error) { - req, err := s.client.NewRequest("GET", path, nil) +func (s *StatsService) getQPS(ctx context.Context, path string) (float32, *http.Response, error) { + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return 0, nil, err } diff --git a/rest/tsig_key.go b/rest/tsig_key.go index 673754a..38ec717 100644 --- a/rest/tsig_key.go +++ b/rest/tsig_key.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -14,8 +15,8 @@ type TsigService service // List returns all tsig keys and basic tsig keys configuration details for each. // // NS1 API docs: https://ns1.com/api/#getlist-tsig-keys -func (s *TsigService) List() ([]*dns.TSIGKey, *http.Response, error) { - req, err := s.client.NewRequest("GET", "tsig", nil) +func (s *TsigService) List(ctx context.Context) ([]*dns.TSIGKey, *http.Response, error) { + req, err := s.client.NewRequest(ctx, "GET", "tsig", nil) if err != nil { return nil, nil, err } @@ -33,10 +34,10 @@ func (s *TsigService) List() ([]*dns.TSIGKey, *http.Response, error) { // Get takes a TSIG key name and returns a single TSIG key and its basic configuration details. // // NS1 API docs: https://ns1.com/api/#getview-tsig-key-details -func (s *TsigService) Get(name string) (*dns.TSIGKey, *http.Response, error) { +func (s *TsigService) Get(ctx context.Context, name string) (*dns.TSIGKey, *http.Response, error) { path := fmt.Sprintf("tsig/%s", name) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -60,10 +61,10 @@ func (s *TsigService) Get(name string) (*dns.TSIGKey, *http.Response, error) { // Create takes a *TSIGkey and creates a new TSIG key. // // NS1 API docs: https://ns1.com/api/#putcreate-a-tsig-key -func (s *TsigService) Create(tk *dns.TSIGKey) (*http.Response, error) { +func (s *TsigService) Create(ctx context.Context, tk *dns.TSIGKey) (*http.Response, error) { path := fmt.Sprintf("tsig/%s", tk.Name) - req, err := s.client.NewRequest("PUT", path, &tk) + req, err := s.client.NewRequest(ctx, "PUT", path, &tk) if err != nil { return nil, err } @@ -86,10 +87,10 @@ func (s *TsigService) Create(tk *dns.TSIGKey) (*http.Response, error) { // Update takes a *TSIGKey and modifies basic details of a TSIG key. // // NS1 API docs: https://ns1.com/api/#postmodify-a-tsig-key -func (s *TsigService) Update(tk *dns.TSIGKey) (*http.Response, error) { +func (s *TsigService) Update(ctx context.Context, tk *dns.TSIGKey) (*http.Response, error) { path := fmt.Sprintf("tsig/%s", tk.Name) - req, err := s.client.NewRequest("POST", path, &tk) + req, err := s.client.NewRequest(ctx, "POST", path, &tk) if err != nil { return nil, err } @@ -112,10 +113,10 @@ func (s *TsigService) Update(tk *dns.TSIGKey) (*http.Response, error) { // Delete takes a TSIG key name and destroys an existing TSIG key. // // NS1 API docs: https://ns1.com/api/#deleteremove-a-tsig-key -func (s *TsigService) Delete(name string) (*http.Response, error) { +func (s *TsigService) Delete(ctx context.Context, name string) (*http.Response, error) { path := fmt.Sprintf("tsig/%s", name) - req, err := s.client.NewRequest("DELETE", path, nil) + req, err := s.client.NewRequest(ctx, "DELETE", path, nil) if err != nil { return nil, err } diff --git a/rest/tsig_key_test.go b/rest/tsig_key_test.go index 19d9fc8..f367414 100644 --- a/rest/tsig_key_test.go +++ b/rest/tsig_key_test.go @@ -1,6 +1,7 @@ package rest_test import ( + "context" "fmt" "net/http" "reflect" @@ -41,7 +42,7 @@ func TestTsigKey(t *testing.T) { require.Nil(t, mock.AddTsigKeyListTestCase(nil, nil, tsigKeys)) - respTsigKeys, _, err := client.TSIG.List() + respTsigKeys, _, err := client.TSIG.List(context.Background()) require.Nil(t, err) require.NotNil(t, respTsigKeys) require.Equal(t, len(tsigKeys), len(respTsigKeys)) @@ -61,7 +62,7 @@ func TestTsigKey(t *testing.T) { nil, nil, "", `{"message": "test error"}`, )) - tsigKeys, resp, err := client.TSIG.List() + tsigKeys, resp, err := client.TSIG.List(context.Background()) require.Nil(t, tsigKeys) require.NotNil(t, err) require.Contains(t, err.Error(), "test error") @@ -82,7 +83,7 @@ func TestTsigKey(t *testing.T) { require.Nil(t, mock.AddTsigKeyGetTestCase("TsigKey1", nil, nil, tsigKey)) - respTsigKey, _, err := client.TSIG.Get("TsigKey1") + respTsigKey, _, err := client.TSIG.Get(context.Background(), "TsigKey1") require.Nil(t, err) require.True(t, reflect.DeepEqual(tsigKey, respTsigKey)) @@ -96,7 +97,7 @@ func TestTsigKey(t *testing.T) { http.MethodGet, fmt.Sprintf("/tsig/%s", "TsigKey1"), http.StatusNotFound, nil, nil, "", `{"message": "TSIG key does not exist"}`, )) - tsigKey, resp, err := client.TSIG.Get("TsigKey1") + tsigKey, resp, err := client.TSIG.Get(context.Background(), "TsigKey1") require.Nil(t, tsigKey) require.NotNil(t, err) require.Contains(t, err.Error(), api.ErrTsigKeyMissing.Error()) @@ -116,7 +117,7 @@ func TestTsigKey(t *testing.T) { require.Nil(t, mock.AddTsigKeyCreateTestCase(nil, nil, tsigKey, tsigKey)) - _, err := client.TSIG.Create(tsigKey) + _, err := client.TSIG.Create(context.Background(), tsigKey) require.Nil(t, err) }) @@ -130,7 +131,7 @@ func TestTsigKey(t *testing.T) { nil, nil, tsigKey, `{"message": "TSIG key already exists"}`, )) - _, err = client.TSIG.Create(tsigKey) + _, err = client.TSIG.Create(context.Background(), tsigKey) require.Contains(t, err.Error(), api.ErrTsigKeyExists.Error()) }) @@ -149,7 +150,7 @@ func TestTsigKey(t *testing.T) { require.Nil(t, mock.AddTsigKeyUpdateTestCase(nil, nil, tsigKey, tsigKey)) - _, err := client.TSIG.Update(tsigKey) + _, err := client.TSIG.Update(context.Background(), tsigKey) require.Nil(t, err) }) @@ -162,7 +163,7 @@ func TestTsigKey(t *testing.T) { http.MethodPost, fmt.Sprintf("/tsig/%s", tsigKey.Name), http.StatusNotFound, nil, nil, tsigKey, `{"message": "TSIG key does not exist"}`, )) - resp, err := client.TSIG.Update(tsigKey) + resp, err := client.TSIG.Update(context.Background(), tsigKey) require.NotNil(t, err) require.Contains(t, err.Error(), api.ErrTsigKeyMissing.Error()) @@ -183,7 +184,7 @@ func TestTsigKey(t *testing.T) { require.Nil(t, mock.AddTsigKeyDeleteTestCase(nil, nil, tsigKey, nil)) - _, err := client.TSIG.Delete(tsigKey.Name) + _, err := client.TSIG.Delete(context.Background(), tsigKey.Name) require.Nil(t, err) }) @@ -196,7 +197,7 @@ func TestTsigKey(t *testing.T) { http.MethodDelete, fmt.Sprintf("tsig/%s", tsigKey.Name), http.StatusNotFound, nil, nil, "", `{"message": "TSIG key does not exist"}`, )) - resp, err := client.TSIG.Delete(tsigKey.Name) + resp, err := client.TSIG.Delete(context.Background(), tsigKey.Name) require.NotNil(t, err) require.Contains(t, err.Error(), api.ErrTsigKeyMissing.Error()) diff --git a/rest/zone.go b/rest/zone.go index 1bf4482..877e6bb 100644 --- a/rest/zone.go +++ b/rest/zone.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -14,8 +15,8 @@ type ZonesService service // List returns all active zones and basic zone configuration details for each. // // NS1 API docs: https://ns1.com/api/#zones-get -func (s *ZonesService) List() ([]*dns.Zone, *http.Response, error) { - req, err := s.client.NewRequest("GET", "zones", nil) +func (s *ZonesService) List(ctx context.Context) ([]*dns.Zone, *http.Response, error) { + req, err := s.client.NewRequest(ctx, "GET", "zones", nil) if err != nil { return nil, nil, err } @@ -37,10 +38,10 @@ func (s *ZonesService) List() ([]*dns.Zone, *http.Response, error) { // Get takes a zone name and returns a single active zone and its basic configuration details. // // NS1 API docs: https://ns1.com/api/#zones-zone-get -func (s *ZonesService) Get(zone string) (*dns.Zone, *http.Response, error) { +func (s *ZonesService) Get(ctx context.Context, zone string) (*dns.Zone, *http.Response, error) { path := fmt.Sprintf("zones/%s", zone) - req, err := s.client.NewRequest("GET", path, nil) + req, err := s.client.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, nil, err } @@ -68,10 +69,10 @@ func (s *ZonesService) Get(zone string) (*dns.Zone, *http.Response, error) { // Create takes a *Zone and creates a new DNS zone. // // NS1 API docs: https://ns1.com/api/#zones-put -func (s *ZonesService) Create(z *dns.Zone) (*http.Response, error) { +func (s *ZonesService) Create(ctx context.Context, z *dns.Zone) (*http.Response, error) { path := fmt.Sprintf("zones/%s", z.Zone) - req, err := s.client.NewRequest("PUT", path, &z) + req, err := s.client.NewRequest(ctx, "PUT", path, &z) if err != nil { return nil, err } @@ -96,10 +97,10 @@ func (s *ZonesService) Create(z *dns.Zone) (*http.Response, error) { // Update takes a *Zone and modifies basic details of a DNS zone. // // NS1 API docs: https://ns1.com/api/#zones-post -func (s *ZonesService) Update(z *dns.Zone) (*http.Response, error) { +func (s *ZonesService) Update(ctx context.Context, z *dns.Zone) (*http.Response, error) { path := fmt.Sprintf("zones/%s", z.Zone) - req, err := s.client.NewRequest("POST", path, &z) + req, err := s.client.NewRequest(ctx, "POST", path, &z) if err != nil { return nil, err } @@ -122,10 +123,10 @@ func (s *ZonesService) Update(z *dns.Zone) (*http.Response, error) { // Delete takes a zone and destroys an existing DNS zone and all records in the zone. // // NS1 API docs: https://ns1.com/api/#zones-delete -func (s *ZonesService) Delete(zone string) (*http.Response, error) { +func (s *ZonesService) Delete(ctx context.Context, zone string) (*http.Response, error) { path := fmt.Sprintf("zones/%s", zone) - req, err := s.client.NewRequest("DELETE", path, nil) + req, err := s.client.NewRequest(ctx, "DELETE", path, nil) if err != nil { return nil, err } @@ -146,9 +147,9 @@ func (s *ZonesService) Delete(zone string) (*http.Response, error) { // nextZones is a pagination helper than gets and appends another list of zones // to the passed list. -func (s *ZonesService) nextZones(v *interface{}, uri string) (*http.Response, error) { +func (s *ZonesService) nextZones(ctx context.Context, v *interface{}, uri string) (*http.Response, error) { tmpZl := []*dns.Zone{} - resp, err := s.client.getURI(&tmpZl, uri) + resp, err := s.client.getURI(ctx, &tmpZl, uri) if err != nil { return resp, err } @@ -164,9 +165,9 @@ func (s *ZonesService) nextZones(v *interface{}, uri string) (*http.Response, er // nextRecords is a pagination helper tha gets and appends another set of // records to the passed zone. -func (s *ZonesService) nextRecords(v *interface{}, uri string) (*http.Response, error) { +func (s *ZonesService) nextRecords(ctx context.Context, v *interface{}, uri string) (*http.Response, error) { var tmpZone dns.Zone - resp, err := s.client.getURI(&tmpZone, uri) + resp, err := s.client.getURI(ctx, &tmpZone, uri) if err != nil { return resp, err } diff --git a/rest/zone_test.go b/rest/zone_test.go index 6b765f4..4a48e95 100644 --- a/rest/zone_test.go +++ b/rest/zone_test.go @@ -1,6 +1,7 @@ package rest_test import ( + "context" "errors" "net/http" "testing" @@ -31,7 +32,7 @@ func TestZone(t *testing.T) { } require.Nil(t, mock.AddZoneListTestCase(nil, nil, zones)) - respZones, _, err := client.Zones.List() + respZones, _, err := client.Zones.List(context.Background()) require.Nil(t, err) require.NotNil(t, respZones) require.Equal(t, len(zones), len(respZones)) @@ -55,7 +56,7 @@ func TestZone(t *testing.T) { require.Nil(t, mock.AddZoneListTestCase(nil, header, zones)) - respZones, resp, err := client.Zones.List() + respZones, resp, err := client.Zones.List(context.Background()) require.Nil(t, err) require.NotNil(t, respZones) require.Equal(t, len(zones), len(respZones)) @@ -75,7 +76,7 @@ func TestZone(t *testing.T) { nil, nil, "", `{"message": "test error"}`, )) - zones, resp, err := client.Zones.List() + zones, resp, err := client.Zones.List(context.Background()) require.Nil(t, zones) require.NotNil(t, err) require.Contains(t, err.Error(), "test error") @@ -84,7 +85,7 @@ func TestZone(t *testing.T) { t.Run("Other", func(t *testing.T) { c := api.NewClient(errorClient{}, api.SetEndpoint("")) - zones, resp, err := c.Zones.List() + zones, resp, err := c.Zones.List(context.Background()) require.Nil(t, resp) require.Error(t, err) require.Nil(t, zones) @@ -110,7 +111,7 @@ func TestZone(t *testing.T) { } require.Nil(t, mock.AddZoneGetTestCase(zoneName, nil, nil, zone)) - respZone, _, err := client.Zones.Get(zoneName) + respZone, _, err := client.Zones.Get(context.Background(), zoneName) require.Nil(t, err) require.NotNil(t, respZone) require.Equal(t, len(zone.Records), len(respZone.Records)) @@ -138,7 +139,7 @@ func TestZone(t *testing.T) { require.Nil(t, mock.AddZoneGetTestCase(zoneName, nil, header, zone)) - respZone, resp, err := client.Zones.Get(zoneName) + respZone, resp, err := client.Zones.Get(context.Background(), zoneName) require.Nil(t, err) require.NotNil(t, respZone) require.Equal(t, len(zone.Records), len(respZone.Records)) @@ -158,7 +159,7 @@ func TestZone(t *testing.T) { nil, nil, "", `{"message": "test error"}`, )) - zone, resp, err := client.Zones.Get(zoneName) + zone, resp, err := client.Zones.Get(context.Background(), zoneName) require.Nil(t, zone) require.NotNil(t, err) require.Contains(t, err.Error(), "test error") @@ -167,7 +168,7 @@ func TestZone(t *testing.T) { t.Run("Other", func(t *testing.T) { c := api.NewClient(errorClient{}, api.SetEndpoint("")) - zones, resp, err := c.Zones.Get(zoneName) + zones, resp, err := c.Zones.Get(context.Background(), zoneName) require.Nil(t, resp) require.Error(t, err) require.Nil(t, zones) @@ -186,7 +187,7 @@ func TestZone(t *testing.T) { require.Nil(t, mock.AddZoneCreateTestCase(nil, nil, zone, zone)) - _, err := client.Zones.Create(zone) + _, err := client.Zones.Create(context.Background(), zone) require.Nil(t, err) }) @@ -198,7 +199,7 @@ func TestZone(t *testing.T) { nil, nil, zone, `{"message": "zone already exists"}`, )) - _, err := client.Zones.Create(zone) + _, err := client.Zones.Create(context.Background(), zone) require.Equal(t, api.ErrZoneExists, err) }) @@ -210,7 +211,7 @@ func TestZone(t *testing.T) { nil, nil, zone, `{"message": "invalid: FQDN already exists"}`, )) - _, err := client.Zones.Create(zone) + _, err := client.Zones.Create(context.Background(), zone) require.Equal(t, api.ErrZoneExists, err) }) @@ -222,7 +223,7 @@ func TestZone(t *testing.T) { nil, nil, zone, `{"message": "invalid: FQDN already exists in the view"}`, )) - _, err := client.Zones.Create(zone) + _, err := client.Zones.Create(context.Background(), zone) require.Equal(t, api.ErrZoneExists, err) }) }) @@ -238,7 +239,7 @@ func TestZone(t *testing.T) { require.Nil(t, mock.AddZoneUpdateTestCase(nil, nil, zone, zone)) - _, err := client.Zones.Update(zone) + _, err := client.Zones.Update(context.Background(), zone) require.Nil(t, err) }) @@ -250,7 +251,7 @@ func TestZone(t *testing.T) { nil, nil, zone, `{"message": "zone not found"}`, )) - _, err := client.Zones.Update(zone) + _, err := client.Zones.Update(context.Background(), zone) require.Equal(t, api.ErrZoneMissing, err) }) }) @@ -261,7 +262,7 @@ func TestZone(t *testing.T) { require.Nil(t, mock.AddZoneDeleteTestCase("delete.zone", nil, nil)) - _, err := client.Zones.Delete("delete.zone") + _, err := client.Zones.Delete(context.Background(), "delete.zone") require.Nil(t, err) }) @@ -273,7 +274,7 @@ func TestZone(t *testing.T) { nil, nil, "", `{"message": "zone not found"}`, )) - _, err := client.Zones.Delete("delete.zone") + _, err := client.Zones.Delete(context.Background(), "delete.zone") require.Equal(t, api.ErrZoneMissing, err) }) })