host_service_system.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Copyright (c) 2016 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. "github.com/vmware/govmomi/vim25"
  17. "github.com/vmware/govmomi/vim25/methods"
  18. "github.com/vmware/govmomi/vim25/mo"
  19. "github.com/vmware/govmomi/vim25/types"
  20. )
  21. type HostServiceSystem struct {
  22. Common
  23. }
  24. func NewHostServiceSystem(c *vim25.Client, ref types.ManagedObjectReference) *HostServiceSystem {
  25. return &HostServiceSystem{
  26. Common: NewCommon(c, ref),
  27. }
  28. }
  29. func (s HostServiceSystem) Service(ctx context.Context) ([]types.HostService, error) {
  30. var ss mo.HostServiceSystem
  31. err := s.Properties(ctx, s.Reference(), []string{"serviceInfo.service"}, &ss)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return ss.ServiceInfo.Service, nil
  36. }
  37. func (s HostServiceSystem) Start(ctx context.Context, id string) error {
  38. req := types.StartService{
  39. This: s.Reference(),
  40. Id: id,
  41. }
  42. _, err := methods.StartService(ctx, s.Client(), &req)
  43. return err
  44. }
  45. func (s HostServiceSystem) Stop(ctx context.Context, id string) error {
  46. req := types.StopService{
  47. This: s.Reference(),
  48. Id: id,
  49. }
  50. _, err := methods.StopService(ctx, s.Client(), &req)
  51. return err
  52. }
  53. func (s HostServiceSystem) Restart(ctx context.Context, id string) error {
  54. req := types.RestartService{
  55. This: s.Reference(),
  56. Id: id,
  57. }
  58. _, err := methods.RestartService(ctx, s.Client(), &req)
  59. return err
  60. }
  61. func (s HostServiceSystem) UpdatePolicy(ctx context.Context, id string, policy string) error {
  62. req := types.UpdateServicePolicy{
  63. This: s.Reference(),
  64. Id: id,
  65. Policy: policy,
  66. }
  67. _, err := methods.UpdateServicePolicy(ctx, s.Client(), &req)
  68. return err
  69. }