-
Notifications
You must be signed in to change notification settings - Fork 2
/
system_certificate_local_group.go
145 lines (115 loc) · 3.58 KB
/
system_certificate_local_group.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
package gofortiadc
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
)
// SystemLocalCertificateGroup represents a local certificate request/response
type SystemLocalCertificateGroup struct {
Mkey string `json:"mkey"`
}
// SystemGetLocalCertificateGroups returns the list of all local certificate groups
func (c *Client) SystemGetLocalCertificateGroups() ([]SystemLocalCertificateGroup, error) {
req, err := c.NewRequest("GET", fmt.Sprintf("%s/api/system_certificate_local_cert_group", c.Address), nil)
if err != nil {
return []SystemLocalCertificateGroup{}, err
}
res, err := c.Client.Do(req)
if err != nil {
return []SystemLocalCertificateGroup{}, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return []SystemLocalCertificateGroup{}, fmt.Errorf("failed to get local certificate groups list with status code: %d", res.StatusCode)
}
body, err := io.ReadAll(res.Body)
if err != nil {
return []SystemLocalCertificateGroup{}, err
}
var SystemLocalCertificateGroupsPayload struct {
Payload []SystemLocalCertificateGroup
}
err = json.Unmarshal(body, &SystemLocalCertificateGroupsPayload)
if err != nil {
return []SystemLocalCertificateGroup{}, err
}
return SystemLocalCertificateGroupsPayload.Payload, nil
}
// SystemGetLocalCertificateGroup returns a local certificate group by name
func (c *Client) SystemGetLocalCertificateGroup(name string) (SystemLocalCertificateGroup, error) {
groups, err := c.SystemGetLocalCertificateGroups()
if err != nil {
return SystemLocalCertificateGroup{}, err
}
for _, group := range groups {
if group.Mkey == name {
return group, nil
}
}
return SystemLocalCertificateGroup{}, fmt.Errorf("local certificate group %s not found: %w", name, ErrNotFound)
}
// SystemCreateLocalCertificateGroup creates a new local certificate group
func (c *Client) SystemCreateLocalCertificateGroup(lcg SystemLocalCertificateGroup) error {
payloadJSON, err := json.Marshal(lcg)
if err != nil {
return err
}
req, err := c.NewRequest("POST", fmt.Sprintf("%s/api/system_certificate_local_cert_group", c.Address), 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 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 creation failed: %s ", getErrorMessage(resJSON.Payload))
}
return nil
}
// SystemDeleteLocalCertificateGroup deletes an existing local certificate group
func (c *Client) SystemDeleteLocalCertificateGroup(name string) error {
if len(name) == 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?mkey=%s", c.Address, name), 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 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 deletion failed: %s", getErrorMessage(resJSON.Payload))
}
return nil
}