Skip to content

Commit

Permalink
feat: add support for listing account activity
Browse files Browse the repository at this point in the history
This adds a new service to enable listing NS1 account activity. The
endpoint supports many url parameters to adjust response behavior (such
as changing/limiting how many activity entries are returned, etc), but
for this initial approach, the sdk just uses the endpoint directly with
the endpoint's normal default values.

I used a small Terraform config to generate 50 dummy DNS records on my
NS1 account to generate some account activity and have confirmed that
the endpoint operates as expected -- the values I expected are returned,
the response is limited to the last 20 activity entries like the
endpoint default, etc.
  • Loading branch information
tjhop committed Apr 4, 2024
1 parent cb9e29b commit de40e55
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
19 changes: 19 additions & 0 deletions mockns1/account_activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mockns1

import (
"net/http"

"gopkg.in/ns1/ns1-go.v2/rest/model/account"
)

// AddActivityListTestCase sets up a test case for the api.Client.Activity.List()
// function
func (s *Service) AddActivityListTestCase(
requestHeaders, responseHeaders http.Header,
response []*account.Activity,
) error {
return s.AddTestCase(
http.MethodGet, "/account/activity", http.StatusOK, requestHeaders,
responseHeaders, "", response,
)
}
10 changes: 10 additions & 0 deletions rest/_examples/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@ func main() {
if _, err := client.APIKeys.Create(&key); err != nil {
log.Fatal(err)
}

activity, _, err := client.Activity.List()
if err != nil {
log.Fatal(err)
}

for _, a := range activity {
b, _ := json.MarshalIndent(a, "", " ")
fmt.Println(string(b))
}
}
29 changes: 29 additions & 0 deletions rest/account_activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package rest

import (
"net/http"

"gopkg.in/ns1/ns1-go.v2/rest/model/account"
)

// ActivityService handles 'account/activity' endpoint.
type ActivityService service

// List returns all activity in the account.
//
// NS1 API docs: https://developer.ibm.com/apis/catalog/ns1--ibm-ns1-connect-api/api/API--ns1--ibm-ns1-connect-api#getActivity
func (s *ActivityService) List() ([]*account.Activity, *http.Response, error) {
// TODO: add support for url parameters to adjust endpoint behavior?
req, err := s.client.NewRequest("GET", "account/activity", nil)
if err != nil {
return nil, nil, err
}

al := []*account.Activity{}
resp, err := s.client.Do(req, &al)
if err != nil {
return nil, resp, err
}

return al, resp, nil
}
2 changes: 2 additions & 0 deletions rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Client struct {
Network *NetworkService
GlobalIPWhitelist *GlobalIPWhitelistService
Datasets *DatasetsService
Activity *ActivityService
}

// NewClient constructs and returns a reference to an instantiated Client.
Expand Down Expand Up @@ -139,6 +140,7 @@ func NewClient(httpClient Doer, options ...func(*Client)) *Client {
c.Network = (*NetworkService)(&c.common)
c.GlobalIPWhitelist = (*GlobalIPWhitelistService)(&c.common)
c.Datasets = (*DatasetsService)(&c.common)
c.Activity = (*ActivityService)(&c.common)

for _, option := range options {
option(c)
Expand Down
13 changes: 13 additions & 0 deletions rest/model/account/activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package account

// Activity wraps an NS1 /account/activity resource
type Activity struct {
UserID string `json:"user_id,omitempty"`
ResourceID string `json:"resource_id,omitempty"`
Timestamp int `json:"timestamp,omitempty"`
UserType string `json:"user_type,omitempty"`
Action string `json:"action,omitempty"`
UserName string `json:"user_name,omitempty"`
ID string `json:"id,omitempty"`
ResourceType string `json:"resource_type,omitempty"`
}

0 comments on commit de40e55

Please sign in to comment.