/
influxdb.go
148 lines (124 loc) · 2.91 KB
/
influxdb.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"fmt"
"net/http"
"net/url"
"time"
influxdbclient "github.com/influxdata/influxdb1-client"
)
func influxdbHandler(w http.ResponseWriter, r *http.Request) {
err := testInfluxDBConnection()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJson(w, map[string]interface{}{
"success": true,
})
}
func testInfluxDBConnection() error {
const (
database = "defaultdb"
measurement = "acceptance_test_meas"
retentionPolicy = "default_retention_policy"
)
testRun := fmt.Sprintf("%x", time.Now().Unix())
var credentials struct {
URI string `json:"uri"`
}
err := getVCAPServiceCredentials("influxdb", &credentials)
if err != nil {
return err
}
u, err := url.Parse(credentials.URI)
if err != nil {
return err
}
if u.User == nil {
return fmt.Errorf("Expected Userinfo on URL, but was nil")
}
username := u.User.Username()
if username == "" {
return fmt.Errorf("Expected username in URL, but was empty")
}
password, present := u.User.Password()
if !present || password == "" {
return fmt.Errorf("Expected password in URL, but was empty")
}
cfg := &influxdbclient.Config{
URL: *u,
Username: username,
Password: password,
}
conn, err := influxdbclient.NewClient(*cfg)
if err != nil {
return err
}
// Ping the database
_, _, err = conn.Ping()
if err != nil {
return err
}
// Show databases
showQuery := influxdbclient.Query{
Command: "SHOW DATABASES",
}
showResp, err := conn.Query(showQuery)
if err != nil {
return err
}
if showResp.Error() != nil {
return fmt.Errorf("Error creating database: %s", showResp.Error())
}
batch := influxdbclient.BatchPoints{
Database: database,
RetentionPolicy: retentionPolicy,
Points: []influxdbclient.Point{
influxdbclient.Point{
Measurement: measurement,
Fields: map[string]interface{}{
"test_run": testRun,
},
},
},
}
writeResp, err := conn.Write(batch)
if err != nil {
return fmt.Errorf("Failed to write points: %s %+v", err, writeResp)
}
readResp, err := conn.Query(influxdbclient.Query{
Database: database,
Command: fmt.Sprintf(
`SELECT time, test_run FROM %s WHERE test_run = '%s'`,
measurement, testRun,
),
})
if err != nil {
return fmt.Errorf(
"Error reading test data: %s %+v",
err, readResp,
)
}
readResult := readResp.Results[0]
if len(readResult.Series) != 1 {
return fmt.Errorf(
"Read %d results, expected 1, response: %+v",
len(readResult.Series), readResp,
)
}
readSerie := readResult.Series[0]
if len(readSerie.Values) != 1 {
return fmt.Errorf(
"Read %d values, expected a 1 value [[time, test_run]], response: %+v",
len(readSerie.Values), readResp,
)
}
readTestRun := fmt.Sprintf("%s", readSerie.Values[0][1])
if readTestRun != testRun {
return fmt.Errorf(
"Read %s, expected %s, response: %+v",
readTestRun, testRun, readResp,
)
}
return nil
}