File tree 1 file changed +27
-3
lines changed
1 file changed +27
-3
lines changed Original file line number Diff line number Diff line change 3
3
"""
4
4
5
5
from influxdb_client import InfluxDBClient
6
+ from influxdb_client .client .write_api import SYNCHRONOUS
7
+ from influxdb_client .rest import ApiException
6
8
7
9
"""
8
10
Define credentials
15
17
16
18
def check_connection ():
17
19
"""Check that the InfluxDB is running."""
18
- pass
20
+ print ("> Checking connection ..." , end = " " )
21
+ client .api_client .call_api ('/ping' , 'GET' )
22
+ print ("ok" )
19
23
20
24
21
25
def check_query ():
22
26
"""Check that the credentials has permission to query from the Bucket"""
23
- pass
27
+ print ("> Checking credentials for query ..." , end = " " )
28
+ try :
29
+ client .query_api ().query (f"from(bucket:\" { bucket } \" ) |> range(start: -1m) |> limit(n:1)" , org )
30
+ except ApiException as e :
31
+ # missing credentials
32
+ if e .status == 404 :
33
+ raise Exception (f"The specified token doesn't have sufficient credentials to read from '{ bucket } ' "
34
+ f"or specified bucket doesn't exists." ) from e
35
+ raise
36
+ print ("ok" )
24
37
25
38
26
39
def check_write ():
27
40
"""Check that the credentials has permission to write into the Bucket"""
28
- pass
41
+ print ("> Checking credentials for write ..." , end = " " )
42
+ try :
43
+ client .write_api (write_options = SYNCHRONOUS ).write (bucket , org , b"" )
44
+ except ApiException as e :
45
+ # missing credentials
46
+ if e .status == 404 :
47
+ raise Exception (f"The specified token doesn't have sufficient credentials to write to '{ bucket } ' "
48
+ f"or specified bucket doesn't exists." ) from e
49
+ # 400 (BadRequest) caused by empty LineProtocol
50
+ if e .status != 400 :
51
+ raise
52
+ print ("ok" )
29
53
30
54
31
55
with InfluxDBClient (url = url , token = token , org = org ) as client :
You can’t perform that action at this time.
0 commit comments