github.com/vmware/govmomi@v0.37.2/vapi/appliance/shutdown/shutdown.go (about)

     1  /*
     2  Copyright (c) 2022 VMware, Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0.
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package shutdown
    18  
    19  import (
    20  	"context"
    21  	"net/http"
    22  
    23  	"github.com/vmware/govmomi/vapi/rest"
    24  )
    25  
    26  const (
    27  	Path     = "/api/appliance/shutdown"
    28  	Action   = "action"
    29  	Cancel   = "cancel"
    30  	PowerOff = "poweroff"
    31  	Reboot   = "reboot"
    32  )
    33  
    34  // Manager provides convenience methods for shutdown Action
    35  type Manager struct {
    36  	*rest.Client
    37  }
    38  
    39  // NewManager creates a new Manager
    40  func NewManager(client *rest.Client) *Manager {
    41  	return &Manager{
    42  		Client: client,
    43  	}
    44  }
    45  
    46  // Cancel cancels pending shutdown action.
    47  func (m *Manager) Cancel(ctx context.Context) error {
    48  	r := m.Resource(Path).WithParam(Action, Cancel)
    49  
    50  	return m.Do(ctx, r.Request(http.MethodPost), nil)
    51  }
    52  
    53  // Spec represents request body class for an operation.
    54  type Spec struct {
    55  	Delay  int    `json:"delay"`
    56  	Reason string `json:"reason"`
    57  }
    58  
    59  // Config defines shutdown configuration returned by the Shutdown.get operation
    60  type Config struct {
    61  	Action       string `json:"action"`
    62  	Reason       string `json:"reason"`
    63  	ShutdownTime string `json:"shutdown_time"`
    64  }
    65  
    66  // Get returns details about the pending shutdown action.
    67  func (m *Manager) Get(ctx context.Context) (Config, error) {
    68  	r := m.Resource(Path)
    69  
    70  	var c Config
    71  
    72  	return c, m.Do(ctx, r.Request(http.MethodGet), &c)
    73  }
    74  
    75  // PowerOff powers off the appliance.
    76  func (m *Manager) PowerOff(ctx context.Context, reason string, delay int) error {
    77  	r := m.Resource(Path).WithParam(Action, PowerOff)
    78  
    79  	return m.Do(ctx, r.Request(http.MethodPost, Spec{
    80  		Delay:  delay,
    81  		Reason: reason,
    82  	}), nil)
    83  }
    84  
    85  // Reboot reboots the appliance
    86  func (m *Manager) Reboot(ctx context.Context, reason string, delay int) error {
    87  	r := m.Resource(Path).WithParam(Action, Reboot)
    88  
    89  	return m.Do(ctx, r.Request(http.MethodPost, Spec{
    90  		Delay:  delay,
    91  		Reason: reason,
    92  	}), nil)
    93  }