-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.go
1433 lines (1160 loc) · 36 KB
/
api.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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package app
// Copyright 2020-2021 Author.
//
// 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.
//
//
// Mustafa mbayramo@vmware.com
package api
import (
"context"
"fmt"
"github.com/golang/glog"
"github.com/google/uuid"
"github.com/spyroot/tcactl/lib/api_errors"
"github.com/spyroot/tcactl/lib/client"
"github.com/spyroot/tcactl/lib/client/response"
"github.com/spyroot/tcactl/lib/client/specs"
"github.com/spyroot/tcactl/lib/models"
"strings"
"time"
)
type VmTemplateFilterType string
const (
//VmwareTemplateK8s default filter for k8s templates
VmwareTemplateK8s VmTemplateFilterType = "k8svm"
// DefaultNamespace - default name space used for placement
DefaultNamespace = "default"
// DefaultFlavor default vdu flavor
DefaultFlavor = "default"
)
// TcaApi - TCA Api interface
// Called need to use NewTcaApi to get instance before
type TcaApi struct {
// rest client used to interact with tca
rest *client.RestClient
}
// NewTcaApi - return instance for API.
func NewTcaApi(r *client.RestClient) (*TcaApi, error) {
if r == nil {
return nil, fmt.Errorf("rest client is nil, initilize rest client first")
}
a := &TcaApi{
rest: r,
}
return a, nil
}
// CloudProviderNotFound error raised if tenant cloud not found
type CloudProviderNotFound struct {
errMsg string
}
// Error - return if cloud provider not found
func (m *CloudProviderNotFound) Error() string {
return m.errMsg + " cloud provider not found"
}
// UnsupportedCloudProvider error raised if tenant cloud not found
type UnsupportedCloudProvider struct {
errMsg string
}
//
func (m *UnsupportedCloudProvider) Error() string {
return m.errMsg + " cloud provider not supported"
}
// GetAllNodePool return a Node pool for particular cluster
// It generally useful to get list only if we need to display all
// in all other cases it efficient to use direct call for cluster.
func (a *TcaApi) GetAllNodePool(ctx context.Context) (*response.NodePool, error) {
clusters, err := a.rest.GetClusters(ctx)
if err != nil {
return nil, err
}
var pools response.NodePool
// get all nodes pools
for _, cluster := range clusters.Clusters {
glog.Infof("Retrieving pool for a cluster name: '%v' uuid: '%v' state '%v'",
cluster.ClusterName, cluster.Id, cluster.Status)
// if cluster in failed state we have no pool.
if len(cluster.Id) == 0 {
glog.Infof("SpecCluster id empty value")
continue
}
clusterPool, poolErr := a.rest.GetClusterNodePools(cluster.Id)
if poolErr != nil {
glog.Error(err)
return nil, err
}
if clusterPool != nil {
glog.Infof("Got pool ids '%v'", clusterPool.GetIds())
spec := clusterPool.Pools
pools.Pools = append(pools.Pools, spec...)
} else {
glog.Error("Node pool is nil")
}
}
return &pools, nil
}
// GetVimTenants method return vim tenants as response.Tenants
// collection.
func (a *TcaApi) GetVimTenants(ctx context.Context) (*response.Tenants, error) {
if a.rest == nil {
return nil, fmt.Errorf("rest interface is nil")
}
return a.rest.GetVimTenants(ctx)
}
// GetCurrentClusterTask get current cluster task
func (a *TcaApi) GetCurrentClusterTask(ctx context.Context, clusterId string) (*models.ClusterTask, error) {
if len(clusterId) == 0 {
return nil, api_errors.NewInvalidTaskId(clusterId)
}
if !IsValidUUID(clusterId) == false {
return nil, api_errors.NewInvalidTaskId(clusterId)
}
clusters, err := a.rest.GetClusters(ctx)
if err != nil {
return nil, err
}
cid, err := clusters.GetClusterId(clusterId)
if err != nil {
return nil, err
}
glog.Infof("Retrieving current task task list for cluster '%v'", cid)
task, err := a.rest.GetClustersTask(ctx, specs.NewClusterTaskQuery(clusterId))
if err != nil {
return nil, err
}
return task, nil
}
// GetVimVMTemplates - return compute cluster attached to cloud provider.
// caller need indicate template type and version.
func (a *TcaApi) GetVimVMTemplates(ctx context.Context, cloudName string,
templateType VmTemplateFilterType, ver string) (*models.VcInventory, error) {
if len(cloudName) == 0 {
return nil, fmt.Errorf("empty cloud provider name")
}
tenants, err := a.rest.GetVimTenants(ctx)
if err != nil {
return nil, err
}
tenant, err := tenants.FindCloudProvider(cloudName)
if err != nil {
return nil, err
}
glog.Infof("Retrieving vm template list for cloud provider "+
"uuid %s url %s", tenant.HcxUUID, tenant.VimURL)
if len(tenant.HcxUUID) == 0 {
return nil, fmt.Errorf("cloud provider hcx uuid is empty string")
}
if !tenant.IsVMware() {
glog.Errorf("unsupported vim")
return nil, &UnsupportedCloudProvider{errMsg: cloudName}
}
_filter := specs.NewVMwareTemplateQuery(tenant.HcxUUID, string(templateType), ver)
t, err := a.rest.GetVMwareTemplates(ctx, _filter)
if err != nil {
return nil, err
}
return t, nil
}
// GetVimFolders - return folders in target cloud provider.
// for VMware VC it list of VM Folders, models.Folders
func (a *TcaApi) GetVimFolders(ctx context.Context, cloudName string) (*models.Folders, error) {
if a.rest == nil {
return nil, fmt.Errorf("rest interface is nil")
}
if len(cloudName) == 0 {
return nil, fmt.Errorf("empty cloud provider name")
}
tenants, err := a.rest.GetVimTenants(ctx)
if err != nil {
return nil, err
}
tenant, err := tenants.FindCloudProvider(cloudName)
if err != nil {
return nil, err
}
glog.Infof("Retrieving vim folders list for cloud provider '%v' , '%v'",
tenant.HcxUUID, tenant.VimURL)
if len(tenant.HcxUUID) == 0 {
return nil, fmt.Errorf("cloud provider hcx uuid is empty string")
}
if !tenant.IsVMware() {
return nil, &UnsupportedCloudProvider{errMsg: cloudName}
}
f := specs.NewVmwareFolderQuery(tenant.HcxUUID)
if f == nil {
return nil, fmt.Errorf("failed create folder filter")
}
t, err := a.rest.GetVMwareFolders(f)
if err != nil {
return nil, err
}
return t, nil
}
// GetVimResourcePool - return resource pool in target VIM.
// caller need indicate template type and version.
func (a *TcaApi) GetVimResourcePool(ctx context.Context, cloudName string) (*models.ResourcePool, error) {
if a.rest == nil {
return nil, fmt.Errorf("rest interface is nil")
}
tenants, err := a.rest.GetVimTenants(ctx)
if err != nil {
return nil, err
}
tenant, err := tenants.FindCloudProvider(cloudName)
if err != nil {
return nil, err
}
glog.Infof("Retrieving network list for cloud provider %v %v", tenant.HcxUUID, tenant.VimURL)
if len(tenant.HcxUUID) == 0 {
return nil, fmt.Errorf("cloud provider is empty")
}
if !tenant.IsVMware() {
return nil, &UnsupportedCloudProvider{errMsg: cloudName}
}
f := specs.NewVMwareResourcePoolQuery(tenant.HcxUUID)
if f == nil {
return nil, fmt.Errorf("failed create folder filter")
}
t, err := a.rest.GetVMwareResourcePool(ctx, f)
if err != nil {
return nil, err
}
return t, nil
}
// validateKubeSpecCheck validate worker and master sub section
// of specs
func validateKubeSpecCheck(spec []models.TypeNode) error {
for _, masterSpec := range spec {
if len(masterSpec.Name) == 0 {
return fmt.Errorf("cluster initialSpec master and worker must contains name")
}
if len(masterSpec.PlacementParams) == 0 {
return fmt.Errorf("cluster initialSpec master and worker " +
"must contains PlacementParams section and each type/name key value")
}
if len(masterSpec.Networks) == 0 {
return fmt.Errorf("cluster initialSpec master " +
"and worker must contains Networks section")
}
for _, network := range masterSpec.Networks {
if len(network.NetworkName) == 0 {
return fmt.Errorf("cluster initialSpec master and worker " +
"each network must contains network name name, key (networkName)")
}
if len(network.Label) == 0 {
return fmt.Errorf("masterNodes each network " +
"must contains network label name, key (label)")
}
}
}
return nil
}
func validateClusterSpec(spec *specs.SpecCluster) error {
if len(spec.Name) == 0 {
return api_errors.NewInvalidSpec("cluster initialSpec must contains name key:value")
}
if len(spec.ClusterPassword) == 0 {
return api_errors.NewInvalidSpec("cluster initialSpec must contains cluster password key:value")
}
if len(spec.ClusterTemplateId) == 0 {
return api_errors.NewInvalidSpec("cluster initialSpec must contains ClusterTemplateId key:value")
}
if len(spec.ClusterType) == 0 {
return api_errors.NewInvalidSpec("cluster initialSpec must contains clusterType and value management or workload key:value")
}
if len(spec.HcxCloudUrl) == 0 {
return api_errors.NewInvalidSpec("cluster initialSpec must contains hcxCloudUrl key:value")
}
if len(spec.EndpointIP) == 0 {
return api_errors.NewInvalidSpec("cluster initialSpec must contains EndpointIP key:value")
}
if len(spec.VmTemplate) == 0 {
return api_errors.NewInvalidSpec("cluster initialSpec must contains vmTemplate key:value")
}
if len(spec.VmTemplate) == 0 {
return api_errors.NewInvalidSpec("cluster initialSpec must contains vmTemplate key:value")
}
if len(spec.MasterNodes) == 0 {
return api_errors.NewInvalidSpec("cluster initialSpec must contains masterNodes section")
}
if len(spec.WorkerNodes) == 0 {
return api_errors.NewInvalidSpec("cluster initialSpec must contains WorkerNodes section")
}
if len(spec.PlacementParams) == 0 {
return api_errors.NewInvalidSpec("cluster initialSpec must contains PlacementParams section")
}
if err := validateKubeSpecCheck(spec.MasterNodes); err != nil {
return err
}
if err := validateKubeSpecCheck(spec.WorkerNodes); err != nil {
return err
}
if spec.ClusterType == string(specs.ClusterWorkload) {
if len(spec.ManagementClusterId) == 0 {
return fmt.Errorf("workload cluster must contain ManagementClusterId key value")
}
}
return nil
}
// NormalizeTemplateId - resolve template name to id
// for a give template type. Both must match in order
// method return ture
func (a *TcaApi) NormalizeTemplateId(IdOrName string, templateType string) (string, error) {
if len(IdOrName) == 0 {
return "", api_errors.NewTemplateNotFound(IdOrName)
}
if len(templateType) == 0 {
return "", api_errors.NewTemplateInvalidType("template type is empty")
}
glog.Infof("Resolving cluster template '%s' to id", IdOrName)
// resolve template id, in case client used name instead id
clusterTemplates, err := a.rest.GetClusterTemplates()
if err != nil {
return "", err
}
t, err := clusterTemplates.GetTemplate(IdOrName)
if err != nil {
return "", err
}
glog.Infof("Resolved cluster template '%s' to template id %s", IdOrName, t.Id)
// check template type
if strings.ToLower(t.ClusterType) != strings.ToLower(templateType) {
return "", fmt.Errorf("found template %s but template type mistmatch, "+
"template type %v %v", IdOrName, t.ClusterType, templateType)
}
glog.Infof("Resolved template to template id %v", t.Id)
return t.Id, nil
}
// doCheckCloudEndpoint
func (a *TcaApi) validateCloudEndpoint(ctx context.Context, cloud string) (*response.TenantsDetails, error) {
// resolve template id, in case client used name instead id
vimTenants, err := a.rest.GetVimTenants(ctx)
if err != nil {
return nil, err
}
if strings.HasPrefix(cloud, "https://") {
tenant, err := vimTenants.FindCloudLink(cloud)
if err != nil {
return nil, err
}
return tenant, nil
}
t, err := vimTenants.FindCloudProvider(cloud)
if err != nil {
return nil, err
}
return t, nil
}
// Validate cloud tenant state
func (a *TcaApi) validateTenant(tenant *response.TenantsDetails) error {
glog.Infof("Validating cloud and tenant state.")
if tenant.VimConn.Status != "ok" {
return fmt.Errorf("cloud provider currently disconected")
}
if strings.ToLower(tenant.VimType) == models.VimTypeKubernetes {
return fmt.Errorf("cloud provider already set to kubernetes")
}
return nil
}
// validatePlacement validate placement cluster placement attributes
func (a *TcaApi) validatePlacement(vim *VmwareVim,
param models.PlacementParams) error {
if models.VmwareTypeFolder(param.Type) == models.TypeFolder {
glog.Infof("Validating vm folder '%s'", param.Name)
if vim.folders.IsValidFolder(param.Name) == false {
return api_errors.NewInvalidSpec("failed find a target folder")
} else {
glog.Infof("Resolved remote datastore folder.")
}
}
if models.VmwareDatastore(param.Type) == models.TypeDataStore {
glog.Infof("Validating datastore '%s'", param.Name)
if vim.clusters.IsValidDatastore(param.Name) == false {
return api_errors.NewInvalidSpec("failed find a target datastore")
} else {
glog.Infof("Resolved remote datastore name.")
}
}
if models.VmwareResourcePool(param.Type) == models.TypeResourcePool {
glog.Infof("Validating resource pool '%s'", param.Name)
if vim.resourcePool.IsValidResource(param.Name) == false {
return api_errors.NewInvalidSpec("failed find a target resource pool")
} else {
glog.Infof("Resolved remote resource pool.")
}
}
if models.ClusterComputeResource(param.Type) == models.TypeClusterComputeResource {
glog.Infof("Validating compute cluster '%s'", param.Name)
if vim.clusters.IsValidClusterCompute(param.Name) == false {
return api_errors.NewInvalidSpec("failed find a cluster compute resource")
} else {
glog.Infof("Resolved remote cluster name.")
}
}
return nil
}
// normalizeDatastoreName adjust store name
func normalizeDatastoreName(ds string) string {
fixedUrl := ""
if ds == "ds:///" {
fixedUrl = ds
} else {
if strings.HasPrefix(ds, "/vmfs") {
fixedUrl = "ds:///" + ds
}
if strings.HasPrefix(ds, "vmfs") {
fixedUrl = "ds:///" + ds
}
}
// fixed '/' at the end
if len(fixedUrl) > 0 && !strings.HasSuffix(fixedUrl, "/") {
fixedUrl = fixedUrl + "/"
}
return fixedUrl
}
type VmwareVim struct {
clusters *models.VMwareClusters
folders *models.Folders
resourcePool *models.ResourcePool
networks *models.CloudNetworks
}
func (a *TcaApi) getVmwareVimState(ctx context.Context, vimName string) (*VmwareVim, error) {
// vc compute
var (
vimState VmwareVim
err error
)
vimState.clusters, err = a.GetVimComputeClusters(ctx, vimName)
if err != nil {
return nil, err
}
// vc folders
vimState.folders, err = a.GetVimFolders(ctx, vimName)
if err != nil {
return nil, err
}
// vc resource pools
vimState.resourcePool, err = a.GetVimResourcePool(ctx, vimName)
if err != nil {
return nil, err
}
vimState.networks, err = a.GetVimNetworks(ctx, vimName)
if err != nil {
return nil, err
}
return &vimState, nil
}
func (a *TcaApi) validateExtensions(ctx context.Context, spec *specs.SpecCluster) error {
repos, err := a.GetRepos(ctx)
if err != nil {
return err
}
if spec.ClusterConfig != nil {
for _, tool := range spec.ClusterConfig.Tools {
if tool.Name == "harbor" {
// adjust
if tool.Properties.Type != "extension" {
tool.Properties.Type = "extension"
}
extId, err := repos.GetRepoId(tool.Properties.ExtensionId)
if err != nil {
return err
}
tool.Properties.ExtensionId = extId
}
}
}
return nil
}
// validateVmwarePlacement method validate placement for VMware VIM
func (a *TcaApi) validateVmwarePlacement(ctx context.Context, spec *specs.SpecCluster, tenant *response.TenantsDetails) error {
vmwareVim, err := a.getVmwareVimState(ctx, tenant.VimName)
if err != nil {
return err
}
// check for VMware vsphere-csi
if spec.ClusterConfig != nil {
for _, s := range spec.ClusterConfig.Csi {
if s.Name == ClusterCsiVsphere {
if s.Properties == nil {
return api_errors.NewInvalidSpec("Invalid vsphere-csi property")
}
if !vmwareVim.clusters.IsValidDatastoreUrl(s.Properties.DatastoreUrl) {
return api_errors.NewInvalidSpec("Invalid vsphere-csi property")
}
ds, err := vmwareVim.clusters.GetDatastoreByUrl(s.Properties.DatastoreUrl)
if err != nil {
return api_errors.NewInvalidSpec(err.Error())
}
s.Properties.DatastoreName = ds.Name
}
}
}
// validate global specString
glog.Infof("Validating global specString placement parameters")
for _, param := range spec.PlacementParams {
if err := a.validatePlacement(vmwareVim, param); err != nil {
return api_errors.NewInvalidSpec(err.Error())
}
}
glog.Infof("Validating master node specString placement parameters")
for i, worker := range spec.WorkerNodes {
for j, n := range worker.Networks {
// normalize port-group name
normalized, err := vmwareVim.networks.NormalizeName(n.NetworkName)
if err != nil {
return api_errors.NewInvalidSpec(err.Error())
}
spec.WorkerNodes[i].Networks[j].NetworkName = normalized
}
for _, param := range worker.PlacementParams {
if err := a.validatePlacement(vmwareVim, param); err != nil {
return api_errors.NewInvalidSpec(err.Error())
}
}
}
glog.Infof("Validating worker node specString placement parameters")
for i, master := range spec.MasterNodes {
for j, n := range master.Networks {
// normalize port-group name
normalized, err := vmwareVim.networks.NormalizeName(n.NetworkName)
if err != nil {
return api_errors.NewInvalidSpec(err.Error())
}
spec.MasterNodes[i].Networks[j].NetworkName = normalized
}
for _, param := range master.PlacementParams {
if err := a.validatePlacement(vmwareVim, param); err != nil {
return api_errors.NewInvalidSpec(err.Error())
}
}
}
return nil
}
// validateCsi validate csi specString
func (a *TcaApi) validateCsi(spec *specs.SpecCluster) error {
if spec.ClusterConfig != nil {
for _, s := range spec.ClusterConfig.Csi {
if s.Name == CLusterCsiNfs {
if len(s.Properties.MountPath) == 0 {
return api_errors.NewInvalidSpec("Invalid nfs client mount path")
}
if len(s.Properties.ServerIP) == 0 {
return api_errors.NewInvalidSpec("Invalid nfs client server address")
}
}
}
}
return nil
}
// validateCsi validate csi specString
func (a *TcaApi) validateVim(spec *specs.SpecCluster, tenant *response.TenantsDetails) error {
if tenant == nil {
return fmt.Errorf("tenant vim is nil")
}
if len(tenant.VimType) == 0 {
return fmt.Errorf("unknown vim type")
}
if tenant.VimConn.Status != "ok" {
return fmt.Errorf("cloud provider currently disconected")
}
if spec.Name == tenant.Name {
spec.HcxCloudUrl = tenant.HcxCloudURL
} else {
return api_errors.NewInvalidSpec("Invalid cloud url, name")
}
return nil
}
// Validate cloud tenant state
func (a *TcaApi) validatePlacements(ctx context.Context, spec *specs.SpecCluster, tenant *response.TenantsDetails) error {
glog.Infof("Validate placement details.")
//if err := a.validateVim(specString, tenant); err != nil {
// return err
//}
if err := a.validateCsi(spec); err != nil {
return err
}
err := a.validateExtensions(ctx, spec)
if err != nil {
return err
}
if tenant.IsVMware() {
glog.Infof("Target cloud provider is VMware cluster, validating vc placements.")
err := a.validateVmwarePlacement(ctx, spec, tenant)
if err != nil {
return err
}
}
return nil
}
// DeleteTenantCluster - deletes tenant cluster
// it accept just name or id.
func (a *TcaApi) DeleteTenantCluster(ctx context.Context, tenantCluster string) (*models.TcaTask, error) {
if a.rest == nil {
return nil, fmt.Errorf("rest interface is nil")
}
vims, err := a.GetVims(ctx)
if err != nil {
return nil, err
}
clouds, err := vims.GetTenantClouds(tenantCluster, models.VimTypeKubernetes)
if err != nil {
return nil, err
}
return a.rest.DeleteTenant(clouds.TenantID)
}
// ResolveVim resolve vim name to id
func (a *TcaApi) ResolveVim(ctx context.Context, name string) (*response.TenantsDetails, error) {
vims, err := a.GetVims(ctx)
if err != nil {
return nil, err
}
provider, err := vims.FindCloudProvider(name)
if err != nil {
return nil, err
}
if provider == nil {
return nil, fmt.Errorf("nil vim")
}
return provider, nil
}
// ResolveVimName resolve name to id
func (a *TcaApi) ResolveVimName(ctx context.Context, name string) (string, error) {
vim, err := a.ResolveVim(ctx, name)
if err != nil {
return "", err
}
return vim.ID, nil
}
func (a *TcaApi) ResolveVimId(ctx context.Context, name string) (string, error) {
vim, err := a.ResolveVim(ctx, name)
if err != nil {
return "", err
}
return vim.VimID, nil
}
func (a *TcaApi) CreateSpecExample(
name string,
t string,
h string,
vm string,
ip string,
p string,
netpath string,
dns string,
) *specs.SpecCluster {
var spec specs.SpecCluster
spec.Name = name
spec.ClusterType = string(specs.ClusterManagement)
spec.ClusterTemplateId = t
spec.HcxCloudUrl = h
spec.VmTemplate = vm
spec.EndpointIP = ip
spec.PlacementParams = []models.PlacementParams{
*models.NewPlacementParams("templates", "Folder"),
*models.NewPlacementParams("vsanDatastore", "Datastore"),
*models.NewPlacementParams("pod03", "ResourcePool"),
*models.NewPlacementParams("core", "IsValidClusterCompute"),
}
spec.ClusterPassword = p
net := models.NewNetworks(string(specs.ClusterManagement),
netpath,
[]string{dns})
master := models.NewTypeNode("master", []models.Networks{*net}, []models.PlacementParams{
*models.NewPlacementParams("Discovered virtual machine", "Folder"),
*models.NewPlacementParams("vsanDatastore", "Datastore"),
*models.NewPlacementParams("pod03", "ResourcePool"),
*models.NewPlacementParams("core", "IsValidClusterCompute"),
})
worker := models.NewTypeNode("note-pool01", []models.Networks{*net}, []models.PlacementParams{
*models.NewPlacementParams("Discovered virtual machine", "Folder"),
*models.NewPlacementParams("vsanDatastore", "Datastore"),
*models.NewPlacementParams("pod", "ResourcePool"),
*models.NewPlacementParams("core", "IsValidClusterCompute"),
})
spec.MasterNodes = []models.TypeNode{*master}
spec.WorkerNodes = []models.TypeNode{*worker}
return &spec
}
// GetClusters get cluster
func (a *TcaApi) GetClusters(ctx context.Context) (*response.Clusters, error) {
if a.rest == nil {
return nil, fmt.Errorf("rest interface is nil")
}
glog.Infof("Retrieving cluster list.")
return a.rest.GetClusters(ctx)
}
// GetClusterNodePools - return node pool for a given id.
func (a *TcaApi) GetClusterNodePools(Id string) (*response.NodePool, error) {
if a.rest == nil {
return nil, fmt.Errorf("rest interface is nil")
}
glog.Infof("Retrieving cluster list.")
return a.rest.GetClusterNodePools(Id)
}
// GetAllNodePools - return all node pool for clusterId
func (a *TcaApi) GetAllNodePools(ctx context.Context) ([]response.NodesSpecs, error) {
glog.Infof("Retrieving node pools.")
var allSpecs []response.NodesSpecs
if a.rest == nil {
return allSpecs, fmt.Errorf("rest interface is nil")
}
clusters, err := a.GetClusters(ctx)
if err != nil {
return allSpecs, err
}
for _, c := range clusters.Clusters {
pools, err := a.GetClusterNodePools(c.Id)
if err != nil {
return allSpecs, err
}
for _, p := range pools.Pools {
r, err := a.rest.GetClusterNodePool(c.Id, p.Id)
if err != nil {
return allSpecs, err
}
allSpecs = append(allSpecs, *r)
}
}
return allSpecs, nil
}
// GetTenant method return tenant as response.Tenants
// if tenant is name, method will lookup by name.
// if tenant is UUID it will lookup by id
// if it has prefix vmware it will lookup by VIM id.
func (a *TcaApi) GetTenant(ctx context.Context, tenant string) (*response.Tenants, error) {
if a.rest == nil {
return nil, fmt.Errorf("rest interface is nil")
}
if len(tenant) == 0 {
return nil, fmt.Errorf("empty tenant")
}
tenants, err := a.rest.GetVimTenants(ctx)
if err != nil {
return nil, err
}
r := response.Tenants{}
if strings.Contains(tenant, "vmware") {
r.TenantsList = tenants.Filter(response.FilterVimId, func(s string) bool {
return s == tenant
})
} else if IsValidUUID(tenant) {
r.TenantsList = tenants.Filter(response.FilterId, func(s string) bool {
return s == tenant
})
} else {
r.TenantsList = tenants.Filter(response.FilterName, func(s string) bool {
return s == tenant
})
}
return &r, err
}
// GetTenantsQuery query tenant information based on
// tenant id and package id
func (a *TcaApi) GetTenantsQuery(tenantId string, nfType string) (*response.Tenants, error) {
glog.Infof("Retrieving tenants.")
if a.rest == nil {
return nil, fmt.Errorf("rest interface is nil")
}
reqFilter := specs.TenantsNfFilter{
Filter: specs.TenantFilter{
NfType: nfType,
},
}
// attach tenant id if need
if len(tenantId) > 0 {
reqFilter.Filter.NfdId = tenantId
}
return a.rest.GetTenantsQuery(&reqFilter)
}
// GetNamedClusterTemplate - return template
// if name is string first resolve template
func (a *TcaApi) GetNamedClusterTemplate(name string) (*response.ClusterTemplateSpec, error) {
glog.Infof("Retrieving vnf packages.")
if a.rest == nil {
return nil, fmt.Errorf("rest interface is nil")
}
var templateId = name
// resolve name first
if IsValidUUID(name) == false {
templates_, err := a.GetClusterTemplates()
if err != nil {
return nil, err
}
templateId, err = templates_.GetTemplateId(name)
if err != nil {
return nil, err
}
}
return a.rest.GetClusterTemplate(templateId)
}
// CreateCnfNewInstance create a new instance of VNF or CNF.
// Dry run will validate request but will not create any CNF.
func (a *TcaApi) CreateCnfNewInstance(ctx context.Context, n *specs.InstanceRequestSpec, isDry bool, isBlocked bool) (*response.LcmInfo, error) {
if a.rest == nil {
return nil, fmt.Errorf("rest interface is nil")
}
if n == nil {
return nil, fmt.Errorf("nil request")
}
var (
repoUsername = n.RepoUsername