-
Notifications
You must be signed in to change notification settings - Fork 56
/
iso.go
160 lines (128 loc) · 4.08 KB
/
iso.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
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
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// ISOService is the interface to interact with the ISO endpoints on the Vultr API
// Link : https://www.vultr.com/api/#tag/iso
type ISOService interface {
Create(ctx context.Context, isoReq *ISOReq) (*ISO, *http.Response, error)
Get(ctx context.Context, isoID string) (*ISO, *http.Response, error)
Delete(ctx context.Context, isoID string) error
List(ctx context.Context, options *ListOptions) ([]ISO, *Meta, *http.Response, error)
ListPublic(ctx context.Context, options *ListOptions) ([]PublicISO, *Meta, *http.Response, error)
}
// ISOServiceHandler handles interaction with the ISO methods for the Vultr API
type ISOServiceHandler struct {
client *Client
}
// ISO represents ISOs currently available on this account.
type ISO struct {
ID string `json:"id"`
DateCreated string `json:"date_created"`
FileName string `json:"filename"`
Size int `json:"size,omitempty"`
MD5Sum string `json:"md5sum,omitempty"`
SHA512Sum string `json:"sha512sum,omitempty"`
Status string `json:"status"`
}
// PublicISO represents public ISOs offered in the Vultr ISO library.
type PublicISO struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
MD5Sum string `json:"md5sum,omitempty"`
}
// ISOReq is used for creating ISOs.
type ISOReq struct {
URL string `json:"url"`
}
type isosBase struct {
ISOs []ISO `json:"isos"`
Meta *Meta `json:"meta"`
}
type isoBase struct {
ISO *ISO `json:"iso"`
}
type publicIsosBase struct {
PublicIsos []PublicISO `json:"public_isos"`
Meta *Meta `json:"meta"`
}
// Create will create a new ISO image on your account
func (i *ISOServiceHandler) Create(ctx context.Context, isoReq *ISOReq) (*ISO, *http.Response, error) {
uri := "/v2/iso"
req, err := i.client.NewRequest(ctx, http.MethodPost, uri, isoReq)
if err != nil {
return nil, nil, err
}
iso := new(isoBase)
resp, err := i.client.DoWithContext(ctx, req, iso)
if err != nil {
return nil, resp, err
}
return iso.ISO, resp, nil
}
// Get an ISO
func (i *ISOServiceHandler) Get(ctx context.Context, isoID string) (*ISO, *http.Response, error) {
uri := fmt.Sprintf("/v2/iso/%s", isoID)
req, err := i.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
iso := new(isoBase)
resp, err := i.client.DoWithContext(ctx, req, iso)
if err != nil {
return nil, resp, err
}
return iso.ISO, resp, nil
}
// Delete will delete an ISO image from your account
func (i *ISOServiceHandler) Delete(ctx context.Context, isoID string) error {
uri := fmt.Sprintf("/v2/iso/%s", isoID)
req, err := i.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
_, err = i.client.DoWithContext(ctx, req, nil)
return err
}
// List will list all ISOs currently available on your account
func (i *ISOServiceHandler) List(ctx context.Context, options *ListOptions) ([]ISO, *Meta, *http.Response, error) { //nolint:dupl
uri := "/v2/iso"
req, err := i.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
iso := new(isosBase)
resp, err := i.client.DoWithContext(ctx, req, iso)
if err != nil {
return nil, nil, resp, err
}
return iso.ISOs, iso.Meta, resp, nil
}
// ListPublic will list public ISOs offered in the Vultr ISO library.
func (i *ISOServiceHandler) ListPublic(ctx context.Context, options *ListOptions) ([]PublicISO, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := "/v2/iso-public"
req, err := i.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
iso := new(publicIsosBase)
resp, err := i.client.DoWithContext(ctx, req, iso)
if err != nil {
return nil, nil, resp, err
}
return iso.PublicIsos, iso.Meta, resp, nil
}