2017-03-31 00:25:27 +02:00
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
2017-05-10 23:54:06 +02:00
|
|
|
"context"
|
2017-03-31 00:25:27 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/errwrap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ImagesClient struct {
|
|
|
|
*Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Images returns a c used for accessing functions pertaining to
|
|
|
|
// Images functionality in the Triton API.
|
|
|
|
func (c *Client) Images() *ImagesClient {
|
|
|
|
return &ImagesClient{c}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ImageFile struct {
|
|
|
|
Compression string `json:"compression"`
|
|
|
|
SHA1 string `json:"sha1"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Image struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
OS string `json:"os"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Requirements map[string]interface{} `json:"requirements"`
|
|
|
|
Homepage string `json:"homepage"`
|
|
|
|
Files []*ImageFile `json:"files"`
|
|
|
|
PublishedAt time.Time `json:"published_at"`
|
|
|
|
Owner string `json:"owner"`
|
|
|
|
Public bool `json:"public"`
|
|
|
|
State string `json:"state"`
|
|
|
|
Tags map[string]string `json:"tags"`
|
|
|
|
EULA string `json:"eula"`
|
|
|
|
ACL []string `json:"acl"`
|
|
|
|
Error TritonError `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ListImagesInput struct{}
|
|
|
|
|
2017-05-10 23:54:06 +02:00
|
|
|
func (client *ImagesClient) ListImages(ctx context.Context, _ *ListImagesInput) ([]*Image, error) {
|
2017-04-28 20:54:11 +02:00
|
|
|
path := fmt.Sprintf("/%s/images", client.accountName)
|
2017-05-10 23:54:06 +02:00
|
|
|
respReader, err := client.executeRequest(ctx, http.MethodGet, path, nil)
|
2017-03-31 00:25:27 +02:00
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error executing ListImages request: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []*Image
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error decoding ListImages response: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetImageInput struct {
|
|
|
|
ImageID string
|
|
|
|
}
|
|
|
|
|
2017-05-10 23:54:06 +02:00
|
|
|
func (client *ImagesClient) GetImage(ctx context.Context, input *GetImageInput) (*Image, error) {
|
2017-03-31 00:25:27 +02:00
|
|
|
path := fmt.Sprintf("/%s/images/%s", client.accountName, input.ImageID)
|
2017-05-10 23:54:06 +02:00
|
|
|
respReader, err := client.executeRequest(ctx, http.MethodGet, path, nil)
|
2017-03-31 00:25:27 +02:00
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error executing GetImage request: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result *Image
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error decoding GetImage response: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteImageInput struct {
|
|
|
|
ImageID string
|
|
|
|
}
|
|
|
|
|
2017-05-10 23:54:06 +02:00
|
|
|
func (client *ImagesClient) DeleteImage(ctx context.Context, input *DeleteImageInput) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
path := fmt.Sprintf("/%s/images/%s", client.accountName, input.ImageID)
|
2017-05-10 23:54:06 +02:00
|
|
|
respReader, err := client.executeRequest(ctx, http.MethodDelete, path, nil)
|
2017-03-31 00:25:27 +02:00
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error executing DeleteKey request: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExportImageInput struct {
|
|
|
|
ImageID string
|
|
|
|
MantaPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
type MantaLocation struct {
|
|
|
|
MantaURL string `json:"manta_url"`
|
|
|
|
ImagePath string `json:"image_path"`
|
|
|
|
ManifestPath string `json:"manifest_path"`
|
|
|
|
}
|
|
|
|
|
2017-05-10 23:54:06 +02:00
|
|
|
func (client *ImagesClient) ExportImage(ctx context.Context, input *ExportImageInput) (*MantaLocation, error) {
|
2017-03-31 00:25:27 +02:00
|
|
|
path := fmt.Sprintf("/%s/images/%s", client.accountName, input.ImageID)
|
|
|
|
query := &url.Values{}
|
|
|
|
query.Set("action", "export")
|
|
|
|
query.Set("manta_path", input.MantaPath)
|
|
|
|
|
2017-05-10 23:54:06 +02:00
|
|
|
respReader, err := client.executeRequestURIParams(ctx, http.MethodGet, path, nil, query)
|
2017-03-31 00:25:27 +02:00
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error executing GetImage request: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result *MantaLocation
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error decoding GetImage response: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateImageFromMachineInput struct {
|
|
|
|
MachineID string `json:"machine"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Version string `json:"version,omitempty"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
HomePage string `json:"homepage,omitempty"`
|
|
|
|
EULA string `json:"eula,omitempty"`
|
|
|
|
ACL []string `json:"acl,omitempty"`
|
2017-04-28 20:54:11 +02:00
|
|
|
Tags map[string]string `json:"tags,omitempty"`
|
2017-03-31 00:25:27 +02:00
|
|
|
}
|
|
|
|
|
2017-05-10 23:54:06 +02:00
|
|
|
func (client *ImagesClient) CreateImageFromMachine(ctx context.Context, input *CreateImageFromMachineInput) (*Image, error) {
|
2017-03-31 00:25:27 +02:00
|
|
|
path := fmt.Sprintf("/%s/images", client.accountName)
|
2017-05-10 23:54:06 +02:00
|
|
|
respReader, err := client.executeRequest(ctx, http.MethodPost, path, input)
|
2017-03-31 00:25:27 +02:00
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error executing CreateImageFromMachine request: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result *Image
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error decoding CreateImageFromMachine response: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateImageInput struct {
|
|
|
|
ImageID string `json:"-"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Version string `json:"version,omitempty"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
HomePage string `json:"homepage,omitempty"`
|
|
|
|
EULA string `json:"eula,omitempty"`
|
|
|
|
ACL []string `json:"acl,omitempty"`
|
2017-04-28 20:54:11 +02:00
|
|
|
Tags map[string]string `json:"tags,omitempty"`
|
2017-03-31 00:25:27 +02:00
|
|
|
}
|
|
|
|
|
2017-05-10 23:54:06 +02:00
|
|
|
func (client *ImagesClient) UpdateImage(ctx context.Context, input *UpdateImageInput) (*Image, error) {
|
2017-03-31 00:25:27 +02:00
|
|
|
path := fmt.Sprintf("/%s/images/%s", client.accountName, input.ImageID)
|
|
|
|
query := &url.Values{}
|
|
|
|
query.Set("action", "update")
|
|
|
|
|
2017-05-10 23:54:06 +02:00
|
|
|
respReader, err := client.executeRequestURIParams(ctx, http.MethodPost, path, input, query)
|
2017-03-31 00:25:27 +02:00
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error executing UpdateImage request: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result *Image
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error decoding UpdateImage response: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|