iso.go raw
1 package govultr
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7
8 "github.com/google/go-querystring/query"
9 )
10
11 // ISOService is the interface to interact with the ISO endpoints on the Vultr API
12 // Link : https://www.vultr.com/api/#tag/iso
13 type ISOService interface {
14 Create(ctx context.Context, isoReq *ISOReq) (*ISO, *http.Response, error)
15 Get(ctx context.Context, isoID string) (*ISO, *http.Response, error)
16 Delete(ctx context.Context, isoID string) error
17 List(ctx context.Context, options *ListOptions) ([]ISO, *Meta, *http.Response, error)
18 ListPublic(ctx context.Context, options *ListOptions) ([]PublicISO, *Meta, *http.Response, error)
19 }
20
21 // ISOServiceHandler handles interaction with the ISO methods for the Vultr API
22 type ISOServiceHandler struct {
23 client *Client
24 }
25
26 // ISO represents ISOs currently available on this account.
27 type ISO struct {
28 ID string `json:"id"`
29 DateCreated string `json:"date_created"`
30 FileName string `json:"filename"`
31 Size int `json:"size,omitempty"`
32 MD5Sum string `json:"md5sum,omitempty"`
33 SHA512Sum string `json:"sha512sum,omitempty"`
34 Status string `json:"status"`
35 }
36
37 // PublicISO represents public ISOs offered in the Vultr ISO library.
38 type PublicISO struct {
39 ID string `json:"id"`
40 Name string `json:"name"`
41 Description string `json:"description"`
42 MD5Sum string `json:"md5sum,omitempty"`
43 }
44
45 // ISOReq is used for creating ISOs.
46 type ISOReq struct {
47 URL string `json:"url"`
48 }
49
50 type isosBase struct {
51 ISOs []ISO `json:"isos"`
52 Meta *Meta `json:"meta"`
53 }
54
55 type isoBase struct {
56 ISO *ISO `json:"iso"`
57 }
58
59 type publicIsosBase struct {
60 PublicIsos []PublicISO `json:"public_isos"`
61 Meta *Meta `json:"meta"`
62 }
63
64 // Create will create a new ISO image on your account
65 func (i *ISOServiceHandler) Create(ctx context.Context, isoReq *ISOReq) (*ISO, *http.Response, error) {
66 uri := "/v2/iso"
67
68 req, err := i.client.NewRequest(ctx, http.MethodPost, uri, isoReq)
69 if err != nil {
70 return nil, nil, err
71 }
72
73 iso := new(isoBase)
74 resp, err := i.client.DoWithContext(ctx, req, iso)
75 if err != nil {
76 return nil, resp, err
77 }
78
79 return iso.ISO, resp, nil
80 }
81
82 // Get an ISO
83 func (i *ISOServiceHandler) Get(ctx context.Context, isoID string) (*ISO, *http.Response, error) {
84 uri := fmt.Sprintf("/v2/iso/%s", isoID)
85
86 req, err := i.client.NewRequest(ctx, http.MethodGet, uri, nil)
87 if err != nil {
88 return nil, nil, err
89 }
90
91 iso := new(isoBase)
92 resp, err := i.client.DoWithContext(ctx, req, iso)
93 if err != nil {
94 return nil, resp, err
95 }
96 return iso.ISO, resp, nil
97 }
98
99 // Delete will delete an ISO image from your account
100 func (i *ISOServiceHandler) Delete(ctx context.Context, isoID string) error {
101 uri := fmt.Sprintf("/v2/iso/%s", isoID)
102
103 req, err := i.client.NewRequest(ctx, http.MethodDelete, uri, nil)
104 if err != nil {
105 return err
106 }
107
108 _, err = i.client.DoWithContext(ctx, req, nil)
109 return err
110 }
111
112 // List will list all ISOs currently available on your account
113 func (i *ISOServiceHandler) List(ctx context.Context, options *ListOptions) ([]ISO, *Meta, *http.Response, error) { //nolint:dupl
114 uri := "/v2/iso"
115
116 req, err := i.client.NewRequest(ctx, http.MethodGet, uri, nil)
117 if err != nil {
118 return nil, nil, nil, err
119 }
120
121 newValues, err := query.Values(options)
122 if err != nil {
123 return nil, nil, nil, err
124 }
125
126 req.URL.RawQuery = newValues.Encode()
127
128 iso := new(isosBase)
129 resp, err := i.client.DoWithContext(ctx, req, iso)
130 if err != nil {
131 return nil, nil, resp, err
132 }
133
134 return iso.ISOs, iso.Meta, resp, nil
135 }
136
137 // ListPublic will list public ISOs offered in the Vultr ISO library.
138 func (i *ISOServiceHandler) ListPublic(ctx context.Context, options *ListOptions) ([]PublicISO, *Meta, *http.Response, error) { //nolint:dupl,lll
139 uri := "/v2/iso-public"
140
141 req, err := i.client.NewRequest(ctx, http.MethodGet, uri, nil)
142 if err != nil {
143 return nil, nil, nil, err
144 }
145
146 newValues, err := query.Values(options)
147 if err != nil {
148 return nil, nil, nil, err
149 }
150
151 req.URL.RawQuery = newValues.Encode()
152
153 iso := new(publicIsosBase)
154 resp, err := i.client.DoWithContext(ctx, req, iso)
155 if err != nil {
156 return nil, nil, resp, err
157 }
158
159 return iso.PublicIsos, iso.Meta, resp, nil
160 }
161