Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 6dfdbf5

Browse files
author
Russell Troxel
committed
Added Opts validation to CreateBackup
1 parent c1072a1 commit 6dfdbf5

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

openstack/compute/v2/extensions/adminactions/requests.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package adminactions
22

3-
import "github.com/rackspace/gophercloud"
3+
import (
4+
"fmt"
5+
6+
"github.com/rackspace/gophercloud"
7+
)
48

59
func actionURL(client *gophercloud.ServiceClient, id string) string {
610
return client.ServiceURL("servers", id, "action")
@@ -21,12 +25,17 @@ type CreateBackupOpts struct {
2125
func (opts CreateBackupOpts) ToCreateBackupMap() (map[string]interface{}, error) {
2226
backup := make(map[string]interface{})
2327

24-
if opts.Name != "" {
25-
backup["name"] = opts.Name
28+
if opts.Name == "" {
29+
return nil, fmt.Errorf("CreateBackupOpts.Name cannot be blank.")
30+
}
31+
if opts.BackupType == "" {
32+
return nil, fmt.Errorf("CreateBackupOpts.BackupType cannot be blank.")
2633
}
27-
if opts.BackupType != "" {
28-
backup["backup_type"] = opts.BackupType
34+
if opts.Rotation < 0 {
35+
return nil, fmt.Errorf("CreateBackupOpts.Rotation must 0 or greater.")
2936
}
37+
backup["name"] = opts.Name
38+
backup["backup_type"] = opts.BackupType
3039
backup["rotation"] = opts.Rotation
3140

3241
return map[string]interface{}{"createBackup": backup}, nil

openstack/compute/v2/extensions/adminactions/requests_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package adminactions
22

33
import (
4+
"fmt"
45
"testing"
56

67
th "github.com/rackspace/gophercloud/testhelper"
@@ -23,6 +24,53 @@ func TestCreateBackup(t *testing.T) {
2324
th.AssertNoErr(t, err)
2425
}
2526

27+
func TestCreateBackupNoName(t *testing.T) {
28+
th.SetupHTTP()
29+
defer th.TeardownHTTP()
30+
31+
mockCreateBackupResponse(t, serverID)
32+
33+
err := CreateBackup(client.ServiceClient(), serverID, CreateBackupOpts{
34+
BackupType: "daily",
35+
Rotation: 1,
36+
}).ExtractErr()
37+
if err == nil {
38+
fmt.Errorf("CreateBackup without a specified Name should throw an Error.")
39+
}
40+
}
41+
42+
func TestCreateBackupNegativeRotation(t *testing.T) {
43+
th.SetupHTTP()
44+
defer th.TeardownHTTP()
45+
46+
mockCreateBackupResponse(t, serverID)
47+
48+
err := CreateBackup(client.ServiceClient(), serverID, CreateBackupOpts{
49+
Name: "Backup 1",
50+
BackupType: "daily",
51+
Rotation: -1,
52+
}).ExtractErr()
53+
if err == nil {
54+
fmt.Errorf("CreateBackup without a negative Rotation should throw an Error.")
55+
}
56+
}
57+
58+
func TestCreateBackupNoType(t *testing.T) {
59+
th.SetupHTTP()
60+
defer th.TeardownHTTP()
61+
62+
mockCreateBackupResponse(t, serverID)
63+
64+
err := CreateBackup(client.ServiceClient(), serverID, CreateBackupOpts{
65+
Name: "Backup 1",
66+
67+
Rotation: 1,
68+
}).ExtractErr()
69+
if err == nil {
70+
fmt.Errorf("CreateBackup without a specified BackupType should throw an Error.")
71+
}
72+
}
73+
2674
func TestInjectNetworkInfo(t *testing.T) {
2775
th.SetupHTTP()
2876
defer th.TeardownHTTP()

0 commit comments

Comments
 (0)