File: plans.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 (113 lines) | stat: -rw-r--r-- 3,093 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
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
package govultr

import (
	"context"
	"net/http"

	"github.com/google/go-querystring/query"
)

// PlanService is the interface to interact with the Plans endpoints on the Vultr API
// Link : https://www.vultr.com/api/#tag/plans
type PlanService interface {
	List(ctx context.Context, planType string, options *ListOptions) ([]Plan, *Meta, error)
	ListBareMetal(ctx context.Context, options *ListOptions) ([]BareMetalPlan, *Meta, error)
}

// PlanServiceHandler handles interaction with the Plans methods for the Vultr API
type PlanServiceHandler struct {
	client *Client
}

// BareMetalPlan represents bare metal plans
type BareMetalPlan struct {
	ID          string   `json:"id"`
	CPUCount    int      `json:"cpu_count"`
	CPUModel    string   `json:"cpu_model"`
	CPUThreads  int      `json:"cpu_threads"`
	RAM         int      `json:"ram"`
	Disk        int      `json:"disk"`
	DiskCount   int      `json:"disk_count"`
	Bandwidth   int      `json:"bandwidth"`
	MonthlyCost float32  `json:"monthly_cost"`
	Type        string   `json:"type"`
	Locations   []string `json:"locations"`
}

// Plan represents vc2, vdc, or vhf
type Plan struct {
	ID          string   `json:"id"`
	VCPUCount   int      `json:"vcpu_count"`
	RAM         int      `json:"ram"`
	Disk        int      `json:"disk"`
	DiskCount   int      `json:"disk_count"`
	Bandwidth   int      `json:"bandwidth"`
	MonthlyCost float32  `json:"monthly_cost"`
	Type        string   `json:"type"`
	GPUVRAM     int      `json:"gpu_vram_gb,omitempty"`
	GPUType     string   `json:"gpu_type,omitempty"`
	Locations   []string `json:"locations"`
}

type plansBase struct {
	Plans []Plan `json:"plans"`
	Meta  *Meta  `json:"meta"`
}

type bareMetalPlansBase struct {
	Plans []BareMetalPlan `json:"plans_metal"`
	Meta  *Meta           `json:"meta"`
}

// List retrieves a list of all active plans.
// planType is optional - pass an empty string to get all plans
func (p *PlanServiceHandler) List(ctx context.Context, planType string, options *ListOptions) ([]Plan, *Meta, error) {
	uri := "/v2/plans"

	req, err := p.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
	}

	if planType != "" {
		newValues.Add("type", planType)
	}

	req.URL.RawQuery = newValues.Encode()

	plans := new(plansBase)
	if err = p.client.DoWithContext(ctx, req, plans); err != nil {
		return nil, nil, err
	}

	return plans.Plans, plans.Meta, nil
}

// ListBareMetal all active bare metal plans.
func (p *PlanServiceHandler) ListBareMetal(ctx context.Context, options *ListOptions) ([]BareMetalPlan, *Meta, error) {
	uri := "/v2/plans-metal"

	req, err := p.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()

	bmPlans := new(bareMetalPlansBase)
	if err = p.client.DoWithContext(ctx, req, bmPlans); err != nil {
		return nil, nil, err
	}

	return bmPlans.Plans, bmPlans.Meta, nil
}