Skip to content

Commit bc4b475

Browse files
committed
api: Add sites
1 parent 3866972 commit bc4b475

File tree

4 files changed

+129
-6
lines changed

4 files changed

+129
-6
lines changed

api/response.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,15 @@ func (c *Client) GetJSONItems(request *http.Request) (items []json.RawMessage, e
123123

124124
err = json.Unmarshal(response.Data, &dataItems)
125125
if err != nil {
126-
return
127-
}
128-
129-
// append items to overall list
130-
for _, item := range dataItems {
131-
items = append(items, item)
126+
// Fall back to adding response.Data to the item list
127+
// This is useful when data is not an array, but an object
128+
err = nil
129+
items = append(items, response.Data)
130+
} else {
131+
// append each item to overall list
132+
for _, item := range dataItems {
133+
items = append(items, item)
134+
}
132135
}
133136

134137
// set nextCursor or break iteration when done

api/sites.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/url"
6+
)
7+
8+
type Site struct {
9+
ID string `json:"id"`
10+
Name string `json:"name"`
11+
SiteType string `json:"siteType"`
12+
}
13+
14+
type SiteResult struct {
15+
// Only license info
16+
AllSites json.RawMessage `json:"allSites"`
17+
Sites []*Site
18+
}
19+
20+
func (c *Client) GetSites(values url.Values) (data []*Site, err error) {
21+
req, err := c.NewRequest("GET", "v2.1/sites?"+values.Encode(), nil)
22+
if err != nil {
23+
return
24+
}
25+
26+
res, err := c.GetJSONItems(req)
27+
if err != nil {
28+
return
29+
}
30+
31+
for _, page := range res {
32+
p := &SiteResult{}
33+
34+
err = json.Unmarshal(page, p)
35+
if err != nil {
36+
return
37+
}
38+
39+
for _, site := range p.Sites {
40+
data = append(data, site)
41+
}
42+
}
43+
44+
return
45+
}

api/sites_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package api_test
2+
3+
import (
4+
"github.com/jarcoal/httpmock"
5+
"github.com/stretchr/testify/assert"
6+
"io/ioutil"
7+
"net/http"
8+
"net/url"
9+
"testing"
10+
)
11+
12+
func TestClient_GetSites(t *testing.T) {
13+
c, cleanup := testClient()
14+
defer cleanup()
15+
16+
httpmock.RegisterResponder("GET", "https://euce1-test.sentinelone.net/web/api/v2.1/sites",
17+
func(req *http.Request) (*http.Response, error) {
18+
data, err := ioutil.ReadFile("testdata/sites.json")
19+
assert.NoError(t, err)
20+
21+
return httpmock.NewBytesResponse(200, data), nil
22+
})
23+
24+
sites, err := c.GetSites(url.Values{})
25+
assert.NoError(t, err)
26+
assert.Len(t, sites, 1)
27+
}
28+
29+
func TestClient_GetSites_Integration(t *testing.T) {
30+
c := envClient(t)
31+
32+
sites, err := c.GetSites(url.Values{})
33+
assert.NoError(t, err)
34+
assert.NotNil(t, sites)
35+
}

api/testdata/sites.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"data": {
3+
"allSites": {
4+
"totalLicenses": "integer",
5+
"activeLicenses": "integer"
6+
},
7+
"sites": [
8+
{
9+
"isDefault": "boolean",
10+
"name": "My Site",
11+
"totalLicenses": "integer",
12+
"suite": "Core",
13+
"registrationToken": "eyJ1cmwiOiAiaHR0cHM6Ly9jb25zb2xlLnNlbnRpbmVsb25lLm5ldCIsICJzaXRlX2tleSI6ICIwNzhkYjliMWUyOTA1Y2NhIn0=",
14+
"state": "active",
15+
"unlimitedExpiration": "boolean",
16+
"activeLicenses": 31,
17+
"id": "182483948009279260",
18+
"externalId": "string",
19+
"siteType": "string",
20+
"updatedAt": "2018-02-27T04:49:26.257525Z",
21+
"creatorId": "225494730938493804",
22+
"expiration": "2018-02-27T04:49:26.257525Z",
23+
"healthStatus": "boolean",
24+
"accountId": "225494730938493804",
25+
"accountName": "SentinelOne",
26+
"creator": "string",
27+
"unlimitedLicenses": "boolean",
28+
"createdAt": "2018-02-27T04:49:26.257525Z",
29+
"sku": "Core"
30+
}
31+
]
32+
},
33+
"pagination": {
34+
"nextCursor": "",
35+
"totalItems": 580
36+
},
37+
"errors": [
38+
{}
39+
]
40+
}

0 commit comments

Comments
 (0)