github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/api/reboot/reboot.go (about) 1 // Copyright 2014 Cloudbase Solutions 2 // Copyright 2014 Canonical Ltd. 3 // Licensed under the AGPLv3, see LICENCE file for details. 4 5 package reboot 6 7 import ( 8 "github.com/juju/errors" 9 "github.com/juju/names" 10 11 "github.com/juju/juju/api/base" 12 "github.com/juju/juju/api/watcher" 13 "github.com/juju/juju/apiserver/params" 14 ) 15 16 // State provides access to an reboot worker's view of the state. 17 type State struct { 18 machineTag names.Tag 19 facade base.FacadeCaller 20 } 21 22 // NewState returns a version of the state that provides functionality 23 // required by the reboot worker. 24 func NewState(caller base.APICaller, machineTag names.MachineTag) *State { 25 26 return &State{ 27 facade: base.NewFacadeCaller(caller, "Reboot"), 28 machineTag: machineTag, 29 } 30 } 31 32 // WatchForRebootEvent returns a watcher.NotifyWatcher that reacts to reboot flag 33 // changes 34 func (st *State) WatchForRebootEvent() (watcher.NotifyWatcher, error) { 35 var result params.NotifyWatchResult 36 37 if err := st.facade.FacadeCall("WatchForRebootEvent", nil, &result); err != nil { 38 return nil, err 39 } 40 if result.Error != nil { 41 return nil, result.Error 42 } 43 44 w := watcher.NewNotifyWatcher(st.facade.RawAPICaller(), result) 45 return w, nil 46 } 47 48 // RequestReboot sets the reboot flag for the calling machine 49 func (st *State) RequestReboot() error { 50 var results params.ErrorResults 51 args := params.Entities{ 52 Entities: []params.Entity{{Tag: st.machineTag.String()}}, 53 } 54 55 err := st.facade.FacadeCall("RequestReboot", args, &results) 56 if err != nil { 57 return errors.Trace(err) 58 } 59 if len(results.Results) != 1 { 60 return errors.Errorf("expected 1 result, got %d", len(results.Results)) 61 } 62 63 if results.Results[0].Error != nil { 64 return errors.Trace(results.Results[0].Error) 65 } 66 return nil 67 } 68 69 // ClearReboot clears the reboot flag for the calling machine 70 func (st *State) ClearReboot() error { 71 var results params.ErrorResults 72 args := params.Entities{ 73 Entities: []params.Entity{{Tag: st.machineTag.String()}}, 74 } 75 76 err := st.facade.FacadeCall("ClearReboot", args, &results) 77 if err != nil { 78 return errors.Trace(err) 79 } 80 81 if len(results.Results) != 1 { 82 return errors.Errorf("expected 1 result, got %d", len(results.Results)) 83 } 84 85 if results.Results[0].Error != nil { 86 return errors.Trace(results.Results[0].Error) 87 } 88 89 return nil 90 } 91 92 // GetRebootAction returns the reboot action for the calling machine 93 func (st *State) GetRebootAction() (params.RebootAction, error) { 94 var results params.RebootActionResults 95 args := params.Entities{ 96 Entities: []params.Entity{{Tag: st.machineTag.String()}}, 97 } 98 99 err := st.facade.FacadeCall("GetRebootAction", args, &results) 100 if err != nil { 101 return params.ShouldDoNothing, err 102 } 103 if len(results.Results) != 1 { 104 return params.ShouldDoNothing, errors.Errorf("expected 1 result, got %d", len(results.Results)) 105 } 106 107 if results.Results[0].Error != nil { 108 return params.ShouldDoNothing, errors.Trace(results.Results[0].Error) 109 } 110 111 return results.Results[0].Result, nil 112 }