1+ import os
2+
3+ from influxdb_client import InfluxDBClient , BucketRetentionRules , PermissionResource , Permission , Authorization , \
4+ WriteOptions
5+ from influxdb_client .client .write_api import WriteType
6+ from influxdb_client .rest import ApiException
7+
8+ HOST_URL = os .environ .get ("INFLUX_HOST" ) if os .environ .get ("INFLUX_HOST" ) is not None else "http://localhost:8086"
9+ TOKEN = os .environ .get ("INFLUX_TOKEN" ) if os .environ .get ("INFLUX_TOKEN" ) is not None else "my-token"
10+ ORG = os .environ .get ("INFLUX_ORG" ) if os .environ .get ("INFLUX_ORG" ) is not None else "my-org"
11+ SYS_BUCKET = os .environ .get ("INFLUX_DB" ) if os .environ .get ("INFLUX_DB" ) is not None else "my-bucket"
12+ BUCKET = "special-bucket"
13+
14+
15+ def create_auths ():
16+ # Create authorizations with an initial client using all-access permissions
17+ with InfluxDBClient (url = HOST_URL , token = TOKEN , org = ORG , debug = False ) as globalClient :
18+ bucket_rules = BucketRetentionRules (type = "expire" , every_seconds = 3600 )
19+ bucket = globalClient .buckets_api ().create_bucket (bucket_name = BUCKET ,
20+ retention_rules = bucket_rules ,
21+ org = ORG )
22+
23+ bucket_permission_resource_r = PermissionResource (org = ORG ,
24+ org_id = bucket .org_id ,
25+ type = "buckets" ,
26+ id = bucket .id )
27+ bucket_permission_resource_w = PermissionResource (org = ORG ,
28+ org_id = bucket .org_id ,
29+ type = "buckets" ,
30+ id = bucket .id )
31+ read_bucket = Permission (action = "read" , resource = bucket_permission_resource_r )
32+ write_bucket = Permission (action = "write" , resource = bucket_permission_resource_w )
33+ permissions = [read_bucket , write_bucket ]
34+ auth_payload = Authorization (org_id = bucket .org_id ,
35+ permissions = permissions ,
36+ description = "Shared bucket auth from Authorization object" ,
37+ id = "auth1_base" )
38+ auth_api = globalClient .authorizations_api ()
39+ # use keyword arguments
40+ auth1 = auth_api .create_authorization (authorization = auth_payload )
41+ # or use positional arguments
42+ auth2 = auth_api .create_authorization (bucket .org_id , permissions )
43+
44+ return auth1 , auth2
45+
46+
47+ def try_sys_bucket (client ):
48+ print ("starting to write" )
49+
50+ w_api = client .write_api (write_options = WriteOptions (write_type = WriteType .synchronous ))
51+ try :
52+ w_api .write (bucket = SYS_BUCKET , record = "cpu,host=r2d2 use=3.14" )
53+ except ApiException as ae :
54+ print (f"Write to { SYS_BUCKET } failed (as expected) due to:" )
55+ print (ae )
56+
57+
58+ def try_restricted_bucket (client ):
59+ print ("starting to write" )
60+ w_api = client .write_api (write_options = WriteOptions (write_type = WriteType .synchronous ))
61+
62+ w_api .write (bucket = BUCKET , record = "cpu,host=r2d2 usage=3.14" )
63+ print ("written" )
64+ print ("now query" )
65+ q_api = client .query_api ()
66+ query = f'''
67+ from(bucket:"{ BUCKET } ")
68+ |> range(start: -5m)
69+ |> filter(fn: (r) => r["_measurement"] == "cpu")'''
70+
71+ tables = q_api .query (query = query , org = ORG )
72+ for table in tables :
73+ for record in table .records :
74+ print (record ["_time" ].isoformat (sep = "T" ) + " | " + record ["host" ] + " | " + record ["_field" ] + "=" + str (record ["_value" ]))
75+
76+
77+ def main ():
78+ """
79+ a1 is generated using a local Authorization instance
80+ a2 is generated using local permissions and an internally created Authorization
81+ :return: void
82+ """
83+ print ("=== Setting up authorizations ===" )
84+ a1 , a2 = create_auths ()
85+
86+ print ("=== Using a1 authorization ===" )
87+ client1 = InfluxDBClient (url = HOST_URL , token = a1 .token , org = ORG , debug = False )
88+ print (" --- Try System Bucket ---" )
89+ try_sys_bucket (client1 )
90+ print (" --- Try Special Bucket ---" )
91+ try_restricted_bucket (client1 )
92+ print ()
93+
94+ print ("=== Using a2 authorization ===" )
95+ client2 = InfluxDBClient (url = HOST_URL , token = a2 .token , org = ORG , debug = False )
96+ print (" --- Try System Bucket ---" )
97+ try_sys_bucket (client2 )
98+ print (" --- Try Special Bucket ---" )
99+ try_restricted_bucket (client2 )
100+
101+
102+ if __name__ == "__main__" :
103+ main ()
0 commit comments