/
rethinkdb.go
51 lines (46 loc) · 1.41 KB
/
rethinkdb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package rethinkdb
import (
"time"
"github.com/Sirupsen/logrus"
"github.com/docker/go-connections/tlsconfig"
"gopkg.in/dancannon/gorethink.v3"
)
// Timing can be embedded into other gorethink models to
// add time tracking fields
type Timing struct {
CreatedAt time.Time `gorethink:"created_at"`
UpdatedAt time.Time `gorethink:"updated_at"`
DeletedAt time.Time `gorethink:"deleted_at"`
}
// AdminConnection sets up an admin RethinkDB connection to the host (`host:port` format)
// using the CA .pem file provided at path `caFile`
func AdminConnection(tlsOpts tlsconfig.Options, host string) (*gorethink.Session, error) {
logrus.Debugf("attempting to connect admin to host %s", host)
t, err := tlsconfig.Client(tlsOpts)
if err != nil {
return nil, err
}
return gorethink.Connect(
gorethink.ConnectOpts{
Address: host,
TLSConfig: t,
},
)
}
// UserConnection sets up a user RethinkDB connection to the host (`host:port` format)
// using the CA .pem file provided at path `caFile`, using the provided username.
func UserConnection(tlsOpts tlsconfig.Options, host, username, password string) (*gorethink.Session, error) {
logrus.Debugf("attempting to connect user %s to host %s", username, host)
t, err := tlsconfig.Client(tlsOpts)
if err != nil {
return nil, err
}
return gorethink.Connect(
gorethink.ConnectOpts{
Address: host,
TLSConfig: t,
Username: username,
Password: password,
},
)
}