Skip to content

Commit

Permalink
[MISC] Add URI (#380)
Browse files Browse the repository at this point in the history
* Add URI

* Bump libs
  • Loading branch information
dragomirp authored Aug 20, 2024
1 parent 3e876da commit 862e113
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
17 changes: 14 additions & 3 deletions lib/charms/tempo_k8s/v1/charm_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ def _remove_stale_otel_sdk_packages():

_remove_stale_otel_sdk_packages()


import functools
import inspect
import logging
Expand Down Expand Up @@ -271,7 +270,7 @@ def _remove_stale_otel_sdk_packages():
# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version

LIBPATCH = 14
LIBPATCH = 15

PYDEPS = ["opentelemetry-exporter-otlp-proto-http==1.21.0"]

Expand All @@ -281,7 +280,6 @@ def _remove_stale_otel_sdk_packages():
# set this to 0 if you are debugging/developing this library source
dev_logger.setLevel(logging.CRITICAL)


_CharmType = Type[CharmBase] # the type CharmBase and any subclass thereof
_C = TypeVar("_C", bound=_CharmType)
_T = TypeVar("_T", bound=type)
Expand Down Expand Up @@ -333,9 +331,22 @@ def _get_tracer() -> Optional[Tracer]:
try:
return tracer.get()
except LookupError:
# fallback: this course-corrects for a user error where charm_tracing symbols are imported
# from different paths (typically charms.tempo_k8s... and lib.charms.tempo_k8s...)
try:
ctx: Context = copy_context()
if context_tracer := _get_tracer_from_context(ctx):
logger.warning(
"Tracer not found in `tracer` context var. "
"Verify that you're importing all `charm_tracing` symbols from the same module path. \n"
"For example, DO"
": `from charms.lib...charm_tracing import foo, bar`. \n"
"DONT: \n"
" \t - `from charms.lib...charm_tracing import foo` \n"
" \t - `from lib...charm_tracing import bar` \n"
"For more info: https://python-notes.curiousefficiency.org/en/latest/python"
"_concepts/import_traps.html#the-double-import-trap"
)
return context_tracer.get()
else:
return None
Expand Down
11 changes: 11 additions & 0 deletions src/relations/pgbouncer_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,27 @@ def update_endpoints(self, relation=None) -> None:
# This is a relation that is going away and finds itself in a broken state
# proceed to the next relation
continue
user = f"relation_id_{relation.id}"
database = self.database_provides.fetch_relation_field(relation.id, "database")
password = self.database_provides.fetch_my_relation_field(relation.id, "password")
# Read-write endpoint
if relation.data[relation.app].get("external-node-connectivity", "false") == "true":
self.database_provides.set_endpoints(relation.id, nodeports["rw"])
self.database_provides.set_read_only_endpoints(relation.id, nodeports["ro"])
self.database_provides.set_uris(
relation.id,
f"postgresql://{user}:{password}@{nodeports['rw']}/{database}",
)
else:
self.database_provides.set_endpoints(relation.id, internal_rw)
self.database_provides.set_read_only_endpoints(
relation.id,
",".join([f"{host}:{internal_port}" for host in internal_hostnames]),
)
self.database_provides.set_uris(
relation.id,
f"postgresql://{user}:{password}@{internal_rw}/{database}",
)

def get_database(self, relation):
"""Gets database name from relation."""
Expand Down
14 changes: 9 additions & 5 deletions tests/integration/relations/pgbouncer_provider/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,19 @@ def check_exposed_connection(credentials, tls):
table_name = "expose_test"
smoke_val = str(uuid4())

host, port = credentials["postgresql"]["endpoints"].split(":")
user = credentials["postgresql"]["username"]
password = credentials["postgresql"]["password"]
database = credentials["postgresql"]["database"]
if tls:
sslmode = "require"
else:
sslmode = "disable"
connstr = f"dbname='{database}' user='{user}' host='{host}' port='{port}' password='{password}' connect_timeout=1 sslmode={sslmode}"
if "uris" in credentials["postgresql"]:
uri = credentials["postgresql"]["uris"]
connstr = f"{uri}?connect_timeout=1&sslmode={sslmode}"
else:
host, port = credentials["postgresql"]["endpoints"].split(":")
user = credentials["postgresql"]["username"]
password = credentials["postgresql"]["password"]
database = credentials["postgresql"]["database"]
connstr = f"dbname='{database}' user='{user}' host='{host}' port='{port}' password='{password}' connect_timeout=1 sslmode={sslmode}"
connection = psycopg2.connect(connstr)
connection.autocommit = True
smoke_query = (
Expand Down

0 comments on commit 862e113

Please sign in to comment.