cluster.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package esxi
  15. import (
  16. "strings"
  17. "github.com/vmware/govmomi/object"
  18. "github.com/vmware/govmomi/vim25/mo"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. )
  23. type SCluster struct {
  24. SManagedObject
  25. }
  26. func NewCluster(manager *SESXiClient, cluster *mo.ClusterComputeResource, dc *SDatacenter) *SCluster {
  27. return &SCluster{SManagedObject: newManagedObject(manager, cluster, dc)}
  28. }
  29. func (cluster *SCluster) listResourcePools() ([]mo.ResourcePool, error) {
  30. var pools []mo.ResourcePool
  31. err := cluster.manager.scanMObjects(cluster.object.Entity().Self, RESOURCEPOOL_PROPS, &pools)
  32. if err != nil {
  33. return nil, errors.Wrap(err, "scanMObjects")
  34. }
  35. return pools, nil
  36. }
  37. func (cluster *SCluster) SyncResourcePool(name string) (*object.ResourcePool, error) {
  38. pools, err := cluster.listResourcePools()
  39. if err != nil {
  40. return nil, errors.Wrap(err, "listResourcePools")
  41. }
  42. for i := range pools {
  43. pool := NewResourcePool(cluster.manager, &pools[i], cluster.datacenter)
  44. if strings.EqualFold(pool.GetId(), name) || strings.EqualFold(strings.Join(pool.GetPath(), "/"), name) ||
  45. strings.EqualFold(strings.Join(pool.GetPath(), "|"), name) ||
  46. strings.EqualFold(pool.GetName(), name) {
  47. log.Infof("SyncResourcePool: %s found", strings.Join(pool.GetPath(), "|"))
  48. return object.NewResourcePool(cluster.manager.client.Client, pools[i].Reference()), nil
  49. }
  50. }
  51. log.Infof("SyncResourcePool: %s not found", name)
  52. return nil, errors.Wrap(cloudprovider.ErrNotFound, "SyncResourcePool")
  53. }