forked from vmware/go-vcloud-director
/
adminorg.go
766 lines (673 loc) · 25.6 KB
/
adminorg.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*
* Copyright 2019 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
*/
package govcd
import (
"fmt"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/jlmorris3827/go-vcloud-director/v2/util"
"github.com/jlmorris3827/go-vcloud-director/v2/types/v56"
)
// AdminOrg gives an admin representation of an org.
// Administrators can delete and update orgs with an admin org object.
// AdminOrg includes all members of the Org element, and adds several
// elements that can be viewed and modified only by system administrators.
// Definition: https://code.vmware.com/apis/220/vcloud#/doc/doc/types/AdminOrgType.html
type AdminOrg struct {
AdminOrg *types.AdminOrg
client *Client
}
func NewAdminOrg(cli *Client) *AdminOrg {
return &AdminOrg{
AdminOrg: new(types.AdminOrg),
client: cli,
}
}
// CreateCatalog creates a catalog with given name and description under the
// the given organization. Returns an AdminCatalog that contains a creation
// task.
// API Documentation: https://code.vmware.com/apis/220/vcloud#/doc/doc/operations/POST-CreateCatalog.html
func (adminOrg *AdminOrg) CreateCatalog(name, description string) (AdminCatalog, error) {
return CreateCatalog(adminOrg.client, adminOrg.AdminOrg.Link, name, description)
}
// CreateCatalogWithStorageProfile is like CreateCatalog, but allows to specify storage profile
func (adminOrg *AdminOrg) CreateCatalogWithStorageProfile(name, description string, storageProfiles *types.CatalogStorageProfiles) (*AdminCatalog, error) {
return CreateCatalogWithStorageProfile(adminOrg.client, adminOrg.AdminOrg.Link, name, description, storageProfiles)
}
// GetAllVDCs returns all depending VDCs for a particular Org
func (adminOrg *AdminOrg) GetAllVDCs(refresh bool) ([]*Vdc, error) {
if refresh {
err := adminOrg.Refresh()
if err != nil {
return nil, err
}
}
allVdcs := make([]*Vdc, len(adminOrg.AdminOrg.Vdcs.Vdcs))
for vdcIndex, vdc := range adminOrg.AdminOrg.Vdcs.Vdcs {
vdc, err := adminOrg.GetVDCByHref(vdc.HREF)
if err != nil {
return nil, fmt.Errorf("error retrieving VDC '%s': %s", vdc.Vdc.Name, err)
}
allVdcs[vdcIndex] = vdc
}
return allVdcs, nil
}
// GetAllStorageProfileReferences traverses all depending VDCs and returns a slice of storage profile references
// available in those VDCs
func (adminOrg *AdminOrg) GetAllStorageProfileReferences(refresh bool) ([]*types.Reference, error) {
if refresh {
err := adminOrg.Refresh()
if err != nil {
return nil, err
}
}
allVdcs, err := adminOrg.GetAllVDCs(refresh)
if err != nil {
return nil, fmt.Errorf("could not retrieve storage profile references: %s", err)
}
allStorageProfileReferences := make([]*types.Reference, 0)
for _, vdc := range allVdcs {
if len(vdc.Vdc.VdcStorageProfiles.VdcStorageProfile) > 0 {
allStorageProfileReferences = append(allStorageProfileReferences, vdc.Vdc.VdcStorageProfiles.VdcStorageProfile...)
}
}
return allStorageProfileReferences, nil
}
// GetStorageProfileReferenceById finds storage profile reference by specified ID in Org or returns ErrorEntityNotFound
func (adminOrg *AdminOrg) GetStorageProfileReferenceById(id string, refresh bool) (*types.Reference, error) {
allStorageProfiles, err := adminOrg.GetAllStorageProfileReferences(refresh)
if err != nil {
return nil, fmt.Errorf("error getting all storage profiles: %s", err)
}
for _, storageProfileReference := range allStorageProfiles {
if storageProfileReference.ID == id {
return storageProfileReference, nil
}
}
return nil, fmt.Errorf("%s: storage profile with ID '%s' not found in Org '%s'",
ErrorEntityNotFound, id, adminOrg.AdminOrg.Name)
}
// Deletes the org, returning an error if the vCD call fails.
// API Documentation: https://code.vmware.com/apis/220/vcloud#/doc/doc/operations/DELETE-Organization.html
func (adminOrg *AdminOrg) Delete(force bool, recursive bool) error {
if force && recursive {
//undeploys vapps
err := adminOrg.undeployAllVApps()
if err != nil {
return fmt.Errorf("error could not undeploy: %s", err)
}
//removes vapps
err = adminOrg.removeAllVApps()
if err != nil {
return fmt.Errorf("error could not remove vapp: %s", err)
}
//removes catalogs
err = adminOrg.removeCatalogs()
if err != nil {
return fmt.Errorf("error could not remove all catalogs: %s", err)
}
//removes networks
err = adminOrg.removeAllOrgNetworks()
if err != nil {
return fmt.Errorf("error could not remove all networks: %s", err)
}
//removes org vdcs
err = adminOrg.removeAllOrgVDCs()
if err != nil {
return fmt.Errorf("error could not remove all vdcs: %s", err)
}
}
// Disable org
err := adminOrg.Disable()
if err != nil {
return fmt.Errorf("error disabling Org %s: %s", adminOrg.AdminOrg.Name, err)
}
// Get admin HREF
orgHREF, err := url.ParseRequestURI(adminOrg.AdminOrg.HREF)
if err != nil {
return fmt.Errorf("error getting AdminOrg HREF %s : %s", adminOrg.AdminOrg.HREF, err)
}
req := adminOrg.client.NewRequest(map[string]string{
"force": strconv.FormatBool(force),
"recursive": strconv.FormatBool(recursive),
}, http.MethodDelete, *orgHREF, nil)
resp, err := checkResp(adminOrg.client.Http.Do(req))
if err != nil {
return fmt.Errorf("error deleting Org %s: %s", adminOrg.AdminOrg.ID, err)
}
task := NewTask(adminOrg.client)
if err = decodeBody(types.BodyTypeXML, resp, task.Task); err != nil {
return fmt.Errorf("error decoding task response: %s", err)
}
return task.WaitTaskCompletion()
}
// Disables the org. Returns an error if the call to vCD fails.
// API Documentation: https://code.vmware.com/apis/220/vcloud#/doc/doc/operations/POST-DisableOrg.html
func (adminOrg *AdminOrg) Disable() error {
orgHREF, err := url.ParseRequestURI(adminOrg.AdminOrg.HREF)
if err != nil {
return fmt.Errorf("error getting AdminOrg HREF %s : %s", adminOrg.AdminOrg.HREF, err)
}
orgHREF.Path += "/action/disable"
return adminOrg.client.ExecuteRequestWithoutResponse(orgHREF.String(), http.MethodPost, "", "error disabling organization: %s", nil)
}
// Updates the Org definition from current org struct contents.
// Any differences that may be legally applied will be updated.
// Returns an error if the call to vCD fails.
// API Documentation: https://code.vmware.com/apis/220/vcloud#/doc/doc/operations/PUT-Organization.html
func (adminOrg *AdminOrg) Update() (Task, error) {
vcomp := &types.AdminOrg{
Xmlns: types.XMLNamespaceVCloud,
Name: adminOrg.AdminOrg.Name,
IsEnabled: adminOrg.AdminOrg.IsEnabled,
FullName: adminOrg.AdminOrg.FullName,
Description: adminOrg.AdminOrg.Description,
OrgSettings: adminOrg.AdminOrg.OrgSettings,
}
// Same workaround used in Org creation, where OrgGeneralSettings properties
// are not set unless UseServerBootSequence is also set
if vcomp.OrgSettings.OrgGeneralSettings != nil {
vcomp.OrgSettings.OrgGeneralSettings.UseServerBootSequence = true
}
// Return the task
return adminOrg.client.ExecuteTaskRequest(adminOrg.AdminOrg.HREF, http.MethodPut,
"application/vnd.vmware.admin.organization+xml", "error updating Org: %s", vcomp)
}
// Undeploys every vapp within an organization
func (adminOrg *AdminOrg) undeployAllVApps() error {
for _, vdcs := range adminOrg.AdminOrg.Vdcs.Vdcs {
adminVdcHREF, err := url.Parse(vdcs.HREF)
if err != nil {
return err
}
vdc, err := adminOrg.getVdcByAdminHREF(adminVdcHREF)
if err != nil {
return fmt.Errorf("error retrieving vapp with url: %s and with error %s", adminVdcHREF.Path, err)
}
err = vdc.undeployAllVdcVApps()
if err != nil {
return fmt.Errorf("error deleting vapp: %s", err)
}
}
return nil
}
// Deletes every vapp within an organization
func (adminOrg *AdminOrg) removeAllVApps() error {
for _, vdcs := range adminOrg.AdminOrg.Vdcs.Vdcs {
adminVdcHREF, err := url.Parse(vdcs.HREF)
if err != nil {
return err
}
vdc, err := adminOrg.getVdcByAdminHREF(adminVdcHREF)
if err != nil {
return fmt.Errorf("error retrieving vapp with url: %s and with error %s", adminVdcHREF.Path, err)
}
err = vdc.removeAllVdcVApps()
if err != nil {
return fmt.Errorf("error deleting vapp: %s", err)
}
}
return nil
}
// Given an adminorg with a valid HREF, the function refetches the adminorg
// and updates the user's adminorg data. Otherwise if the function fails,
// it returns an error. Users should use refresh whenever they have
// a stale org due to the creation/update/deletion of a resource
// within the org or the org itself.
func (adminOrg *AdminOrg) Refresh() error {
if *adminOrg == (AdminOrg{}) {
return fmt.Errorf("cannot refresh, Object is empty")
}
// Empty struct before a new unmarshal, otherwise we end up with duplicate
// elements in slices.
unmarshalledAdminOrg := &types.AdminOrg{}
_, err := adminOrg.client.ExecuteRequest(adminOrg.AdminOrg.HREF, http.MethodGet,
"", "error refreshing organization: %s", nil, unmarshalledAdminOrg)
if err != nil {
return err
}
adminOrg.AdminOrg = unmarshalledAdminOrg
return nil
}
// Gets a vdc within org associated with an admin vdc url
func (adminOrg *AdminOrg) getVdcByAdminHREF(adminVdcUrl *url.URL) (*Vdc, error) {
// get non admin vdc path
vdcURL := adminOrg.client.VCDHREF
vdcURL.Path += strings.Split(adminVdcUrl.Path, "/api/admin")[1] //gets id
vdc := NewVdc(adminOrg.client)
_, err := adminOrg.client.ExecuteRequest(vdcURL.String(), http.MethodGet,
"", "error retrieving vdc: %s", nil, vdc.Vdc)
return vdc, err
}
// Removes all vdcs in a org
func (adminOrg *AdminOrg) removeAllOrgVDCs() error {
for _, vdcs := range adminOrg.AdminOrg.Vdcs.Vdcs {
adminVdcUrl := adminOrg.client.VCDHREF
splitVdcId := strings.Split(vdcs.HREF, "/api/vdc/")
if len(splitVdcId) == 1 {
adminVdcUrl.Path += "/admin/vdc/" + strings.Split(vdcs.HREF, "/api/admin/vdc/")[1] + "/action/disable"
} else {
adminVdcUrl.Path += "/admin/vdc/" + splitVdcId[1] + "/action/disable"
}
req := adminOrg.client.NewRequest(map[string]string{}, http.MethodPost, adminVdcUrl, nil)
_, err := checkResp(adminOrg.client.Http.Do(req))
if err != nil {
return fmt.Errorf("error disabling vdc: %s", err)
}
// Get admin vdc HREF for normal deletion
adminVdcUrl.Path = strings.Split(adminVdcUrl.Path, "/action/disable")[0]
req = adminOrg.client.NewRequest(map[string]string{
"recursive": "true",
"force": "true",
}, http.MethodDelete, adminVdcUrl, nil)
resp, err := checkResp(adminOrg.client.Http.Do(req))
if err != nil {
return fmt.Errorf("error deleting vdc: %s", err)
}
task := NewTask(adminOrg.client)
if err = decodeBody(types.BodyTypeXML, resp, task.Task); err != nil {
return fmt.Errorf("error decoding task response: %s", err)
}
if task.Task.Status == "error" {
return fmt.Errorf("vdc not properly destroyed")
}
err = task.WaitTaskCompletion()
if err != nil {
return fmt.Errorf("couldn't finish removing vdc %s", err)
}
}
return nil
}
// Removes All networks in the org
func (adminOrg *AdminOrg) removeAllOrgNetworks() error {
for _, networks := range adminOrg.AdminOrg.Networks.Networks {
// Get Network HREF
networkHREF := adminOrg.client.VCDHREF
networkHREF.Path += "/admin/network/" + strings.Split(networks.HREF, "/api/admin/network/")[1] //gets id
task, err := adminOrg.client.ExecuteTaskRequest(networkHREF.String(), http.MethodDelete,
"", "error deleting network: %s", nil)
if err != nil {
return err
}
if task.Task.Status == "error" {
return fmt.Errorf("network not properly destroyed")
}
err = task.WaitTaskCompletion()
if err != nil {
return fmt.Errorf("couldn't finish removing network %s", err)
}
}
return nil
}
// removeCatalogs force removal of all organization catalogs
func (adminOrg *AdminOrg) removeCatalogs() error {
for _, catalog := range adminOrg.AdminOrg.Catalogs.Catalog {
isCatalogFromSameOrg, err := isCatalogFromSameOrg(adminOrg, catalog.Name)
if err != nil {
return fmt.Errorf("error deleting catalog: %s", err)
}
if isCatalogFromSameOrg {
// Get Catalog HREF
catalogHREF := adminOrg.client.VCDHREF
catalogHREF.Path += "/admin/catalog/" + strings.Split(catalog.HREF, "/api/admin/catalog/")[1] //gets id
req := adminOrg.client.NewRequest(map[string]string{
"force": "true",
"recursive": "true",
}, http.MethodDelete, catalogHREF, nil)
_, err := checkResp(adminOrg.client.Http.Do(req))
if err != nil {
return fmt.Errorf("error deleting catalog: %s, %s", err, catalogHREF.Path)
}
}
}
return nil
}
// isCatalogFromSameOrg checks if catalog is in same Org. Shared catalogs from other Org are showed as normal one
// in some API responses.
func isCatalogFromSameOrg(adminOrg *AdminOrg, catalogName string) (bool, error) {
foundCatalogs, err := adminOrg.FindAdminCatalogRecords(catalogName)
if err != nil {
return false, err
}
if len(foundCatalogs) == 1 {
return true, nil
}
return false, nil
}
// FindAdminCatalogRecords uses catalog name to return AdminCatalogRecord information.
func (adminOrg *AdminOrg) FindAdminCatalogRecords(name string) ([]*types.CatalogRecord, error) {
util.Logger.Printf("[DEBUG] FindAdminCatalogRecords with name: %s and org name: %s", name, adminOrg.AdminOrg.Name)
results, err := adminOrg.client.QueryWithNotEncodedParams(nil, map[string]string{
"type": "adminCatalog",
"filter": fmt.Sprintf("name==%s;orgName==%s", url.QueryEscape(name), url.QueryEscape(adminOrg.AdminOrg.Name)),
"filterEncoded": "true",
})
if err != nil {
return nil, err
}
util.Logger.Printf("[DEBUG] FindAdminCatalogRecords returned with : %#v and error: %s", results.Results.AdminCatalogRecord, err)
return results.Results.AdminCatalogRecord, nil
}
// Given a valid catalog name, FindAdminCatalog returns an AdminCatalog object.
// If no catalog is found, then returns an empty AdminCatalog and no error.
// Otherwise it returns an error. Function allows user to use an AdminOrg
// to also fetch a Catalog. If user does not have proper credentials to
// perform administrator tasks then function returns an error.
// API Documentation: https://code.vmware.com/apis/220/vcloud#/doc/doc/operations/GET-Catalog-AdminView.html
// Deprecated: Use adminOrg.GetAdminCatalog instead
func (adminOrg *AdminOrg) FindAdminCatalog(catalogName string) (AdminCatalog, error) {
for _, catalog := range adminOrg.AdminOrg.Catalogs.Catalog {
// Get Catalog HREF
if catalog.Name == catalogName {
adminCatalog := NewAdminCatalog(adminOrg.client)
_, err := adminOrg.client.ExecuteRequest(catalog.HREF, http.MethodGet,
"", "error retrieving catalog: %s", nil, adminCatalog.AdminCatalog)
// The request was successful
return *adminCatalog, err
}
}
return AdminCatalog{}, nil
}
// Given a valid catalog name, FindCatalog returns a Catalog object.
// If no catalog is found, then returns an empty catalog and no error.
// Otherwise it returns an error. Function allows user to use an AdminOrg
// to also fetch a Catalog.
// Deprecated: Use adminOrg.GetCatalogByName instead
func (adminOrg *AdminOrg) FindCatalog(catalogName string) (Catalog, error) {
for _, catalog := range adminOrg.AdminOrg.Catalogs.Catalog {
// Get Catalog HREF
if catalog.Name == catalogName {
catalogURL := adminOrg.client.VCDHREF
catalogURL.Path += "/catalog/" + strings.Split(catalog.HREF, "/api/admin/catalog/")[1] //gets id
cat := NewCatalog(adminOrg.client)
_, err := adminOrg.client.ExecuteRequest(catalogURL.String(), http.MethodGet,
"", "error retrieving catalog: %s", nil, cat.Catalog)
// The request was successful
return *cat, err
}
}
return Catalog{}, nil
}
// GetCatalogByHref finds a Catalog by HREF
// On success, returns a pointer to the Catalog structure and a nil error
// On failure, returns a nil pointer and an error
func (adminOrg *AdminOrg) GetCatalogByHref(catalogHref string) (*Catalog, error) {
splitByAdminHREF := strings.Split(catalogHref, "/api/admin")
// admin user and normal user will have different urls
var catalogHREF string
if len(splitByAdminHREF) == 1 {
catalogHREF = catalogHref
} else {
catalogHREF = splitByAdminHREF[0] + "/api" + splitByAdminHREF[1]
}
cat := NewCatalog(adminOrg.client)
_, err := adminOrg.client.ExecuteRequest(catalogHREF, http.MethodGet,
"", "error retrieving catalog: %s", nil, cat.Catalog)
if err != nil {
return nil, err
}
// The request was successful
return cat, nil
}
// GetCatalogByName finds a Catalog by Name
// On success, returns a pointer to the Catalog structure and a nil error
// On failure, returns a nil pointer and an error
func (adminOrg *AdminOrg) GetCatalogByName(catalogName string, refresh bool) (*Catalog, error) {
if refresh {
err := adminOrg.Refresh()
if err != nil {
return nil, err
}
}
for _, catalog := range adminOrg.AdminOrg.Catalogs.Catalog {
if catalog.Name == catalogName {
return adminOrg.GetCatalogByHref(catalog.HREF)
}
}
return nil, ErrorEntityNotFound
}
// Extracts an UUID from a string, regardless of surrounding text
// Returns an empty string if no UUID was found
func extractUuid(input string) string {
reGetID := regexp.MustCompile(`([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})`)
matchListId := reGetID.FindAllStringSubmatch(input, -1)
if len(matchListId) > 0 && len(matchListId[0]) > 0 {
return matchListId[0][1]
}
return ""
}
// equalIds compares two IDs and return true if they are the same
// The comparison happens by extracting the bare UUID from both the
// wanted ID and the found one.
// When the found ID is empty, it used the HREF for such comparison,
// This function is useful when the reference structure in the parent lookup list
// may lack the ID (such as in Org.Links, AdminOrg.Catalogs) or has an ID
// that is only a UUID without prefixes (such as in CatalogItem list)
//
// wantedId is the input string to compare
// foundId is the ID field in the reference record (can be empty)
// foundHref is the HREF field in the reference record (should never be empty)
func equalIds(wantedId, foundId, foundHref string) bool {
wantedUuid := extractUuid(wantedId)
foundUuid := ""
if wantedUuid == "" {
return false
}
if foundId != "" {
// In some entities, the ID is a simple UUID without prefix
foundUuid = extractUuid(foundId)
} else {
foundUuid = extractUuid(foundHref)
}
return foundUuid == wantedUuid
}
// GetCatalogById finds a Catalog by ID
// On success, returns a pointer to the Catalog structure and a nil error
// On failure, returns a nil pointer and an error
func (adminOrg *AdminOrg) GetCatalogById(catalogId string, refresh bool) (*Catalog, error) {
if refresh {
err := adminOrg.Refresh()
if err != nil {
return nil, err
}
}
for _, catalog := range adminOrg.AdminOrg.Catalogs.Catalog {
if equalIds(catalogId, catalog.ID, catalog.HREF) {
return adminOrg.GetCatalogByHref(catalog.HREF)
}
}
return nil, ErrorEntityNotFound
}
// GetCatalogByNameOrId finds a Catalog by name or ID
// On success, returns a pointer to the Catalog structure and a nil error
// On failure, returns a nil pointer and an error
func (adminOrg *AdminOrg) GetCatalogByNameOrId(identifier string, refresh bool) (*Catalog, error) {
getByName := func(name string, refresh bool) (interface{}, error) { return adminOrg.GetCatalogByName(name, refresh) }
getById := func(id string, refresh bool) (interface{}, error) { return adminOrg.GetCatalogById(id, refresh) }
entity, err := getEntityByNameOrId(getByName, getById, identifier, refresh)
if entity == nil {
return nil, err
}
return entity.(*Catalog), err
}
// GetAdminCatalogByHref finds an AdminCatalog by HREF
// On success, returns a pointer to the Catalog structure and a nil error
// On failure, returns a nil pointer and an error
func (adminOrg *AdminOrg) GetAdminCatalogByHref(catalogHref string) (*AdminCatalog, error) {
adminCatalog := NewAdminCatalog(adminOrg.client)
_, err := adminOrg.client.ExecuteRequest(catalogHref, http.MethodGet,
"", "error retrieving catalog: %s", nil, adminCatalog.AdminCatalog)
if err != nil {
return nil, err
}
// The request was successful
return adminCatalog, nil
}
// GetCatalogByName finds an AdminCatalog by Name
// On success, returns a pointer to the AdminCatalog structure and a nil error
// On failure, returns a nil pointer and an error
func (adminOrg *AdminOrg) GetAdminCatalogByName(catalogName string, refresh bool) (*AdminCatalog, error) {
if refresh {
err := adminOrg.Refresh()
if err != nil {
return nil, err
}
}
for _, catalog := range adminOrg.AdminOrg.Catalogs.Catalog {
// Get Catalog HREF
if catalog.Name == catalogName {
return adminOrg.GetAdminCatalogByHref(catalog.HREF)
}
}
return nil, ErrorEntityNotFound
}
// GetCatalogById finds an AdminCatalog by ID
// On success, returns a pointer to the AdminCatalog structure and a nil error
// On failure, returns a nil pointer and an error
func (adminOrg *AdminOrg) GetAdminCatalogById(catalogId string, refresh bool) (*AdminCatalog, error) {
if refresh {
err := adminOrg.Refresh()
if err != nil {
return nil, err
}
}
for _, catalog := range adminOrg.AdminOrg.Catalogs.Catalog {
// Get Catalog HREF
if equalIds(catalogId, catalog.ID, catalog.HREF) {
return adminOrg.GetAdminCatalogByHref(catalog.HREF)
}
}
return nil, ErrorEntityNotFound
}
// GetAdminCatalogByNameOrId finds an AdminCatalog by name or ID
// On success, returns a pointer to the AdminCatalog structure and a nil error
// On failure, returns a nil pointer and an error
func (adminOrg *AdminOrg) GetAdminCatalogByNameOrId(identifier string, refresh bool) (*AdminCatalog, error) {
getByName := func(name string, refresh bool) (interface{}, error) {
return adminOrg.GetAdminCatalogByName(name, refresh)
}
getById := func(id string, refresh bool) (interface{}, error) {
return adminOrg.GetAdminCatalogById(id, refresh)
}
entity, err := getEntityByNameOrId(getByName, getById, identifier, refresh)
if entity == nil {
return nil, err
}
return entity.(*AdminCatalog), err
}
// GetVDCByHref retrieves a VDC using a direct call with the HREF
func (adminOrg *AdminOrg) GetVDCByHref(vdcHref string) (*Vdc, error) {
splitByAdminHREF := strings.Split(vdcHref, "/api/admin")
// admin user and normal user will have different urls
var vdcHREF string
if len(splitByAdminHREF) == 1 {
vdcHREF = vdcHref
} else {
vdcHREF = splitByAdminHREF[0] + "/api" + splitByAdminHREF[1]
}
vdc := NewVdc(adminOrg.client)
_, err := adminOrg.client.ExecuteRequest(vdcHREF, http.MethodGet,
"", "error getting vdc: %s", nil, vdc.Vdc)
if err != nil {
return nil, err
}
return vdc, nil
}
// GetVDCByName finds a VDC by Name
// On success, returns a pointer to the Vdc structure and a nil error
// On failure, returns a nil pointer and an error
func (adminOrg *AdminOrg) GetVDCByName(vdcName string, refresh bool) (*Vdc, error) {
if refresh {
err := adminOrg.Refresh()
if err != nil {
return nil, err
}
}
for _, vdc := range adminOrg.AdminOrg.Vdcs.Vdcs {
if vdc.Name == vdcName {
return adminOrg.GetVDCByHref(vdc.HREF)
}
}
return nil, ErrorEntityNotFound
}
// GetVDCById finds a VDC by ID
// On success, returns a pointer to the Vdc structure and a nil error
// On failure, returns a nil pointer and an error
func (adminOrg *AdminOrg) GetVDCById(vdcId string, refresh bool) (*Vdc, error) {
if refresh {
err := adminOrg.Refresh()
if err != nil {
return nil, err
}
}
for _, vdc := range adminOrg.AdminOrg.Vdcs.Vdcs {
if equalIds(vdcId, vdc.ID, vdc.HREF) {
return adminOrg.GetVDCByHref(vdc.HREF)
}
}
return nil, ErrorEntityNotFound
}
// GetVDCByNameOrId finds a VDC by name or ID
// On success, returns a pointer to the VDC structure and a nil error
// On failure, returns a nil pointer and an error
func (adminOrg *AdminOrg) GetVDCByNameOrId(identifier string, refresh bool) (*Vdc, error) {
getByName := func(name string, refresh bool) (interface{}, error) { return adminOrg.GetVDCByName(name, refresh) }
getById := func(id string, refresh bool) (interface{}, error) { return adminOrg.GetVDCById(id, refresh) }
entity, err := getEntityByNameOrId(getByName, getById, identifier, refresh)
if entity == nil {
return nil, err
}
return entity.(*Vdc), err
}
// If user specifies valid vdc name then this returns a vdc object.
// If no vdc is found, then it returns an empty vdc and no error.
// Otherwise it returns an empty vdc and an error. This function
// allows users to use an AdminOrg to fetch a vdc as well.
// Deprecated: Use adminOrg.GetVDCByName instead
func (adminOrg *AdminOrg) GetVdcByName(vdcname string) (Vdc, error) {
for _, vdcs := range adminOrg.AdminOrg.Vdcs.Vdcs {
if vdcs.Name == vdcname {
splitByAdminHREF := strings.Split(vdcs.HREF, "/api/admin")
// admin user and normal user will have different urls
var vdcHREF string
if len(splitByAdminHREF) == 1 {
vdcHREF = vdcs.HREF
} else {
vdcHREF = splitByAdminHREF[0] + "/api" + splitByAdminHREF[1]
}
vdc := NewVdc(adminOrg.client)
_, err := adminOrg.client.ExecuteRequest(vdcHREF, http.MethodGet,
"", "error getting vdc: %s", nil, vdc.Vdc)
return *vdc, err
}
}
return Vdc{}, nil
}
// QueryCatalogList returns a list of catalogs for this organization
func (adminOrg *AdminOrg) QueryCatalogList() ([]*types.CatalogRecord, error) {
util.Logger.Printf("[DEBUG] QueryCatalogList with org name %s", adminOrg.AdminOrg.Name)
queryType := types.QtCatalog
if adminOrg.client.IsSysAdmin {
queryType = types.QtAdminCatalog
}
results, err := adminOrg.client.cumulativeQuery(queryType, nil, map[string]string{
"type": queryType,
"filter": fmt.Sprintf("orgName==%s", url.QueryEscape(adminOrg.AdminOrg.Name)),
"filterEncoded": "true",
})
if err != nil {
return nil, err
}
var catalogs []*types.CatalogRecord
if adminOrg.client.IsSysAdmin {
catalogs = results.Results.AdminCatalogRecord
} else {
catalogs = results.Results.CatalogRecord
}
util.Logger.Printf("[DEBUG] QueryCatalogList returned with : %#v and error: %s", catalogs, err)
return catalogs, nil
}