Debian Codegraph @

cloudapi/images.go

raw symbols SCIP
1 package cloudapi
2
3 import (
4 "fmt"
5 "net/http"
6
7 "github.com/joyent/gocommon/client"
8 "github.com/joyent/gocommon/errors"
9 )
10
11 // Image represent the software packages that will be available on newly provisioned machines
12 type Image struct {
13 Id string // Unique identifier for the image
14 Name string // Image friendly name
15 OS string // Underlying operating system
16 Version string // Image version
17 Type string // Image type, one of 'smartmachine' or 'virtualmachine'
18 Description string // Image description
19 Requirements map[string]interface{} // Minimum requirements for provisioning a machine with this image, e.g. 'password' indicates that a password must be provided
20 Homepage string // URL for a web page including detailed information for this image (new in API version 7.0)
21 PublishedAt string `json:"published_at"` // Time this image has been made publicly available (new in API version 7.0)
22 Public bool // Indicates if the image is publicly available (new in API version 7.1)
23 State string // Current image state. One of 'active', 'unactivated', 'disabled', 'creating', 'failed' (new in API version 7.1)
24 Tags map[string]string // A map of key/value pairs that allows clients to categorize images by any given criteria (new in API version 7.1)
25 EULA string // URL of the End User License Agreement (EULA) for the image (new in API version 7.1)
26 ACL []string // An array of account UUIDs given access to a private image. The field is only relevant to private images (new in API version 7.1)
27 Owner string // The UUID of the user owning the image
28 }
29
30 // ExportImageOpts represent the option that can be specified
31 // when exporting an image.
32 type ExportImageOpts struct {
33 MantaPath string `json:"manta_path"` // The Manta path prefix to use when exporting the image
34 }
35
36 // MantaLocation represent the properties that allow a user
37 // to retrieve the image file and manifest from Manta
38 type MantaLocation struct {
39 MantaURL string `json:"manta_url"` // Manta datacenter URL
40 ImagePath string `json:"image_path"` // Path to the image
41 ManifestPath string `json:"manifest_path"` // Path to the image manifest
42 }
43
44 // CreateImageFromMachineOpts represent the option that can be specified
45 // when creating a new image from an existing machine.
46 type CreateImageFromMachineOpts struct {
47 Machine string `json:"machine"` // The machine UUID from which the image is to be created
48 Name string `json:"name"` // Image name
49 Version string `json:"version"` // Image version
50 Description string `json:"description,omitempty"` // Image description
51 Homepage string `json:"homepage,omitempty"` // URL for a web page including detailed information for this image
52 EULA string `json:"eula,omitempty"` // URL of the End User License Agreement (EULA) for the image
53 ACL []string `json:"acl,omitempty"` // An array of account UUIDs given access to a private image. The field is only relevant to private images
54 Tags map[string]string `json:"tags,omitempty"` // A map of key/value pairs that allows clients to categorize images by any given criteria
55 }
56
57 // ListImages provides a list of images available in the datacenter.
58 // See API docs: http://apidocs.joyent.com/cloudapi/#ListImages
59 func (c *Client) ListImages(filter *Filter) ([]Image, error) {
60 var resp []Image
61 req := request{
62 method: client.GET,
63 url: apiImages,
64 filter: filter,
65 resp: &resp,
66 }
67 if _, err := c.sendRequest(req); err != nil {
68 return nil, errors.Newf(err, "failed to get list of images")
69 }
70 return resp, nil
71 }
72
73 // GetImage returns the image specified by imageId.
74 // See API docs: http://apidocs.joyent.com/cloudapi/#GetImage
75 func (c *Client) GetImage(imageID string) (*Image, error) {
76 var resp Image
77 req := request{
78 method: client.GET,
79 url: makeURL(apiImages, imageID),
80 resp: &resp,
81 }
82 if _, err := c.sendRequest(req); err != nil {
83 return nil, errors.Newf(err, "failed to get image with id: %s", imageID)
84 }
85 return &resp, nil
86 }
87
88 // DeleteImage (Beta) Delete the image specified by imageId. Must be image owner to do so.
89 // See API docs: http://apidocs.joyent.com/cloudapi/#DeleteImage
90 func (c *Client) DeleteImage(imageID string) error {
91 req := request{
92 method: client.DELETE,
93 url: makeURL(apiImages, imageID),
94 expectedStatus: http.StatusNoContent,
95 }
96 if _, err := c.sendRequest(req); err != nil {
97 return errors.Newf(err, "failed to delete image with id: %s", imageID)
98 }
99 return nil
100 }
101
102 // ExportImage (Beta) Exports an image to the specified Manta path.
103 // See API docs: http://apidocs.joyent.com/cloudapi/#ListImages
104 func (c *Client) ExportImage(imageID string, opts ExportImageOpts) (*MantaLocation, error) {
105 var resp MantaLocation
106 req := request{
107 method: client.POST,
108 url: fmt.Sprintf("%s/%s?action=%s", apiImages, imageID, actionExport),
109 reqValue: opts,
110 resp: &resp,
111 }
112 if _, err := c.sendRequest(req); err != nil {
113 return nil, errors.Newf(err, "failed to export image %s to %s", imageID, opts.MantaPath)
114 }
115 return &resp, nil
116 }
117
118 // CreateImageFromMachine (Beta) Create a new custom image from a machine.
119 // See API docs: http://apidocs.joyent.com/cloudapi/#ListImages
120 func (c *Client) CreateImageFromMachine(opts CreateImageFromMachineOpts) (*Image, error) {
121 var resp Image
122 req := request{
123 method: client.POST,
124 url: apiImages,
125 reqValue: opts,
126 resp: &resp,
127 expectedStatus: http.StatusCreated,
128 }
129 if _, err := c.sendRequest(req); err != nil {
130 return nil, errors.Newf(err, "failed to create image from machine %s", opts.Machine)
131 }
132 return &resp, nil
133 }