plans.go raw

   1  package govultr
   2  
   3  import (
   4  	"context"
   5  	"net/http"
   6  
   7  	"github.com/google/go-querystring/query"
   8  )
   9  
  10  // PlanService is the interface to interact with the Plans endpoints on the Vultr API
  11  // Link : https://www.vultr.com/api/#tag/plans
  12  type PlanService interface {
  13  	List(ctx context.Context, planType string, options *ListOptions) ([]Plan, *Meta, *http.Response, error)
  14  	ListBareMetal(ctx context.Context, options *ListOptions) ([]BareMetalPlan, *Meta, *http.Response, error)
  15  }
  16  
  17  // PlanServiceHandler handles interaction with the Plans methods for the Vultr API
  18  type PlanServiceHandler struct {
  19  	client *Client
  20  }
  21  
  22  // BareMetalPlan represents bare metal plans
  23  type BareMetalPlan struct {
  24  	ID          string   `json:"id"`
  25  	CPUCount    int      `json:"cpu_count"`
  26  	CPUModel    string   `json:"cpu_model"`
  27  	CPUThreads  int      `json:"cpu_threads"`
  28  	RAM         int      `json:"ram"`
  29  	Disk        int      `json:"disk"`
  30  	DiskCount   int      `json:"disk_count"`
  31  	Bandwidth   int      `json:"bandwidth"`
  32  	MonthlyCost float32  `json:"monthly_cost"`
  33  	Type        string   `json:"type"`
  34  	Locations   []string `json:"locations"`
  35  }
  36  
  37  // Plan represents vc2, vdc, or vhf
  38  type Plan struct {
  39  	ID          string   `json:"id"`
  40  	VCPUCount   int      `json:"vcpu_count"`
  41  	RAM         int      `json:"ram"`
  42  	Disk        int      `json:"disk"`
  43  	DiskCount   int      `json:"disk_count"`
  44  	Bandwidth   int      `json:"bandwidth"`
  45  	MonthlyCost float32  `json:"monthly_cost"`
  46  	Type        string   `json:"type"`
  47  	GPUVRAM     int      `json:"gpu_vram_gb,omitempty"`
  48  	GPUType     string   `json:"gpu_type,omitempty"`
  49  	Locations   []string `json:"locations"`
  50  }
  51  
  52  type plansBase struct {
  53  	Plans []Plan `json:"plans"`
  54  	Meta  *Meta  `json:"meta"`
  55  }
  56  
  57  type bareMetalPlansBase struct {
  58  	Plans []BareMetalPlan `json:"plans_metal"`
  59  	Meta  *Meta           `json:"meta"`
  60  }
  61  
  62  // List retrieves a list of all active plans.
  63  // planType is optional - pass an empty string to get all plans
  64  func (p *PlanServiceHandler) List(ctx context.Context, planType string, options *ListOptions) ([]Plan, *Meta, *http.Response, error) {
  65  	uri := "/v2/plans"
  66  
  67  	req, err := p.client.NewRequest(ctx, http.MethodGet, uri, nil)
  68  	if err != nil {
  69  		return nil, nil, nil, err
  70  	}
  71  
  72  	newValues, err := query.Values(options)
  73  	if err != nil {
  74  		return nil, nil, nil, err
  75  	}
  76  
  77  	if planType != "" {
  78  		newValues.Add("type", planType)
  79  	}
  80  
  81  	req.URL.RawQuery = newValues.Encode()
  82  
  83  	plans := new(plansBase)
  84  	resp, err := p.client.DoWithContext(ctx, req, plans)
  85  	if err != nil {
  86  		return nil, nil, resp, err
  87  	}
  88  
  89  	return plans.Plans, plans.Meta, resp, nil
  90  }
  91  
  92  // ListBareMetal all active bare metal plans.
  93  func (p *PlanServiceHandler) ListBareMetal(ctx context.Context, options *ListOptions) ([]BareMetalPlan, *Meta, *http.Response, error) { //nolint:dupl,lll
  94  	uri := "/v2/plans-metal"
  95  
  96  	req, err := p.client.NewRequest(ctx, http.MethodGet, uri, nil)
  97  	if err != nil {
  98  		return nil, nil, nil, err
  99  	}
 100  
 101  	newValues, err := query.Values(options)
 102  	if err != nil {
 103  		return nil, nil, nil, err
 104  	}
 105  
 106  	req.URL.RawQuery = newValues.Encode()
 107  
 108  	bmPlans := new(bareMetalPlansBase)
 109  	resp, err := p.client.DoWithContext(ctx, req, bmPlans)
 110  	if err != nil {
 111  		return nil, nil, nil, err
 112  	}
 113  
 114  	return bmPlans.Plans, bmPlans.Meta, resp, nil
 115  }
 116