Skip to content

Commit

Permalink
feat: add machine Power On/Off Operations (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
sachin120 committed Jan 2, 2024
1 parent 9a315a9 commit 299a5c1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ type Machine interface {
Lock(systemID string, comment string) (*entity.Machine, error)
ClearDefaultGateways(systemID string) (*entity.Machine, error)
GetPowerParameters(systemID string) (map[string]interface{}, error)
PowerOn(systemID string, params *entity.MachinePowerOnParams) (*entity.Machine, error)
PowerOff(systemID string, params *entity.MachinePowerOffParams) (*entity.Machine, error)
GetPowerState(systemID string) (*entity.MachinePowerState, error)
}
40 changes: 40 additions & 0 deletions client/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,43 @@ func (m *Machine) GetPowerParameters(systemID string) (map[string]interface{}, e

return params, err
}

// PowerOn machine
func (m *Machine) PowerOn(systemID string, params *entity.MachinePowerOnParams) (*entity.Machine, error) {
qsp, err := query.Values(params)
if err != nil {
return nil, err
}

machine := new(entity.Machine)
err = m.client(systemID).Post("power_on", qsp, func(data []byte) error {
return json.Unmarshal(data, machine)
})

return machine, err
}

// PowerOff machine
func (m *Machine) PowerOff(systemID string, params *entity.MachinePowerOffParams) (*entity.Machine, error) {
qsp, err := query.Values(params)
if err != nil {
return nil, err
}

machine := new(entity.Machine)
err = m.client(systemID).Post("power_off", qsp, func(data []byte) error {
return json.Unmarshal(data, machine)
})

return machine, err
}

// GetPowerState of the machine
func (m *Machine) GetPowerState(systemID string) (*entity.MachinePowerState, error) {
ps := new(entity.MachinePowerState)
err := m.client(systemID).Get("query_power_state", url.Values{}, func(data []byte) error {
return json.Unmarshal(data, ps)
})

return ps, err
}
18 changes: 18 additions & 0 deletions entity/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ type MachineServiceSet struct {
StatusInfo string `json:"status_info,omitempty"`
}

// MachinePowerState represent current machines power state
type MachinePowerState struct {
State string `json:"state,omitempty"`
}

// MachineParams enumerates the parameters for the machine update operation
type MachineParams struct {
PowerType string `url:"power_type,omitempty"`
Expand Down Expand Up @@ -276,3 +281,16 @@ type MachineReleaseParams struct {
QuickErase bool `url:"quick_erase,omitempty"`
SecureErase bool `url:"secure_erase,omitempty"`
}

// MachinePowerOnParams enumerates the parameters for the machine power on operation
// UserData should be Base64-encoded data
type MachinePowerOnParams struct {
Comment string `url:"comment,omitempty"`
UserData string `url:"user_data,omitempty"`
}

// MachinePowerOffParams enumerates the parameters for the machine power off operation
type MachinePowerOffParams struct {
Comment string `url:"comment,omitempty"`
StopMode string `url:"stop_mode,omitempty"`
}

0 comments on commit 299a5c1

Please sign in to comment.