Skip to content

Commit bac0c92

Browse files
author
Dongsu Park
committed
machine1: add a method ListMachines
Add a DBUS method for `ListMachines` that lists all active machines.
1 parent eee3db3 commit bac0c92

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

machine1/dbus.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ type Conn struct {
3737
object dbus.BusObject
3838
}
3939

40+
// MachineStatus is a set of necessary info for each machine
41+
type MachineStatus struct {
42+
Name string // The primary machine name as string
43+
Class string // The machine class as string
44+
Service string // The machine service as string
45+
JobPath dbus.ObjectPath // The job object path
46+
}
47+
4048
// New() establishes a connection to the system bus and authenticates.
4149
func New() (*Conn, error) {
4250
c := new(Conn)
@@ -139,3 +147,47 @@ func (c *Conn) TerminateMachine(name string) error {
139147
func (c *Conn) RegisterMachine(name string, id []byte, service string, class string, pid int, root_directory string) error {
140148
return c.object.Call(dbusInterface+".RegisterMachine", 0, name, id, service, class, uint32(pid), root_directory).Err
141149
}
150+
151+
func machineFromInterfaces(machine []interface{}) (*MachineStatus, error) {
152+
if len(machine) < 4 {
153+
return nil, fmt.Errorf("invalid number of machine fields: %d", len(machine))
154+
}
155+
name, ok := machine[0].(string)
156+
if !ok {
157+
return nil, fmt.Errorf("failed to typecast machine field 0 to string")
158+
}
159+
class, ok := machine[1].(string)
160+
if !ok {
161+
return nil, fmt.Errorf("failed to typecast class field 1 to string")
162+
}
163+
service, ok := machine[2].(string)
164+
if !ok {
165+
return nil, fmt.Errorf("failed to typecast service field 2 to string")
166+
}
167+
jobpath, ok := machine[3].(dbus.ObjectPath)
168+
if !ok {
169+
return nil, fmt.Errorf("failed to typecast jobpath field 3 to ObjectPath")
170+
}
171+
172+
ret := MachineStatus{Name: name, Class: class, Service: service, JobPath: jobpath}
173+
return &ret, nil
174+
}
175+
176+
// ListMachines returns an array of all currently running machines.
177+
func (c *Conn) ListMachines() ([]MachineStatus, error) {
178+
result := make([][]interface{}, 0)
179+
if err := c.object.Call(dbusInterface+".ListMachines", 0).Store(&result); err != nil {
180+
return nil, err
181+
}
182+
183+
machs := []MachineStatus{}
184+
for _, i := range result {
185+
machine, err := machineFromInterfaces(i)
186+
if err != nil {
187+
return nil, err
188+
}
189+
machs = append(machs, *machine)
190+
}
191+
192+
return machs, nil
193+
}

machine1/dbus_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ func TestMachine(t *testing.T) {
8080
t.Fatalf("did not find machine named %s", machineName)
8181
}
8282

83+
listMachines, getErr := conn.ListMachines()
84+
if getErr != nil {
85+
t.Fatal(getErr)
86+
}
87+
88+
// listMachines includes also `.host`, so by default the length should be greater than 1
89+
if len(listMachines) <= 1 {
90+
t.Fatalf("did not find any machine")
91+
}
92+
8393
tErr := conn.TerminateMachine(machineName)
8494
if tErr != nil {
8595
t.Fatal(tErr)

0 commit comments

Comments
 (0)