snapshot.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  // SnapshotService is the interface to interact with Snapshot endpoints on the Vultr API
  12  // Link : https://www.vultr.com/api/#tag/snapshot
  13  type SnapshotService interface {
  14  	Create(ctx context.Context, snapshotReq *SnapshotReq) (*Snapshot, *http.Response, error)
  15  	CreateFromURL(ctx context.Context, snapshotURLReq *SnapshotURLReq) (*Snapshot, *http.Response, error)
  16  	Get(ctx context.Context, snapshotID string) (*Snapshot, *http.Response, error)
  17  	Delete(ctx context.Context, snapshotID string) error
  18  	List(ctx context.Context, options *ListOptions) ([]Snapshot, *Meta, *http.Response, error)
  19  }
  20  
  21  // SnapshotServiceHandler handles interaction with the snapshot methods for the Vultr API
  22  type SnapshotServiceHandler struct {
  23  	client *Client
  24  }
  25  
  26  // Snapshot represents a Vultr snapshot
  27  type Snapshot struct {
  28  	ID             string `json:"id"`
  29  	DateCreated    string `json:"date_created"`
  30  	Description    string `json:"description"`
  31  	Size           int    `json:"size"`
  32  	CompressedSize int    `json:"compressed_size"`
  33  	Status         string `json:"status"`
  34  	OsID           int    `json:"os_id"`
  35  	AppID          int    `json:"app_id"`
  36  }
  37  
  38  // SnapshotReq struct is used to create snapshots.
  39  type SnapshotReq struct {
  40  	InstanceID  string `json:"instance_id,omitempty"`
  41  	Description string `json:"description,omitempty"`
  42  }
  43  
  44  // SnapshotURLReq struct is used to create snapshots from a URL.
  45  type SnapshotURLReq struct {
  46  	URL         string `json:"url"`
  47  	Description string `json:"description,omitempty"`
  48  	UEFI        *bool  `json:"uefi"`
  49  }
  50  
  51  type snapshotsBase struct {
  52  	Snapshots []Snapshot `json:"snapshots"`
  53  	Meta      *Meta      `json:"meta"`
  54  }
  55  
  56  type snapshotBase struct {
  57  	Snapshot *Snapshot `json:"snapshot"`
  58  }
  59  
  60  // Create makes a snapshot of a provided server
  61  func (s *SnapshotServiceHandler) Create(ctx context.Context, snapshotReq *SnapshotReq) (*Snapshot, *http.Response, error) {
  62  	uri := "/v2/snapshots"
  63  
  64  	req, err := s.client.NewRequest(ctx, http.MethodPost, uri, snapshotReq)
  65  	if err != nil {
  66  		return nil, nil, err
  67  	}
  68  
  69  	snapshot := new(snapshotBase)
  70  	resp, err := s.client.DoWithContext(ctx, req, snapshot)
  71  	if err != nil {
  72  		return nil, resp, err
  73  	}
  74  
  75  	return snapshot.Snapshot, resp, nil
  76  }
  77  
  78  // CreateFromURL will create a snapshot based on an image iso from a URL you provide
  79  func (s *SnapshotServiceHandler) CreateFromURL(ctx context.Context, snapshotURLReq *SnapshotURLReq) (*Snapshot, *http.Response, error) {
  80  	uri := "/v2/snapshots/create-from-url"
  81  
  82  	req, err := s.client.NewRequest(ctx, http.MethodPost, uri, snapshotURLReq)
  83  	if err != nil {
  84  		return nil, nil, err
  85  	}
  86  
  87  	snapshot := new(snapshotBase)
  88  	resp, err := s.client.DoWithContext(ctx, req, snapshot)
  89  	if err != nil {
  90  		return nil, resp, err
  91  	}
  92  
  93  	return snapshot.Snapshot, resp, nil
  94  }
  95  
  96  // Get a specific snapshot
  97  func (s *SnapshotServiceHandler) Get(ctx context.Context, snapshotID string) (*Snapshot, *http.Response, error) {
  98  	uri := fmt.Sprintf("/v2/snapshots/%s", snapshotID)
  99  
 100  	req, err := s.client.NewRequest(ctx, http.MethodGet, uri, nil)
 101  	if err != nil {
 102  		return nil, nil, err
 103  	}
 104  
 105  	snapshot := new(snapshotBase)
 106  	resp, err := s.client.DoWithContext(ctx, req, snapshot)
 107  	if err != nil {
 108  		return nil, resp, err
 109  	}
 110  
 111  	return snapshot.Snapshot, resp, nil
 112  }
 113  
 114  // Delete a snapshot.
 115  func (s *SnapshotServiceHandler) Delete(ctx context.Context, snapshotID string) error {
 116  	uri := fmt.Sprintf("/v2/snapshots/%s", snapshotID)
 117  
 118  	req, err := s.client.NewRequest(ctx, http.MethodDelete, uri, nil)
 119  	if err != nil {
 120  		return err
 121  	}
 122  
 123  	_, err = s.client.DoWithContext(ctx, req, nil)
 124  	return err
 125  }
 126  
 127  // List all available snapshots.
 128  func (s *SnapshotServiceHandler) List(ctx context.Context, options *ListOptions) ([]Snapshot, *Meta, *http.Response, error) { //nolint:dupl
 129  	uri := "/v2/snapshots"
 130  
 131  	req, err := s.client.NewRequest(ctx, http.MethodGet, uri, nil)
 132  	if err != nil {
 133  		return nil, nil, nil, err
 134  	}
 135  	newValues, err := query.Values(options)
 136  	if err != nil {
 137  		return nil, nil, nil, err
 138  	}
 139  
 140  	req.URL.RawQuery = newValues.Encode()
 141  
 142  	snapshots := new(snapshotsBase)
 143  	resp, err := s.client.DoWithContext(ctx, req, snapshots)
 144  	if err != nil {
 145  		return nil, nil, resp, err
 146  	}
 147  
 148  	return snapshots.Snapshots, snapshots.Meta, resp, nil
 149  }
 150