File: application.go

package info (click to toggle)
golang-github-vultr-govultr 2.17.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 604 kB
  • sloc: makefile: 2
file content (60 lines) | stat: -rw-r--r-- 1,663 bytes parent folder | download
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
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.
// Link : https://www.vultr.com/api/#tag/application
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 all available apps that can be used to deployed with vultr Instances.
type Application struct {
	ID         int    `json:"id"`
	Name       string `json:"name"`
	ShortName  string `json:"short_name"`
	DeployName string `json:"deploy_name"`
	Type       string `json:"type"`
	Vendor     string `json:"vendor"`
	ImageID    string `json:"image_id"`
}

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
}