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

     1  /*
     2  Copyright (c) 2015-2023 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  	"fmt"
    22  	"path"
    23  
    24  	"github.com/vmware/govmomi/property"
    25  	"github.com/vmware/govmomi/vim25"
    26  	"github.com/vmware/govmomi/vim25/methods"
    27  	"github.com/vmware/govmomi/vim25/mo"
    28  	"github.com/vmware/govmomi/vim25/types"
    29  )
    30  
    31  type ComputeResource struct {
    32  	Common
    33  }
    34  
    35  func NewComputeResource(c *vim25.Client, ref types.ManagedObjectReference) *ComputeResource {
    36  	return &ComputeResource{
    37  		Common: NewCommon(c, ref),
    38  	}
    39  }
    40  
    41  func (c ComputeResource) Hosts(ctx context.Context) ([]*HostSystem, error) {
    42  	var cr mo.ComputeResource
    43  
    44  	err := c.Properties(ctx, c.Reference(), []string{"host"}, &cr)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	if len(cr.Host) == 0 {
    50  		return nil, nil
    51  	}
    52  
    53  	var hs []mo.HostSystem
    54  	pc := property.DefaultCollector(c.Client())
    55  	err = pc.Retrieve(ctx, cr.Host, []string{"name"}, &hs)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	var hosts []*HostSystem
    61  
    62  	for _, h := range hs {
    63  		host := NewHostSystem(c.Client(), h.Reference())
    64  		host.InventoryPath = path.Join(c.InventoryPath, h.Name)
    65  		hosts = append(hosts, host)
    66  	}
    67  
    68  	return hosts, nil
    69  }
    70  
    71  func (c ComputeResource) Datastores(ctx context.Context) ([]*Datastore, error) {
    72  	var cr mo.ComputeResource
    73  
    74  	err := c.Properties(ctx, c.Reference(), []string{"datastore"}, &cr)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	var dss []*Datastore
    80  	for _, ref := range cr.Datastore {
    81  		ds := NewDatastore(c.c, ref)
    82  		dss = append(dss, ds)
    83  	}
    84  
    85  	return dss, nil
    86  }
    87  
    88  func (c ComputeResource) EnvironmentBrowser(ctx context.Context) (*EnvironmentBrowser, error) {
    89  	var cr mo.ComputeResource
    90  
    91  	err := c.Properties(ctx, c.Reference(), []string{"environmentBrowser"}, &cr)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	if cr.EnvironmentBrowser == nil {
    97  		return nil, fmt.Errorf("%s: nil environmentBrowser", c.Reference())
    98  	}
    99  
   100  	return NewEnvironmentBrowser(c.c, *cr.EnvironmentBrowser), nil
   101  }
   102  
   103  func (c ComputeResource) ResourcePool(ctx context.Context) (*ResourcePool, error) {
   104  	var cr mo.ComputeResource
   105  
   106  	err := c.Properties(ctx, c.Reference(), []string{"resourcePool"}, &cr)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  
   111  	return NewResourcePool(c.c, *cr.ResourcePool), nil
   112  }
   113  
   114  func (c ComputeResource) Reconfigure(ctx context.Context, spec types.BaseComputeResourceConfigSpec, modify bool) (*Task, error) {
   115  	req := types.ReconfigureComputeResource_Task{
   116  		This:   c.Reference(),
   117  		Spec:   spec,
   118  		Modify: modify,
   119  	}
   120  
   121  	res, err := methods.ReconfigureComputeResource_Task(ctx, c.c, &req)
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  
   126  	return NewTask(c.c, res.Returnval), nil
   127  }