yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/esxi/mobase.go (about)

     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  
    15  package esxi
    16  
    17  import (
    18  	"strings"
    19  
    20  	"github.com/vmware/govmomi/vim25/mo"
    21  
    22  	"yunion.io/x/log"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  )
    26  
    27  type SManagedObject struct {
    28  	manager    *SESXiClient
    29  	datacenter *SDatacenter
    30  	object     mo.Entity
    31  
    32  	path []string
    33  }
    34  
    35  func newManagedObject(manager *SESXiClient, moobj mo.Entity, dc *SDatacenter) SManagedObject {
    36  	return SManagedObject{manager: manager, object: moobj, datacenter: dc}
    37  }
    38  
    39  func (self *SManagedObject) GetName() string {
    40  	return self.object.Entity().Name
    41  }
    42  
    43  func (self *SManagedObject) GetId() string {
    44  	return moRefId(self.object.Entity().Self)
    45  }
    46  
    47  func (self *SManagedObject) GetType() string {
    48  	return moRefType(self.object.Entity().Self)
    49  }
    50  
    51  func (self *SManagedObject) getCurrentParentEntity() *mo.ManagedEntity {
    52  	return self.getParentEntity(self.object.Entity())
    53  }
    54  
    55  func (self *SManagedObject) getParentEntity(obj *mo.ManagedEntity) *mo.ManagedEntity {
    56  	parent := obj.Parent
    57  	if parent != nil {
    58  		var entity mo.ManagedEntity
    59  		err := self.manager.reference2Object(*parent, []string{"name", "parent"}, &entity)
    60  		if err != nil {
    61  			log.Errorf("%s", err)
    62  			return nil
    63  		}
    64  		// log.Debugf("getParentEntity %s %s %s", entity.Self.Type, entity.Self.Value, entity.Name)
    65  		return &entity
    66  	}
    67  	return nil
    68  }
    69  
    70  func (self *SManagedObject) fetchPath() []string {
    71  	path := make([]string, 0)
    72  	obj := self.object.Entity()
    73  	for obj != nil {
    74  		path = append(path, obj.Name)
    75  		obj = self.getParentEntity(obj)
    76  	}
    77  	reverseArray(path)
    78  	return path
    79  }
    80  
    81  func (self *SManagedObject) GetPath() []string {
    82  	if self.path == nil {
    83  		self.path = self.fetchPath()
    84  	}
    85  	return self.path
    86  }
    87  
    88  func (self *SManagedObject) findInParents(objType string) *mo.ManagedEntity {
    89  	obj := self.object.Entity()
    90  
    91  	for obj != nil && obj.Self.Type != objType {
    92  		log.Debugf("find %s want %s", obj.Self.Type, objType)
    93  		obj = self.getParentEntity(obj)
    94  	}
    95  
    96  	return obj
    97  }
    98  
    99  func (self *SManagedObject) fetchDatacenter() (*SDatacenter, error) {
   100  	me := self.findInParents("Datacenter")
   101  	if me == nil {
   102  		return nil, cloudprovider.ErrNotFound
   103  	}
   104  	return self.manager.FindDatacenterByMoId(me.Self.Value)
   105  }
   106  
   107  func (self *SManagedObject) GetDatacenter() (*SDatacenter, error) {
   108  	if self.datacenter == nil {
   109  		dc, err := self.fetchDatacenter()
   110  		if err != nil {
   111  			return nil, err
   112  		}
   113  		self.datacenter = dc
   114  	}
   115  	return self.datacenter, nil
   116  }
   117  
   118  func (self *SManagedObject) GetDatacenterPath() []string {
   119  	dc, err := self.GetDatacenter()
   120  	if err != nil {
   121  		log.Errorf("cannot find datacenter")
   122  		return nil
   123  	}
   124  	path := dc.GetPath()
   125  	return path[1:]
   126  }
   127  
   128  func (self *SManagedObject) GetDatacenterPathString() string {
   129  	path := self.GetDatacenterPath()
   130  	if path != nil {
   131  		return strings.Join(path, "/")
   132  	}
   133  	return ""
   134  }
   135  
   136  func (self *SManagedObject) getManagerUri() string {
   137  	return self.manager.getUrl()
   138  }