startup_script.go raw

   1  package govultr
   2  
   3  import (
   4  	"context"
   5  	"fmt"
   6  	"net/http"
   7  
   8  	"github.com/google/go-querystring/query"
   9  )
  10  
  11  const scriptPath = "/v2/startup-scripts"
  12  
  13  // StartupScriptService is the interface to interact with the startup script endpoints on the Vultr API
  14  // Link : https://www.vultr.com/api/#tag/startup
  15  type StartupScriptService interface { //nolint:dupl
  16  	Create(ctx context.Context, req *StartupScriptReq) (*StartupScript, *http.Response, error)
  17  	Get(ctx context.Context, scriptID string) (*StartupScript, *http.Response, error)
  18  	Update(ctx context.Context, scriptID string, scriptReq *StartupScriptReq) error
  19  	Delete(ctx context.Context, scriptID string) error
  20  	List(ctx context.Context, options *ListOptions) ([]StartupScript, *Meta, *http.Response, error)
  21  }
  22  
  23  // StartupScriptServiceHandler handles interaction with the startup script methods for the Vultr API
  24  type StartupScriptServiceHandler struct {
  25  	client *Client
  26  }
  27  
  28  // StartupScript represents an startup script on Vultr
  29  type StartupScript struct {
  30  	ID           string `json:"id"`
  31  	DateCreated  string `json:"date_created"`
  32  	DateModified string `json:"date_modified"`
  33  	Name         string `json:"name"`
  34  	Type         string `json:"type"`
  35  	Script       string `json:"script"`
  36  }
  37  
  38  // StartupScriptReq is the user struct for create and update calls
  39  type StartupScriptReq struct {
  40  	Name   string `json:"name,omitempty"`
  41  	Type   string `json:"type,omitempty"`
  42  	Script string `json:"script,omitempty"`
  43  }
  44  
  45  type startupScriptsBase struct {
  46  	StartupScripts []StartupScript `json:"startup_scripts"`
  47  	Meta           *Meta           `json:"meta"`
  48  }
  49  
  50  type startupScriptBase struct {
  51  	StartupScript *StartupScript `json:"startup_script"`
  52  }
  53  
  54  var _ StartupScriptService = &StartupScriptServiceHandler{}
  55  
  56  // Create a startup script
  57  func (s *StartupScriptServiceHandler) Create(ctx context.Context, scriptReq *StartupScriptReq) (*StartupScript, *http.Response, error) {
  58  	req, err := s.client.NewRequest(ctx, http.MethodPost, scriptPath, scriptReq)
  59  	if err != nil {
  60  		return nil, nil, err
  61  	}
  62  
  63  	script := new(startupScriptBase)
  64  	resp, err := s.client.DoWithContext(ctx, req, script)
  65  	if err != nil {
  66  		return nil, resp, err
  67  	}
  68  
  69  	return script.StartupScript, resp, nil
  70  }
  71  
  72  // Get a single startup script
  73  func (s *StartupScriptServiceHandler) Get(ctx context.Context, scriptID string) (*StartupScript, *http.Response, error) {
  74  	uri := fmt.Sprintf("%s/%s", scriptPath, scriptID)
  75  
  76  	req, err := s.client.NewRequest(ctx, http.MethodGet, uri, nil)
  77  	if err != nil {
  78  		return nil, nil, err
  79  	}
  80  
  81  	script := new(startupScriptBase)
  82  	resp, err := s.client.DoWithContext(ctx, req, script)
  83  	if err != nil {
  84  		return nil, resp, err
  85  	}
  86  
  87  	return script.StartupScript, resp, nil
  88  }
  89  
  90  // Update will update the given startup script. Empty strings will be ignored.
  91  func (s *StartupScriptServiceHandler) Update(ctx context.Context, scriptID string, scriptReq *StartupScriptReq) error {
  92  	uri := fmt.Sprintf("%s/%s", scriptPath, scriptID)
  93  
  94  	req, err := s.client.NewRequest(ctx, http.MethodPatch, uri, scriptReq)
  95  	if err != nil {
  96  		return err
  97  	}
  98  
  99  	_, err = s.client.DoWithContext(ctx, req, nil)
 100  	return err
 101  }
 102  
 103  // Delete the specified startup script from your account.
 104  func (s *StartupScriptServiceHandler) Delete(ctx context.Context, scriptID string) error {
 105  	uri := fmt.Sprintf("%s/%s", scriptPath, scriptID)
 106  
 107  	req, err := s.client.NewRequest(ctx, http.MethodDelete, uri, nil)
 108  	if err != nil {
 109  		return err
 110  	}
 111  
 112  	_, err = s.client.DoWithContext(ctx, req, nil)
 113  	return err
 114  }
 115  
 116  // List all the startup scripts associated with your Vultr account
 117  func (s *StartupScriptServiceHandler) List(ctx context.Context, options *ListOptions) ([]StartupScript, *Meta, *http.Response, error) { //nolint:dupl,lll
 118  	req, err := s.client.NewRequest(ctx, http.MethodGet, scriptPath, nil)
 119  	if err != nil {
 120  		return nil, nil, nil, err
 121  	}
 122  
 123  	newValues, err := query.Values(options)
 124  	if err != nil {
 125  		return nil, nil, nil, err
 126  	}
 127  	req.URL.RawQuery = newValues.Encode()
 128  
 129  	scripts := new(startupScriptsBase)
 130  	resp, err := s.client.DoWithContext(ctx, req, scripts)
 131  	if err != nil {
 132  		return nil, nil, resp, err
 133  	}
 134  
 135  	return scripts.StartupScripts, scripts.Meta, resp, nil
 136  }
 137