-
Notifications
You must be signed in to change notification settings - Fork 20
/
backup.go
118 lines (99 loc) · 3.26 KB
/
backup.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package f5
import (
"encoding/json"
"fmt"
)
// Backup REST paths
const (
PathBackup = "/mgmt/tm/shared/sys/backup"
)
// BackupResponse holds attributes returned by requests on the backup API.
type BackupResponse struct {
// Unique ID to identify uniquely the backup action.
ID string `json:"id"`
// Name of the file in which the backup is saved to or restord from.
File string `json:"file"`
// Type of action performed. Possible values are:
// - BACKUP
// - RESTORE
// - RESTORE_WITH_NO_LICENSE
// - BACKUP_WITH_NO_PRIVATE_KEYS
// - BACKUP_WITH_ENCRYPTION
// - BACKUP_WITH_NO_PRIVATE_KEYS_WITH_ENCRYPTION
// - RESTORE_WITH_ENCRYPTION
// - RESTORE_WITH_NO_LICENSE_WITH_ENCRYPTION
// - CLEANUP
Action string `json:"action"`
// Status of the backup. Possible values are:
// - CREATED
// - STARTED
// - CANCEL_REQUESTED
// - CANCELED
// - FAILED
// - FINISHED
Status string `json:"status"`
}
// IsDone reports whether the status indicates that the action is terminated,
// even if it is an error or that the task has been canceled.
func (resp BackupResponse) IsDone() bool {
switch resp.Status {
case "STARTED", "CANCEL_REQUESTED":
return false
}
return true
}
// IsFailure reports whether the status is FAILED.
func (resp BackupResponse) IsFailure() bool {
return resp.Status == "FAILED"
}
// IsCanceled reports whether the status is CANCELED.
func (resp BackupResponse) IsCanceled() bool {
return resp.Status == "CANCELED"
}
// IsSuccess reports whether the status is FINISHED.
func (resp BackupResponse) IsSuccess() bool {
return resp.Status == "FINISHED"
}
// Backup creates a backup remotely saved into a file named according to the
// provided filename.
func (c *Client) Backup(filename string) (*BackupResponse, error) {
return c.backupRequest("BACKUP", filename)
}
// CheckBackup fetches the status of a backup process.
func (c *Client) CheckBackup(id string) (*BackupResponse, error) {
var backupResp BackupResponse
if err := c.ReadQuery(PathBackup+"/"+id, &backupResp); err != nil {
return nil, fmt.Errorf("cannot read backup status: %v", err)
}
return &backupResp, nil
}
// RestoreBackup restores a backup from a file having the provided filename and
// located into /var/local/ucs directory.
func (c *Client) RestoreBackup(filename string) (*BackupResponse, error) {
return c.backupRequest("RESTORE", filename)
}
// RestoreBackupWithNoLicense works exactly as RestoreBackup but do no check the
// license.
func (c *Client) RestoreBackupWithNoLicense(filename string) (*BackupResponse, error) {
return c.backupRequest("RESTORE_WITH_NO_LICENSE", filename)
}
func (c *Client) backupRequest(action, filename string) (*BackupResponse, error) {
data := map[string]interface{}{
"file": filename,
"action": action,
}
resp, err := c.SendRequest("POST", PathBackup, data)
if err != nil {
return nil, fmt.Errorf("error while requesting ucs backup: %v", err)
}
defer resp.Body.Close()
if err := c.ReadError(resp); err != nil {
return nil, fmt.Errorf("backup api returned an error: %v", err)
}
var backupResp BackupResponse
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&backupResp); err != nil {
return nil, fmt.Errorf("backup api returned a malformed json response: %v", err)
}
return &backupResp, nil
}