OLD | NEW |
(Empty) | |
| 1 // The glance package provides a way to access the OpenStack Image Service APIs. |
| 2 // See http://docs.openstack.org/api/openstack-image-service/2.0/content/. |
| 3 package glance |
| 4 |
| 5 import ( |
| 6 "fmt" |
| 7 "launchpad.net/goose/client" |
| 8 goosehttp "launchpad.net/goose/http" |
| 9 "net/http" |
| 10 ) |
| 11 |
| 12 const ( |
| 13 apiImages = "/images" |
| 14 apiImagesDetail = "/images/detail" |
| 15 ) |
| 16 |
| 17 // Client provides a means to access the OpenStack Image Service. |
| 18 type Client struct { |
| 19 client client.Client |
| 20 } |
| 21 |
| 22 func New(client client.Client) *Client { |
| 23 return &Client{client} |
| 24 } |
| 25 |
| 26 type Link struct { |
| 27 Href string |
| 28 Rel string |
| 29 Type string |
| 30 } |
| 31 |
| 32 type Image struct { |
| 33 Id string |
| 34 Name string |
| 35 Links []Link |
| 36 } |
| 37 |
| 38 // ListImages lists IDs, names, and links for available images. |
| 39 func (c *Client) ListImages() ([]Image, error) { |
| 40 var resp struct { |
| 41 Images []Image |
| 42 } |
| 43 requestData := goosehttp.RequestData{RespValue: &resp, ExpectedStatus: [
]int{http.StatusOK}} |
| 44 err := c.client.SendRequest(client.GET, "compute", apiImages, &requestDa
ta, |
| 45 "failed to get list of images") |
| 46 return resp.Images, err |
| 47 } |
| 48 |
| 49 type ImageMetadata struct { |
| 50 Architecture string |
| 51 State string `json:"image_state"` |
| 52 Location string `json:"image_location"` |
| 53 KernelId interface{} `json:"kernel_id"` |
| 54 ProjectId interface{} `json:"project_id"` |
| 55 RAMDiskId interface{} `json:"ramdisk_id"` |
| 56 OwnerId interface{} `json:"owner_id"` |
| 57 } |
| 58 |
| 59 type ImageDetail struct { |
| 60 Id string |
| 61 Name string |
| 62 Created string |
| 63 Updated string |
| 64 Progress int |
| 65 Status string |
| 66 MinimumRAM int `json:"minRam"` |
| 67 MinimumDisk int `json:"minDisk"` |
| 68 Links []Link |
| 69 Metadata ImageMetadata |
| 70 } |
| 71 |
| 72 // ListImageDetails lists all details for available images. |
| 73 func (c *Client) ListImagesDetail() ([]ImageDetail, error) { |
| 74 var resp struct { |
| 75 Images []ImageDetail |
| 76 } |
| 77 requestData := goosehttp.RequestData{RespValue: &resp} |
| 78 err := c.client.SendRequest(client.GET, "compute", apiImagesDetail, &req
uestData, |
| 79 "failed to get list of images details") |
| 80 return resp.Images, err |
| 81 } |
| 82 |
| 83 // GetImageDetail lists details of the specified image. |
| 84 func (c *Client) GetImageDetail(imageId string) (ImageDetail, error) { |
| 85 var resp struct { |
| 86 Image ImageDetail |
| 87 } |
| 88 url := fmt.Sprintf("%s/%s", apiImages, imageId) |
| 89 requestData := goosehttp.RequestData{RespValue: &resp} |
| 90 err := c.client.SendRequest(client.GET, "compute", url, &requestData, |
| 91 "failed to get details for imageId=%s", imageId) |
| 92 return resp.Image, err |
| 93 } |
OLD | NEW |