|
| 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