backup.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 // BackupService is the interface to interact with the backup endpoint on the Vultr API
12 // Link : https://www.vultr.com/api/#tag/backup
13 type BackupService interface {
14 Get(ctx context.Context, backupID string) (*Backup, *http.Response, error)
15 List(ctx context.Context, options *ListOptions) ([]Backup, *Meta, *http.Response, error)
16 }
17
18 // BackupServiceHandler handles interaction with the backup methods for the Vultr API
19 type BackupServiceHandler struct {
20 client *Client
21 }
22
23 // Backup represents a Vultr backup
24 type Backup struct {
25 ID string `json:"id"`
26 DateCreated string `json:"date_created"`
27 Description string `json:"description"`
28 Size int `json:"size"`
29 Status string `json:"status"`
30 }
31
32 type backupsBase struct {
33 Backups []Backup `json:"backups"`
34 Meta *Meta `json:"meta"`
35 }
36
37 type backupBase struct {
38 Backup *Backup `json:"backup"`
39 }
40
41 // Get retrieves a backup that matches the given backupID
42 func (b *BackupServiceHandler) Get(ctx context.Context, backupID string) (*Backup, *http.Response, error) {
43 uri := fmt.Sprintf("/v2/backups/%s", backupID)
44 req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
45
46 if err != nil {
47 return nil, nil, err
48 }
49
50 backup := new(backupBase)
51 resp, err := b.client.DoWithContext(ctx, req, backup)
52 if err != nil {
53 return nil, resp, err
54 }
55
56 return backup.Backup, resp, nil
57 }
58
59 // List retrieves a list of all backups on the current account
60 func (b *BackupServiceHandler) List(ctx context.Context, options *ListOptions) ([]Backup, *Meta, *http.Response, error) { //nolint:dupl
61 uri := "/v2/backups"
62 req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
63
64 if err != nil {
65 return nil, nil, nil, err
66 }
67
68 newValues, err := query.Values(options)
69 if err != nil {
70 return nil, nil, nil, err
71 }
72
73 req.URL.RawQuery = newValues.Encode()
74
75 backups := new(backupsBase)
76 resp, err := b.client.DoWithContext(ctx, req, backups)
77 if err != nil {
78 return nil, nil, resp, err
79 }
80
81 return backups.Backups, backups.Meta, resp, nil
82 }
83