-
Notifications
You must be signed in to change notification settings - Fork 22
/
influx_connection.go
58 lines (46 loc) · 1.4 KB
/
influx_connection.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
52
53
54
55
56
57
58
package connections
import (
"fmt"
"os"
influx "github.com/influxdata/influxdb/client/v2"
)
// Environment variable names to be used for the InfluxDB connection
const (
UserEnvVar = "INFLUX_USERNAME"
PassEnvVar = "INFLUX_PASSWORD"
)
// InfluxConnectionParams represents the parameters required to open a InfluxDB connection
type InfluxConnectionParams struct {
Server string
Username string
Password string
Database string
}
// InfluxConnectionService creates new clients connected to some Influx server
type InfluxConnectionService interface {
NewConnection(*InfluxConnectionParams) (influx.Client, error)
}
type defaultInfluxConnectionService struct{}
// NewInfluxConnectionService creates a new instance of the service
func NewInfluxConnectionService() InfluxConnectionService {
return &defaultInfluxConnectionService{}
}
func (s *defaultInfluxConnectionService) NewConnection(params *InfluxConnectionParams) (influx.Client, error) {
if params == nil {
return nil, fmt.Errorf("Connection params shouldn't be nil")
}
var user, pass string
if params.Username != "" {
user = params.Username
} else {
user = os.Getenv(UserEnvVar)
}
if params.Password != "" {
pass = params.Password
} else {
pass = os.Getenv(PassEnvVar)
}
clientConfig := influx.HTTPConfig{Addr: params.Server, Username: user, Password: pass}
newClient, err := influx.NewHTTPClient(clientConfig)
return newClient, err
}