@@ -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.
4149func New () (* Conn , error ) {
4250 c := new (Conn )
@@ -139,3 +147,47 @@ func (c *Conn) TerminateMachine(name string) error {
139147func (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+ }
0 commit comments