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

Catch permission errors #25

Merged
merged 1 commit into from
Sep 10, 2024
Merged
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
34 changes: 21 additions & 13 deletions geoip2influx/influxv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from influxdb_client.client.organizations_api import OrganizationsApi
from requests.exceptions import ConnectionError
from influxdb_client.client.exceptions import InfluxDBError
from influxdb_client.rest import ApiException
from influxdb_client.client.write_api import SYNCHRONOUS, WriteOptions

from .influx_base import InfluxBase
Expand Down Expand Up @@ -125,7 +126,7 @@ def create_influx_client(self, debug = True, enable_gzip:bool = False, **kwargs)
def test_connection(self) -> None:
try:
if not self.influx.ping():
raise
raise ConnectionError("InfluxDB ping failed")
self.version: str = self.influx.version()
self.logger.debug("InfluxDB version: %s", self.version)
except Exception:
Expand Down Expand Up @@ -155,23 +156,25 @@ def create_bucket(self) -> None:
try:
if self.bucket_exists():
return
self.logger.info("Creating bucket.")
self.logger.debug("Trying to create bucket '%s'.", self.bucket)
bucket_description: str = f"Bucket for storing GeoIP data for {self.bucket}"
bucket_retention = BucketRetentionRules(type="expire",every_seconds=self.retention)
self.bucket_api.create_bucket(bucket_name=self.bucket, org=self.org, description=bucket_description, retention_rules=bucket_retention)
if self.bucket_exists():
self.logger.info("Bucket %s created.", self.bucket)
self.logger.info("Bucket '%s' created.", self.bucket)
except InfluxDBError as exc:
if "Are you using token with sufficient permission?" in exc.message:
self.logger.debug("Not authorized to create buckets")
except Exception:
self.logger.exception("Error creating bucket %s.", self.bucket)

def bucket_exists(self) -> bool:
"""Check if the bucket exists."""
try:
buckets = self.bucket_api.find_buckets_iter(org=self.org)
if buckets and self.bucket in [bucket.name for bucket in buckets]:
self.logger.debug("Bucket %s exists.", self.bucket)
if self.bucket_api.find_bucket_by_name(self.bucket):
self.logger.debug("Bucket '%s' exists.", self.bucket)
return True
self.logger.debug("Bucket %s does not exist.", self.bucket)
self.logger.debug("Bucket '%s' does not exist or no read permissions", self.bucket)
return False
except Exception:
self.logger.exception("Error checking bucket %s.", self.bucket)
Expand All @@ -181,21 +184,26 @@ def create_org(self) -> None:
if self.org_exists():
return
try:
self.logger.info("Creating organization.")
self.logger.debug("Trying to create organization '%s'.", self.org)
self.org_api.create_organization(name=self.org)
if self.org_exists():
self.logger.info("Organization %s created.", self.org)
self.logger.info("Organization '%s' created.", self.org)
except ApiException as exc:
if exc.reason == "Unauthorized":
self.logger.debug("Not authorized to create organizations")
else:
self.logger.exception("Error creating organization '%s'.")
except Exception:
self.logger.exception("Error creating organization %s.", self.org)
self.logger.exception("Error creating organization '%s'.", self.org)

def org_exists(self) -> bool:
"""Check if the organization exists."""
try:
orgs = self.org_api.find_organizations(org=self.org)
if orgs and self.org in [org.name for org in orgs]:
self.logger.debug("Organization %s exists.", self.org)
self.logger.debug("Organization '%s' exists.", self.org)
return True
self.logger.debug("Organization %s does not exist.", self.org)
self.logger.debug("Organization '%s' does not exist or no read permissions", self.org)
return False
except Exception:
self.logger.exception("Error checking organization %s.", self.org)
self.logger.exception("Error reading organization '%s'.", self.org)