Skip to content

Commit b1f0b3a

Browse files
authored
Merge pull request #233 from tjhop/feat/account-activity-support
feat: add support for listing account activity
2 parents cb9e29b + 5e39f52 commit b1f0b3a

File tree

6 files changed

+168
-0
lines changed

6 files changed

+168
-0
lines changed

mockns1/account_activity.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package mockns1
2+
3+
import (
4+
"net/http"
5+
6+
api "gopkg.in/ns1/ns1-go.v2/rest"
7+
"gopkg.in/ns1/ns1-go.v2/rest/model/account"
8+
)
9+
10+
// AddActivityListTestCase sets up a test case for the api.Client.Activity.List()
11+
// function
12+
func (s *Service) AddActivityListTestCase(
13+
requestHeaders, responseHeaders http.Header,
14+
response []*account.Activity,
15+
params ...api.Param,
16+
) error {
17+
return s.AddTestCase(
18+
http.MethodGet, "/account/activity", http.StatusOK, requestHeaders,
19+
responseHeaders, "", response, params...,
20+
)
21+
}

rest/_examples/account.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,14 @@ func main() {
6767
if _, err := client.APIKeys.Create(&key); err != nil {
6868
log.Fatal(err)
6969
}
70+
71+
activity, _, err := client.Activity.List()
72+
if err != nil {
73+
log.Fatal(err)
74+
}
75+
76+
for _, a := range activity {
77+
b, _ := json.MarshalIndent(a, "", " ")
78+
fmt.Println(string(b))
79+
}
7080
}

rest/account_activity.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package rest
2+
3+
import (
4+
"net/http"
5+
6+
"gopkg.in/ns1/ns1-go.v2/rest/model/account"
7+
)
8+
9+
// ActivityService handles 'account/activity' endpoint.
10+
type ActivityService service
11+
12+
// List returns all activity in the account. It accepts a variadic number of
13+
// optional URL parameters that can be used to edit the endpoint's behavior.
14+
// Parameters are in the form of a `rest.Param` struct. The full list of valid
15+
// parameters for this endpoint are available in the documentation.
16+
//
17+
// NS1 API docs: https://developer.ibm.com/apis/catalog/ns1--ibm-ns1-connect-api/api/API--ns1--ibm-ns1-connect-api#getActivity
18+
func (s *ActivityService) List(params ...Param) ([]*account.Activity, *http.Response, error) {
19+
req, err := s.client.NewRequest("GET", "account/activity", nil)
20+
if err != nil {
21+
return nil, nil, err
22+
}
23+
24+
al := []*account.Activity{}
25+
resp, err := s.client.Do(req, &al, params...)
26+
if err != nil {
27+
return nil, resp, err
28+
}
29+
30+
return al, resp, nil
31+
}

rest/account_activity_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package rest_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
"gopkg.in/ns1/ns1-go.v2/mockns1"
9+
api "gopkg.in/ns1/ns1-go.v2/rest"
10+
"gopkg.in/ns1/ns1-go.v2/rest/model/account"
11+
)
12+
13+
func TestActivity(t *testing.T) {
14+
mock, doer, err := mockns1.New(t)
15+
require.Nil(t, err)
16+
defer mock.Shutdown()
17+
client := api.NewClient(doer, api.SetEndpoint("https://"+mock.Address+"/v1/"))
18+
19+
t.Run("List", func(t *testing.T) {
20+
activity := []*account.Activity{
21+
{
22+
UserID: "user-1",
23+
ResourceID: "resource-id-1",
24+
Timestamp: 567,
25+
UserType: "apikey",
26+
Action: "create",
27+
UserName: "username-1",
28+
ID: "id-1",
29+
ResourceType: "record",
30+
},
31+
{
32+
UserID: "user-2",
33+
ResourceID: "resource-id-2",
34+
Timestamp: 567,
35+
UserType: "apikey",
36+
Action: "delete",
37+
UserName: "username-2",
38+
ID: "id-2",
39+
ResourceType: "record",
40+
},
41+
}
42+
43+
t.Run("list default activity", func(t *testing.T) {
44+
defer mock.ClearTestCases()
45+
46+
require.Nil(t, mock.AddActivityListTestCase(nil, nil, activity))
47+
48+
respActivity, _, err := client.Activity.List()
49+
require.Nil(t, err)
50+
require.NotNil(t, respActivity)
51+
require.Equal(t, len(activity), len(respActivity))
52+
53+
for i := range activity {
54+
require.Equal(t, activity[i], respActivity[i], i)
55+
}
56+
})
57+
58+
t.Run("list most recent 1 activity", func(t *testing.T) {
59+
defer mock.ClearTestCases()
60+
61+
limit := 1
62+
params := []api.Param{{Key: "limit", Value: fmt.Sprintf("%d", limit)}}
63+
64+
require.Nil(t, mock.AddActivityListTestCase(nil, nil, []*account.Activity{activity[0]}, params...))
65+
66+
respActivity, _, err := client.Activity.List(params...)
67+
require.Nil(t, err)
68+
require.NotNil(t, respActivity)
69+
require.Equal(t, limit, len(respActivity))
70+
require.Equal(t, activity[0], respActivity[0])
71+
})
72+
73+
t.Run("list all dns record activity, multiple params", func(t *testing.T) {
74+
defer mock.ClearTestCases()
75+
76+
limit := 1000
77+
params := []api.Param{{Key: "limit", Value: fmt.Sprintf("%d", limit)}, {Key: "resource_type", Value: "record"}}
78+
79+
require.Nil(t, mock.AddActivityListTestCase(nil, nil, activity, params...))
80+
81+
respActivity, _, err := client.Activity.List(params...)
82+
require.Nil(t, err)
83+
require.NotNil(t, respActivity)
84+
require.Equal(t, len(activity), len(respActivity))
85+
86+
for i := range activity {
87+
require.Equal(t, activity[i], respActivity[i], i)
88+
}
89+
})
90+
})
91+
}

rest/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ type Client struct {
9191
Network *NetworkService
9292
GlobalIPWhitelist *GlobalIPWhitelistService
9393
Datasets *DatasetsService
94+
Activity *ActivityService
9495
}
9596

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

143145
for _, option := range options {
144146
option(c)

rest/model/account/activity.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package account
2+
3+
// Activity wraps an NS1 /account/activity resource
4+
type Activity struct {
5+
UserID string `json:"user_id,omitempty"`
6+
ResourceID string `json:"resource_id,omitempty"`
7+
Timestamp int `json:"timestamp,omitempty"`
8+
UserType string `json:"user_type,omitempty"`
9+
Action string `json:"action,omitempty"`
10+
UserName string `json:"user_name,omitempty"`
11+
ID string `json:"id,omitempty"`
12+
ResourceType string `json:"resource_type,omitempty"`
13+
}

0 commit comments

Comments
 (0)