Skip to content

feat: add PingService to check status of OSS and Cloud instance #352

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

Merged
merged 3 commits into from
Oct 22, 2021
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 1.23.0 [unreleased]

### Deprecates
- `InfluxDBClient.health()`: instead use `InfluxDBClient.ping()`

### Features
1. [#352](https://github.com/influxdata/influxdb-client-python/pull/352): Add `PingService` to check status of OSS and Cloud instance

## 1.22.0 [2021-10-22]

### Features
Expand Down
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1146,9 +1146,9 @@ The following forward compatible APIs are available:
======================================================= ==================================================================================================== =======
API Endpoint Description
======================================================= ==================================================================================================== =======
`query_api.py <influxdb_client/client/query_api.py>`_ `/api/v2/query <https://docs.influxdata.com/influxdb/latest/tools/api/#api-v2-query-http-endpoint>`_ Query data in InfluxDB 1.8.0+ using the InfluxDB 2.0 API and `Flux <https://docs.influxdata.com/flux/latest/>`_ (endpoint should be enabled by `flux-enabled option <https://docs.influxdata.com/influxdb/v1.8/administration/config/#flux-enabled-false>`_)
`write_api.py <influxdb_client/client/write_api.py>`_ `/api/v2/write <https://docs.influxdata.com/influxdb/latest/tools/api/#api-v2-write-http-endpoint>`_ Write data to InfluxDB 1.8.0+ using the InfluxDB 2.0 API
`health() <influxdb_client/client/influxdb_client.py>`_ `/health <https://docs.influxdata.com/influxdb/latest/tools/api/#health-http-endpointt>`_ Check the health of your InfluxDB instance
`query_api.py <influxdb_client/client/query_api.py>`_ `/api/v2/query <https://docs.influxdata.com/influxdb/v1.8/tools/api/#apiv2query-http-endpoint>`_ Query data in InfluxDB 1.8.0+ using the InfluxDB 2.0 API and `Flux <https://docs.influxdata.com/flux/latest/>`_ (endpoint should be enabled by `flux-enabled option <https://docs.influxdata.com/influxdb/v1.8/administration/config/#flux-enabled-false>`_)
`write_api.py <influxdb_client/client/write_api.py>`_ `/api/v2/write <https://docs.influxdata.com/influxdb/v1.8/tools/api/#apiv2write-http-endpoint>`_ Write data to InfluxDB 1.8.0+ using the InfluxDB 2.0 API
`ping() <influxdb_client/client/influxdb_client.py>`_ `/ping <https://docs.influxdata.com/influxdb/v1.8/tools/api/#ping-http-endpoint>`_ Check the status of your InfluxDB instance
======================================================= ==================================================================================================== =======

For detail info see `InfluxDB 1.8 example <examples/influxdb_18_example.py>`_.
Expand Down
1 change: 1 addition & 0 deletions influxdb_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from influxdb_client.service.notification_endpoints_service import NotificationEndpointsService
from influxdb_client.service.notification_rules_service import NotificationRulesService
from influxdb_client.service.organizations_service import OrganizationsService
from influxdb_client.service.ping_service import PingService
from influxdb_client.service.query_service import QueryService
from influxdb_client.service.ready_service import ReadyService
from influxdb_client.service.rules_service import RulesService
Expand Down
1 change: 1 addition & 0 deletions influxdb_client/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from influxdb_client.service.notification_endpoints_service import NotificationEndpointsService
from influxdb_client.service.notification_rules_service import NotificationRulesService
from influxdb_client.service.organizations_service import OrganizationsService
from influxdb_client.service.ping_service import PingService
from influxdb_client.service.query_service import QueryService
from influxdb_client.service.ready_service import ReadyService
from influxdb_client.service.rules_service import RulesService
Expand Down
38 changes: 37 additions & 1 deletion influxdb_client/client/influxdb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from __future__ import absolute_import

import configparser
import logging
import os
import base64
import warnings

from influxdb_client import Configuration, ApiClient, HealthCheck, HealthService, Ready, ReadyService
from influxdb_client import Configuration, ApiClient, HealthCheck, HealthService, Ready, ReadyService, PingService
from influxdb_client.client.authorizations_api import AuthorizationsApi
from influxdb_client.client.bucket_api import BucketsApi
from influxdb_client.client.delete_api import DeleteApi
Expand All @@ -18,6 +20,9 @@
from influxdb_client.client.write_api import WriteApi, WriteOptions, PointSettings


logger = logging.getLogger(__name__)


class InfluxDBClient(object):
"""InfluxDBClient is client for InfluxDB v2."""

Expand Down Expand Up @@ -403,6 +408,7 @@ def health(self) -> HealthCheck:

:return: HealthCheck
"""
warnings.warn("This method is deprecated. Call 'ping()' instead.", DeprecationWarning)
health_service = HealthService(self.api_client)

try:
Expand All @@ -411,6 +417,36 @@ def health(self) -> HealthCheck:
except Exception as e:
return HealthCheck(name="influxdb", message=str(e), status="fail")

def ping(self) -> bool:
"""
Return the the status of InfluxDB instance.

:return: The status of InfluxDB.
"""
ping_service = PingService(self.api_client)

try:
ping_service.get_ping()
return True
except Exception as ex:
logger.error("Unexpected error during /ping: %s", ex)
return False

def version(self) -> str:
"""
Return the version of the connected InfluxDB Server.

:return: The version of InfluxDB.
"""
ping_service = PingService(self.api_client)

response = ping_service.get_ping_with_http_info(_return_http_data_only=False)
if response is not None and len(response) >= 3:
if 'X-Influxdb-Version' in response[2]:
return response[2]['X-Influxdb-Version']

return "unknown"

def ready(self) -> Ready:
"""
Get The readiness of the InfluxDB 2.0.
Expand Down
1 change: 1 addition & 0 deletions influxdb_client/client/write/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from influxdb_client.service.notification_endpoints_service import NotificationEndpointsService
from influxdb_client.service.notification_rules_service import NotificationRulesService
from influxdb_client.service.organizations_service import OrganizationsService
from influxdb_client.service.ping_service import PingService
from influxdb_client.service.query_service import QueryService
from influxdb_client.service.ready_service import ReadyService
from influxdb_client.service.rules_service import RulesService
Expand Down
1 change: 1 addition & 0 deletions influxdb_client/service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from influxdb_client.service.notification_endpoints_service import NotificationEndpointsService
from influxdb_client.service.notification_rules_service import NotificationRulesService
from influxdb_client.service.organizations_service import OrganizationsService
from influxdb_client.service.ping_service import PingService
from influxdb_client.service.query_service import QueryService
from influxdb_client.service.ready_service import ReadyService
from influxdb_client.service.rules_service import RulesService
Expand Down
209 changes: 209 additions & 0 deletions influxdb_client/service/ping_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
# coding: utf-8

"""
Influx OSS API Service.

The InfluxDB v2 API provides a programmatic interface for all interactions with InfluxDB. Access the InfluxDB API using the `/api/v2/` endpoint. # noqa: E501

OpenAPI spec version: 2.0.0
Generated by: https://openapi-generator.tech
"""


from __future__ import absolute_import

import re # noqa: F401

# python 2 and python 3 compatibility library
import six


class PingService(object):
"""NOTE: This class is auto generated by OpenAPI Generator.

Ref: https://openapi-generator.tech

Do not edit the class manually.
"""

def __init__(self, api_client=None): # noqa: E501,D401,D403
"""PingService - a operation defined in OpenAPI."""
if api_client is None:
raise ValueError("Invalid value for `api_client`, must be defined.")
self.api_client = api_client

def get_ping(self, **kwargs): # noqa: E501,D401,D403
"""Checks the status of InfluxDB instance and version of InfluxDB..

This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async_req=True
>>> thread = api.get_ping(async_req=True)
>>> result = thread.get()

:param async_req bool
:return: None
If the method is called asynchronously,
returns the request thread.
""" # noqa: E501
kwargs['_return_http_data_only'] = True
if kwargs.get('async_req'):
return self.get_ping_with_http_info(**kwargs) # noqa: E501
else:
(data) = self.get_ping_with_http_info(**kwargs) # noqa: E501
return data

def get_ping_with_http_info(self, **kwargs): # noqa: E501,D401,D403
"""Checks the status of InfluxDB instance and version of InfluxDB..

This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async_req=True
>>> thread = api.get_ping_with_http_info(async_req=True)
>>> result = thread.get()

:param async_req bool
:return: None
If the method is called asynchronously,
returns the request thread.
""" # noqa: E501
local_var_params = locals()

all_params = [] # noqa: E501
all_params.append('async_req')
all_params.append('_return_http_data_only')
all_params.append('_preload_content')
all_params.append('_request_timeout')
all_params.append('urlopen_kw')

for key, val in six.iteritems(local_var_params['kwargs']):
if key not in all_params:
raise TypeError(
"Got an unexpected keyword argument '%s'"
" to method get_ping" % key
)
local_var_params[key] = val
del local_var_params['kwargs']

collection_formats = {}

path_params = {}

query_params = []

header_params = {}

form_params = []
local_var_files = {}

body_params = None
# Authentication setting
auth_settings = [] # noqa: E501

# urlopen optional setting
urlopen_kw = None
if 'urlopen_kw' in kwargs:
urlopen_kw = kwargs['urlopen_kw']

return self.api_client.call_api(
'/ping', 'GET',
path_params,
query_params,
header_params,
body=body_params,
post_params=form_params,
files=local_var_files,
response_type=None, # noqa: E501
auth_settings=auth_settings,
async_req=local_var_params.get('async_req'),
_return_http_data_only=local_var_params.get('_return_http_data_only'), # noqa: E501
_preload_content=local_var_params.get('_preload_content', True),
_request_timeout=local_var_params.get('_request_timeout'),
collection_formats=collection_formats,
urlopen_kw=urlopen_kw)

def head_ping(self, **kwargs): # noqa: E501,D401,D403
"""Checks the status of InfluxDB instance and version of InfluxDB..

This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async_req=True
>>> thread = api.head_ping(async_req=True)
>>> result = thread.get()

:param async_req bool
:return: None
If the method is called asynchronously,
returns the request thread.
""" # noqa: E501
kwargs['_return_http_data_only'] = True
if kwargs.get('async_req'):
return self.head_ping_with_http_info(**kwargs) # noqa: E501
else:
(data) = self.head_ping_with_http_info(**kwargs) # noqa: E501
return data

def head_ping_with_http_info(self, **kwargs): # noqa: E501,D401,D403
"""Checks the status of InfluxDB instance and version of InfluxDB..

This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async_req=True
>>> thread = api.head_ping_with_http_info(async_req=True)
>>> result = thread.get()

:param async_req bool
:return: None
If the method is called asynchronously,
returns the request thread.
""" # noqa: E501
local_var_params = locals()

all_params = [] # noqa: E501
all_params.append('async_req')
all_params.append('_return_http_data_only')
all_params.append('_preload_content')
all_params.append('_request_timeout')
all_params.append('urlopen_kw')

for key, val in six.iteritems(local_var_params['kwargs']):
if key not in all_params:
raise TypeError(
"Got an unexpected keyword argument '%s'"
" to method head_ping" % key
)
local_var_params[key] = val
del local_var_params['kwargs']

collection_formats = {}

path_params = {}

query_params = []

header_params = {}

form_params = []
local_var_files = {}

body_params = None
# Authentication setting
auth_settings = [] # noqa: E501

# urlopen optional setting
urlopen_kw = None
if 'urlopen_kw' in kwargs:
urlopen_kw = kwargs['urlopen_kw']

return self.api_client.call_api(
'/ping', 'HEAD',
path_params,
query_params,
header_params,
body=body_params,
post_params=form_params,
files=local_var_files,
response_type=None, # noqa: E501
auth_settings=auth_settings,
async_req=local_var_params.get('async_req'),
_return_http_data_only=local_var_params.get('_return_http_data_only'), # noqa: E501
_preload_content=local_var_params.get('_preload_content', True),
_request_timeout=local_var_params.get('_request_timeout'),
collection_formats=collection_formats,
urlopen_kw=urlopen_kw)
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from influxdb_client.service.notification_endpoints_service import NotificationEndpointsService
from influxdb_client.service.notification_rules_service import NotificationRulesService
from influxdb_client.service.organizations_service import OrganizationsService
from influxdb_client.service.ping_service import PingService
from influxdb_client.service.query_service import QueryService
from influxdb_client.service.ready_service import ReadyService
from influxdb_client.service.rules_service import RulesService
Expand Down
Loading