forked from vultr/govultr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.go
57 lines (44 loc) · 1.42 KB
/
application.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
package govultr
import (
"context"
"net/http"
"github.com/google/go-querystring/query"
)
// ApplicationService is the interface to interact with the Application endpoint on the Vultr API
type ApplicationService interface {
List(ctx context.Context, options *ListOptions) ([]Application, *Meta, error)
}
// ApplicationServiceHandler handles interaction with the application methods for the Vultr API
type ApplicationServiceHandler struct {
client *Client
}
// Application represents a Vultr application
type Application struct {
ID int `json:"id"`
Name string `json:"name"`
ShortName string `json:"short_name"`
DeployName string `json:"deploy_name"`
}
type applicationBase struct {
Applications []Application `json:"applications"`
Meta *Meta `json:"meta"`
}
// List retrieves a list of available applications that can be launched when creating a Vultr instance
func (a *ApplicationServiceHandler) List(ctx context.Context, options *ListOptions) ([]Application, *Meta, error) {
uri := "/v2/applications"
req, err := a.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
apps := new(applicationBase)
err = a.client.DoWithContext(ctx, req, apps)
if err != nil {
return nil, nil, err
}
return apps.Applications, apps.Meta, nil
}