~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/gopkg.in/goose.v1/glance/glance.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

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