-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserverorg.go
82 lines (68 loc) · 2.23 KB
/
serverorg.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 pritunl
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// ServerOrgRequest represents a request to attach or detach an organization to a server
type ServerOrgRequest struct {
ID string `json:"id"`
Server string `json:"server"`
Name string `json:"name"`
}
// ServerOrgResponse represents a server organization response
type ServerOrgResponse struct {
ID string `json:"id"`
Server string `json:"server"`
Name string `json:"name"`
}
// ServerOrgAttach attaches an organization to a server
func (c *Client) ServerOrgAttach(ctx context.Context, srvId string, orgId string, newServerOrg ServerOrgRequest) ([]ServerOrgResponse, error) {
// Marshal the ServerOrgRequest into JSON data
serverOrgData, err := json.Marshal(newServerOrg)
if err != nil {
return nil, fmt.Errorf("failed to marshal serverorg data: %w", err)
}
// Construct the API path for attaching an organization
path := fmt.Sprintf("/server/%s/organization/%s", srvId, orgId)
// Send an authenticated PUT request to attach the organization
response, err := c.AuthRequest(ctx, http.MethodPut, path, serverOrgData)
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 serverorgs []ServerOrgResponse
if err := handleUnmarshal(body, &serverorgs); err != nil {
return nil, err
}
// Return the slice of serverorgs
return serverorgs, nil
}
// ServerOrgDetach detaches an organization from a server
func (c *Client) ServerOrgDetach(ctx context.Context, srvId string, orgId string) ([]ServerOrgResponse, error) {
// Construct the API path for detaching an organization
path := fmt.Sprintf("/server/%s/organization/%s", srvId, orgId)
// Send an authenticated DELETE request to detach the organization
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 serverorgs []ServerOrgResponse
if err := handleUnmarshal(body, &serverorgs); err != nil {
return nil, err
}
// Return the slice of serverorgs
return serverorgs, nil
}