resourcepool.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/vim25/mo"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/multicloud"
  20. "yunion.io/x/log"
  21. )
  22. var RESOURCEPOOL_PROPS = []string{"resourcePool"}
  23. type SResourcePool struct {
  24. multicloud.SProjectBase
  25. multicloud.STagBase
  26. SManagedObject
  27. }
  28. func (pool *SResourcePool) GetGlobalId() string {
  29. return pool.GetId()
  30. }
  31. func (pool *SResourcePool) GetStatus() string {
  32. return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
  33. }
  34. func (pool *SResourcePool) getParentEntity(obj *mo.ManagedEntity) *mo.ManagedEntity {
  35. parent := obj.Parent
  36. if parent != nil {
  37. var entity mo.ManagedEntity
  38. err := pool.manager.reference2Object(*parent, []string{"name", "parent"}, &entity)
  39. if err != nil {
  40. log.Errorf("%s", err)
  41. return nil
  42. }
  43. return &entity
  44. }
  45. return nil
  46. }
  47. func (pool *SResourcePool) IsDefault() bool {
  48. return strings.EqualFold(pool.GetName(), "Resources")
  49. }
  50. func (pool *SResourcePool) fetchPath() []string {
  51. path := []string{pool.GetName()}
  52. obj := pool.object.Entity()
  53. for obj != nil {
  54. obj = pool.getParentEntity(obj)
  55. if obj == nil || (obj.Self.Type == "ResourcePool" && obj.Name == "Resources") {
  56. break
  57. }
  58. path = append(path, obj.Name)
  59. }
  60. reverseArray(path)
  61. return path
  62. }
  63. func (pool *SResourcePool) GetPath() []string {
  64. if pool.path == nil {
  65. pool.path = pool.fetchPath()
  66. }
  67. return pool.path
  68. }
  69. func NewResourcePool(manager *SESXiClient, rp *mo.ResourcePool, dc *SDatacenter) *SResourcePool {
  70. return &SResourcePool{SManagedObject: newManagedObject(manager, rp, dc)}
  71. }