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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
package govultr
import (
"context"
"net/http"
)
// PlanService is the interface to interact with the Plans endpoints on the Vultr API
// Link: https://www.vultr.com/api/#plans
type PlanService interface {
List(ctx context.Context, planType string) ([]Plan, error)
GetBareMetalList(ctx context.Context) ([]BareMetalPlan, error)
GetVc2List(ctx context.Context) ([]VCPlan, error)
GetVdc2List(ctx context.Context) ([]VCPlan, error)
GetVc2zList(ctx context.Context) ([]VCPlan, error)
}
// PlanServiceHandler handles interaction with the Plans methods for the Vultr API
type PlanServiceHandler struct {
Client *Client
}
// Plan represents available Plans that Vultr offers
type Plan struct {
PlanID int `json:"VPSPLANID,string"`
Name string `json:"name"`
VCPUs int `json:"vcpu_count,string"`
RAM string `json:"ram"`
Disk string `json:"disk"`
Bandwidth string `json:"bandwidth"`
BandwidthGB string `json:"bandwidth_gb"`
Price string `json:"price_per_month"`
Windows bool `json:"windows"`
PlanType string `json:"plan_type"`
Regions []int `json:"available_locations"`
Deprecated bool `json:"deprecated"`
}
// BareMetalPlan represents bare metal plans
type BareMetalPlan struct {
PlanID string `json:"METALPLANID"`
Name string `json:"name"`
CPUs int `json:"cpu_count"`
CPUModel string `json:"cpu_model"`
RAM int `json:"ram"`
Disk string `json:"disk"`
BandwidthTB int `json:"bandwidth_tb"`
Price int `json:"price_per_month"`
PlanType string `json:"plan_type"`
Deprecated bool `json:"deprecated"`
Regions []int `json:"available_locations"`
}
// VCPlan represents either a vdc2 or a vc2 plan
type VCPlan struct {
PlanID string `json:"VPSPLANID"`
Name string `json:"name"`
VCPUs string `json:"vcpu_count"`
RAM string `json:"ram"`
Disk string `json:"disk"`
Bandwidth string `json:"bandwidth"`
BandwidthGB string `json:"bandwidth_gb"`
Price string `json:"price_per_month"`
PlanType string `json:"plan_type"`
}
// 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) ([]Plan, error) {
uri := "/v1/plans/list"
req, err := p.Client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
if planType != "" {
q := req.URL.Query()
q.Add("type", planType)
req.URL.RawQuery = q.Encode()
}
var planMap map[string]Plan
err = p.Client.DoWithContext(ctx, req, &planMap)
if err != nil {
return nil, err
}
var plans []Plan
for _, p := range planMap {
plans = append(plans, p)
}
return plans, nil
}
// GetBareMetalList retrieves a list of all active bare metal plans.
func (p *PlanServiceHandler) GetBareMetalList(ctx context.Context) ([]BareMetalPlan, error) {
uri := "/v1/plans/list_baremetal"
req, err := p.Client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
var bareMetalMap map[string]BareMetalPlan
err = p.Client.DoWithContext(ctx, req, &bareMetalMap)
if err != nil {
return nil, err
}
var bareMetalPlan []BareMetalPlan
for _, b := range bareMetalMap {
bareMetalPlan = append(bareMetalPlan, b)
}
return bareMetalPlan, nil
}
// GetVc2List retrieve a list of all active vc2 plans.
func (p *PlanServiceHandler) GetVc2List(ctx context.Context) ([]VCPlan, error) {
uri := "/v1/plans/list_vc2"
req, err := p.Client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
var vc2Plan map[string]VCPlan
err = p.Client.DoWithContext(ctx, req, &vc2Plan)
if err != nil {
return nil, err
}
var vc2 []VCPlan
for _, p := range vc2Plan {
vc2 = append(vc2, p)
}
return vc2, nil
}
// GetVdc2List Retrieve a list of all active vdc2 plans
func (p *PlanServiceHandler) GetVdc2List(ctx context.Context) ([]VCPlan, error) {
uri := "/v1/plans/list_vdc2"
req, err := p.Client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
var vdc2Map map[string]VCPlan
err = p.Client.DoWithContext(ctx, req, &vdc2Map)
if err != nil {
return nil, err
}
var vdc2 []VCPlan
for _, p := range vdc2Map {
vdc2 = append(vdc2, p)
}
return vdc2, nil
}
// GetVc2zList Retrieve a list of all active vc2z plans (high frequency)
func (p *PlanServiceHandler) GetVc2zList(ctx context.Context) ([]VCPlan, error) {
uri := "/v1/plans/list_vc2z"
req, err := p.Client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
var vc2zMap map[string]VCPlan
err = p.Client.DoWithContext(ctx, req, &vc2zMap)
if err != nil {
return nil, err
}
var vc2z []VCPlan
for _, p := range vc2zMap {
vc2z = append(vc2z, p)
}
return vc2z, nil
}
|