-
Notifications
You must be signed in to change notification settings - Fork 9
/
config-store-record.go
129 lines (103 loc) · 3.13 KB
/
config-store-record.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
package cloud66
import (
"net/url"
"strconv"
"strings"
)
const (
BundledConfigStoreAccountScope = "account"
BundledConfigStoreStackScope = "stack"
)
type BundledConfigStoreRecords struct {
Records []BundledConfigStoreRecord `json:"records" yaml:"records"`
}
type BundledConfigStoreRecord struct {
ConfigStoreRecord `yaml:",inline"`
Scope string `json:"scope" yaml:"scope"`
}
type ConfigStoreRecord struct {
Key string `json:"key" yaml:"key"`
RawValue string `json:"raw_value" yaml:"raw_value"`
Metadata map[string]string `json:"metadata" yaml:"metadata"`
Ttl int `json:"ttl" yaml:"ttl"`
}
type configStoreRequestWrapper struct {
Record *ConfigStoreRecord `json:"record" yaml:"record"`
}
func (c *Client) GetConfigStoreRecords(namespace string) ([]ConfigStoreRecord, error) {
var p Pagination
var result []ConfigStoreRecord
queryStrings := make(map[string]string)
queryStrings["page"] = "1"
for {
req, err := c.NewRequest("GET", "/configstore/namespaces/"+namespace+"/records.json", nil, queryStrings)
if err != nil {
return nil, err
}
var intermediateResult []ConfigStoreRecord
err = c.DoReq(req, &intermediateResult, &p)
if err != nil {
return nil, err
}
result = append(result, intermediateResult...)
if p.Current < p.Next {
queryStrings["page"] = strconv.Itoa(p.Next)
} else {
break
}
}
return result, nil
}
func (c *Client) GetConfigStoreRecord(namespace, key string) (*ConfigStoreRecord, error) {
req, err := c.NewRequest("GET", "/configstore/namespaces/"+namespace+"/records/"+urlEncodedKey(key)+".json", nil, nil)
if err != nil {
return nil, err
}
var result *ConfigStoreRecord
err = c.DoReq(req, &result, nil)
if err != nil {
return nil, err
}
return result, nil
}
func (c *Client) CreateConfigStoreRecord(namespace string, record *ConfigStoreRecord) (*ConfigStoreRecord, error) {
req, err := c.NewRequest("POST", "/configstore/namespaces/"+namespace+"/records.json", &configStoreRequestWrapper{Record: record}, nil)
if err != nil {
return nil, err
}
var result *ConfigStoreRecord
err = c.DoReq(req, &result, nil)
if err != nil {
return nil, err
}
return result, nil
}
func (c *Client) UpdateConfigStoreRecord(namespace, key string, record *ConfigStoreRecord) (*ConfigStoreRecord, error) {
req, err := c.NewRequest("PUT", "/configstore/namespaces/"+namespace+"/records/"+urlEncodedKey(key)+".json", &configStoreRequestWrapper{Record: record}, nil)
if err != nil {
return nil, err
}
var result *ConfigStoreRecord
err = c.DoReq(req, &result, nil)
if err != nil {
return nil, err
}
return result, nil
}
func (c *Client) DeleteConfigStoreRecord(namespace, key string) (*ConfigStoreRecord, error) {
req, err := c.NewRequest("DELETE", "/configstore/namespaces/"+namespace+"/records/"+urlEncodedKey(key)+".json", nil, nil)
if err != nil {
return nil, err
}
var result *ConfigStoreRecord
err = c.DoReq(req, &result, nil)
if err != nil {
return nil, err
}
return result, nil
}
func urlEncodedKey(key string) string {
result := url.QueryEscape(key)
result = strings.ReplaceAll(result, ".", "%2E")
return result
}