|
| 1 | +""" |
| 2 | +How to use Templates and Stack API. |
| 3 | +""" |
| 4 | +import datetime |
| 5 | + |
| 6 | +from influxdb_client import InfluxDBClient, TemplatesService, TemplateApply, TemplateApplyRemotes, PatchStackRequest, \ |
| 7 | + TemplateApplyTemplate |
| 8 | + |
| 9 | +""" |
| 10 | +Define credentials |
| 11 | +""" |
| 12 | +url = 'http://localhost:8086' |
| 13 | +token = 'my-token' |
| 14 | +bucket_name = 'my-bucket' |
| 15 | +org_name = 'my-org' |
| 16 | + |
| 17 | +with InfluxDBClient(url=url, token=token, org=org_name, debug=True) 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 | + """ |
| 25 | + Initialize Template service |
| 26 | + """ |
| 27 | + templates_service = TemplatesService(api_client=client.api_client) |
| 28 | + |
| 29 | + """ |
| 30 | + Apply 'Linux System Monitoring Template' |
| 31 | + """ |
| 32 | + template_yaml_url = "https://raw.githubusercontent.com/influxdata/community-templates/master/linux_system/linux_system.yml" # noqa: E501 |
| 33 | + template_linux = templates_service.apply_template( |
| 34 | + template_apply=TemplateApply(dry_run=False, |
| 35 | + org_id=org.id, |
| 36 | + remotes=[TemplateApplyRemotes(url=template_yaml_url)])) |
| 37 | + """ |
| 38 | + Set Stack name |
| 39 | + """ |
| 40 | + templates_service.update_stack(stack_id=template_linux.stack_id, |
| 41 | + patch_stack_request=PatchStackRequest(name="linux_system")) |
| 42 | + |
| 43 | + """ |
| 44 | + Create template as an inline definition |
| 45 | + """ |
| 46 | + template_definition = { |
| 47 | + "apiVersion": "influxdata.com/v2alpha1", |
| 48 | + "kind": "Bucket", |
| 49 | + "metadata": {"name": "template-bucket"}, |
| 50 | + "spec": {"description": "bucket 1 description"} |
| 51 | + } |
| 52 | + template_inline = templates_service.apply_template( |
| 53 | + template_apply=TemplateApply(dry_run=False, |
| 54 | + org_id=org.id, |
| 55 | + template=TemplateApplyTemplate(content_type="json", |
| 56 | + contents=[template_definition]))) |
| 57 | + """ |
| 58 | + Set Stack name |
| 59 | + """ |
| 60 | + templates_service.update_stack(stack_id=template_inline.stack_id, |
| 61 | + patch_stack_request=PatchStackRequest(name="inline_stack")) |
| 62 | + |
| 63 | + """ |
| 64 | + List installed stacks |
| 65 | + """ |
| 66 | + print(f"\n------- List -------\n") |
| 67 | + stacks = templates_service.list_stacks(org_id=org.id).stacks |
| 68 | + print("\n".join([f" ---\n ID: {it.id}\n Stack: {it}" for it in stacks])) |
| 69 | + print("---") |
| 70 | + |
| 71 | + """ |
| 72 | + Delete previously created Stack |
| 73 | + """ |
| 74 | + print(f"------- Delete -------\n") |
| 75 | + templates_service.delete_stack(stack_id=template_linux.stack_id, org_id=org.id) |
| 76 | + print(f" Successfully deleted stack: '{template_linux.stack_id}'") |
0 commit comments