Skip to content

Commit 15efe49

Browse files
committed
chore: Replace /rest with /api in vcsa.shutdown API's
Closes: #2757
1 parent a2241de commit 15efe49

File tree

8 files changed

+104
-91
lines changed

8 files changed

+104
-91
lines changed

govc/USAGE.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5241,8 +5241,11 @@ Options:
52415241
Usage: govc vcsa.shutdown.cancel [OPTIONS]
52425242
52435243
Cancel pending shutdown action.
5244+
5245+
Note: This command requires vCenter 7.0.2 or higher.
5246+
52445247
Examples:
5245-
govc vcsa.shutdown.cancel
5248+
govc vcsa.shutdown.cancel
52465249
52475250
Options:
52485251
```
@@ -5253,8 +5256,11 @@ Options:
52535256
Usage: govc vcsa.shutdown.get [OPTIONS]
52545257
52555258
Get details about the pending shutdown action.
5259+
5260+
Note: This command requires vCenter 7.0.2 or higher.
5261+
52565262
Examples:
5257-
govc vcsa.shutdown.get
5263+
govc vcsa.shutdown.get
52585264
52595265
Options:
52605266
```
@@ -5265,8 +5271,11 @@ Options:
52655271
Usage: govc vcsa.shutdown.poweroff [OPTIONS] REASON
52665272
52675273
Power off the appliance.
5274+
5275+
Note: This command requires vCenter 7.0.2 or higher.
5276+
52685277
Examples:
5269-
govc vcsa.shutdown.poweroff -delay 10 "powering off for maintenance"
5278+
govc vcsa.shutdown.poweroff -delay 10 "powering off for maintenance"
52705279
52715280
Options:
52725281
-delay=0 Minutes after which poweroff should start.
@@ -5278,8 +5287,11 @@ Options:
52785287
Usage: govc vcsa.shutdown.reboot [OPTIONS] REASON
52795288
52805289
Reboot the appliance.
5290+
5291+
Note: This command requires vCenter 7.0.2 or higher.
5292+
52815293
Examples:
5282-
govc vcsa.shutdown.reboot -delay 10 "rebooting for maintenance"
5294+
govc vcsa.shutdown.reboot -delay 10 "rebooting for maintenance"
52835295
52845296
Options:
52855297
-delay=0 Minutes after which reboot should start.

