-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathorganization.go
155 lines (129 loc) · 4.48 KB
/
organization.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
package pritunl
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// OrganizationRequest represents the structure of the organization request
type OrganizationRequest struct {
Name string `json:"name"`
AuthApi bool `json:"auth_api"`
AuthToken bool `json:"auth_token"` // Addition for Put Method
AuthSecret bool `json:"auth_secret"` // Addition for Put Method
}
// OrganizationResponse represents the structure of the organization response
type OrganizationResponse struct {
ID string `json:"id"`
Name string `json:"name"`
AuthApi bool `json:"auth_api"`
AuthToken bool `json:"auth_token"`
AuthSecret bool `json:"auth_secret"`
UserCount int `json:"user_count"`
}
// OrganizationGet retrieves a organization or organizations on the server
func (c *Client) OrganizationGet(ctx context.Context, orgId ...string) ([]OrganizationResponse, error) {
// The API path for the organization
path := "/organization"
// Handle optional orgId argument
if len(orgId) > 0 {
path = fmt.Sprintf("%s/%s", path, orgId[0]) // Use the first element if orgId is provided
}
// Send an authenticated HTTP GET request to the API
response, err := c.AuthRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
// Handle the HTTP response
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of OrganizationResponse
var organizations []OrganizationResponse
if err := handleUnmarshal(body, &organizations); err != nil {
return nil, err
}
// Return the slice of organizations
return organizations, nil
}
// OrganizationCreate creates a new organization on the server
func (c *Client) OrganizationCreate(ctx context.Context, newOrganization OrganizationRequest) ([]OrganizationResponse, error) {
// Marshal the OrganizationRequest struct into JSON data
orgData, err := json.Marshal(newOrganization)
if err != nil {
return nil, fmt.Errorf("failed to marshal organization data: %w", err)
}
// The API path for the organization
path := "/organization"
// Send an authenticated HTTP POST request to the API with the organization data
response, err := c.AuthRequest(ctx, http.MethodPost, path, orgData)
if err != nil {
return nil, err
}
// Handle the HTTP response
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of OrganizationResponse
var organizations []OrganizationResponse
if err := handleUnmarshal(body, &organizations); err != nil {
return nil, err
}
// Return the slice of organizations
return organizations, nil
}
// OrganizationUpdate updates an existing organization on the server
func (c *Client) OrganizationUpdate(ctx context.Context, orgId string, updateOrganization OrganizationRequest) ([]OrganizationResponse, error) {
// Marshal the OrganizationRequest struct into JSON data
orgData, err := json.Marshal(updateOrganization)
if err != nil {
return nil, fmt.Errorf("failed to marshal organization data: %w", err)
}
// Construct the API path for the organization
path := fmt.Sprintf("/organization/%s", orgId)
// Send an authenticated HTTP PUT request to the API with the organization data
response, err := c.AuthRequest(ctx, http.MethodPut, path, orgData)
if err != nil {
return nil, err
}
// Handle the HTTP response
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of OrganizationResponse
var organizations []OrganizationResponse
if err := handleUnmarshal(body, &organizations); err != nil {
return nil, err
}
// Return the slice of organizations
return organizations, nil
}
// OrganizationDelete deletes an existing organization on the server
func (c *Client) OrganizationDelete(ctx context.Context, orgId string) ([]OrganizationResponse, error) {
// The API path for the organization
path := fmt.Sprintf("/organization/%s", orgId)
// Send an authenticated HTTP DELETE request to the API
response, err := c.AuthRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
// Handle the HTTP response
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data using the helper function
var organizations []OrganizationResponse
if err := handleUnmarshal(body, &organizations); err != nil {
return nil, err
}
// Return the slice of organizations
return organizations, nil
}