-
Notifications
You must be signed in to change notification settings - Fork 0
/
apic.go
451 lines (382 loc) · 13.8 KB
/
apic.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package aci
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/ciscoecosystem/aci-go-client/client"
"github.com/ciscoecosystem/aci-go-client/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jgomezve/aci-k8s-operator/pkg/utils"
)
type ApicClient struct {
host string
user string
password string
client *client.Client
}
type ApicInterface interface {
CreateTenant(name, description string) error
DeleteTenant(name string) error
CreateApplicationProfile(name, description, tenantName string) error
DeleteApplicationProfile(name, tenantName string) error
ApplicationProfileExists(name, tenantName string) (bool, error)
EmptyApplicationProfile(name, tenantName string) (bool, error)
CreateEndpointGroup(name, description, appName, tenantName, bdName, vmmName string) error
DeleteEndpointGroup(name, appName, tenantName string) error
CreateFilterAndFilterEntry(tenantName, name, eth, ip string, port int) error
DeleteFilter(tenantName, name string) error
FilterExists(name, tenantName string) (bool, error)
CreateContract(tenantName, name string, filters []string) error
DeleteContract(tenantName, name string) error
InheritContractFromMaster(epgName, appName, tenantName, appMasterName, epgMasterName string) error
EpgExists(name, appName, tenantName string) (bool, error)
AddTagAnnotationToEpg(name, appName, tenantName, key, value string) error
RemoveTagAnnotation(name, appName, tenantName, key string) error
GetEpgWithAnnotation(appName, tenantName, key string) ([]string, error)
GetAnnotationsEpg(name, appName, tenantName string) ([]string, error)
AddTagAnnotationToFilter(name, tenantName, key, value string) error
GetFilterWithAnnotation(tenantName, key string) ([]string, error)
ConsumeContract(epgName, appName, tenantName, conName string) error
ProvideContract(epgName, appName, tenantName, conName string) error
DeleteContractConsumer(epgName, appName, tenantName, conName string) error
DeleteContractProvider(epgName, appName, tenantName, conName string) error
GetContractFilters(contractName, tenantName string) ([]string, error)
DeleteFilterFromSubjectContract(subjectName, tenantName, filter string) error
GetContracts(epgName, appName, tenantName string) (map[string][]string, error)
}
func NewApicClient(host, user, password, privateKey string) (*ApicClient, error) {
if privateKey == "" {
ac := &ApicClient{
host: host,
user: user,
password: password,
client: client.GetClient(fmt.Sprintf("https://%s/", host), user, client.Password(password), client.Insecure(true), client.SkipLoggingPayload(true)),
}
return ac, ac.client.Authenticate()
} else {
ac := &ApicClient{
host: host,
user: user,
password: password,
client: client.GetClient(fmt.Sprintf("https://%s/", host), user, client.PrivateKey(privateKey), client.AdminCert(fmt.Sprintf("%s.crt", user)), client.Insecure(true), client.SkipLoggingPayload(true)),
}
// Test the client
_, err := ac.client.ListSystem()
return ac, err
}
// TODO: Re-use connection
}
/*
Tenant Function
*/
func (ac *ApicClient) CreateTenant(name, description string) error {
fvTenantAttr := models.TenantAttributes{}
fvTenant := models.NewTenant(fmt.Sprintf("tn-%s", name), "uni", description, fvTenantAttr)
err := ac.client.Save(fvTenant)
if err != nil {
return err
}
return nil
}
func (ac *ApicClient) DeleteTenant(name string) error {
err := ac.client.DeleteTenant(name)
if err != nil {
return err
}
return nil
}
/*
Application Profile function
*/
func (ac *ApicClient) CreateApplicationProfile(name, description, tenantName string) error {
fvAppAttr := models.ApplicationProfileAttributes{}
fvAppAttr.Annotation = "orchestrator:kubernetes"
fvApp := models.NewApplicationProfile(fmt.Sprintf("ap-%s", name), fmt.Sprintf("uni/tn-%s", tenantName), description, fvAppAttr)
err := ac.client.Save(fvApp)
if err != nil {
return err
}
if err != nil {
return err
}
return nil
}
func (ac *ApicClient) DeleteApplicationProfile(name, tenantName string) error {
err := ac.client.DeleteApplicationProfile(name, tenantName)
if err != nil {
return err
}
return nil
}
func (ac *ApicClient) EmptyApplicationProfile(name, tenantName string) (bool, error) {
epgList, err := ac.client.ListApplicationEPG(name, tenantName)
if err != nil {
if err.Error() == "Error retrieving Object: Object may not exists" {
return true, nil
}
return false, err
}
return len(epgList) == 0, nil
}
func (ac *ApicClient) ApplicationProfileExists(name, tenantName string) (bool, error) {
fvAppCont, err := ac.client.Get(fmt.Sprintf("uni/tn-%s/ap-%s", tenantName, name))
if err != nil {
// TODO: Check when it is an real error
return false, err
}
fvApp := models.ApplicationProfileFromContainer(fvAppCont)
if fvApp.DistinguishedName == "" {
return false, nil
}
return true, nil
}
/*
Endpoint Groups functions
*/
func (ac *ApicClient) CreateEndpointGroup(name, description, appName, tenantName, bdName, vmmName string) error {
fvAEpgAttr := models.ApplicationEPGAttributes{}
fvAEpgAttr.Annotation = "orchestrator:kubernetes"
fvAEpg := models.NewApplicationEPG(fmt.Sprintf("epg-%s", name), fmt.Sprintf("uni/tn-%s/ap-%s", tenantName, appName), description, fvAEpgAttr)
if err := ac.client.Save(fvAEpg); err != nil {
return err
}
if err := ac.client.CreateRelationfvRsBdFromApplicationEPG(fvAEpg.DistinguishedName, bdName); err != nil {
return err
}
tDn := fmt.Sprintf("uni/vmmp-Kubernetes/dom-%s", vmmName)
if err := ac.client.CreateRelationfvRsDomAttFromApplicationEPG(fvAEpg.DistinguishedName, tDn); err != nil {
return err
}
return nil
}
func (ac *ApicClient) DeleteEndpointGroup(name, appName, tenantName string) error {
err := ac.client.DeleteApplicationEPG(name, appName, tenantName)
if err != nil {
return err
}
return nil
}
func (ac *ApicClient) EpgExists(name, appName, tenantName string) (bool, error) {
fvAEPgCont, err := ac.client.Get(fmt.Sprintf("uni/tn-%s/ap-%s/epg-%s", tenantName, appName, name))
if err != nil {
// TODO: Check when is an actual error
return false, err
}
fvAEPg := models.ApplicationEPGFromContainer(fvAEPgCont)
if fvAEPg.DistinguishedName == "" {
return false, nil
}
return true, nil
}
// Add Annotation (key=value) to the EPG object
func (ac *ApicClient) AddTagAnnotationToEpg(name, appName, tenantName, key, value string) error {
parentDn := fmt.Sprintf("uni/tn-%s/ap-%s/epg-%s", tenantName, appName, name)
if err := ac.AddTagAnnotation(key, value, parentDn); err != nil {
return err
}
return nil
}
// Remove Annotation (key=value) to the EPG object
func (ac *ApicClient) RemoveTagAnnotation(name, appName, tenantName, key string) error {
parentDn := fmt.Sprintf("uni/tn-%s/ap-%s/epg-%s", tenantName, appName, name)
if err := ac.client.DeleteAnnotation(key, parentDn); err != nil {
return err
}
return nil
}
// Get list of EPG with an annotation with specific key
func (ac *ApicClient) GetEpgWithAnnotation(appName, tenantName, key string) ([]string, error) {
epgs := []string{}
epgList, err := ac.client.ListApplicationEPG(appName, tenantName)
if err != nil {
return []string{}, err
}
for _, epg := range epgList {
_, err := ac.client.ReadAnnotation(key, epg.DistinguishedName)
if err == nil {
epgs = append(epgs, epg.Name)
}
}
return epgs, nil
}
func (ac *ApicClient) GetAnnotationsEpg(name, appName, tenantName string) ([]string, error) {
r, _ := regexp.Compile(fmt.Sprintf("uni/tn-%s/ap-%s/epg-%s/", tenantName, appName, name))
annotations := []string{}
annotationList, err := ac.client.ListAnnotation()
if err != nil {
return []string{}, err
}
for _, ann := range annotationList {
if r.Match([]byte(ann.DistinguishedName)) {
annotations = append(annotations, ann.Key)
}
}
return annotations, nil
}
// Contract in the same tenant
func (ac *ApicClient) ConsumeContract(epgName, appName, tenantName, conName string) error {
fvRsConsAtt := models.ContractConsumerAttributes{}
fvRsConsAtt.TnVzBrCPName = conName
//fvRsConsAtt.TDn = fmt.Sprintf("uni/tn-%s/brc-%s", tenantName, conName)
epgDn := fmt.Sprintf("uni/tn-%s/ap-%s/epg-%s", tenantName, appName, epgName)
fvRsCons := models.NewContractConsumer(fmt.Sprintf("rscons-%s", conName), epgDn, fvRsConsAtt)
err := ac.client.Save(fvRsCons)
if err != nil {
return err
}
return nil
}
// Contract in the same tenant
func (ac *ApicClient) ProvideContract(epgName, appName, tenantName, conName string) error {
fvRsProvAtt := models.ContractProviderAttributes{}
fvRsProvAtt.TnVzBrCPName = conName
//fvRsProvAtt.TDn = fmt.Sprintf("uni/tn-%s/brc-%s", tenantName, conName)
epgDn := fmt.Sprintf("uni/tn-%s/ap-%s/epg-%s", tenantName, appName, epgName)
fvRsCons := models.NewContractProvider(fmt.Sprintf("rsprov-%s", conName), epgDn, fvRsProvAtt)
err := ac.client.Save(fvRsCons)
if err != nil {
return err
}
return nil
}
func (ac *ApicClient) GetContracts(epgName, appName, tenantName string) (map[string][]string, error) {
return map[string][]string{}, nil
}
func (ac *ApicClient) DeleteContractConsumer(epgName, appName, tenantName, conName string) error {
return ac.client.DeleteContractConsumer(conName, epgName, appName, tenantName)
}
func (ac *ApicClient) DeleteContractProvider(epgName, appName, tenantName, conName string) error {
return ac.client.DeleteContractProvider(conName, epgName, appName, tenantName)
}
func (ac *ApicClient) InheritContractFromMaster(epgName, appName, tenantName, appMasterName, epgMasterName string) error {
epgDn := fmt.Sprintf("uni/tn-%s/ap-%s/epg-%s", tenantName, appName, epgName)
tDn := fmt.Sprintf("uni/tn-%s/ap-%s/epg-%s", tenantName, appMasterName, epgMasterName)
if err := ac.client.CreateRelationfvRsSecInheritedFromApplicationEPG(epgDn, tDn); err != nil {
return err
}
return nil
}
/*
Contract functions
*/
func (ac *ApicClient) CreateContract(tenantName, name string, filters []string) error {
vzBrCPAttr := models.ContractAttributes{}
vzBrCPAttr.Name = name
vzBrCPAttr.Annotation = "orchestrator:kubernetes"
vzSubjAttr := models.ContractSubjectAttributes{}
vzSubjAttr.Name = name
vzSubjAttr.RevFltPorts = "yes"
vzBrCP := models.NewContract(fmt.Sprintf("brc-%s", name), fmt.Sprintf("uni/tn-%s", tenantName), "", vzBrCPAttr)
err := ac.client.Save(vzBrCP)
if err != nil {
return err
}
vzSubj := models.NewContractSubject(fmt.Sprintf("subj-%s", name), vzBrCP.DistinguishedName, "", vzSubjAttr)
err = ac.client.Save(vzSubj)
if err != nil {
return err
}
for _, flt := range filters {
err = ac.client.CreateRelationvzRsSubjFiltAttFromContractSubject(vzSubj.DistinguishedName, flt)
if err != nil {
return err
}
}
return nil
}
func (ac *ApicClient) GetContractFilters(contractName, tenantName string) ([]string, error) {
dn := fmt.Sprintf("uni/tn-%s/brc-%s/subj-%s", tenantName, contractName, contractName)
filters, err := ac.client.ReadRelationvzRsSubjFiltAttFromContractSubject(dn)
if err != nil {
return []string{}, err
}
filtersName := []string{}
for _, flt := range utils.ToStringList(filters.(*schema.Set).List()) {
r, _ := regexp.Compile("/flt-[a-zA-Z0-9_]*")
fltName := r.FindString(flt)
filtersName = append(filtersName, strings.Replace(fltName, "/flt-", "", -1))
}
return filtersName, nil
}
func (ac *ApicClient) DeleteFilterFromSubjectContract(subjectName, tenantName, filter string) error {
dn := fmt.Sprintf("uni/tn-%s/brc-%s/subj-%s", tenantName, subjectName, subjectName)
return ac.client.DeleteRelationvzRsSubjFiltAttFromContractSubject(dn, filter)
}
func (ac *ApicClient) DeleteContract(tenantName, name string) error {
err := ac.client.DeleteContract(name, tenantName)
if err != nil {
return err
}
return nil
}
func (ac *ApicClient) CreateFilterAndFilterEntry(tenantName, name, eth, ip string, port int) error {
vzFilterAttr := models.FilterAttributes{}
vzFilterAttr.Annotation = "orchestrator:kubernetes"
vzEntryAttr := models.FilterEntryAttributes{}
vzEntryAttr.EtherT = eth
vzEntryAttr.Prot = ip
vzEntryAttr.DFromPort = strconv.Itoa(port)
vzEntryAttr.DToPort = strconv.Itoa(port)
fvFilter := models.NewFilter(fmt.Sprintf("flt-%s", name), fmt.Sprintf("uni/tn-%s", tenantName), "", vzFilterAttr)
err := ac.client.Save(fvFilter)
if err != nil {
return err
}
fvFilterEntry := models.NewFilterEntry(fmt.Sprintf("e-%s", name), fvFilter.DistinguishedName, "", vzEntryAttr)
err = ac.client.Save(fvFilterEntry)
if err != nil {
return err
}
return nil
}
func (ac *ApicClient) DeleteFilter(tenantName, name string) error {
err := ac.client.DeleteFilter(name, tenantName)
if err != nil {
return err
}
return nil
}
func (ac *ApicClient) AddTagAnnotation(key, value, parentDn string) error {
tag := models.NewAnnotation(fmt.Sprintf("annotationKey-[%s]", key), parentDn, models.AnnotationAttributes{Key: key, Value: value})
err := ac.client.Save(tag)
if err != nil {
return err
}
return nil
}
func (ac *ApicClient) FilterExists(name, tenantName string) (bool, error) {
fvFilterCont, err := ac.client.Get(fmt.Sprintf("uni/tn-%s/flt-%s", tenantName, name))
if err != nil {
// TODO: Check when is an actual error
return false, nil
}
fvFilter := models.FilterFromContainer(fvFilterCont)
if fvFilter.DistinguishedName == "" {
return false, nil
}
return true, nil
}
// Add Annotation (key=value) to the EPG object
func (ac *ApicClient) AddTagAnnotationToFilter(name, tenantName, key, value string) error {
parentDn := fmt.Sprintf("uni/tn-%s/flt-%s", tenantName, name)
if err := ac.AddTagAnnotation(key, value, parentDn); err != nil {
return err
}
return nil
}
// Get list of Filters with an annotation with specific key
func (ac *ApicClient) GetFilterWithAnnotation(tenantName, key string) ([]string, error) {
filters := []string{}
filterList, err := ac.client.ListFilter(tenantName)
if err != nil {
return []string{}, err
}
for _, flt := range filterList {
_, err := ac.client.ReadAnnotation(key, flt.DistinguishedName)
if err == nil {
filters = append(filters, flt.Name)
}
}
return filters, nil
}