-
Notifications
You must be signed in to change notification settings - Fork 1
/
peers.go
129 lines (114 loc) · 3.78 KB
/
peers.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
/*
Copyright © 2024 EdgeFarm Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package netbird
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
type PeerGroup struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
PeersCount int `json:"peers_count,omitempty"`
Issued string `json:"issued,omitempty"`
}
type Peers []Peer
type Peer struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
IP string `json:"ip,omitempty"`
Connected bool `json:"connected,omitempty"`
LastSeen string `json:"last_seen,omitempty"`
Os string `json:"os,omitempty"`
Version string `json:"version,omitempty"`
Groups []PeerGroup `json:"groups,omitempty"`
SSHEnabled bool `json:"ssh_enabled,omitempty"`
UserID string `json:"user_id,omitempty"`
Hostname string `json:"hostname,omitempty"`
UIVersion string `json:"ui_version,omitempty"`
DNSLabel string `json:"dns_label,omitempty"`
LoginExpirationEnabled bool `json:"login_expiration_enabled,omitempty"`
LoginExpired bool `json:"login_expired,omitempty"`
LastLogin string `json:"last_login,omitempty"`
ApprovalRequired bool `json:"approval_required,omitempty"`
AccessiblePeers []AccessiblePeers `json:"accessible_peers,omitempty"`
AcessiblePeersCount int `json:"accessible_peers_count,omitempty"`
}
type AccessiblePeers struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
IP string `json:"ip,omitempty"`
DNSLabel string `json:"dns_label,omitempty"`
UserID string `json:"user_id,omitempty"`
}
func (c *Client) ListPeers() (Peers, error) {
peers := Peers{}
body, err := c.doCall(http.MethodGet, "api/peers", nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &peers)
if err != nil {
return nil, err
}
return peers, nil
}
func (c *Client) GetPeer(id string) (Peer, error) {
peer := Peer{}
body, err := c.doCall(http.MethodGet, fmt.Sprintf("api/peers/%s", id), nil)
if err != nil {
return Peer{}, err
}
err = json.Unmarshal(body, &peer)
if err != nil {
return Peer{}, err
}
return peer, nil
}
func (c *Client) GetPeerIdByHostname(hostname string) (string, error) {
peers, err := c.ListPeers()
if err != nil {
return "", err
}
for _, peer := range peers {
if peer.Hostname == hostname {
return peer.ID, nil
}
}
return "", fmt.Errorf("peer not found")
}
func (c *Client) DeletePeer(id string) error {
_, err := c.doCall(http.MethodDelete, fmt.Sprintf("api/peers/%s", id), nil)
if err != nil {
return err
}
return nil
}
func (c *Client) UpdatePeer(p *Peer) (*Peer, error) {
j, err := json.Marshal(p)
if err != nil {
return nil, err
}
data := strings.NewReader(string(j))
body, err := c.doCall(http.MethodPut, fmt.Sprintf("api/peers/%s", p.ID), data)
if err != nil {
return nil, err
}
resp := Peer{}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}