github.com/vmware/govmomi@v0.51.0/vapi/appliance/shutdown/shutdown.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package shutdown 6 7 import ( 8 "context" 9 "net/http" 10 11 "github.com/vmware/govmomi/vapi/rest" 12 ) 13 14 const ( 15 Path = "/api/appliance/shutdown" 16 Action = "action" 17 Cancel = "cancel" 18 PowerOff = "poweroff" 19 Reboot = "reboot" 20 ) 21 22 // Manager provides convenience methods for shutdown Action 23 type Manager struct { 24 *rest.Client 25 } 26 27 // NewManager creates a new Manager 28 func NewManager(client *rest.Client) *Manager { 29 return &Manager{ 30 Client: client, 31 } 32 } 33 34 // Cancel cancels pending shutdown action. 35 func (m *Manager) Cancel(ctx context.Context) error { 36 r := m.Resource(Path).WithParam(Action, Cancel) 37 38 return m.Do(ctx, r.Request(http.MethodPost), nil) 39 } 40 41 // Spec represents request body class for an operation. 42 type Spec struct { 43 Delay int `json:"delay"` 44 Reason string `json:"reason"` 45 } 46 47 // Config defines shutdown configuration returned by the Shutdown.get operation 48 type Config struct { 49 Action string `json:"action"` 50 Reason string `json:"reason"` 51 ShutdownTime string `json:"shutdown_time"` 52 } 53 54 // Get returns details about the pending shutdown action. 55 func (m *Manager) Get(ctx context.Context) (Config, error) { 56 r := m.Resource(Path) 57 58 var c Config 59 60 return c, m.Do(ctx, r.Request(http.MethodGet), &c) 61 } 62 63 // PowerOff powers off the appliance. 64 func (m *Manager) PowerOff(ctx context.Context, reason string, delay int) error { 65 r := m.Resource(Path).WithParam(Action, PowerOff) 66 67 return m.Do(ctx, r.Request(http.MethodPost, Spec{ 68 Delay: delay, 69 Reason: reason, 70 }), nil) 71 } 72 73 // Reboot reboots the appliance 74 func (m *Manager) Reboot(ctx context.Context, reason string, delay int) error { 75 r := m.Resource(Path).WithParam(Action, Reboot) 76 77 return m.Do(ctx, r.Request(http.MethodPost, Spec{ 78 Delay: delay, 79 Reason: reason, 80 }), nil) 81 }