1 package govultr
2
3 import (
4 "context"
5 "net/http"
6
7 "github.com/google/go-querystring/query"
8 )
9
10 // OSService is the interface to interact with the operating system endpoint on the Vultr API
11 // Link : https://www.vultr.com/api/#tag/os
12 type OSService interface {
13 List(ctx context.Context, options *ListOptions) ([]OS, *Meta, *http.Response, error)
14 }
15
16 // OSServiceHandler handles interaction with the operating system methods for the Vultr API
17 type OSServiceHandler struct {
18 client *Client
19 }
20
21 // OS represents a Vultr operating system
22 type OS struct {
23 ID int `json:"id"`
24 Name string `json:"name"`
25 Arch string `json:"arch"`
26 Family string `json:"family"`
27 }
28
29 type osBase struct {
30 OS []OS `json:"os"`
31 Meta *Meta `json:"meta"`
32 }
33
34 var _ OSService = &OSServiceHandler{}
35
36 // List retrieves a list of available operating systems.
37 func (o *OSServiceHandler) List(ctx context.Context, options *ListOptions) ([]OS, *Meta, *http.Response, error) { //nolint:dupl
38 uri := "/v2/os"
39 req, err := o.client.NewRequest(ctx, http.MethodGet, uri, nil)
40 if err != nil {
41 return nil, nil, nil, err
42 }
43
44 newValues, err := query.Values(options)
45 if err != nil {
46 return nil, nil, nil, err
47 }
48 req.URL.RawQuery = newValues.Encode()
49
50 os := new(osBase)
51 resp, err := o.client.DoWithContext(ctx, req, os)
52 if err != nil {
53 return nil, nil, resp, err
54 }
55
56 return os.OS, os.Meta, resp, nil
57 }
58