-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.go
117 lines (96 loc) · 2.8 KB
/
images.go
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
package godo
import "fmt"
// Image represents a Digitalocean image.
type Image struct {
ID int `json:"id"`
Name string `json:"name"`
Distribution string `json:"distribution"`
Slug string `json:"slug"`
Public bool `json:"public"`
RegionIDs []int `json:"regions"`
RegionSlugs []string `json:"region_slugs"`
}
// DeleteImage deletes an image. There is no way to restore a deleted image so be careful and ensure any data is properly backed up.
func (c *Client) DeleteImage(ID interface{}) error {
var DOResp struct {
Status Status `json:"status"`
Message string `json:"message"`
}
var s string
switch ID.(type) {
case string, int:
s = fmt.Sprintf("/images/%v/destroy", ID)
default:
return fmt.Errorf("ID must be either a string or integer")
}
err := c.doGet(s, &DOResp)
if err != nil {
return err
}
if DOResp.Status == StatusError {
return fmt.Errorf("could not delete image with ID %v: %v", ID, DOResp.Message)
}
return nil
}
// GetAllImages returns all available images for the client ID.
func (c *Client) GetAllImages() ([]Image, error) {
var DOResp struct {
Status Status `json:"status"`
Images []Image `json:"images"`
Message string `json:"message"`
}
err := c.doGet("/images", &DOResp)
if err != nil {
return nil, err
}
if DOResp.Status == StatusError {
return nil, fmt.Errorf("could not get images: %v", DOResp.Message)
}
return DOResp.Images, nil
}
// GetImageByID returns information about an image by its ID, which can be either integer or string
func (c *Client) GetImageByID(ID interface{}) (*Image, error) {
var DOResp struct {
Status Status `json:"status"`
Image Image `json:"image"`
Message string `json:"message"`
}
var s string
switch ID.(type) {
case string, int:
s = fmt.Sprintf("/images/%v", ID)
default:
return nil, fmt.Errorf("ID must be either a string or integer")
}
err := c.doGet(s, &DOResp)
if err != nil {
return nil, err
}
if DOResp.Status == StatusError {
return nil, fmt.Errorf("could not get image with ID %v: %v", ID, DOResp.Message)
}
return &DOResp.Image, nil
}
// TransferImage transfers an image to a specified region. Returns an event ID on success.
func (c *Client) TransferImage(ID interface{}, regionID int) (int, error) {
var DOResp struct {
Status Status `json:"status"`
EventID int `json:"event_id"`
Message string `json:"message"`
}
var s string
switch ID.(type) {
case string, int:
s = fmt.Sprintf("/images/%v/transfer?region_id=%d", ID, regionID)
default:
return 0, fmt.Errorf("ID must be either a string or integer")
}
err := c.doGet(s, &DOResp)
if err != nil {
return 0, err
}
if DOResp.Status == StatusError {
return 0, fmt.Errorf("could not transfer image with ID %v: %v", ID, DOResp.Message)
}
return DOResp.EventID, nil
}