-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.go
165 lines (134 loc) · 4.06 KB
/
nodes.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package headscale
import (
"context"
"net/http"
"time"
)
type Node struct {
ID string `json:"id"`
MachineKey string `json:"machineKey"`
NodeKey string `json:"nodeKey"`
DiscoKey string `json:"discoKey"`
IPAddresses []string `json:"ipAddresses"`
Name string `json:"name"`
User User `json:"user"`
LastSeen time.Time `json:"lastSeen"`
Expiry time.Time `json:"expiry"`
PreAuthKey PreAuthKey `json:"preAuthKey"`
CreatedAt time.Time `json:"createdAt"`
RegisterMethod string `json:"registerMethod"`
ForcedTags []string `json:"forcedTags"`
InvalidTags []string `json:"invalidTags"`
ValidTags []string `json:"validTags"`
GivenName string `json:"givenName"`
Online bool `json:"online"`
}
type NodeResponse struct {
Node Node `json:"node"`
}
type NodesResponse struct {
Nodes []Node `json:"nodes"`
}
type NodeResource struct {
Client HeadscaleClientInterface
}
func (n *NodeResource) List(ctx context.Context) (NodesResponse, error) {
var nodes NodesResponse
url := n.Client.buildURL("node")
req, err := n.Client.buildRequest(ctx, http.MethodGet, url, requestOptions{})
if err != nil {
return nodes, err
}
err = n.Client.do(ctx, req, &nodes)
return nodes, err
}
func (n *NodeResource) Get(ctx context.Context, id string) (NodeResponse, error) {
var node NodeResponse
url := n.Client.buildURL("node", id)
req, err := n.Client.buildRequest(ctx, http.MethodGet, url, requestOptions{})
if err != nil {
return node, err
}
err = n.Client.do(ctx, req, &node)
return node, err
}
func (n *NodeResource) Register(ctx context.Context, user, key string) (NodeResponse, error) {
var node NodeResponse
url := n.Client.buildURL("node")
req, err := n.Client.buildRequest(ctx, http.MethodPost, url, requestOptions{
queryParams: map[string]interface{}{
"user": user,
"key": key,
},
})
if err != nil {
return node, err
}
err = n.Client.do(ctx, req, &node)
return node, err
}
func (n *NodeResource) Delete(ctx context.Context, id string) error {
url := n.Client.buildURL("node", id)
req, err := n.Client.buildRequest(ctx, http.MethodDelete, url, requestOptions{})
if err != nil {
return err
}
return n.Client.do(ctx, req, nil)
}
func (n *NodeResource) Expire(ctx context.Context, id string) error {
url := n.Client.buildURL("node", id, "expire")
req, err := n.Client.buildRequest(ctx, http.MethodPost, url, requestOptions{})
if err != nil {
return err
}
return n.Client.do(ctx, req, nil)
}
func (n *NodeResource) Rename(ctx context.Context, id, name string) (NodeResponse, error) {
var node NodeResponse
url := n.Client.buildURL("node", id, "rename", name)
req, err := n.Client.buildRequest(ctx, http.MethodPost, url, requestOptions{})
if err != nil {
return node, err
}
err = n.Client.do(ctx, req, &node)
return node, err
}
func (n *NodeResource) GetRoutes(ctx context.Context, id string) (RoutesResponse, error) {
var routes RoutesResponse
url := n.Client.buildURL("node", id, "routes")
req, err := n.Client.buildRequest(ctx, http.MethodGet, url, requestOptions{})
if err != nil {
return routes, err
}
err = n.Client.do(ctx, req, &routes)
return routes, err
}
type AddTagsRequest struct {
Tags []string `json:"tags"`
}
func (n *NodeResource) AddTags(ctx context.Context, id string, tags []string) (NodeResponse, error) {
var node NodeResponse
url := n.Client.buildURL("node", id, "tags")
req, err := n.Client.buildRequest(ctx, http.MethodPost, url, requestOptions{
body: AddTagsRequest{Tags: tags},
})
if err != nil {
return node, err
}
err = n.Client.do(ctx, req, &node)
return node, err
}
func (n *NodeResource) UpdateUser(ctx context.Context, id, user string) (NodeResponse, error) {
var node NodeResponse
url := n.Client.buildURL("node", id, "user")
req, err := n.Client.buildRequest(ctx, http.MethodPost, url, requestOptions{
queryParams: map[string]interface{}{
"user": user,
},
})
if err != nil {
return node, err
}
err = n.Client.do(ctx, req, &node)
return node, err
}