-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackup.go
150 lines (122 loc) · 4.28 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package gocd
import (
"encoding/json"
"net/http"
"path/filepath"
"github.com/nikhilsbhat/gocd-sdk-go/pkg/errors"
"github.com/jinzhu/copier"
)
// GetBackupConfig fetches information of backup configured in GoCD server.
func (conf *client) GetBackupConfig() (BackupConfig, error) {
newClient := &client{}
if err := copier.CopyWithOption(newClient, conf, copier.Option{IgnoreEmpty: true, DeepCopy: true}); err != nil {
return BackupConfig{}, err
}
var backUpConf BackupConfig
resp, err := newClient.httpClient.R().
SetHeaders(map[string]string{
"Accept": HeaderVersionOne,
}).
Get(BackupConfigEndpoint)
if err != nil {
return BackupConfig{}, &errors.APIError{Err: err, Message: "get backup information"}
}
if resp.StatusCode() != http.StatusOK {
return BackupConfig{}, &errors.NonOkError{Code: resp.StatusCode(), Response: resp}
}
if err = json.Unmarshal(resp.Body(), &backUpConf); err != nil {
return BackupConfig{}, &errors.MarshalError{Err: err}
}
return backUpConf, nil
}
// CreateOrUpdateBackupConfig will either create or update the config repo, it creates one if not created else update the existing with newer configuration.
func (conf *client) CreateOrUpdateBackupConfig(backup BackupConfig) error {
newClient := &client{}
if err := copier.CopyWithOption(newClient, conf, copier.Option{IgnoreEmpty: true, DeepCopy: true}); err != nil {
return err
}
resp, err := newClient.httpClient.R().
SetHeaders(map[string]string{
"Accept": HeaderVersionOne,
"Content-Type": ContentJSON,
}).
SetBody(backup).
Post(BackupConfigEndpoint)
if err != nil {
return &errors.APIError{Err: err, Message: "create/update backup configuration"}
}
if resp.StatusCode() != http.StatusOK {
return &errors.NonOkError{Code: resp.StatusCode(), Response: resp}
}
return nil
}
// DeleteBackupConfig deletes the backup config configured in GoCD.
func (conf *client) DeleteBackupConfig() error {
newClient := &client{}
if err := copier.CopyWithOption(newClient, conf, copier.Option{IgnoreEmpty: true, DeepCopy: true}); err != nil {
return err
}
resp, err := newClient.httpClient.R().
SetHeaders(map[string]string{
"Accept": HeaderVersionOne,
}).
Delete(BackupConfigEndpoint)
if err != nil {
return &errors.APIError{Err: err, Message: "delete backup configuration"}
}
if resp.StatusCode() != http.StatusOK {
return &errors.NonOkError{Code: resp.StatusCode(), Response: resp}
}
return nil
}
// GetBackup gets the information of the backup which was taken earlier.
func (conf *client) GetBackup(backupID string) (BackupStats, error) {
newClient := &client{}
if err := copier.CopyWithOption(newClient, conf, copier.Option{IgnoreEmpty: true, DeepCopy: true}); err != nil {
return BackupStats{}, err
}
var backUpStats BackupStats
resp, err := newClient.httpClient.R().
SetHeaders(map[string]string{
"Accept": HeaderVersionTwo,
}).
Get(filepath.Join(BackupStatsEndpoint, backupID))
if err != nil {
return backUpStats, &errors.APIError{Err: err, Message: "get backup stats"}
}
if resp.StatusCode() != http.StatusOK {
return backUpStats, &errors.NonOkError{Code: resp.StatusCode(), Response: resp}
}
if err = json.Unmarshal(resp.Body(), &backUpStats); err != nil {
return backUpStats, &errors.MarshalError{Err: err}
}
return backUpStats, nil
}
func (conf *client) ScheduleBackup() (map[string]string, error) {
var backupStats map[string]string
newClient := &client{}
if err := copier.CopyWithOption(newClient, conf, copier.Option{IgnoreEmpty: true, DeepCopy: true}); err != nil {
return backupStats, err
}
resp, err := newClient.httpClient.R().
SetHeaders(map[string]string{
"Accept": HeaderVersionTwo,
HeaderConfirm: "true",
}).
Post(BackupStatsEndpoint)
if err != nil {
return backupStats, &errors.APIError{Err: err, Message: "schedule backup"}
}
if resp.StatusCode() != http.StatusAccepted {
return backupStats, &errors.NonOkError{Code: resp.StatusCode(), Response: resp}
}
if len(resp.Header().Get(LocationHeader)) == 0 {
return backupStats, &errors.NilHeaderError{Header: "Location", Message: "getting backup stats"}
}
_, backUpID := filepath.Split(resp.Header().Get(LocationHeader))
backupStats = map[string]string{
"BackUpID": backUpID,
"RetryAfter": resp.Header().Get("Retry-After"),
}
return backupStats, nil
}