application.go raw

   1  package govultr
   2  
   3  import (
   4  	"context"
   5  	"net/http"
   6  
   7  	"github.com/google/go-querystring/query"
   8  )
   9  
  10  // ApplicationService is the interface to interact with the Application endpoint on the Vultr API.
  11  // Link : https://www.vultr.com/api/#tag/application
  12  type ApplicationService interface {
  13  	List(ctx context.Context, options *ListOptions) ([]Application, *Meta, *http.Response, error)
  14  }
  15  
  16  // ApplicationServiceHandler handles interaction with the application methods for the Vultr API.
  17  type ApplicationServiceHandler struct {
  18  	client *Client
  19  }
  20  
  21  // Application represents all available apps that can be used to deployed with vultr Instances.
  22  type Application struct {
  23  	ID         int    `json:"id"`
  24  	Name       string `json:"name"`
  25  	ShortName  string `json:"short_name"`
  26  	DeployName string `json:"deploy_name"`
  27  	Type       string `json:"type"`
  28  	Vendor     string `json:"vendor"`
  29  	ImageID    string `json:"image_id"`
  30  }
  31  
  32  type applicationBase struct {
  33  	Applications []Application `json:"applications"`
  34  	Meta         *Meta         `json:"meta"`
  35  }
  36  
  37  // List retrieves a list of available applications that can be launched when creating a Vultr instance
  38  func (a *ApplicationServiceHandler) List(ctx context.Context, options *ListOptions) ([]Application, *Meta, *http.Response, error) { //nolint:dupl,lll
  39  	uri := "/v2/applications"
  40  
  41  	req, err := a.client.NewRequest(ctx, http.MethodGet, uri, nil)
  42  	if err != nil {
  43  		return nil, nil, nil, err
  44  	}
  45  
  46  	newValues, err := query.Values(options)
  47  	if err != nil {
  48  		return nil, nil, nil, err
  49  	}
  50  
  51  	req.URL.RawQuery = newValues.Encode()
  52  	apps := new(applicationBase)
  53  
  54  	resp, err := a.client.DoWithContext(ctx, req, apps)
  55  	if err != nil {
  56  		return nil, nil, resp, err
  57  	}
  58  
  59  	return apps.Applications, apps.Meta, resp, nil
  60  }
  61