Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update to the latest swagger API, add an example "How to use Templates and Stack API" #399

Merged
merged 4 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
33 changes: 33 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
## 1.26.0 [unreleased]

### Breaking Changes

This release introduces a support for new version of InfluxDB OSS API definitions - [oss.yml](https://github.com/influxdata/openapi/blob/master/contracts/oss.yml). The following breaking changes are in underlying API services and doesn't affect common apis such as - `WriteApi`, `QueryApi`, `BucketsApi`, `OrganizationsApi`...
- Add `LegacyAuthorizationsService` to deal with legacy authorizations
- Add `ResourceService` to retrieve all knows resources
- Add `BackupService` to represents the data backup functions of InfluxDB
- Add `ReplicationsService` to represents the replication functions of InfluxDB
- Add `RestoreService` to represents the data restore functions of InfluxDB
- Add `ConfigService` to retrieve InfluxDB's runtime configuration
- Add `RemoteConnectionsService` to deal with registered remote InfluxDB connections
- Add `TelegrafPluginsService` to retrieve all Telegraf's plugins
- Update `TemplatesService` to deal with `Stack` and `Template` API
- `DBRPsService`:
- doesn't requires `org_id` parameter for operations
- `get_dbr_ps_id` operation uses `DBRPGet` as a type of result
- `patch_dbrpid` operation uses `DBRPGet` as a type of result
- `post_dbrp` operation uses `DBRPCreate` as a type of request
- `DefaultService`:
- `get_routes` operation is moved to `RoutesService`
- `get_telegraf_plugin` operation is moved to `TelegrafsService`
- `post_signin` operation is moved to `SigninService`
- `post_signout` operation is moved to `SignoutService`
- `OrganizationsService`:
- `get_orgs_id_secrets` operation is moved to `SecretsService`
- `patch_orgs_id_secrets` operation is moved to `SecretsService`
- `post_orgs_id_secrets` operation is moved to `SecretsService`
- Remove `DocumentApi` in favour of [InfluxDB Community Templates](https://github.com/influxdata/community-templates). For more info see - [influxdb#19300](https://github.com/influxdata/influxdb/pull/19300), [openapi#192](https://github.com/influxdata/openapi/pull/192)
- `TelegrafsService` uses `TelegrafPluginRequest` to create `Telegraf` configuration
- `TelegrafsService` uses `TelegrafPluginRequest` to update `Telegraf` configuration

### API
1. [#399](https://github.com/influxdata/influxdb-client-python/pull/399): Use the latest InfluxDB OSS API definitions to generated APIs

## 1.25.0 [2022-01-20]

### Features
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [buckets_management.py](buckets_management.py) - How to create, list and delete Buckets
- [monitoring_and_alerting.py](monitoring_and_alerting.py) - How to create the Check with Slack notification.
- [task_example.py](task_example.py) - How to create a Task by API
- [templates_management.py](templates_management.py) - How to use Templates and Stack API

## Others
- [influx_cloud.py](influx_cloud.py) - How to connect to InfluxDB 2 Cloud
Expand Down
76 changes: 76 additions & 0 deletions examples/templates_management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
How to use Templates and Stack API.
"""
import datetime

from influxdb_client import InfluxDBClient, TemplatesService, TemplateApply, TemplateApplyRemotes, PatchStackRequest, \
TemplateApplyTemplate

"""
Define credentials
"""
url = 'http://localhost:8086'
token = 'my-token'
bucket_name = 'my-bucket'
org_name = 'my-org'

with InfluxDBClient(url=url, token=token, org=org_name, debug=True) as client:
uniqueId = str(datetime.datetime.now())
"""
Find Organization ID by Organization API.
"""
org = client.organizations_api().find_organizations(org=org_name)[0]

"""
Initialize Template service
"""
templates_service = TemplatesService(api_client=client.api_client)

"""
Apply 'Linux System Monitoring Template'
"""
template_yaml_url = "https://raw.githubusercontent.com/influxdata/community-templates/master/linux_system/linux_system.yml" # noqa: E501
template_linux = templates_service.apply_template(
template_apply=TemplateApply(dry_run=False,
org_id=org.id,
remotes=[TemplateApplyRemotes(url=template_yaml_url)]))
"""
Set Stack name
"""
templates_service.update_stack(stack_id=template_linux.stack_id,
patch_stack_request=PatchStackRequest(name="linux_system"))

"""
Create template as an inline definition
"""
template_definition = {
"apiVersion": "influxdata.com/v2alpha1",
"kind": "Bucket",
"metadata": {"name": "template-bucket"},
"spec": {"description": "bucket 1 description"}
}
template_inline = templates_service.apply_template(
template_apply=TemplateApply(dry_run=False,
org_id=org.id,
template=TemplateApplyTemplate(content_type="json",
contents=[template_definition])))
"""
Set Stack name
"""
templates_service.update_stack(stack_id=template_inline.stack_id,
patch_stack_request=PatchStackRequest(name="inline_stack"))

"""
List installed stacks
"""
print(f"\n------- List -------\n")
stacks = templates_service.list_stacks(org_id=org.id).stacks
print("\n".join([f" ---\n ID: {it.id}\n Stack: {it}" for it in stacks]))
print("---")

"""
Delete previously created Stack
"""
print(f"------- Delete -------\n")
templates_service.delete_stack(stack_id=template_linux.stack_id, org_id=org.id)
print(f" Successfully deleted stack: '{template_linux.stack_id}'")
Loading