-
Notifications
You must be signed in to change notification settings - Fork 2
/
serverhost.go
85 lines (71 loc) · 2.28 KB
/
serverhost.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
package pritunl
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// ServerHostRequest represents a request to attach or detach a host to a server
type ServerHostRequest struct {
ID string `json:"id"`
Server string `json:"server"`
Name string `json:"name"`
Address string `json:"address"`
}
// ServerHostResponse represents a server host response
type ServerHostResponse struct {
ID string `json:"id"`
Server string `json:"server"`
Status string `json:"status"`
Name string `json:"name"`
Address string `json:"address"`
}
// ServerHostAttach attaches a host to a server
func (c *Client) ServerHostAttach(ctx context.Context, srvId string, hostId string, newServerHost ServerHostRequest) ([]ServerHostResponse, error) {
// Marshal the ServerHostRequest into JSON data
serverHostData, err := json.Marshal(newServerHost)
if err != nil {
return nil, fmt.Errorf("failed to marshal serverhost data: %w", err)
}
// Construct the API path for attaching a host
path := fmt.Sprintf("/server/%s/host/%s", srvId, hostId)
// Send an authenticated PUT request to attach the host
response, err := c.AuthRequest(ctx, http.MethodPut, path, serverHostData)
if err != nil {
return nil, err
}
// Handle the response and unmarshal the JSON data
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
var serverhosts []ServerHostResponse
if err := handleUnmarshal(body, &serverhosts); err != nil {
return nil, err
}
// Return the slice of serverhosts
return serverhosts, nil
}
// ServerHostDetach detaches a host from a server
func (c *Client) ServerHostDetach(ctx context.Context, srvId string, hostId string) ([]ServerHostResponse, error) {
// Construct the API path for detaching a host
path := fmt.Sprintf("/server/%s/host/%s", srvId, hostId)
// Send an authenticated DELETE request to detach the host
response, err := c.AuthRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
// Handle the response and unmarshal the JSON data
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
var serverhosts []ServerHostResponse
if err := handleUnmarshal(body, &serverhosts); err != nil {
return nil, err
}
// Return the slice of serverhosts
return serverhosts, nil
}