govc/vcsa/shutdown/cancel.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ func (cmd *cancel) Register(ctx context.Context, f *flag.FlagSet) {
4040

4141
func (cmd *cancel) Description() string {
4242
return `Cancel pending shutdown action.
43+
44+
Note: This command requires vCenter 7.0.2 or higher.
45+
4346
Examples:
44-
govc vcsa.shutdown.cancel`
47+
govc vcsa.shutdown.cancel`
4548
}
4649

4750
func (cmd *cancel) Run(ctx context.Context, f *flag.FlagSet) error {

govc/vcsa/shutdown/get.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ func (cmd *get) Process(ctx context.Context) error {
5757

5858
func (cmd *get) Description() string {
5959
return `Get details about the pending shutdown action.
60+
61+
Note: This command requires vCenter 7.0.2 or higher.
62+
6063
Examples:
61-
govc vcsa.shutdown.get`
64+
govc vcsa.shutdown.get`
6265
}
6366

64-
type shutdownStatus struct {
65-
Values shutdown.Config `json:"values"`
67+
type config struct {
68+
shutdown.Config
6669
}
6770

6871
func (cmd *get) Run(ctx context.Context, f *flag.FlagSet) error {
@@ -71,23 +74,23 @@ func (cmd *get) Run(ctx context.Context, f *flag.FlagSet) error {
7174
return err
7275
}
7376

74-
s := shutdown.NewManager(c)
77+
m := shutdown.NewManager(c)
7578

76-
r, err := s.Get(ctx)
79+
s, err := m.Get(ctx)
7780
if err != nil {
7881
return err
7982
}
8083

81-
return cmd.WriteResult(shutdownStatus{
82-
Values: r,
84+
return cmd.WriteResult(config{
85+
s,
8386
})
8487
}
8588

86-
func (res shutdownStatus) Write(w io.Writer) error {
89+
func (c config) Write(w io.Writer) error {
8790
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
88-
fmt.Fprintf(tw, "Action:%s\n", res.Values.Action)
89-
fmt.Fprintf(tw, "Reason:%s\n", res.Values.Reason)
90-
fmt.Fprintf(tw, "ShutDown Time:%s\n", res.Values.ShutdownTime)
91+
fmt.Fprintf(tw, "Action:%s\n", c.Action)
92+
fmt.Fprintf(tw, "Reason:%s\n", c.Reason)
93+
fmt.Fprintf(tw, "ShutDown Time:%s\n", c.ShutdownTime)
9194

9295
return tw.Flush()
9396
}

govc/vcsa/shutdown/poweroff.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ func (cmd *powerOff) Usage() string {
5252

5353
func (cmd *powerOff) Description() string {
5454
return `Power off the appliance.
55+
56+
Note: This command requires vCenter 7.0.2 or higher.
57+
5558
Examples:
56-
govc vcsa.shutdown.poweroff -delay 10 "powering off for maintenance"`
59+
govc vcsa.shutdown.poweroff -delay 10 "powering off for maintenance"`
5760
}
5861

5962
func (cmd *powerOff) Run(ctx context.Context, f *flag.FlagSet) error {

govc/vcsa/shutdown/reboot.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ func (cmd *reboot) Usage() string {
6060

6161
func (cmd *reboot) Description() string {
6262
return `Reboot the appliance.
63+
64+
Note: This command requires vCenter 7.0.2 or higher.
65+
6366
Examples:
64-
govc vcsa.shutdown.reboot -delay 10 "rebooting for maintenance"`
67+
govc vcsa.shutdown.reboot -delay 10 "rebooting for maintenance"`
6568
}
6669

6770
func (cmd *reboot) Run(ctx context.Context, f *flag.FlagSet) error {

vapi/appliance/shutdown/shutdown.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
)
2525

2626
const (
27-
Path = "/appliance/shutdown"
27+
Path = "/api/appliance/shutdown"
2828
Action = "action"
2929
Cancel = "cancel"
3030
PowerOff = "poweroff"
@@ -56,7 +56,7 @@ type Spec struct {
5656
Reason string `json:"reason"`
5757
}
5858

59-
// Config defines shutdown configuration returned by the Shutdown.get operation
59+
// Config defines shutdown configuration returned by the Shutdown.get operation
6060
type Config struct {
6161
Action string `json:"action"`
6262
Reason string `json:"reason"`
@@ -73,23 +73,21 @@ func (m *Manager) Get(ctx context.Context) (Config, error) {
7373
}
7474

7575
// PowerOff powers off the appliance.
76-
func (m *Manager) PowerOff(ctx context.Context, powerOffReason string, delay int) error {
76+
func (m *Manager) PowerOff(ctx context.Context, reason string, delay int) error {
7777
r := m.Resource(Path).WithParam(Action, PowerOff)
78-
s := Spec{
79-
Delay: delay,
80-
Reason: powerOffReason,
81-
}
8278

83-
return m.Do(ctx, r.Request(http.MethodPost, s), nil)
79+
return m.Do(ctx, r.Request(http.MethodPost, Spec{
80+
Delay: delay,
81+
Reason: reason,
82+
}), nil)
8483
}
8584

8685
// Reboot reboots the appliance
87-
func (m *Manager) Reboot(ctx context.Context, rebootReason string, delay int) error {
86+
func (m *Manager) Reboot(ctx context.Context, reason string, delay int) error {
8887
r := m.Resource(Path).WithParam(Action, Reboot)
89-
s := Spec{
90-
Delay: delay,
91-
Reason: rebootReason,
92-
}
9388

94-
return m.Do(ctx, r.Request(http.MethodPost, s), nil)
89+
return m.Do(ctx, r.Request(http.MethodPost, Spec{
90+
Delay: delay,
91+
Reason: reason,
92+
}), nil)
9593
}

vapi/appliance/simulator/simulator.go

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ import (
2121
"log"
2222
"net/http"
2323
"net/url"
24+
"time"
2425

2526
"github.com/vmware/govmomi/simulator"
2627
"github.com/vmware/govmomi/vapi/appliance/access/consolecli"
2728
"github.com/vmware/govmomi/vapi/appliance/access/dcui"
2829
"github.com/vmware/govmomi/vapi/appliance/access/shell"
2930
"github.com/vmware/govmomi/vapi/appliance/access/ssh"
31+
"github.com/vmware/govmomi/vapi/appliance/shutdown"
3032
vapi "github.com/vmware/govmomi/vapi/simulator"
3133
)
3234

@@ -38,21 +40,23 @@ func init() {
3840

3941
// Handler implements the Appliance API simulator
4042
type Handler struct {
41-
URL *url.URL
42-
consolecli consolecli.Access
43-
dcui dcui.Access
44-
ssh ssh.Access
45-
shell shell.Access
43+
URL *url.URL
44+
consolecli consolecli.Access
45+
dcui dcui.Access
46+
ssh ssh.Access
47+
shell shell.Access
48+
shutdownConfig shutdown.Config
4649
}
4750

4851
// New creates a Handler instance
4952
func New(u *url.URL) *Handler {
5053
return &Handler{
51-
URL: nil,
52-
consolecli: consolecli.Access{Enabled: false},
53-
dcui: dcui.Access{Enabled: false},
54-
ssh: ssh.Access{Enabled: false},
55-
shell: shell.Access{Enabled: false, Timeout: 0},
54+
URL: nil,
55+
consolecli: consolecli.Access{Enabled: false},
56+
dcui: dcui.Access{Enabled: false},
57+
ssh: ssh.Access{Enabled: false},
58+
shell: shell.Access{Enabled: false, Timeout: 0},
59+
shutdownConfig: shutdown.Config{},
5660
}
5761
}
5862

@@ -62,6 +66,7 @@ func (h *Handler) Register(s *simulator.Service, r *simulator.Registry) {
6266
s.HandleFunc(dcui.Path, h.dcuiAccess)
6367
s.HandleFunc(ssh.Path, h.sshAccess)
6468
s.HandleFunc(shell.Path, h.shellAccess)
69+
s.HandleFunc(shutdown.Path, h.shutdown)
6570
}
6671

6772
func (h *Handler) decode(r *http.Request, w http.ResponseWriter, val interface{}) bool {
@@ -147,3 +152,38 @@ func (h *Handler) shellAccess(writer http.ResponseWriter, request *http.Request)
147152
http.NotFound(writer, request)
148153
}
149154
}
155+
156+
func (h *Handler) shutdown(w http.ResponseWriter, r *http.Request) {
157+
switch r.Method {
158+
case http.MethodGet:
159+
vapi.StatusOK(w, h.shutdownConfig)
160+
case http.MethodPost:
161+
switch r.URL.Query().Get(shutdown.Action) {
162+
case shutdown.Cancel:
163+
h.shutdownConfig.ShutdownTime = ""
164+
h.shutdownConfig.Action = ""
165+
h.shutdownConfig.Reason = ""
166+
w.WriteHeader(http.StatusNoContent)
167+
case shutdown.Reboot:
168+
var spec shutdown.Spec
169+
if h.decode(r, w, &spec) {
170+
h.shutdownConfig.ShutdownTime = time.Now().UTC().Add(time.Duration(spec.Delay) * time.Minute).String()
171+
h.shutdownConfig.Reason = spec.Reason
172+
h.shutdownConfig.Action = shutdown.Reboot
173+
w.WriteHeader(http.StatusNoContent)
174+
}
175+
case shutdown.PowerOff:
176+
var spec shutdown.Spec
177+
if h.decode(r, w, &spec) {
178+
h.shutdownConfig.ShutdownTime = time.Now().UTC().Add(time.Duration(spec.Delay) * time.Minute).String()
179+
h.shutdownConfig.Reason = spec.Reason
180+
h.shutdownConfig.Action = shutdown.PowerOff
181+
w.WriteHeader(http.StatusNoContent)
182+
}
183+
default:
184+
http.NotFound(w, r)
185+
}
186+
default:
187+
http.NotFound(w, r)
188+
}
189+
}

vapi/simulator/simulator.go

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import (
4343
"github.com/vmware/govmomi/object"
4444
"github.com/vmware/govmomi/ovf"
4545
"github.com/vmware/govmomi/simulator"
46-
"github.com/vmware/govmomi/vapi/appliance/shutdown"
4746
"github.com/vmware/govmomi/vapi/internal"
4847
"github.com/vmware/govmomi/vapi/library"
4948
"github.com/vmware/govmomi/vapi/rest"
@@ -92,7 +91,6 @@ type handler struct {
9291
Library map[string]*content
9392
Update map[string]update
9493
Download map[string]download
95-
Config shutdown.Config
9694
}
9795

9896
func init() {
@@ -116,7 +114,6 @@ func New(u *url.URL, settings []vim.BaseOptionValue) (string, http.Handler) {
116114
Library: make(map[string]*content),
117115
Update: make(map[string]update),
118116
Download: make(map[string]download),
119-
Config: shutdown.Config{},
120117
}
121118

122119
handlers := []struct {
@@ -158,7 +155,6 @@ func New(u *url.URL, settings []vim.BaseOptionValue) (string, http.Handler) {
158155
{internal.VCenterVMTXLibraryItem + "/", s.libraryItemTemplateID},
159156
{internal.VCenterVM + "/", s.vmID},
160157
{internal.DebugEcho, s.debugEcho},
161-
{shutdown.Path, s.shutdown},
162158
}
163159

164160
for i := range handlers {
@@ -2103,48 +2099,3 @@ func (s *handler) vmID(w http.ResponseWriter, r *http.Request) {
21032099
func (s *handler) debugEcho(w http.ResponseWriter, r *http.Request) {
21042100
r.Write(w)
21052101
}
2106-
2107-
func (s *handler) shutdown(w http.ResponseWriter, r *http.Request) {
2108-
delayShutdown := func(delay int) {
2109-
go time.AfterFunc(time.Duration(delay)*time.Minute, func() {
2110-
s.Lock()
2111-
s.Config.ShutdownTime = ""
2112-
s.Config.Action = ""
2113-
s.Config.Reason = ""
2114-
s.Unlock()
2115-
})
2116-
}
2117-
2118-
switch r.Method {
2119-
case http.MethodGet:
2120-
OK(w, s.Config)
2121-
case http.MethodPost:
2122-
switch r.URL.Query().Get(shutdown.Action) {
2123-
case shutdown.Cancel:
2124-
delayShutdown(0)
2125-
w.WriteHeader(http.StatusNoContent)
2126-
case shutdown.Reboot:
2127-
var spec shutdown.Spec
2128-
if s.decode(r, w, &spec) {
2129-
s.Config.ShutdownTime = time.Now().UTC().Add(time.Duration(spec.Delay) * time.Minute).String()
2130-
s.Config.Reason = spec.Reason
2131-
s.Config.Action = shutdown.Reboot
2132-
delayShutdown(spec.Delay)
2133-
w.WriteHeader(http.StatusNoContent)
2134-
}
2135-
case shutdown.PowerOff:
2136-
var spec shutdown.Spec
2137-
if s.decode(r, w, &spec) {
2138-
s.Config.ShutdownTime = time.Now().Local().Add(time.Duration(spec.Delay) * time.Minute).String()
2139-
s.Config.Reason = spec.Reason
2140-
s.Config.Action = shutdown.PowerOff
2141-
delayShutdown(spec.Delay)
2142-
w.WriteHeader(http.StatusNoContent)
2143-
}
2144-
default:
2145-
http.NotFound(w, r)
2146-
}
2147-
default:
2148-
http.NotFound(w, r)
2149-
}
2150-
}

0 commit comments

Comments
 (0)