host_system.go 3.7 KB

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