Skip to content

Commit

Permalink
APPS-985 Return map of all backups (#48)
Browse files Browse the repository at this point in the history
* add validation for storage path

* Make filter by policy optional in the /backup/full/list endpoint

* update swag spec

* add tests

* indentation

* pre-allocate

* pre-allocate
  • Loading branch information
korotkov-aerospike authored Nov 12, 2023
1 parent d4ad103 commit 20412c8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 39 deletions.
26 changes: 16 additions & 10 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@
],
"responses": {
"200": {
"description": "Full backups",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/model.BackupDetails"
"description": "Full backups by policy",
"schema": {
"type": "object",
"additionalProperties": {
"type": "array",
"items": {
"$ref": "#/definitions/model.BackupDetails"
}
}
}
},
Expand Down Expand Up @@ -85,11 +88,14 @@
],
"responses": {
"200": {
"description": "Incremental backups",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/model.BackupDetails"
"description": "Incremental backups by policy",
"schema": {
"type": "object",
"additionalProperties": {
"type": "array",
"items": {
"$ref": "#/definitions/model.BackupDetails"
}
}
}
},
Expand Down
20 changes: 12 additions & 8 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,13 @@ paths:
- application/json
responses:
"200":
description: Full backups
description: Full backups by policy
schema:
items:
$ref: '#/definitions/model.BackupDetails'
type: array
additionalProperties:
items:
$ref: '#/definitions/model.BackupDetails'
type: array
type: object
"404":
description: Not Found
schema:
Expand All @@ -294,11 +296,13 @@ paths:
- application/json
responses:
"200":
description: Incremental backups
description: Incremental backups by policy
schema:
items:
$ref: '#/definitions/model.BackupDetails'
type: array
additionalProperties:
items:
$ref: '#/definitions/model.BackupDetails'
type: array
type: object
"404":
description: Not Found
schema:
Expand Down
52 changes: 31 additions & 21 deletions internal/server/backup_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// @Produce json
// @Param name query string true "Backup policy name"
// @Router /backup/full/list [get]
// @Success 200 {array} model.BackupDetails "Full backups"
// @Success 200 {object} map[string][]model.BackupDetails "Full backups by policy"
// @Failure 404 {string} string ""
func (ws *HTTPServer) getAvailableFullBackups(w http.ResponseWriter, r *http.Request) {
ws.getAvailableBackups(w, r, func(backend service.BackupBackend) ([]model.BackupDetails, error) {
Expand All @@ -29,7 +29,7 @@ func (ws *HTTPServer) getAvailableFullBackups(w http.ResponseWriter, r *http.Req
// @Produce json
// @Param name query string true "Backup policy name"
// @Router /backup/incremental/list [get]
// @Success 200 {array} model.BackupDetails "Incremental backups"
// @Success 200 {object} map[string][]model.BackupDetails "Incremental backups by policy"
// @Failure 404 {string} string ""
func (ws *HTTPServer) getAvailableIncrementalBackups(w http.ResponseWriter, r *http.Request) {
ws.getAvailableBackups(w, r, func(backend service.BackupBackend) ([]model.BackupDetails, error) {
Expand All @@ -42,26 +42,23 @@ func (ws *HTTPServer) getAvailableBackups(
r *http.Request,
backupListFunc func(service.BackupBackend) ([]model.BackupDetails, error)) {

policyName := r.URL.Query().Get("name")
if policyName == "" {
http.Error(w, "Undefined policy name", http.StatusBadRequest)
return
}

backend, exists := ws.backupBackends[policyName]
if !exists {
http.Error(w, "Backup backend does not exist for "+policyName, http.StatusNotFound)
return
policies := ws.requestedPolicies(r)
policyToBackups := make(map[string][]model.BackupDetails)
for _, policyName := range policies {
backend, exists := ws.backupBackends[policyName]
if !exists {
http.Error(w, "Backup backend does not exist for "+policyName, http.StatusNotFound)
return
}
list, err := backupListFunc(backend)
if err != nil {
slog.Error("Failed to retrieve backup list", "err", err)
http.Error(w, "Failed to retrieve backup list", http.StatusInternalServerError)
return
}
policyToBackups[policyName] = list
}

list, err := backupListFunc(backend)
if err != nil {
slog.Error("Failed to retrieve backup list", "err", err)
http.Error(w, "Failed to retrieve backup list", http.StatusInternalServerError)
return
}

response, err := json.Marshal(list)
response, err := json.Marshal(policyToBackups)
if err != nil {
slog.Error("Failed to parse backup list", "err", err)
http.Error(w, "Failed to parse backup list", http.StatusInternalServerError)
Expand All @@ -72,3 +69,16 @@ func (ws *HTTPServer) getAvailableBackups(
w.WriteHeader(http.StatusOK)
_, _ = w.Write(response)
}

// return an array containing single policy from request (if present), or all policies
func (ws *HTTPServer) requestedPolicies(r *http.Request) []string {
queryPolicyName := r.URL.Query().Get("name")
if queryPolicyName != "" {
return []string{queryPolicyName}
}
policies := make([]string, len(ws.config.BackupPolicy))
for i, p := range ws.config.BackupPolicy {
policies[i] = *p.Name
}
return policies
}
3 changes: 3 additions & 0 deletions pkg/service/configuration_service_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@ func validate(b *model.BackupStorage) error {
if b.Type == nil {
return errors.New("storage type is required")
}
if b.Path == nil {
return errors.New("storage path is required")
}
return nil
}
1 change: 1 addition & 0 deletions pkg/service/configuration_service_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestAddStorage(t *testing.T) {
newStorage := &model.BackupStorage{
Name: ptr.String("storage"),
Type: util.Ptr(model.Local),
Path: ptr.String("path"),
}

err := AddStorage(config, newStorage)
Expand Down

0 comments on commit 20412c8

Please sign in to comment.