github.com/vmware/govmomi@v0.37.2/object/host_system.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  	"fmt"
    22  	"net"
    23  
    24  	"github.com/vmware/govmomi/internal"
    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 HostSystem struct {
    32  	Common
    33  }
    34  
    35  func NewHostSystem(c *vim25.Client, ref types.ManagedObjectReference) *HostSystem {
    36  	return &HostSystem{
    37  		Common: NewCommon(c, ref),
    38  	}
    39  }
    40  
    41  func (h HostSystem) ConfigManager() *HostConfigManager {
    42  	return NewHostConfigManager(h.c, h.Reference())
    43  }
    44  
    45  func (h HostSystem) ResourcePool(ctx context.Context) (*ResourcePool, error) {
    46  	var mh mo.HostSystem
    47  
    48  	err := h.Properties(ctx, h.Reference(), []string{"parent"}, &mh)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	var mcr *mo.ComputeResource
    54  	var parent interface{}
    55  
    56  	switch mh.Parent.Type {
    57  	case "ComputeResource":
    58  		mcr = new(mo.ComputeResource)
    59  		parent = mcr
    60  	case "ClusterComputeResource":
    61  		mcc := new(mo.ClusterComputeResource)
    62  		mcr = &mcc.ComputeResource
    63  		parent = mcc
    64  	default:
    65  		return nil, fmt.Errorf("unknown host parent type: %s", mh.Parent.Type)
    66  	}
    67  
    68  	err = h.Properties(ctx, *mh.Parent, []string{"resourcePool"}, parent)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	pool := NewResourcePool(h.c, *mcr.ResourcePool)
    74  	return pool, nil
    75  }
    76  
    77  func (h HostSystem) ManagementIPs(ctx context.Context) ([]net.IP, error) {
    78  	var mh mo.HostSystem
    79  
    80  	err := h.Properties(ctx, h.Reference(), []string{"config.virtualNicManagerInfo.netConfig"}, &mh)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	config := mh.Config
    86  	if config == nil {
    87  		return nil, nil
    88  	}
    89  
    90  	info := config.VirtualNicManagerInfo
    91  	if info == nil {
    92  		return nil, nil
    93  	}
    94  
    95  	return internal.HostSystemManagementIPs(info.NetConfig), nil
    96  }
    97  
    98  func (h HostSystem) Disconnect(ctx context.Context) (*Task, error) {
    99  	req := types.DisconnectHost_Task{
   100  		This: h.Reference(),
   101  	}
   102  
   103  	res, err := methods.DisconnectHost_Task(ctx, h.c, &req)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	return NewTask(h.c, res.Returnval), nil
   109  }
   110  
   111  func (h HostSystem) Reconnect(ctx context.Context, cnxSpec *types.HostConnectSpec, reconnectSpec *types.HostSystemReconnectSpec) (*Task, error) {
   112  	req := types.ReconnectHost_Task{
   113  		This:          h.Reference(),
   114  		CnxSpec:       cnxSpec,
   115  		ReconnectSpec: reconnectSpec,
   116  	}
   117  
   118  	res, err := methods.ReconnectHost_Task(ctx, h.c, &req)
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	return NewTask(h.c, res.Returnval), nil
   124  }
   125  
   126  func (h HostSystem) EnterMaintenanceMode(ctx context.Context, timeout int32, evacuate bool, spec *types.HostMaintenanceSpec) (*Task, error) {
   127  	req := types.EnterMaintenanceMode_Task{
   128  		This:                  h.Reference(),
   129  		Timeout:               timeout,
   130  		EvacuatePoweredOffVms: types.NewBool(evacuate),
   131  		MaintenanceSpec:       spec,
   132  	}
   133  
   134  	res, err := methods.EnterMaintenanceMode_Task(ctx, h.c, &req)
   135  	if err != nil {
   136  		return nil, err
   137  	}
   138  
   139  	return NewTask(h.c, res.Returnval), nil
   140  }
   141  
   142  func (h HostSystem) ExitMaintenanceMode(ctx context.Context, timeout int32) (*Task, error) {
   143  	req := types.ExitMaintenanceMode_Task{
   144  		This:    h.Reference(),
   145  		Timeout: timeout,
   146  	}
   147  
   148  	res, err := methods.ExitMaintenanceMode_Task(ctx, h.c, &req)
   149  	if err != nil {
   150  		return nil, err
   151  	}
   152  
   153  	return NewTask(h.c, res.Returnval), nil
   154  }