-
Notifications
You must be signed in to change notification settings - Fork 0
/
defined.go
82 lines (68 loc) · 1.64 KB
/
defined.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
type Host struct {
ID string `json:"id"`
IPAddress string `json:"ipAddress"`
Hostname string `json:"name"`
Tags []string `json:"tags"`
}
type hostsResponse struct {
Data []Host `json:"data"`
Metadata struct {
HasNextPage bool `json:"hasNextPage"`
Cursor string `json:"cursor"`
} `json:"metadata"`
}
func FilterHosts(dnToken string, filterFunc func(Host) bool) ([]Host, error) {
hosts := []Host{}
cursor := ""
for {
// Fetch the next page of hosts
params := url.Values{
"cursor": []string{cursor},
"pageSize": []string{"500"},
}
req, err := http.NewRequest("GET", "https://api.defined.net/v1/hosts?"+params.Encode(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Bearer "+dnToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, body)
}
// Decode the response body into a slice of Hosts
var respHosts hostsResponse
err = json.Unmarshal(body, &respHosts)
if err != nil {
return nil, err
}
// Filter the hosts
for _, host := range respHosts.Data {
if filterFunc(host) {
hosts = append(hosts, host)
}
}
// Fetch the next page if there is one
if !respHosts.Metadata.HasNextPage {
break
}
cursor = respHosts.Metadata.Cursor
}
return hosts, nil
}