github.com/vmware/govmomi@v0.37.2/object/cluster_compute_resource.go (about)

     1  /*
     2  Copyright (c) 2015 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package object
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/vmware/govmomi/vim25"
    23  	"github.com/vmware/govmomi/vim25/methods"
    24  	"github.com/vmware/govmomi/vim25/mo"
    25  	"github.com/vmware/govmomi/vim25/types"
    26  )
    27  
    28  type ClusterComputeResource struct {
    29  	ComputeResource
    30  }
    31  
    32  func NewClusterComputeResource(c *vim25.Client, ref types.ManagedObjectReference) *ClusterComputeResource {
    33  	return &ClusterComputeResource{
    34  		ComputeResource: *NewComputeResource(c, ref),
    35  	}
    36  }
    37  
    38  func (c ClusterComputeResource) Configuration(ctx context.Context) (*types.ClusterConfigInfoEx, error) {
    39  	var obj mo.ClusterComputeResource
    40  
    41  	err := c.Properties(ctx, c.Reference(), []string{"configurationEx"}, &obj)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	return obj.ConfigurationEx.(*types.ClusterConfigInfoEx), nil
    47  }
    48  
    49  func (c ClusterComputeResource) AddHost(ctx context.Context, spec types.HostConnectSpec, asConnected bool, license *string, resourcePool *types.ManagedObjectReference) (*Task, error) {
    50  	req := types.AddHost_Task{
    51  		This:        c.Reference(),
    52  		Spec:        spec,
    53  		AsConnected: asConnected,
    54  	}
    55  
    56  	if license != nil {
    57  		req.License = *license
    58  	}
    59  
    60  	if resourcePool != nil {
    61  		req.ResourcePool = resourcePool
    62  	}
    63  
    64  	res, err := methods.AddHost_Task(ctx, c.c, &req)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	return NewTask(c.c, res.Returnval), nil
    70  }
    71  
    72  func (c ClusterComputeResource) MoveInto(ctx context.Context, hosts ...*HostSystem) (*Task, error) {
    73  	req := types.MoveInto_Task{
    74  		This: c.Reference(),
    75  	}
    76  
    77  	hostReferences := make([]types.ManagedObjectReference, len(hosts))
    78  	for i, host := range hosts {
    79  		hostReferences[i] = host.Reference()
    80  	}
    81  	req.Host = hostReferences
    82  
    83  	res, err := methods.MoveInto_Task(ctx, c.c, &req)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	return NewTask(c.c, res.Returnval), nil
    89  }
    90  
    91  func (c ClusterComputeResource) PlaceVm(ctx context.Context, spec types.PlacementSpec) (*types.PlacementResult, error) {
    92  	req := types.PlaceVm{
    93  		This:          c.Reference(),
    94  		PlacementSpec: spec,
    95  	}
    96  
    97  	res, err := methods.PlaceVm(ctx, c.c, &req)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	return &res.Returnval, nil
   103  }