Skip to content

Commit 7260ea5

Browse files
author
Dongsu Park
committed
machine1: add a method ListImages
Add a DBUS method for `ListImages` that lists all images under `/var/lib/machines`.
1 parent bac0c92 commit 7260ea5

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

machine1/dbus.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
4960
func 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+
}

machine1/dbus_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package machine1
1919
import (
2020
"fmt"
2121
"math/rand"
22+
"os"
2223
"os/exec"
24+
"path/filepath"
2325
"testing"
2426
"time"
2527

@@ -112,3 +114,31 @@ func generateRandomLabel(n int) string {
112114
}
113115
return string(s)
114116
}
117+
118+
func TestImages(t *testing.T) {
119+
imageName := machinePrefix + generateRandomLabel(8)
120+
imagePath := filepath.Join("/var/lib/machines", imageName)
121+
122+
if _, err := os.Create(imagePath); err != nil {
123+
t.Fatal(err)
124+
}
125+
defer os.Remove(imagePath)
126+
127+
if err := os.Truncate(imagePath, 500*1024*1024); err != nil {
128+
t.Fatal(err)
129+
}
130+
131+
conn, newErr := New()
132+
if newErr != nil {
133+
t.Fatal(newErr)
134+
}
135+
136+
listImages, listErr := conn.ListImages()
137+
if listErr != nil {
138+
t.Fatal(listErr)
139+
}
140+
141+
if len(listImages) < 1 {
142+
t.Fatalf("did not find any image")
143+
}
144+
}

0 commit comments

Comments
 (0)