Skip to content

Commit

Permalink
Merge pull request #267 from digitalocean/asb/db-firewall-get
Browse files Browse the repository at this point in the history
Return []DatabaseFirewallRule in addition to raw response.
  • Loading branch information
bentranter authored Oct 30, 2019
2 parents 028fa7b + 3885b4b commit f9210ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
16 changes: 11 additions & 5 deletions databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type DatabasesService interface {
DeleteReplica(context.Context, string, string) (*Response, error)
GetEvictionPolicy(context.Context, string) (string, *Response, error)
SetEvictionPolicy(context.Context, string, string) (*Response, error)
GetFirewallRules(context.Context, string) (*Response, error)
GetFirewallRules(context.Context, string) ([]DatabaseFirewallRule, *Response, error)
UpdateFirewallRules(context.Context, string, *DatabaseUpdateFirewallRulesRequest) (*Response, error)
}

Expand Down Expand Up @@ -277,7 +277,7 @@ type evictionPolicyRoot struct {
}

type databaseFirewallRuleRoot struct {
Rules []*DatabaseFirewallRule `json:"rules"`
Rules []DatabaseFirewallRule `json:"rules"`
}

func (d Database) URN() string {
Expand Down Expand Up @@ -692,14 +692,20 @@ func (svc *DatabasesServiceOp) SetEvictionPolicy(ctx context.Context, databaseID
}

// GetFirewallRules loads the inbound sources for a given cluster.
func (svc *DatabasesServiceOp) GetFirewallRules(ctx context.Context, databaseID string) (*Response, error) {
func (svc *DatabasesServiceOp) GetFirewallRules(ctx context.Context, databaseID string) ([]DatabaseFirewallRule, *Response, error) {
path := fmt.Sprintf(databaseFirewallRulesPath, databaseID)
root := new(databaseFirewallRuleRoot)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
return nil, nil, err
}
return svc.client.Do(ctx, req, root)

resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root.Rules, resp, nil
}

// UpdateFirewallRules sets the inbound sources for a given cluster.
Expand Down
12 changes: 11 additions & 1 deletion databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,15 @@ func TestDatabases_GetFirewallRules(t *testing.T) {

path := fmt.Sprintf("/v2/databases/%s/firewall", dbID)

want := []DatabaseFirewallRule{
{
Type: "ip_addr",
Value: "192.168.1.1",
UUID: "deadbeef-dead-4aa5-beef-deadbeef347d",
ClusterUUID: "deadbeef-dead-4aa5-beef-deadbeef347d",
},
}

body := ` {"rules": [{
"type": "ip_addr",
"value": "192.168.1.1",
Expand All @@ -1204,8 +1213,9 @@ func TestDatabases_GetFirewallRules(t *testing.T) {
fmt.Fprint(w, body)
})

_, err := client.Databases.GetFirewallRules(ctx, dbID)
got, _, err := client.Databases.GetFirewallRules(ctx, dbID)
require.NoError(t, err)
require.Equal(t, want, got)
}

func TestDatabases_UpdateFirewallRules(t *testing.T) {
Expand Down

0 comments on commit f9210ed

Please sign in to comment.