Skip to content

Commit 26d6590

Browse files
authored
Merge pull request #13 from digitalocean/mdl-ovsdb-refactor
ovsdb: refactor client into multiple files
2 parents 3a75100 + 876967d commit 26d6590

File tree

5 files changed

+136
-79
lines changed

5 files changed

+136
-79
lines changed

ovsdb/client.go

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package ovsdb
1616

1717
import (
18-
"bytes"
1918
"encoding/json"
2019
"fmt"
2120
"io"
@@ -115,16 +114,6 @@ func (c *Client) Close() error {
115114
return err
116115
}
117116

118-
// ListDatabases returns the name of all databases known to the OVSDB server.
119-
func (c *Client) ListDatabases() ([]string, error) {
120-
var dbs []string
121-
if err := c.rpc("list_dbs", &dbs); err != nil {
122-
return nil, err
123-
}
124-
125-
return dbs, nil
126-
}
127-
128117
// rpc performs a single RPC request, and checks the response for errors.
129118
func (c *Client) rpc(method string, out interface{}, args ...interface{}) error {
130119
// Unmarshal results into empty struct if no out specified.
@@ -233,45 +222,6 @@ func (c *Client) doCallback(id int, res rpcResponse) {
233222
delete(c.callbacks, id)
234223
}
235224

236-
// A result is used to unmarshal JSON-RPC results, and to check for any errors.
237-
type result struct {
238-
Reply interface{}
239-
Err *Error
240-
}
241-
242-
// errPrefix is a prefix that occurs if an error is present in a JSON-RPC response.
243-
var errPrefix = []byte(`{"error":`)
244-
245-
func (r *result) UnmarshalJSON(b []byte) error {
246-
// No error? Return the result.
247-
if !bytes.HasPrefix(b, errPrefix) {
248-
return json.Unmarshal(b, r.Reply)
249-
}
250-
251-
// Found an error, unmarshal and return it later.
252-
var e Error
253-
if err := json.Unmarshal(b, &e); err != nil {
254-
return err
255-
}
256-
257-
r.Err = &e
258-
return nil
259-
}
260-
261-
var _ error = &Error{}
262-
263-
// An Error is an error returned by an OVSDB server. Its fields can be
264-
// used to determine the cause of an error.
265-
type Error struct {
266-
Err string `json:"error"`
267-
Details string `json:"details"`
268-
Syntax string `json:"syntax"`
269-
}
270-
271-
func (e *Error) Error() string {
272-
return fmt.Sprintf("%s: %s: %s", e.Err, e.Details, e.Syntax)
273-
}
274-
275225
func panicf(format string, a ...interface{}) {
276226
panic(fmt.Sprintf(format, a...))
277227
}

ovsdb/client_test.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -73,35 +73,6 @@ func TestClientOVSDBError(t *testing.T) {
7373
}
7474
}
7575

76-
func TestClientListDatabases(t *testing.T) {
77-
want := []string{"Open_vSwitch", "test"}
78-
79-
c, _, done := testClient(t, func(req jsonrpc.Request) jsonrpc.Response {
80-
if diff := cmp.Diff("list_dbs", req.Method); diff != "" {
81-
panicf("unexpected RPC method (-want +got):\n%s", diff)
82-
}
83-
84-
if diff := cmp.Diff(0, len(req.Params)); diff != "" {
85-
panicf("unexpected number of RPC parameters (-want +got):\n%s", diff)
86-
}
87-
88-
return jsonrpc.Response{
89-
ID: intPtr(1),
90-
Result: mustMarshalJSON(t, want),
91-
}
92-
})
93-
defer done()
94-
95-
dbs, err := c.ListDatabases()
96-
if err != nil {
97-
t.Fatalf("failed to list databases: %v", err)
98-
}
99-
100-
if diff := cmp.Diff(want, dbs); diff != "" {
101-
t.Fatalf("unexpected databases (-want +got):\n%s", diff)
102-
}
103-
}
104-
10576
func testClient(t *testing.T, fn jsonrpc.TestFunc) (*ovsdb.Client, chan<- *jsonrpc.Response, func()) {
10677
t.Helper()
10778

ovsdb/result.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2017 DigitalOcean.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package ovsdb
16+
17+
import (
18+
"bytes"
19+
"encoding/json"
20+
"fmt"
21+
)
22+
23+
// A result is used to unmarshal JSON-RPC results, and to check for any errors.
24+
type result struct {
25+
Reply interface{}
26+
Err *Error
27+
}
28+
29+
// errPrefix is a prefix that occurs if an error is present in a JSON-RPC response.
30+
var errPrefix = []byte(`{"error":`)
31+
32+
func (r *result) UnmarshalJSON(b []byte) error {
33+
// No error? Return the result.
34+
if !bytes.HasPrefix(b, errPrefix) {
35+
return json.Unmarshal(b, r.Reply)
36+
}
37+
38+
// Found an error, unmarshal and return it later.
39+
var e Error
40+
if err := json.Unmarshal(b, &e); err != nil {
41+
return err
42+
}
43+
44+
r.Err = &e
45+
return nil
46+
}
47+
48+
var _ error = &Error{}
49+
50+
// An Error is an error returned by an OVSDB server. Its fields can be
51+
// used to determine the cause of an error.
52+
type Error struct {
53+
Err string `json:"error"`
54+
Details string `json:"details"`
55+
Syntax string `json:"syntax"`
56+
}
57+
58+
func (e *Error) Error() string {
59+
return fmt.Sprintf("%s: %s: %s", e.Err, e.Details, e.Syntax)
60+
}

ovsdb/rpc.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2017 DigitalOcean.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package ovsdb
16+
17+
// ListDatabases returns the name of all databases known to the OVSDB server.
18+
func (c *Client) ListDatabases() ([]string, error) {
19+
var dbs []string
20+
if err := c.rpc("list_dbs", &dbs); err != nil {
21+
return nil, err
22+
}
23+
24+
return dbs, nil
25+
}

ovsdb/rpc_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2017 DigitalOcean.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package ovsdb_test
16+
17+
import (
18+
"testing"
19+
20+
"github.com/digitalocean/go-openvswitch/ovsdb/internal/jsonrpc"
21+
"github.com/google/go-cmp/cmp"
22+
)
23+
24+
func TestClientListDatabases(t *testing.T) {
25+
want := []string{"Open_vSwitch", "test"}
26+
27+
c, _, done := testClient(t, func(req jsonrpc.Request) jsonrpc.Response {
28+
if diff := cmp.Diff("list_dbs", req.Method); diff != "" {
29+
panicf("unexpected RPC method (-want +got):\n%s", diff)
30+
}
31+
32+
if diff := cmp.Diff(0, len(req.Params)); diff != "" {
33+
panicf("unexpected number of RPC parameters (-want +got):\n%s", diff)
34+
}
35+
36+
return jsonrpc.Response{
37+
ID: intPtr(1),
38+
Result: mustMarshalJSON(t, want),
39+
}
40+
})
41+
defer done()
42+
43+
dbs, err := c.ListDatabases()
44+
if err != nil {
45+
t.Fatalf("failed to list databases: %v", err)
46+
}
47+
48+
if diff := cmp.Diff(want, dbs); diff != "" {
49+
t.Fatalf("unexpected databases (-want +got):\n%s", diff)
50+
}
51+
}

0 commit comments

Comments
 (0)