forked from udhos/acigo
/
tenant.go
193 lines (129 loc) · 4.54 KB
/
tenant.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package aci
import (
"bytes"
"encoding/json"
"fmt"
"time"
)
func jsonTenantAdd(name, descr, namealias, annotation string) string {
prefix := fmt.Sprintf(`{"fvTenant":{"attributes":{"name":"%s","nameAlias":"%s","annotation":"%s","status":"created"`, name, namealias, annotation)
suffix := "}}}"
var middle string
if descr != "" {
middle = fmt.Sprintf(`,"descr":"%s"`, descr)
}
return prefix + middle + suffix
}
func jsonTenantUpdate(name, descr, namealias string) string {
prefix := fmt.Sprintf(`{"fvTenant":{"attributes":{"name":"%s"`, name)
suffix := "}}}"
var middle string
if namealias != "" {
middle = fmt.Sprintf(`,"nameAlias":"%s"`, namealias)
}
if descr != "" {
middle += fmt.Sprintf(`,"descr":"%s"`, descr)
}
return prefix + middle + suffix
}
func jsonTenantDel(name string) string {
return fmt.Sprintf(`{"fvTenant":{"attributes":{"name":"%s","status":"deleted"}}}`, name)
}
// TenantAdd creates a new tenant.
func (c *Client) TenantAdd(name, descr, namealias, annotation string) error {
api := "/api/mo/uni.json"
jsonTenant := jsonTenantAdd(name, descr, namealias, annotation)
url := c.getURL(api)
c.debugf("tenant add: url=%s json=%s", url, jsonTenant)
body, errPost := c.post(url, contentTypeJSON, bytes.NewBufferString(jsonTenant))
if errPost != nil {
return errPost
}
c.debugf("tenant add: reply: %s", string(body))
return parseJSONError(body)
}
// TenantUpdate creates a new tenant.
func (c *Client) TenantUpdate(name, descr, namealias string) error {
api := "/api/mo/uni.json"
jsonTenant := jsonTenantUpdate(name, descr, namealias)
url := c.getURL(api)
c.debugf("tenant add: url=%s json=%s", url, jsonTenant)
body, errPost := c.post(url, contentTypeJSON, bytes.NewBufferString(jsonTenant))
if errPost != nil {
return errPost
}
c.debugf("tenant add: reply: %s", string(body))
return parseJSONError(body)
}
// TenantDel deletes an existing tenant.
func (c *Client) TenantDel(name string) error {
api := "/api/mo/uni.json"
jsonTenant := jsonTenantDel(name)
url := c.getURL(api)
//url += "?rsp-subtree=modified" // demand response
c.debugf("tenant del: url=%s json=%s", url, jsonTenant)
body, errPost := c.post(url, contentTypeJSON, bytes.NewBufferString(jsonTenant))
if errPost != nil {
return errPost
}
c.debugf("tenant del: reply: %s", string(body))
return parseJSONError(body)
}
// TenantList retrieves the list of tenants.
func (c *Client) TenantList() ([]map[string]interface{}, error) {
key := "fvTenant"
api := "/api/node/class/" + key + ".json"
url := c.getURL(api)
c.debugf("TenantList: url=%s", url)
body, errGet := c.get(url)
if errGet != nil {
return nil, errGet
}
c.debugf("TenantList: reply: %s", string(body))
return jsonImdataAttributes(c, body, key, "TenantList")
}
// TenantSubscribe subscribes to tenant notifications.
// The subscriptionId is returned.
func (c *Client) TenantSubscribe() (string, error) {
api := "/api/class/fvTenant.json?subscription=yes"
url := c.getURL(api)
c.debugf("tenant subscribe: url=%s", url)
body, errGet := c.get(url)
if errGet != nil {
return "", errGet
}
c.debugf("tenant subscribe: reply: %s", string(body))
var reply interface{}
errJSON := json.Unmarshal(body, &reply)
if errJSON != nil {
return "", errJSON
}
sub, subError := mapGet(reply, "subscriptionId")
if subError != nil {
return "", fmt.Errorf("tentant subscribe error %v: %s", subError, string(body))
}
subscriptionId, isStr := sub.(string)
if !isStr {
return "", fmt.Errorf("subId not a string %v: %s", sub, string(body))
}
c.debugf("TenantSubscribe: subscriptionId=%s", subscriptionId)
return subscriptionId, nil
}
// TenantSubscriptionTimeout gets the subscription timeout.
// In order to keep the subscription active, TenantSubscriptionRefresh() must be called at a period lower than the timeout reported by TenantSubscriptionTimeout().
func (c *Client) TenantSubscriptionTimeout() time.Duration {
return 60 * time.Second // ACI API docs says this value is fixed
}
// TenantSubscriptionRefresh refreshes a subscription.
// In order to keep the subscription active, TenantSubscriptionRefresh() must be called at a period lower than the timeout reported by TenantSubscriptionTimeout().
func (c *Client) TenantSubscriptionRefresh(subscriptionId string) error {
api := "/api/subscriptionRefresh.json?id=" + subscriptionId
url := c.getURL(api)
c.debugf("TenantSubscriptionRefresh: url=%s", url)
body, errGet := c.get(url)
if errGet != nil {
return errGet
}
c.debugf("TenantSubscriptionRefresh: reply: %s", string(body))
return parseJSONError(body)
}