forked from bizflycloud/gobizfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalert_secret.go
121 lines (103 loc) · 2.79 KB
/
alert_secret.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
package gobizfly
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strings"
)
// SecretsCreateRequest contains receiver information.
type SecretsCreateRequest struct {
Name string `json:"name,omitempty"`
}
// SecretCreateRequest represents create new secret request payload.
type SecretCreateRequest struct {
Name string `json:"name,omitempty"`
}
// Secrets contains secrets information.
type Secrets struct {
Created string `json:"_created,omitempty"`
ID string `json:"_id"`
Name string `json:"name"`
ProjectID string `json:"project_id,omitempty"`
Secret string `json:"secret,omitempty"`
UserID string `json:"user_id,omitempty"`
}
func (s *secrets) resourcePath() string {
return strings.Join([]string{secretsResourcePath}, "/")
}
func (s *secrets) itemPath(id string) string {
return strings.Join([]string{secretsResourcePath, id}, "/")
}
// List secrets
func (s *secrets) List(ctx context.Context, filters *string) ([]*Secrets, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, cloudwatcherServiceName, s.resourcePath(), nil)
if err != nil {
return nil, err
}
if filters != nil {
q := req.URL.Query()
q.Add("where", *filters)
req.URL.RawQuery = q.Encode()
}
resp, err := s.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data struct {
Secrets []*Secrets `json:"_items"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data.Secrets, nil
}
// Create a secret
func (s *secrets) Create(ctx context.Context, scr *SecretsCreateRequest) (*ResponseRequest, error) {
req, err := s.client.NewRequest(ctx, http.MethodPost, cloudwatcherServiceName, s.resourcePath(), &scr)
if err != nil {
return nil, err
}
resp, err := s.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var respData = &ResponseRequest{}
if err := json.NewDecoder(resp.Body).Decode(respData); err != nil {
return nil, err
}
return respData, nil
}
// Get a secret
func (s *secrets) Get(ctx context.Context, id string) (*Secrets, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, cloudwatcherServiceName, s.itemPath(id), nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
secret := &Secrets{}
if err := json.NewDecoder(resp.Body).Decode(secret); err != nil {
return nil, err
}
return secret, nil
}
// Delete secret
func (s *secrets) Delete(ctx context.Context, id string) error {
req, err := s.client.NewRequest(ctx, http.MethodDelete, cloudwatcherServiceName, s.itemPath(id), nil)
if err != nil {
return err
}
resp, err := s.client.Do(ctx, req)
if err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return resp.Body.Close()
}