Skip to content

Commit

Permalink
Add delete,get,find for database backup
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Anarase <vishal@civo.com>
  • Loading branch information
Vishal Anarase committed Mar 18, 2024
1 parent 7bf7f85 commit 6354363
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 21 deletions.
11 changes: 6 additions & 5 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ type SupportedSoftwareVersion struct {

// RestoreDatabaseRequest is the request body for restoring a database
type RestoreDatabaseRequest struct {
Name string `json:"name"`
Software string `json:"software"`
NetworkID string `json:"network_id"`
Backup string `json:"backup"`
Region string `json:"region"`
// Name is the name of the database restore
Name string `json:"name"`
// Backup is the name of the database backup
Backup string `json:"backup"`
// Region is the name of the region
Region string `json:"region"`
}

// ListDatabases returns a list of all databases
Expand Down
98 changes: 82 additions & 16 deletions database_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"
)

// DatabaseBackup represents a backup
type DatabaseBackup struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Software string `json:"software,omitempty"`
Status string `json:"status,omitempty"`
Schedule string `json:"schedule,omitempty"`
DatabaseName string `json:"database_name,omitempty"`
DatabaseID string `json:"database_id,omitempty"`
Backup string `json:"backup,omitempty"`
IsScheduled bool `json:"is_scheduled,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Software string `json:"software,omitempty"`
Status string `json:"status,omitempty"`
Schedule string `json:"schedule,omitempty"`
DatabaseName string `json:"database_name,omitempty"`
DatabaseID string `json:"database_id,omitempty"`
IsScheduled bool `json:"is_scheduled,omitempty"`
CreatedAt time.Time `json:"created_at"`
}

// PaginatedDatabaseBackup is the structure for list response from DB endpoint
Expand All @@ -29,19 +31,24 @@ type PaginatedDatabaseBackup struct {

// DatabaseBackupCreateRequest represents a backup create request
type DatabaseBackupCreateRequest struct {
Name string `json:"name"`
// Name is the name of the database backup to be created
Name string `json:"name"`
// Schedule is used for scheduled backup
Schedule string `json:"schedule"`
Count int32 `json:"count"`
Type string `json:"type"`
Region string `json:"region"`
// Types is used for manual backup
Type string `json:"type"`
// Region is name of the region
Region string `json:"region"`
}

// DatabaseBackupUpdateRequest represents a backup update request
type DatabaseBackupUpdateRequest struct {
Name string `json:"name"`
// Name is name name of the backup
Name string `json:"name"`
// Schedule is schedule for scheduled backup
Schedule string `json:"schedule"`
Count int32 `json:"count"`
Region string `json:"region"`
// Region is name of the region
Region string `json:"region"`
}

// ListDatabaseBackup lists backups for database
Expand Down Expand Up @@ -88,3 +95,62 @@ func (c *Client) CreateDatabaseBackup(did string, v *DatabaseBackupCreateRequest

return result, nil
}

// DeleteDatabaseBackup deletes a database backup
func (c *Client) DeleteDatabaseBackup(dbid, id string) (*SimpleResponse, error) {
resp, err := c.SendDeleteRequest(fmt.Sprintf("/v2/databases/%s/backups/%s", dbid, id))
if err != nil {
return nil, decodeError(err)
}

return c.DecodeSimpleResponse(resp)
}

// GetDatabase finds a database by the database UUID

Check failure on line 109 in database_backup.go

View workflow job for this annotation

GitHub Actions / test (1.17.x, ubuntu-latest)

comment on exported method Client.GetDatabaseBackup should be of the form "GetDatabaseBackup ..."

Check failure on line 109 in database_backup.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

comment on exported method Client.GetDatabaseBackup should be of the form "GetDatabaseBackup ..."
func (c *Client) GetDatabaseBackup(dbid, id string) (*DatabaseBackup, error) {
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/databases/%s/backups/%s", dbid, id))
if err != nil {
return nil, decodeError(err)
}

bk := &DatabaseBackup{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(bk); err != nil {
return nil, err
}

return bk, nil
}

// FindDatabase finds a database by either part of the ID or part of the name

Check failure on line 124 in database_backup.go

View workflow job for this annotation

GitHub Actions / test (1.17.x, ubuntu-latest)

comment on exported method Client.FindDatabaseBackup should be of the form "FindDatabaseBackup ..."

Check failure on line 124 in database_backup.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

comment on exported method Client.FindDatabaseBackup should be of the form "FindDatabaseBackup ..."
func (c *Client) FindDatabaseBackup(dbid, search string) (*DatabaseBackup, error) {
backups, err := c.ListDatabaseBackup(dbid)
if err != nil {
return nil, decodeError(err)
}

exactMatch := false
partialMatchesCount := 0
result := DatabaseBackup{}

for _, value := range backups.Items {
if strings.EqualFold(value.Name, search) || value.ID == search {
exactMatch = true
result = value
} else if strings.Contains(strings.ToUpper(value.Name), strings.ToUpper(search)) || strings.Contains(value.ID, search) {
if !exactMatch {
result = value
partialMatchesCount++
}
}
}

if exactMatch || partialMatchesCount == 1 {
return &result, nil
} else if partialMatchesCount > 1 {
err := fmt.Errorf("unable to find %s because there were multiple matches", search)
return nil, MultipleMatchesError.wrap(err)
} else {
err := fmt.Errorf("unable to find %s, zero matches", search)
return nil, ZeroMatchesError.wrap(err)
}
}

0 comments on commit 6354363

Please sign in to comment.