Skip to content

Simple way to test connection and auth info #131

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

Closed
mdegat01 opened this issue Jul 10, 2020 · 3 comments · Fixed by #269
Closed

Simple way to test connection and auth info #131

mdegat01 opened this issue Jul 10, 2020 · 3 comments · Fixed by #269
Labels
enhancement New feature or request
Milestone

Comments

@mdegat01
Copy link

Proposal:
When using this library if the config information (url, org, bucket and token) is collected from users then there needs to be a way to test that configuration to see if:

  • There is an Influx 2.0 deployment listening at that URL
  • The Org, Bucket and Token details provided are valid at that URL
  • The token has the required permissions based on the use case (Read and/or Write to that org and bucket)

I think the library should include a simple way to do this test which succeeds without exception on success and fails with an exception otherwise.

Current behavior:
I believe the best way available to do this test today is the following:

  • To test a write, do a synchronous write of b"". On success it will fail with an Invalid InputsApiException with the message 'writing requires points'. On failure it will return a urllib3.exceptions.MaxRetryError on connection failure and an Unauthorized ApiException if the other config is invalid or a permissions failure.
  • To test a read, execute a query of buckets(). This returns the buckets that can be seen with the provided config on success. On failure it will return a urllib3.exceptions.MaxRetryError on connnection failure and an Unauthorized ApiException if the config is invalid

Desired behavior:
First, assuming I am correct about those being the ideal ways to do a test read and write it would be good if they were added to the doc as cookbook examples.

Also ideally the test write should only throw an exception in case of failure. Perhaps instead of using the normal write method for this test then write_api could have a test method that does a write(b"") behind the scenes and only returns an exception in case of connection or authorization failure?

Alternatives considered:
I noticed that the InfluxDBClient class also has a method called health and ready which seem like they should be usable for doing a connection check. I wasn't sure how to make them work though or if they were implemented yet.

Use case:
I feel like users of this library will often be collecting config information that they then need to test. Even if no enhancements are prioritized at this time around this it would be good if the documentation included an example of the best practice way to do this.

@bednar bednar added this to the 1.9.0 milestone Jul 10, 2020
@bednar bednar added the documentation Improvements or additions to documentation label Jul 10, 2020
@bednar bednar removed this from the 1.9.0 milestone Jul 10, 2020
@bednar bednar added enhancement New feature or request and removed documentation Improvements or additions to documentation labels Jul 10, 2020
@bednar
Copy link
Contributor

bednar commented Jul 14, 2020

Hi @mdegat01,

the health() is useful to check the connection. Try this code:

from influxdb_client import InfluxDBClient

client = InfluxDBClient(url="http://localhost:9999",
                        token="my-token",
                        org="my-org",)

health = client.health()
if health.status == "pass":
    print("Connection success.")
else:
    print(f"Connection failure: {health.message}!")

client.__del__()

I will take a look to solution for check permission to the read and the write from a particular bucket.

Regards

@malwinweiler
Copy link

Hi @bednar,

your solution works to test basic connectivity to the DB in question.

However, it does not address testing whether the supplied token has appropriate rights to. e.g. write data.
Based on the documentation I could not find pre-defined functions to do such tests, do you happen to have some suggestions for that as well, similar to mdegat01's suggestions?

Best regards,
M. Weiler

@bednar
Copy link
Contributor

bednar commented Nov 1, 2021

Hi @malwinweiler,

currently the InfluxDB doesn't have API to test the token to have appropriate rights.

You can use something similar as is in this pending PR: https://github.com/influxdata/influxdb-client-python/pull/269/files#diff-294bf3f7a19058974064e47c8e95dce7383a1716d1e59822a81fefca09ce7d48

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants