2016-02-22 14:44:12 -05:00
|
|
|
package godo
|
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-11-21 12:04:55 -05:00
|
|
|
"net/http"
|
2017-04-08 15:52:57 -04:00
|
|
|
)
|
2016-02-22 14:44:12 -05:00
|
|
|
|
|
|
|
const imageBasePath = "v2/images"
|
|
|
|
|
|
|
|
// ImagesService is an interface for interfacing with the images
|
|
|
|
// endpoints of the DigitalOcean API
|
|
|
|
// See: https://developers.digitalocean.com/documentation/v2#images
|
|
|
|
type ImagesService interface {
|
2017-04-08 15:52:57 -04:00
|
|
|
List(context.Context, *ListOptions) ([]Image, *Response, error)
|
|
|
|
ListDistribution(ctx context.Context, opt *ListOptions) ([]Image, *Response, error)
|
|
|
|
ListApplication(ctx context.Context, opt *ListOptions) ([]Image, *Response, error)
|
|
|
|
ListUser(ctx context.Context, opt *ListOptions) ([]Image, *Response, error)
|
2018-11-21 12:04:55 -05:00
|
|
|
ListByTag(ctx context.Context, tag string, opt *ListOptions) ([]Image, *Response, error)
|
2017-04-08 15:52:57 -04:00
|
|
|
GetByID(context.Context, int) (*Image, *Response, error)
|
|
|
|
GetBySlug(context.Context, string) (*Image, *Response, error)
|
2018-11-21 12:04:55 -05:00
|
|
|
Create(context.Context, *CustomImageCreateRequest) (*Image, *Response, error)
|
2017-04-08 15:52:57 -04:00
|
|
|
Update(context.Context, int, *ImageUpdateRequest) (*Image, *Response, error)
|
|
|
|
Delete(context.Context, int) (*Response, error)
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ImagesServiceOp handles communication with the image related methods of the
|
|
|
|
// DigitalOcean API.
|
|
|
|
type ImagesServiceOp struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ ImagesService = &ImagesServiceOp{}
|
|
|
|
|
|
|
|
// Image represents a DigitalOcean Image
|
|
|
|
type Image struct {
|
2018-11-21 12:04:55 -05:00
|
|
|
ID int `json:"id,float64,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Distribution string `json:"distribution,omitempty"`
|
|
|
|
Slug string `json:"slug,omitempty"`
|
|
|
|
Public bool `json:"public,omitempty"`
|
|
|
|
Regions []string `json:"regions,omitempty"`
|
|
|
|
MinDiskSize int `json:"min_disk_size,omitempty"`
|
|
|
|
SizeGigaBytes float64 `json:"size_gigabytes,omitempty"`
|
|
|
|
Created string `json:"created_at,omitempty"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
Tags []string `json:"tags,omitempty"`
|
|
|
|
Status string `json:"status,omitempty"`
|
|
|
|
ErrorMessage string `json:"error_message,omitempty"`
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ImageUpdateRequest represents a request to update an image.
|
|
|
|
type ImageUpdateRequest struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
// CustomImageCreateRequest represents a request to create a custom image.
|
|
|
|
type CustomImageCreateRequest struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Url string `json:"url"`
|
|
|
|
Region string `json:"region"`
|
|
|
|
Distribution string `json:"distribution,omitempty"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
Tags []string `json:"tags,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
type imageRoot struct {
|
2017-04-08 15:52:57 -04:00
|
|
|
Image *Image
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type imagesRoot struct {
|
|
|
|
Images []Image
|
|
|
|
Links *Links `json:"links"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type listImageOptions struct {
|
|
|
|
Private bool `url:"private,omitempty"`
|
|
|
|
Type string `url:"type,omitempty"`
|
2018-11-21 12:04:55 -05:00
|
|
|
Tag string `url:"tag_name,omitempty"`
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i Image) String() string {
|
|
|
|
return Stringify(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List lists all the images available.
|
2017-04-08 15:52:57 -04:00
|
|
|
func (s *ImagesServiceOp) List(ctx context.Context, opt *ListOptions) ([]Image, *Response, error) {
|
|
|
|
return s.list(ctx, opt, nil)
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListDistribution lists all the distribution images.
|
2017-04-08 15:52:57 -04:00
|
|
|
func (s *ImagesServiceOp) ListDistribution(ctx context.Context, opt *ListOptions) ([]Image, *Response, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
listOpt := listImageOptions{Type: "distribution"}
|
2017-04-08 15:52:57 -04:00
|
|
|
return s.list(ctx, opt, &listOpt)
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListApplication lists all the application images.
|
2017-04-08 15:52:57 -04:00
|
|
|
func (s *ImagesServiceOp) ListApplication(ctx context.Context, opt *ListOptions) ([]Image, *Response, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
listOpt := listImageOptions{Type: "application"}
|
2017-04-08 15:52:57 -04:00
|
|
|
return s.list(ctx, opt, &listOpt)
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListUser lists all the user images.
|
2017-04-08 15:52:57 -04:00
|
|
|
func (s *ImagesServiceOp) ListUser(ctx context.Context, opt *ListOptions) ([]Image, *Response, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
listOpt := listImageOptions{Private: true}
|
2017-04-08 15:52:57 -04:00
|
|
|
return s.list(ctx, opt, &listOpt)
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
// ListByTag lists all images with a specific tag applied.
|
|
|
|
func (s *ImagesServiceOp) ListByTag(ctx context.Context, tag string, opt *ListOptions) ([]Image, *Response, error) {
|
|
|
|
listOpt := listImageOptions{Tag: tag}
|
|
|
|
return s.list(ctx, opt, &listOpt)
|
|
|
|
}
|
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
// GetByID retrieves an image by id.
|
2017-04-08 15:52:57 -04:00
|
|
|
func (s *ImagesServiceOp) GetByID(ctx context.Context, imageID int) (*Image, *Response, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
if imageID < 1 {
|
|
|
|
return nil, nil, NewArgError("imageID", "cannot be less than 1")
|
|
|
|
}
|
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
return s.get(ctx, interface{}(imageID))
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBySlug retrieves an image by slug.
|
2017-04-08 15:52:57 -04:00
|
|
|
func (s *ImagesServiceOp) GetBySlug(ctx context.Context, slug string) (*Image, *Response, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
if len(slug) < 1 {
|
|
|
|
return nil, nil, NewArgError("slug", "cannot be blank")
|
|
|
|
}
|
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
return s.get(ctx, interface{}(slug))
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
func (s *ImagesServiceOp) Create(ctx context.Context, createRequest *CustomImageCreateRequest) (*Image, *Response, error) {
|
|
|
|
if createRequest == nil {
|
|
|
|
return nil, nil, NewArgError("createRequest", "cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(ctx, http.MethodPost, imageBasePath, createRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(imageRoot)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Image, resp, err
|
|
|
|
}
|
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
// Update an image name.
|
2017-04-08 15:52:57 -04:00
|
|
|
func (s *ImagesServiceOp) Update(ctx context.Context, imageID int, updateRequest *ImageUpdateRequest) (*Image, *Response, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
if imageID < 1 {
|
|
|
|
return nil, nil, NewArgError("imageID", "cannot be less than 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateRequest == nil {
|
|
|
|
return nil, nil, NewArgError("updateRequest", "cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("%s/%d", imageBasePath, imageID)
|
2018-11-21 12:04:55 -05:00
|
|
|
req, err := s.client.NewRequest(ctx, http.MethodPut, path, updateRequest)
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(imageRoot)
|
2018-11-21 12:04:55 -05:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
return root.Image, resp, err
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete an image.
|
2017-04-08 15:52:57 -04:00
|
|
|
func (s *ImagesServiceOp) Delete(ctx context.Context, imageID int) (*Response, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
if imageID < 1 {
|
|
|
|
return nil, NewArgError("imageID", "cannot be less than 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("%s/%d", imageBasePath, imageID)
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
resp, err := s.client.Do(ctx, req, nil)
|
2016-02-22 14:44:12 -05:00
|
|
|
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method for getting an individual image
|
2017-04-08 15:52:57 -04:00
|
|
|
func (s *ImagesServiceOp) get(ctx context.Context, ID interface{}) (*Image, *Response, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
path := fmt.Sprintf("%s/%v", imageBasePath, ID)
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(imageRoot)
|
2018-11-21 12:04:55 -05:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
return root.Image, resp, err
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method for listing images
|
2017-04-08 15:52:57 -04:00
|
|
|
func (s *ImagesServiceOp) list(ctx context.Context, opt *ListOptions, listOpt *listImageOptions) ([]Image, *Response, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
path := imageBasePath
|
|
|
|
path, err := addOptions(path, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
path, err = addOptions(path, listOpt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(imagesRoot)
|
2018-11-21 12:04:55 -05:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
if l := root.Links; l != nil {
|
|
|
|
resp.Links = l
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Images, resp, err
|
|
|
|
}
|