@@ -45,6 +45,17 @@ type MachineStatus struct {
4545 JobPath dbus.ObjectPath // The job object path
4646}
4747
48+ // ImageStatus is a set of necessary info for each machine image
49+ type ImageStatus struct {
50+ Name string // The primary image name as string
51+ ImageType string // The image type as string
52+ Readonly bool // whether it's readonly or not
53+ CreateTime uint64 // time when it's created
54+ ModifyTime uint64 // time when it's modified
55+ DiskUsage uint64 // used disk space
56+ JobPath dbus.ObjectPath // The job object path
57+ }
58+
4859// New() establishes a connection to the system bus and authenticates.
4960func New () (* Conn , error ) {
5061 c := new (Conn )
@@ -191,3 +202,59 @@ func (c *Conn) ListMachines() ([]MachineStatus, error) {
191202
192203 return machs , nil
193204}
205+
206+ func imageFromInterfaces (image []interface {}) (* ImageStatus , error ) {
207+ if len (image ) < 7 {
208+ return nil , fmt .Errorf ("invalid number of image fields: %d" , len (image ))
209+ }
210+ name , ok := image [0 ].(string )
211+ if ! ok {
212+ return nil , fmt .Errorf ("failed to typecast image field 0 to string" )
213+ }
214+ imagetype , ok := image [1 ].(string )
215+ if ! ok {
216+ return nil , fmt .Errorf ("failed to typecast imagetype field 1 to string" )
217+ }
218+ readonly , ok := image [2 ].(bool )
219+ if ! ok {
220+ return nil , fmt .Errorf ("failed to typecast readonly field 2 to bool" )
221+ }
222+ createtime , ok := image [3 ].(uint64 )
223+ if ! ok {
224+ return nil , fmt .Errorf ("failed to typecast createtime field 3 to uint64" )
225+ }
226+ modifytime , ok := image [4 ].(uint64 )
227+ if ! ok {
228+ return nil , fmt .Errorf ("failed to typecast modifytime field 4 to uint64" )
229+ }
230+ diskusage , ok := image [5 ].(uint64 )
231+ if ! ok {
232+ return nil , fmt .Errorf ("failed to typecast diskusage field 5 to uint64" )
233+ }
234+ jobpath , ok := image [6 ].(dbus.ObjectPath )
235+ if ! ok {
236+ return nil , fmt .Errorf ("failed to typecast jobpath field 6 to ObjectPath" )
237+ }
238+
239+ ret := ImageStatus {Name : name , ImageType : imagetype , Readonly : readonly , CreateTime : createtime , ModifyTime : modifytime , DiskUsage : diskusage , JobPath : jobpath }
240+ return & ret , nil
241+ }
242+
243+ // ListImages returns an array of all currently available images.
244+ func (c * Conn ) ListImages () ([]ImageStatus , error ) {
245+ result := make ([][]interface {}, 0 )
246+ if err := c .object .Call (dbusInterface + ".ListImages" , 0 ).Store (& result ); err != nil {
247+ return nil , err
248+ }
249+
250+ images := []ImageStatus {}
251+ for _ , i := range result {
252+ image , err := imageFromInterfaces (i )
253+ if err != nil {
254+ return nil , err
255+ }
256+ images = append (images , * image )
257+ }
258+
259+ return images , nil
260+ }
0 commit comments