-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathmicro_droplet_images.go
More file actions
170 lines (139 loc) · 5.35 KB
/
Copy pathmicro_droplet_images.go
File metadata and controls
170 lines (139 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package godo
import (
"context"
"fmt"
"net/http"
)
const microDropletImageBasePath = "v2/microdroplets/images"
// MicroDropletImageStatus represents the lifecycle status of a MicroDroplet image.
type MicroDropletImageStatus string
// Possible states for a MicroDroplet image.
const (
MicroDropletImageStatusUnknown = MicroDropletImageStatus("IMAGE_UNKNOWN")
MicroDropletImageStatusImporting = MicroDropletImageStatus("IMAGE_IMPORTING")
MicroDropletImageStatusAvailable = MicroDropletImageStatus("IMAGE_AVAILABLE")
MicroDropletImageStatusFailed = MicroDropletImageStatus("IMAGE_FAILED")
MicroDropletImageStatusDeleted = MicroDropletImageStatus("IMAGE_DELETED")
)
// MicroDropletImagesService is an interface for interfacing with the
// MicroDroplet image endpoints of the DigitalOcean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/MicroDroplets
type MicroDropletImagesService interface {
List(ctx context.Context, opt *ListOptions) ([]MicroDropletImage, *Response, error)
Get(ctx context.Context, id string) (*MicroDropletImage, *Response, error)
Create(ctx context.Context, createRequest *MicroDropletImageCreateRequest) (*MicroDropletImage, *Response, error)
Delete(ctx context.Context, id string) (*Response, error)
}
// MicroDropletImagesServiceOp handles communication with the MicroDroplet
// image related methods of the DigitalOcean API.
type MicroDropletImagesServiceOp struct {
client *Client
}
var _ MicroDropletImagesService = &MicroDropletImagesServiceOp{}
// MicroDropletImage represents an OCI image imported for use with MicroDroplets.
type MicroDropletImage struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Region string `json:"region,omitempty"`
Source string `json:"source,omitempty"`
Status MicroDropletImageStatus `json:"status,omitempty"`
Created string `json:"created_at,omitempty"`
}
// MicroDropletImageCreateRequest represents a request to import a new
// MicroDroplet image from a public OCI ref or a DOCR ref.
type MicroDropletImageCreateRequest struct {
Name string `json:"name"`
Region string `json:"region"`
Source string `json:"source"`
}
// String returns a human-readable description of a MicroDropletImage.
func (i MicroDropletImage) String() string {
return Stringify(i)
}
// URN returns the MicroDropletImage ID in a valid DO API URN form.
func (i MicroDropletImage) URN() string {
return ToURN("microdroplet_image", i.ID)
}
// String returns a human-readable description of a MicroDropletImageCreateRequest.
func (r MicroDropletImageCreateRequest) String() string {
return Stringify(r)
}
type microDropletImageRoot struct {
Image *MicroDropletImage `json:"image"`
}
type microDropletImagesRoot struct {
Images []MicroDropletImage `json:"images"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}
// List lists all MicroDroplet images, with optional pagination.
func (s *MicroDropletImagesServiceOp) List(ctx context.Context, opt *ListOptions) ([]MicroDropletImage, *Response, error) {
path, err := addOptions(microDropletImageBasePath, opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(microDropletImagesRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Images, resp, nil
}
// Get retrieves a MicroDroplet image by its ID.
func (s *MicroDropletImagesServiceOp) Get(ctx context.Context, id string) (*MicroDropletImage, *Response, error) {
if id == "" {
return nil, nil, NewArgError("id", "cannot be empty")
}
path := fmt.Sprintf("%s/%s", microDropletImageBasePath, id)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(microDropletImageRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Image, resp, nil
}
// Create imports a new MicroDroplet image from an OCI ref. Image import is
// asynchronous; callers should poll Get and inspect Status until it reports
// MicroDropletImageStatusAvailable or MicroDropletImageStatusFailed.
func (s *MicroDropletImagesServiceOp) Create(ctx context.Context, createRequest *MicroDropletImageCreateRequest) (*MicroDropletImage, *Response, error) {
if createRequest == nil {
return nil, nil, NewArgError("createRequest", "cannot be nil")
}
req, err := s.client.NewRequest(ctx, http.MethodPost, microDropletImageBasePath, createRequest)
if err != nil {
return nil, nil, err
}
root := new(microDropletImageRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Image, resp, nil
}
// Delete removes a MicroDroplet image by its ID. The DigitalOcean API returns
// a 204 on success and does not include a response body.
func (s *MicroDropletImagesServiceOp) Delete(ctx context.Context, id string) (*Response, error) {
if id == "" {
return nil, NewArgError("id", "cannot be empty")
}
path := fmt.Sprintf("%s/%s", microDropletImageBasePath, id)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}