yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/esxi/cluster.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  	"context"
    19  	"fmt"
    20  
    21  	"github.com/vmware/govmomi/object"
    22  	"github.com/vmware/govmomi/vim25/mo"
    23  	"github.com/vmware/govmomi/vim25/types"
    24  
    25  	"yunion.io/x/pkg/errors"
    26  
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  )
    29  
    30  type SCluster struct {
    31  	SManagedObject
    32  }
    33  
    34  func NewCluster(manager *SESXiClient, cluster *mo.ClusterComputeResource, dc *SDatacenter) *SCluster {
    35  	return &SCluster{SManagedObject: newManagedObject(manager, cluster, dc)}
    36  }
    37  
    38  func (cluster *SCluster) ListResourcePools() ([]mo.ResourcePool, error) {
    39  	var pools, result []mo.ResourcePool
    40  	err := cluster.manager.scanMObjects(cluster.object.Entity().Self, RESOURCEPOOL_PROPS, &pools)
    41  	if err != nil {
    42  		return nil, errors.Wrap(err, "scanMObjects")
    43  	}
    44  	for i := range pools {
    45  		if pools[i].Parent.Type == "ClusterComputeResource" {
    46  			continue
    47  		}
    48  		result = append(result, pools[i])
    49  	}
    50  	return result, nil
    51  }
    52  
    53  func (cluster *SCluster) getDefaultResourcePool() (mo.ResourcePool, error) {
    54  	pools := []mo.ResourcePool{}
    55  	err := cluster.manager.scanMObjects(cluster.object.Entity().Self, RESOURCEPOOL_PROPS, &pools)
    56  	if err != nil {
    57  		return mo.ResourcePool{}, errors.Wrap(err, "scanMObjects")
    58  	}
    59  	for i := range pools {
    60  		if pools[i].Parent.Type == "ClusterComputeResource" {
    61  			return pools[i], nil
    62  		}
    63  	}
    64  	return mo.ResourcePool{}, cloudprovider.ErrNotFound
    65  }
    66  
    67  func (cluster *SCluster) CreateResourcePool(name string) (*mo.ResourcePool, error) {
    68  	if len(name) == 0 {
    69  		return nil, errors.Error("empty name str")
    70  	}
    71  	root, err := cluster.getDefaultResourcePool()
    72  	if err != nil {
    73  		return nil, errors.Wrap(err, "getDefaultResourcePool")
    74  	}
    75  	pool := object.NewResourcePool(cluster.manager.client.Client, root.Reference())
    76  	pool.InventoryPath = fmt.Sprintf("/%s/host/%s/Resources", cluster.datacenter.GetName(), cluster.GetName())
    77  	_, err = pool.Create(context.Background(), name, types.DefaultResourceConfigSpec())
    78  	if err != nil {
    79  		return nil, errors.Wrap(err, "pool.Create")
    80  	}
    81  
    82  	pools, err := cluster.ListResourcePools()
    83  	if err != nil {
    84  		return nil, errors.Wrap(err, "listResourcePools")
    85  	}
    86  	for i := range pools {
    87  		p := NewResourcePool(cluster.manager, &pools[i], cluster.datacenter)
    88  		if p.GetName() == name {
    89  			return &pools[i], nil
    90  		}
    91  	}
    92  	return nil, errors.Wrap(cloudprovider.ErrNotFound, "AfterCreate")
    93  }
    94  
    95  func (cluster *SCluster) SyncResourcePool(name string) (*mo.ResourcePool, error) {
    96  	pools, err := cluster.ListResourcePools()
    97  	if err != nil {
    98  		return nil, errors.Wrap(err, "ListResourcePools")
    99  	}
   100  	for i := range pools {
   101  		if pools[i].Entity().Name == name {
   102  			return &pools[i], nil
   103  		}
   104  	}
   105  	return cluster.CreateResourcePool(name)
   106  }
   107  
   108  func (cluster *SCluster) getoCluster() *mo.ClusterComputeResource {
   109  	return cluster.object.(*mo.ClusterComputeResource)
   110  }