Skip to content

Commit 0bacef8

Browse files
committed
feat: add how to use Cloud Functions
1 parent a86a561 commit 0bacef8

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
- [influx_cloud.py](influx_cloud.py) - How to connect to InfluxDB 2 Cloud
2525
- [influxdb_18_example.py](influxdb_18_example.py) - How to connect to InfluxDB 1.8
2626
- [nanosecond_precision.py](nanosecond_precision.py) - How to use nanoseconds precision
27+
- [managed_functions.py](managed_functions.py) - How to use Cloud Managed Functions
2728

examples/managed_functions.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
How to use Cloud Managed Functions
3+
"""
4+
import datetime
5+
6+
from influxdb_client import InfluxDBClient, FunctionCreateRequest, FunctionLanguage, FunctionInvocationParams
7+
from influxdb_client.service import FunctionsService
8+
9+
"""
10+
Define credentials
11+
"""
12+
influx_cloud_url = 'https://us-west-2-1.aws.cloud2.influxdata.com'
13+
influx_cloud_token = '...'
14+
bucket_name = '...'
15+
org_name = '...'
16+
17+
with InfluxDBClient(url=influx_cloud_url, token=influx_cloud_token, org=org_name, debug=True, timeout=20_000) as client:
18+
uniqueId = str(datetime.datetime.now())
19+
"""
20+
Find Organization ID by Organization API.
21+
"""
22+
org = client.organizations_api().find_organizations(org=org_name)[0]
23+
24+
functions_service = FunctionsService(api_client=client.api_client)
25+
26+
"""
27+
Create Managed function
28+
"""
29+
print(f"------- Create -------\n")
30+
create_request = FunctionCreateRequest(name=f"my_func_{uniqueId}",
31+
description="my first try",
32+
language=FunctionLanguage.FLUX,
33+
org_id=org.id,
34+
script=f"from(bucket: \"bucket-for-testing-2\") |> range(start: -7d) |> limit(n:2)")
35+
36+
created_function = functions_service.post_functions(function_create_request=create_request)
37+
print(created_function)
38+
39+
"""
40+
Invoke Function
41+
"""
42+
print(f"\n------- Invoke Function: -------\n")
43+
response = functions_service.post_functions_id_invoke(function_id=created_function.id,
44+
function_invocation_params=FunctionInvocationParams(
45+
params={"bucket_name": bucket_name}))
46+
47+
"""
48+
List all Functions
49+
"""
50+
print(f"\n------- Functions: -------\n")
51+
functions = functions_service.get_functions(org=org).functions
52+
print("\n".join([f" ---\n ID: {it.id}\n Name: {it.name}\n Description: {it.description}" for it in functions]))
53+
print("---")
54+
55+
"""
56+
Delete previously created Function
57+
"""
58+
print(f"------- Delete -------\n")
59+
functions_service.delete_functions_id(created_function.id)
60+
print(f" successfully deleted function: {created_function.name}")

0 commit comments

Comments
 (0)