This repository has been archived by the owner on May 23, 2023. It is now read-only.
/
kvm.go
94 lines (82 loc) · 2.28 KB
/
kvm.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
package apigee
import (
"path"
)
const kvmPath = "keyvaluemaps"
// KVMService is an interface for interfacing with the Apigee Edge Admin API
// dealing with kvm.
type KVMService interface {
Get(mapname string) (*KVM, *Response, error)
Create(kvm KVM) (*Response, error)
UpdateEntry(kvmName string, entry Entry) (*Response, error)
AddEntry(kvmName string, entry Entry) (*Response, error)
}
// Entry is an entry in the KVM
type Entry struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}
// KVM represents an Apigee KVM
type KVM struct {
Name string `json:"name,omitempty"`
Encrypted bool `json:"encrypted,omitempty"`
Entries []Entry `json:"entry,omitempty"`
}
// GetValue returns a value from the KVM
func (k *KVM) GetValue(name string) (v string, ok bool) {
for _, e := range k.Entries {
if e.Name == name {
return e.Value, true
}
}
return
}
// KVMServiceOp represents a KVM service operation
type KVMServiceOp struct {
client *EdgeClient
}
var _ KVMService = &KVMServiceOp{}
// Get returns a response given a KVM map name
func (s *KVMServiceOp) Get(mapname string) (*KVM, *Response, error) {
path := path.Join(kvmPath, mapname)
req, e := s.client.NewRequest("GET", path, nil)
if e != nil {
return nil, nil, e
}
returnedKVM := KVM{}
resp, e := s.client.Do(req, &returnedKVM)
if e != nil {
return nil, resp, e
}
return &returnedKVM, resp, e
}
// Create creates a KVM and returns a response
func (s *KVMServiceOp) Create(kvm KVM) (*Response, error) {
path := path.Join(kvmPath)
req, e := s.client.NewRequest("POST", path, kvm)
if e != nil {
return nil, e
}
resp, e := s.client.Do(req, &kvm)
return resp, e
}
// UpdateEntry updates a KVM entry
func (s *KVMServiceOp) UpdateEntry(kvmName string, entry Entry) (*Response, error) {
path := path.Join(kvmPath, kvmName, "entries", entry.Name)
req, e := s.client.NewRequest("POST", path, entry)
if e != nil {
return nil, e
}
resp, e := s.client.Do(req, &entry)
return resp, e
}
// AddEntry add an entry to the KVM
func (s *KVMServiceOp) AddEntry(kvmName string, entry Entry) (*Response, error) {
path := path.Join(kvmPath, kvmName, "entries")
req, e := s.client.NewRequest("POST", path, entry)
if e != nil {
return nil, e
}
resp, e := s.client.Do(req, &entry)
return resp, e
}