Skip to content

Commit

Permalink
feat: add machine, machines related API endpoint interactions (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
sachin120 committed Jan 10, 2024
1 parent 8123a5d commit aa4013a
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ type Machine interface {
Deploy(systemID string, params *entity.MachineDeployParams) (*entity.Machine, error)
Release(systemID string, params *entity.MachineReleaseParams) (*entity.Machine, error)
Lock(systemID string, comment string) (*entity.Machine, error)
Unlock(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)
SetWorkloadAnnotations(systemID string, params map[string]string) (*entity.Machine, error)
RescueMode(systemID string) (*entity.Machine, error)
ExitRescueMode(systemID string) (*entity.Machine, error)
Abort(systemID string, comment string) (*entity.Machine, error)
MarkBroken(systemID string, comment string) (*entity.Machine, error)
MarkFixed(systemID string, comment string) (*entity.Machine, error)
GetToken(systemID string) (*entity.MachineToken, error)
}
1 change: 1 addition & 0 deletions api/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type Machines interface {
Allocate(params *entity.MachineAllocateParams) (*entity.Machine, error)
AcceptAll() error
Release(systemID []string, comment string) error
ListAllocated() ([]entity.Machine, error)
}
90 changes: 90 additions & 0 deletions client/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ func (m *Machine) Lock(systemID string, comment string) (*entity.Machine, error)
return machine, err
}

// Unlock marks a machine as ‘Unlocked’, to allow changes
func (m *Machine) Unlock(systemID string, comment string) (*entity.Machine, error) {
qsp := make(url.Values)
if comment != "" {
qsp.Set("comment", comment)
}

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

return machine, err
}

// ClearDefaultGateways clears default gateways.
func (m *Machine) ClearDefaultGateways(systemID string) (*entity.Machine, error) {
machine := new(entity.Machine)
Expand Down Expand Up @@ -186,3 +201,78 @@ func (m *Machine) SetWorkloadAnnotations(systemID string, params map[string]stri

return machine, err
}

// RescueMode begins the rescue mode process on a given machine
func (m *Machine) RescueMode(systemID string) (*entity.Machine, error) {
machine := new(entity.Machine)
err := m.client(systemID).Post("rescue_mode", url.Values{}, func(data []byte) error {
return json.Unmarshal(data, &machine)
})

return machine, err
}

// ExitRescueMode exits the rescue mode process on a given machine
func (m *Machine) ExitRescueMode(systemID string) (*entity.Machine, error) {
machine := new(entity.Machine)
err := m.client(systemID).Post("exit_rescue_mode", url.Values{}, func(data []byte) error {
return json.Unmarshal(data, &machine)
})

return machine, err
}

// Abort aborts machine's current operation
func (m *Machine) Abort(systemID string, comment string) (*entity.Machine, error) {
qsp := make(url.Values)
if comment != "" {
qsp.Set("comment", comment)
}

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

return machine, err
}

// MarkBroken marks a machine as ‘Broken’
func (m *Machine) MarkBroken(systemID string, comment string) (*entity.Machine, error) {
qsp := make(url.Values)
if comment != "" {
qsp.Set("comment", comment)
}

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

return machine, err
}

// MarkFixed marks a machine as ‘Fixed’
func (m *Machine) MarkFixed(systemID string, comment string) (*entity.Machine, error) {
qsp := make(url.Values)
if comment != "" {
qsp.Set("comment", comment)
}

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

return machine, err
}

// GetToken gets the machine token for a given machine
func (m *Machine) GetToken(systemID string) (*entity.MachineToken, error) {
machineToken := new(entity.MachineToken)
err := m.client(systemID).Get("get_token", url.Values{}, func(data []byte) error {
return json.Unmarshal(data, machineToken)
})

return machineToken, err
}
10 changes: 10 additions & 0 deletions client/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,13 @@ func (m *Machines) AcceptAll() error {
qsp := url.Values{}
return m.client().Post("accept_all", qsp, func(data []byte) error { return nil })
}

// ListAllocated lists allocated machines
func (m *Machines) ListAllocated() ([]entity.Machine, error) {
machines := make([]entity.Machine, 0)
err := m.client().Get("list_allocated", url.Values{}, func(data []byte) error {
return json.Unmarshal(data, &machines)
})

return machines, err
}
7 changes: 7 additions & 0 deletions entity/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ type MachinePowerState struct {
State string `json:"state,omitempty"`
}

// MachineToken represents Machine token
type MachineToken struct {
TokenKey string `json:"token_key,omitempty"`
TokenSecret string `json:"token_secret,omitempty"`
ConsumerKey string `json:"consumer_key,omitempty"`
}

// MachineParams enumerates the parameters for the machine update operation
type MachineParams struct {
PowerType string `url:"power_type,omitempty"`
Expand Down

0 comments on commit aa4013a

Please sign in to comment.