File: backup.go

package info (click to toggle)
golang-github-vultr-govultr 2.17.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 604 kB
  • sloc: makefile: 2
file content (80 lines) | stat: -rw-r--r-- 1,986 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package govultr

import (
	"context"
	"fmt"
	"net/http"

	"github.com/google/go-querystring/query"
)

// BackupService is the interface to interact with the backup endpoint on the Vultr API
// Link : https://www.vultr.com/api/#tag/backup
type BackupService interface {
	Get(ctx context.Context, backupID string) (*Backup, error)
	List(ctx context.Context, options *ListOptions) ([]Backup, *Meta, error)
}

// BackupServiceHandler handles interaction with the backup methods for the Vultr API
type BackupServiceHandler struct {
	client *Client
}

// Backup represents a Vultr backup
type Backup struct {
	ID          string `json:"id"`
	DateCreated string `json:"date_created"`
	Description string `json:"description"`
	Size        int    `json:"size"`
	Status      string `json:"status"`
}

type backupsBase struct {
	Backups []Backup `json:"backups"`
	Meta    *Meta    `json:"meta"`
}

type backupBase struct {
	Backup *Backup `json:"backup"`
}

// Get retrieves a backup that matches the given backupID
func (b *BackupServiceHandler) Get(ctx context.Context, backupID string) (*Backup, error) {
	uri := fmt.Sprintf("/v2/backups/%s", backupID)
	req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)

	if err != nil {
		return nil, err
	}

	backup := new(backupBase)
	if err := b.client.DoWithContext(ctx, req, backup); err != nil {
		return nil, err
	}

	return backup.Backup, nil
}

// List retrieves a list of all backups on the current account
func (b *BackupServiceHandler) List(ctx context.Context, options *ListOptions) ([]Backup, *Meta, error) {
	uri := "/v2/backups"
	req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)

	if err != nil {
		return nil, nil, err
	}

	newValues, err := query.Values(options)
	if err != nil {
		return nil, nil, err
	}

	req.URL.RawQuery = newValues.Encode()

	backups := new(backupsBase)
	if err = b.client.DoWithContext(ctx, req, backups); err != nil {
		return nil, nil, err
	}

	return backups.Backups, backups.Meta, nil
}