forked from DinoChiesa/go-apigee-edge
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathKeyValueMap.go
107 lines (80 loc) · 2.77 KB
/
KeyValueMap.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
package apigee
import "path"
// KeyValueMapService is an interface for interfacing with the Apigee Edge Admin API
// dealing with KeyValueMap.
type KeyValueMapService interface {
Get(string, string) (*KeyValueMap, *Response, error)
Create(KeyValueMap, string) (*KeyValueMap, *Response, error)
Delete(string, string) (*Response, error)
//update is not implemented as the API is being deprectated. See KeyValueMapEntry.
// Update(KeyValueMap, string) (*KeyValueMap, *Response, error)
}
// KeyValueMapServiceOp holds creds
type KeyValueMapServiceOp struct {
client *EdgeClient
}
var _ KeyValueMapService = &KeyValueMapServiceOp{}
// EntryStruct Holds the Key value map entry
type EntryStruct struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}
// KeyValueMap Holds the Key value map
type KeyValueMap struct {
Name string `json:"name,omitempty"`
Encrypted bool `json:"encrypted,omitempty"`
Entry []EntryStruct `json:"entry,omitempty"`
}
// Get the Keyvaluemap
func (s *KeyValueMapServiceOp) Get(name string, env string) (*KeyValueMap, *Response, error) {
path := path.Join("environments", env, "keyvaluemaps", name)
req, e := s.client.NewRequest("GET", path, nil, "")
if e != nil {
return nil, nil, e
}
returnedKeyValueMap := KeyValueMap{}
resp, e := s.client.Do(req, &returnedKeyValueMap)
if e != nil {
return nil, resp, e
}
return &returnedKeyValueMap, resp, e
}
// Create a new key value map
func (s *KeyValueMapServiceOp) Create(keyValueMap KeyValueMap, env string) (*KeyValueMap, *Response, error) {
return postOrPutKeyValueMap(keyValueMap, env, "POST", s)
}
// Update an existing key value map
//func (s *KeyValueMapServiceOp) Update(keyValueMap KeyValueMap, env string) (*KeyValueMap, *Response, error) {
// return postOrPutKeyValueMap(keyValueMap, env, "PUT", s)
//}
// Delete an existing key value map
func (s *KeyValueMapServiceOp) Delete(name string, env string) (*Response, error) {
path := path.Join("environments", env, "keyvaluemaps", name)
req, e := s.client.NewRequest("DELETE", path, nil, "")
if e != nil {
return nil, e
}
resp, e := s.client.Do(req, nil)
if e != nil {
return resp, e
}
return resp, e
}
func postOrPutKeyValueMap(keyValueMap KeyValueMap, env string, opType string, s *KeyValueMapServiceOp) (*KeyValueMap, *Response, error) {
uripath := ""
if opType == "PUT" {
uripath = path.Join("environments", env, "keyvaluemaps", keyValueMap.Name)
} else {
uripath = path.Join("environments", env, "keyvaluemaps")
}
req, e := s.client.NewRequest(opType, uripath, keyValueMap, "")
if e != nil {
return nil, nil, e
}
returnedKeyValueMap := KeyValueMap{}
resp, e := s.client.Do(req, &returnedKeyValueMap)
if e != nil {
return nil, resp, e
}
return &returnedKeyValueMap, resp, e
}