This repository has been archived by the owner on May 23, 2023. It is now read-only.
/
bindings.go
319 lines (283 loc) · 8.05 KB
/
bindings.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bindings
import (
"fmt"
"net/http"
"os"
"sort"
"strings"
"text/template"
"github.com/apigee/istio-mixer-adapter/adapter/product"
"github.com/apigee/istio-mixer-adapter/apigee-istio/shared"
"github.com/spf13/cobra"
)
const (
servicesAttr = "istio-services"
productsURLFormat = "/v1/organizations/%s/apiproducts" // ManagementBase
productAttrPathFormat = "/v1/organizations/%s/apiproducts/%s/attributes" // ManagementBase, prod
)
type bindings struct {
*shared.RootArgs
products []product.APIProduct
}
// Cmd returns base command
func Cmd(rootArgs *shared.RootArgs, printf, fatalf shared.FormatFn) *cobra.Command {
cfg := &bindings{RootArgs: rootArgs}
c := &cobra.Command{
Use: "bindings",
Short: "Manage Apigee Product to Istio Service bindings",
Long: "Manage Apigee Product to Istio Service bindings.",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return rootArgs.Resolve(false)
},
}
c.AddCommand(cmdBindingsList(cfg, printf, fatalf))
c.AddCommand(cmdBindingsAdd(cfg, printf, fatalf))
c.AddCommand(cmdBindingsRemove(cfg, printf, fatalf))
return c
}
func cmdBindingsList(b *bindings, printf, fatalf shared.FormatFn) *cobra.Command {
c := &cobra.Command{
Use: "list",
Short: "List Apigee Product to Istio Service bindings",
Long: "List Apigee Product to Istio Service bindings",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
b.cmdList(printf, fatalf)
},
}
return c
}
func cmdBindingsAdd(b *bindings, printf, fatalf shared.FormatFn) *cobra.Command {
c := &cobra.Command{
Use: "add [service name] [product name]",
Short: "Add Istio Service binding to Apigee Product",
Long: "Add Istio Service binding to Apigee Product",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
serviceName := args[0]
productName := args[1]
p, err := b.getProduct(productName)
if err != nil {
fatalf("%v", err)
}
if p == nil {
fatalf("invalid product name: %s", productName)
}
b.bindService(p, serviceName, printf, fatalf)
},
}
return c
}
func cmdBindingsRemove(b *bindings, printf, fatalf shared.FormatFn) *cobra.Command {
c := &cobra.Command{
Use: "remove [service name] [product name]",
Short: "Remove Istio Service binding from Apigee Product",
Long: "Remove Istio Service binding from Apigee Product",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
serviceName := args[0]
productName := args[1]
p, err := b.getProduct(productName)
if err != nil {
fatalf("%v", err)
}
if p == nil {
fatalf("invalid product name: %s", productName)
}
b.unbindService(p, serviceName, printf, fatalf)
},
}
return c
}
func (b *bindings) getProduct(name string) (*product.APIProduct, error) {
products, err := b.getProducts()
if err != nil {
return nil, err
}
for _, p := range products {
if p.Name == name {
return &p, nil
}
}
return nil, nil
}
func (b *bindings) getProducts() ([]product.APIProduct, error) {
if b.products != nil {
return b.products, nil
}
req, err := b.Client.NewRequest(http.MethodGet, "", nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}
req.URL.Path = fmt.Sprintf(productsURLFormat, b.Org) // hack: negate client's base URL
req.URL.RawQuery = "expand=true"
var res product.APIResponse
resp, err := b.Client.Do(req, &res)
if err != nil {
return nil, fmt.Errorf("error retrieving products: %v", err)
}
defer resp.Body.Close()
return res.APIProducts, nil
}
func (b *bindings) cmdList(printf, fatalf shared.FormatFn) error {
products, err := b.getProducts()
if err != nil {
fatalf("%v", err)
}
var bound, unbound []product.APIProduct
for _, p := range products {
// server returns empty scopes as array with a single empty string, remove for consistency
if len(p.Scopes) == 1 && p.Scopes[0] == "" {
p.Scopes = []string{}
}
// server may return empty quota field as "null"
if p.QuotaLimit == "null" {
p.QuotaLimit = ""
}
p.Targets = p.GetBoundServices()
if p.Targets == nil {
unbound = append(unbound, p)
} else {
bound = append(bound, p)
}
}
sort.Sort(byName(bound))
sort.Sort(byName(unbound))
data := struct {
Bound []product.APIProduct
Unbound []product.APIProduct
}{
Bound: bound,
Unbound: unbound,
}
tmp := template.New("products")
tmp.Funcs(template.FuncMap{
"scopes": func(in []string) string { return strings.Join(in, ",") },
})
tmp, err = tmp.Parse(productsTemplate)
if err != nil {
fatalf("failed to create template: %v", err)
}
err = tmp.Execute(os.Stdout, data)
if err != nil {
fatalf("failed to execute template: %v", err)
}
return nil
}
func (b *bindings) bindService(p *product.APIProduct, service string, printf, fatalf shared.FormatFn) {
boundServices := p.GetBoundServices()
if _, ok := indexOf(boundServices, service); ok {
fatalf("service %s is already bound to %s", service, p.Name)
}
err := b.updateServiceBindings(p, append(boundServices, service))
if err != nil {
fatalf("error binding service %s to %s: %v", service, p.Name, err)
}
printf("product %s is now bound to: %s", p.Name, service)
}
func (b *bindings) unbindService(p *product.APIProduct, service string, printf, fatalf shared.FormatFn) {
boundServices := p.GetBoundServices()
i, ok := indexOf(boundServices, service)
if !ok {
fatalf("service %s is not bound to %s", service, p.Name)
}
boundServices = append(boundServices[:i], boundServices[i+1:]...)
err := b.updateServiceBindings(p, boundServices)
if err != nil {
fatalf("error removing service %s from %s: %v", service, p.Name, err)
}
printf("product %s is no longer bound to: %s", p.Name, service)
}
func (b *bindings) updateServiceBindings(p *product.APIProduct, bindings []string) error {
bindingsString := strings.Join(bindings, ",")
var attributes []product.Attribute
for _, a := range p.Attributes {
if a.Name != servicesAttr {
attributes = append(attributes, a)
}
}
attributes = append(attributes, product.Attribute{
Name: servicesAttr,
Value: bindingsString,
})
newAttrs := attrUpdate{
Attributes: attributes,
}
req, err := b.Client.NewRequest(http.MethodPost, "", newAttrs)
if err != nil {
return err
}
path := fmt.Sprintf(productAttrPathFormat, b.Org, p.Name)
req.URL.Path = path // hack: negate client's base URL
var attrResult attrUpdate
_, err = b.Client.Do(req, &attrResult)
return err
}
func indexOf(array []string, val string) (index int, exists bool) {
index = -1
for i, v := range array {
if val == v {
index = i
exists = true
break
}
}
return
}
type attrUpdate struct {
Attributes []product.Attribute `json:"attribute,omitempty"`
}
type byName []product.APIProduct
func (a byName) Len() int { return len(a) }
func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byName) Less(i, j int) bool { return a[i].Name < a[j].Name }
const productsTemplate = `
{{- define "product"}}
{{.Name}}:
{{- if .Scopes}}
Scopes: {{scopes (.Scopes)}}
{{- end}}
{{- if .QuotaLimit}}
Quota: {{.QuotaLimit}} requests every {{.QuotaInterval}} {{.QuotaTimeUnit}}
{{- end}}
{{- if .Targets}}
Service bindings:
{{- range .Targets}}
{{.}}
{{- end}}
Paths:
{{- range .Resources}}
{{.}}
{{- end}}
{{- end}}
{{- end}}
API Products
============
{{- if .Bound}}
Bound
-----
{{- range .Bound}}
{{- template "product" .}}
{{- end}}
{{- end}}
{{- if .Unbound}}
Unbound
-------
{{- range .Unbound}}
{{- template "product" .}}
{{- end}}
{{- end}}
`