| 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 |
|
| 12 |
type Image struct { |
| 13 |
Id string |
| 14 |
Name string |
| 15 |
OS string |
| 16 |
Version string |
| 17 |
Type string |
| 18 |
Description string |
| 19 |
Requirements map[string]interface{} |
| 20 |
Homepage string |
| 21 |
PublishedAt string `json:"published_at"` |
| 22 |
Public bool |
| 23 |
State string |
| 24 |
Tags map[string]string |
| 25 |
EULA string |
| 26 |
ACL []string |
| 27 |
Owner string |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
type ExportImageOpts struct { |
| 33 |
MantaPath string `json:"manta_path"` |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
type MantaLocation struct { |
| 39 |
MantaURL string `json:"manta_url"` |
| 40 |
ImagePath string `json:"image_path"` |
| 41 |
ManifestPath string `json:"manifest_path"` |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
type CreateImageFromMachineOpts struct { |
| 47 |
Machine string `json:"machine"` |
| 48 |
Name string `json:"name"` |
| 49 |
Version string `json:"version"` |
| 50 |
Description string `json:"description,omitempty"` |
| 51 |
Homepage string `json:"homepage,omitempty"` |
| 52 |
EULA string `json:"eula,omitempty"` |
| 53 |
ACL []string `json:"acl,omitempty"` |
| 54 |
Tags map[string]string `json:"tags,omitempty"` |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 74 |
|
| 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 |
|
| 89 |
|
| 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 |
|
| 103 |
|
| 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 |
|
| 119 |
|
| 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 |
} |