1/* 2Copyright (c) 2015 VMware, Inc. All Rights Reserved. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package object 18 19import ( 20 "context" 21 "fmt" 22 "net" 23 24 "github.com/vmware/govmomi/vim25" 25 "github.com/vmware/govmomi/vim25/methods" 26 "github.com/vmware/govmomi/vim25/mo" 27 "github.com/vmware/govmomi/vim25/types" 28) 29 30type HostSystem struct { 31 Common 32} 33 34func NewHostSystem(c *vim25.Client, ref types.ManagedObjectReference) *HostSystem { 35 return &HostSystem{ 36 Common: NewCommon(c, ref), 37 } 38} 39 40func (h HostSystem) ConfigManager() *HostConfigManager { 41 return NewHostConfigManager(h.c, h.Reference()) 42} 43 44func (h HostSystem) ResourcePool(ctx context.Context) (*ResourcePool, error) { 45 var mh mo.HostSystem 46 47 err := h.Properties(ctx, h.Reference(), []string{"parent"}, &mh) 48 if err != nil { 49 return nil, err 50 } 51 52 var mcr *mo.ComputeResource 53 var parent interface{} 54 55 switch mh.Parent.Type { 56 case "ComputeResource": 57 mcr = new(mo.ComputeResource) 58 parent = mcr 59 case "ClusterComputeResource": 60 mcc := new(mo.ClusterComputeResource) 61 mcr = &mcc.ComputeResource 62 parent = mcc 63 default: 64 return nil, fmt.Errorf("unknown host parent type: %s", mh.Parent.Type) 65 } 66 67 err = h.Properties(ctx, *mh.Parent, []string{"resourcePool"}, parent) 68 if err != nil { 69 return nil, err 70 } 71 72 pool := NewResourcePool(h.c, *mcr.ResourcePool) 73 return pool, nil 74} 75 76func (h HostSystem) ManagementIPs(ctx context.Context) ([]net.IP, error) { 77 var mh mo.HostSystem 78 79 err := h.Properties(ctx, h.Reference(), []string{"config.virtualNicManagerInfo.netConfig"}, &mh) 80 if err != nil { 81 return nil, err 82 } 83 84 var ips []net.IP 85 for _, nc := range mh.Config.VirtualNicManagerInfo.NetConfig { 86 if nc.NicType == "management" && len(nc.CandidateVnic) > 0 { 87 ip := net.ParseIP(nc.CandidateVnic[0].Spec.Ip.IpAddress) 88 if ip != nil { 89 ips = append(ips, ip) 90 } 91 } 92 } 93 94 return ips, nil 95} 96 97func (h HostSystem) Disconnect(ctx context.Context) (*Task, error) { 98 req := types.DisconnectHost_Task{ 99 This: h.Reference(), 100 } 101 102 res, err := methods.DisconnectHost_Task(ctx, h.c, &req) 103 if err != nil { 104 return nil, err 105 } 106 107 return NewTask(h.c, res.Returnval), nil 108} 109 110func (h HostSystem) Reconnect(ctx context.Context, cnxSpec *types.HostConnectSpec, reconnectSpec *types.HostSystemReconnectSpec) (*Task, error) { 111 req := types.ReconnectHost_Task{ 112 This: h.Reference(), 113 CnxSpec: cnxSpec, 114 ReconnectSpec: reconnectSpec, 115 } 116 117 res, err := methods.ReconnectHost_Task(ctx, h.c, &req) 118 if err != nil { 119 return nil, err 120 } 121 122 return NewTask(h.c, res.Returnval), nil 123} 124 125func (h HostSystem) EnterMaintenanceMode(ctx context.Context, timeout int32, evacuate bool, spec *types.HostMaintenanceSpec) (*Task, error) { 126 req := types.EnterMaintenanceMode_Task{ 127 This: h.Reference(), 128 Timeout: timeout, 129 EvacuatePoweredOffVms: types.NewBool(evacuate), 130 MaintenanceSpec: spec, 131 } 132 133 res, err := methods.EnterMaintenanceMode_Task(ctx, h.c, &req) 134 if err != nil { 135 return nil, err 136 } 137 138 return NewTask(h.c, res.Returnval), nil 139} 140 141func (h HostSystem) ExitMaintenanceMode(ctx context.Context, timeout int32) (*Task, error) { 142 req := types.ExitMaintenanceMode_Task{ 143 This: h.Reference(), 144 Timeout: timeout, 145 } 146 147 res, err := methods.ExitMaintenanceMode_Task(ctx, h.c, &req) 148 if err != nil { 149 return nil, err 150 } 151 152 return NewTask(h.c, res.Returnval), nil 153} 154