1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

DIGITAL OCEAN: Update github.com/digitalocean/godo (#513)

This commit is contained in:
Tom Limoncelli
2019-06-30 08:58:25 -04:00
committed by GitHub
parent 9ff8f6926f
commit d5ff6d6e5c
34 changed files with 2469 additions and 148 deletions

View File

@ -1,10 +1,9 @@
package godo
import (
"context"
"fmt"
"net/http"
"github.com/digitalocean/godo/context"
)
const imageBasePath = "v2/images"
@ -17,8 +16,10 @@ type ImagesService interface {
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)
ListByTag(ctx context.Context, tag string, opt *ListOptions) ([]Image, *Response, error)
GetByID(context.Context, int) (*Image, *Response, error)
GetBySlug(context.Context, string) (*Image, *Response, error)
Create(context.Context, *CustomImageCreateRequest) (*Image, *Response, error)
Update(context.Context, int, *ImageUpdateRequest) (*Image, *Response, error)
Delete(context.Context, int) (*Response, error)
}
@ -33,15 +34,20 @@ var _ ImagesService = &ImagesServiceOp{}
// Image represents a DigitalOcean Image
type Image struct {
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"`
Created string `json:"created_at,omitempty"`
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"`
}
// ImageUpdateRequest represents a request to update an image.
@ -49,6 +55,16 @@ type ImageUpdateRequest struct {
Name string `json:"name"`
}
// 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"`
}
type imageRoot struct {
Image *Image
}
@ -61,6 +77,7 @@ type imagesRoot struct {
type listImageOptions struct {
Private bool `url:"private,omitempty"`
Type string `url:"type,omitempty"`
Tag string `url:"tag_name,omitempty"`
}
func (i Image) String() string {
@ -90,6 +107,12 @@ func (s *ImagesServiceOp) ListUser(ctx context.Context, opt *ListOptions) ([]Ima
return s.list(ctx, opt, &listOpt)
}
// 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)
}
// GetByID retrieves an image by id.
func (s *ImagesServiceOp) GetByID(ctx context.Context, imageID int) (*Image, *Response, error) {
if imageID < 1 {
@ -108,6 +131,25 @@ func (s *ImagesServiceOp) GetBySlug(ctx context.Context, slug string) (*Image, *
return s.get(ctx, interface{}(slug))
}
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
}
// Update an image name.
func (s *ImagesServiceOp) Update(ctx context.Context, imageID int, updateRequest *ImageUpdateRequest) (*Image, *Response, error) {
if imageID < 1 {
@ -119,7 +161,7 @@ func (s *ImagesServiceOp) Update(ctx context.Context, imageID int, updateRequest
}
path := fmt.Sprintf("%s/%d", imageBasePath, imageID)
req, err := s.client.NewRequest(ctx, "PUT", path, updateRequest)
req, err := s.client.NewRequest(ctx, http.MethodPut, path, updateRequest)
if err != nil {
return nil, nil, err
}