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/internal"
  19. "github.com/vmware/govmomi/vim25"
  20. "github.com/vmware/govmomi/vim25/methods"
  21. "github.com/vmware/govmomi/vim25/mo"
  22. "github.com/vmware/govmomi/vim25/types"
  23. )
  24. type HostSystem struct {
  25. Common
  26. }
  27. func NewHostSystem(c *vim25.Client, ref types.ManagedObjectReference) *HostSystem {
  28. return &HostSystem{
  29. Common: NewCommon(c, ref),
  30. }
  31. }
  32. func (h HostSystem) ConfigManager() *HostConfigManager {
  33. return NewHostConfigManager(h.c, h.Reference())
  34. }
  35. func (h HostSystem) ResourcePool(ctx context.Context) (*ResourcePool, error) {
  36. var mh mo.HostSystem
  37. err := h.Properties(ctx, h.Reference(), []string{"parent"}, &mh)
  38. if err != nil {
  39. return nil, err
  40. }
  41. var mcr *mo.ComputeResource
  42. var parent interface{}
  43. switch mh.Parent.Type {
  44. case "ComputeResource":
  45. mcr = new(mo.ComputeResource)
  46. parent = mcr
  47. case "ClusterComputeResource":
  48. mcc := new(mo.ClusterComputeResource)
  49. mcr = &mcc.ComputeResource
  50. parent = mcc
  51. default:
  52. return nil, fmt.Errorf("unknown host parent type: %s", mh.Parent.Type)
  53. }
  54. err = h.Properties(ctx, *mh.Parent, []string{"resourcePool"}, parent)
  55. if err != nil {
  56. return nil, err
  57. }
  58. pool := NewResourcePool(h.c, *mcr.ResourcePool)
  59. return pool, nil
  60. }
  61. func (h HostSystem) ManagementIPs(ctx context.Context) ([]net.IP, error) {
  62. var mh mo.HostSystem
  63. err := h.Properties(ctx, h.Reference(), []string{"config.virtualNicManagerInfo.netConfig"}, &mh)
  64. if err != nil {
  65. return nil, err
  66. }
  67. config := mh.Config
  68. if config == nil {
  69. return nil, nil
  70. }
  71. info := config.VirtualNicManagerInfo
  72. if info == nil {
  73. return nil, nil
  74. }
  75. return internal.HostSystemManagementIPs(info.NetConfig), 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. }