diff --git a/database_backup.go b/database_backup.go index 8d65a5c..d3c1f48 100644 --- a/database_backup.go +++ b/database_backup.go @@ -88,3 +88,17 @@ func (c *Client) CreateDatabaseBackup(did string, v *DatabaseBackupCreateRequest return result, nil } + +// GetDatabaseBackup gets a particular database backup +func (c *Client) GetDatabaseBackup(did string, backupID string) (*DatabaseBackup, error) { + body, err := c.SendGetRequest(fmt.Sprintf("/v2/databases/%s/backups/%s/", did, backupID)) + if err != nil { + return nil, decodeError(err) + } + result := &DatabaseBackup{} + if err := json.NewDecoder(bytes.NewReader(body)).Decode(result); err != nil { + return nil, err + } + + return result, nil +} diff --git a/database_backup_test.go b/database_backup_test.go new file mode 100644 index 0000000..0adb962 --- /dev/null +++ b/database_backup_test.go @@ -0,0 +1,165 @@ +package civogo + +import ( + "reflect" + "testing" +) + +func TestCreateDatabaseBackup(t *testing.T) { + client, server, _ := NewClientForTesting(map[string]string{ + "/v2/databases/12345/backups": `{ + "id": "backup123", + "name": "initial-backup", + "software": "MySQL", + "status": "In Progress", + "schedule": "manual", + "database_name": "test-db", + "database_id": "12345", + "is_scheduled": false + }`, + }) + defer server.Close() + + v := &DatabaseBackupCreateRequest{ + Name: "initial-backup", + Schedule: "manual", + Count: 1, + Type: "full", + Region: "us-east", + } + got, err := client.CreateDatabaseBackup("12345", v) + if err != nil { + t.Errorf("CreateDatabaseBackup returned an error: %s", err) + return + } + + expected := &DatabaseBackup{ + ID: "backup123", + Name: "initial-backup", + Software: "MySQL", + Status: "In Progress", + Schedule: "manual", + DatabaseName: "test-db", + DatabaseID: "12345", + IsScheduled: false, + } + + if !reflect.DeepEqual(got, expected) { + t.Errorf("Expected %+v, got %+v", expected, got) + } +} + +func TestUpdateDatabaseBackup(t *testing.T) { + client, server, _ := NewClientForTesting(map[string]string{ + "/v2/databases/12345/backups": `{ + "id": "backup123", + "name": "updated-backup", + "schedule": "weekly", + "count": 2, + "region": "us-west" + }`, + }) + defer server.Close() + + v := &DatabaseBackupUpdateRequest{ + Name: "updated-backup", + Schedule: "weekly", + Count: 2, + Region: "us-west", + } + got, err := client.UpdateDatabaseBackup("12345", v) + if err != nil { + t.Errorf("UpdateDatabaseBackup returned an error: %s", err) + return + } + + expected := &DatabaseBackup{ + ID: "backup123", + Name: "updated-backup", + Schedule: "weekly", + } + + if !reflect.DeepEqual(got, expected) { + t.Errorf("Expected %+v, got %+v", expected, got) + } +} + +func TestGetDatabaseBackup(t *testing.T) { + client, server, _ := NewClientForTesting(map[string]string{ + "/v2/databases/12345/backups/backup123": `{ + "id": "backup123", + "name": "nightly-backup", + "software": "MySQL", + "status": "Completed", + "schedule": "daily", + "database_name": "test-db", + "database_id": "12345", + "backup": "url-to-backup", + "is_scheduled": true + }`, + }) + defer server.Close() + + got, err := client.GetDatabaseBackup("12345", "backup123") + if err != nil { + t.Errorf("GetDatabaseBackup returned an error: %s", err) + return + } + + expected := &DatabaseBackup{ + ID: "backup123", + Name: "nightly-backup", + Software: "MySQL", + Status: "Completed", + Schedule: "daily", + DatabaseName: "test-db", + DatabaseID: "12345", + Backup: "url-to-backup", + IsScheduled: true, + } + + if !reflect.DeepEqual(got, expected) { + t.Errorf("Expected %+v, got %+v", expected, got) + } +} + +func TestListDatabaseBackup(t *testing.T) { + client, server, _ := NewClientForTesting(map[string]string{ + "/v2/databases/12345/backups": `{ + "page": 1, + "per_page": 2, + "pages": 1, + "items": [ + { + "id": "backup123", + "name": "initial-backup" + }, + { + "id": "backup124", + "name": "second-backup" + } + ] + }`, + }) + defer server.Close() + + got, err := client.ListDatabaseBackup("12345") + if err != nil { + t.Errorf("ListDatabaseBackup returned an error: %s", err) + return + } + + expected := &PaginatedDatabaseBackup{ + Page: 1, + PerPage: 2, + Pages: 1, + Items: []DatabaseBackup{ + {ID: "backup123", Name: "initial-backup"}, + {ID: "backup124", Name: "second-backup"}, + }, + } + + if !reflect.DeepEqual(got, expected) { + t.Errorf("Expected %+v, got %+v", expected, got) + } +} diff --git a/fake_client.go b/fake_client.go index 29b6acd..4769ad0 100644 --- a/fake_client.go +++ b/fake_client.go @@ -184,6 +184,12 @@ type Clienter interface { CreateLoadBalancer(r *LoadBalancerConfig) (*LoadBalancer, error) UpdateLoadBalancer(id string, r *LoadBalancerUpdateConfig) (*LoadBalancer, error) DeleteLoadBalancer(id string) (*SimpleResponse, error) + + // DataBase Backup + CreateDatabaseBackup(dbID string, v *DatabaseBackupCreateRequest) (*DatabaseBackup, error) + GetDatabaseBackup(dbID string, backupID string) (*DatabaseBackup, error) + UpdateDatabaseBackup(dbID string, v *DatabaseBackupUpdateRequest) (*DatabaseBackup, error) + ListDatabaseBackup(dbID string) (*PaginatedDatabaseBackup, error) } // NewFakeClient initializes a Client that doesn't attach to a @@ -1847,3 +1853,70 @@ func (c *FakeClient) UnassignIP(id, region string) (*SimpleResponse, error) { Result: "success", }, nil } + +// CreateDatabaseBackup creates a fake db backup +func (c *FakeClient) CreateDatabaseBackup(dbID string, v *DatabaseBackupCreateRequest) (*DatabaseBackup, error) { + return &DatabaseBackup{ + ID: c.generateID(), + Name: v.Name, + Software: "postgresSQL", + Status: "ready", + Schedule: v.Schedule, + DatabaseName: "root", + DatabaseID: dbID, + Backup: "", + IsScheduled: false, + }, nil +} + +// GetDatabaseBackup gets a fake db backup +func (c *FakeClient) GetDatabaseBackup(dbID string, backupID string) (*DatabaseBackup, error) { + return &DatabaseBackup{ + ID: backupID, + Name: "test-db-backup", + Software: "postgresSQL", + Status: "ready", + Schedule: "monthly", + DatabaseName: "root", + DatabaseID: dbID, + Backup: "", + IsScheduled: false, + }, nil +} + +// UpdateDatabaseBackup updates a fake db backup +func (c *FakeClient) UpdateDatabaseBackup(dbID string, v *DatabaseBackupUpdateRequest) (*DatabaseBackup, error) { + return &DatabaseBackup{ + ID: dbID, + Name: v.Name, + Software: "postgresSQL", + Status: "ready", + Schedule: v.Schedule, + DatabaseName: "root", + DatabaseID: dbID, + Backup: "", + IsScheduled: false, + }, nil +} + +// ListDatabaseBackup lists one fake db backup +func (c *FakeClient) ListDatabaseBackup(dbID string) (*PaginatedDatabaseBackup, error) { + return &PaginatedDatabaseBackup{ + Page: 1, + PerPage: 20, + Pages: 10, + Items: []DatabaseBackup{ + { + ID: c.generateID(), + Name: "test-db-backup", + Software: "postgresSQL", + Status: "ready", + Schedule: "monthly", + DatabaseName: "root", + DatabaseID: dbID, + Backup: "", + IsScheduled: false, + }, + }, + }, nil +}