-
Notifications
You must be signed in to change notification settings - Fork 2
/
system_certificate_local_group_member.go
194 lines (154 loc) · 5.36 KB
/
system_certificate_local_group_member.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package gofortiadc
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
)
// SystemLocalCertificateGroupMember represents a local certificate request/response
type SystemLocalCertificateGroupMember struct {
OCSPStapling string `json:"OCSP_stapling"`
Default string `json:"default"`
ExtraOCSPStapling string `json:"extra_OCSP_stapling"`
ExtraIntermediateCag string `json:"extra_intermediate_cag"`
ExtraLocalCert string `json:"extra_local_cert"`
IntermediateCag string `json:"intermediate_cag"`
LocalCert string `json:"local_cert"`
Mkey string `json:"mkey"`
}
// SystemGetLocalCertificateGroupMembers returns the list of all local certificate group members
func (c *Client) SystemGetLocalCertificateGroupMembers(group string) ([]SystemLocalCertificateGroupMember, error) {
req, err := c.NewRequest("GET", fmt.Sprintf("%s/api/system_certificate_local_cert_group_child_group_member?pkey=%s", c.Address, group), nil)
if err != nil {
return []SystemLocalCertificateGroupMember{}, err
}
res, err := c.Client.Do(req)
if err != nil {
return []SystemLocalCertificateGroupMember{}, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return []SystemLocalCertificateGroupMember{}, fmt.Errorf("failed to get local certificate group members list with status code: %d", res.StatusCode)
}
body, err := io.ReadAll(res.Body)
if err != nil {
return []SystemLocalCertificateGroupMember{}, err
}
var SystemLocalCertificateGroupMembersPayload struct {
Payload []SystemLocalCertificateGroupMember
}
err = json.Unmarshal(body, &SystemLocalCertificateGroupMembersPayload)
if err != nil {
return []SystemLocalCertificateGroupMember{}, err
}
return SystemLocalCertificateGroupMembersPayload.Payload, nil
}
// SystemGetLocalCertificateGroupMember returns a local certificate group member by mkey
func (c *Client) SystemGetLocalCertificateGroupMember(group, mkey string) (SystemLocalCertificateGroupMember, error) {
members, err := c.SystemGetLocalCertificateGroupMembers(group)
if err != nil {
return SystemLocalCertificateGroupMember{}, err
}
for _, member := range members {
if member.Mkey == mkey {
return member, nil
}
}
return SystemLocalCertificateGroupMember{}, fmt.Errorf("local certificate group member %s not found: %w", mkey, ErrNotFound)
}
// SystemCreateLocalCertificateGroupMember creates a new local certificate group member
func (c *Client) SystemCreateLocalCertificateGroupMember(group string, lcgm SystemLocalCertificateGroupMember) error {
payloadJSON, err := json.Marshal(lcgm)
if err != nil {
return err
}
req, err := c.NewRequest("POST", fmt.Sprintf("%s/api/system_certificate_local_cert_group_child_group_member?pkey=%s", c.Address, group), bytes.NewReader(payloadJSON))
if err != nil {
return err
}
res, err := c.Client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("local certificate group member creation failed with status code: %d", res.StatusCode)
}
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
resJSON := struct{ Payload int }{}
err = json.Unmarshal(body, &resJSON)
if err != nil {
return err
}
if resJSON.Payload != 0 {
return fmt.Errorf("local certificate group member creation failed: %s ", getErrorMessage(resJSON.Payload))
}
return nil
}
// SystemUpdateLocalCertificateGroupMember updates an existing local certificate group member
func (c *Client) SystemUpdateLocalCertificateGroupMember(group, mkey string, lcgm SystemLocalCertificateGroupMember) error {
payloadJSON, err := json.Marshal(lcgm)
if err != nil {
return err
}
req, err := c.NewRequest("PUT", fmt.Sprintf("%s/api/system_certificate_local_cert_group_child_group_member?mkey=%s&pkey=%s", c.Address, mkey, group), bytes.NewReader(payloadJSON))
if err != nil {
return err
}
res, err := c.Client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("local certificate group member update failed with status code: %d", res.StatusCode)
}
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
resJSON := struct{ Payload int }{}
err = json.Unmarshal(body, &resJSON)
if err != nil {
return err
}
if resJSON.Payload != 0 {
return fmt.Errorf("local certificate group member update failed: %s", getErrorMessage(resJSON.Payload))
}
return nil
}
// SystemDeleteLocalCertificateGroupMember deletes an existing local certificate group member
func (c *Client) SystemDeleteLocalCertificateGroupMember(group, mkey string) error {
if len(group) == 0 {
return errors.New("local certificate group name cannot be empty")
}
req, err := c.NewRequest("DELETE", fmt.Sprintf("%s/api/system_certificate_local_cert_group_child_group_member?mkey=%s&pkey=%s", c.Address, mkey, group), nil)
if err != nil {
return err
}
res, err := c.Client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("local certificate group member deletion failed with status code: %d", res.StatusCode)
}
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
resJSON := struct{ Payload int }{}
err = json.Unmarshal(body, &resJSON)
if err != nil {
return err
}
if resJSON.Payload != 0 {
return fmt.Errorf("local certificate group member deletion failed: %s", getErrorMessage(resJSON.Payload))
}
return nil
}