Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
chore: update charm libraries (#27)
Browse files Browse the repository at this point in the history
Co-authored-by: Github Actions <[email protected]>
  • Loading branch information
observability-noctua-bot and Github Actions authored Apr 29, 2023
1 parent 0165308 commit 7d71d2b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
56 changes: 27 additions & 29 deletions lib/charms/grafana_k8s/v0/grafana_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def __init__(self, *args):
# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version

LIBPATCH = 29
LIBPATCH = 30

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -582,7 +582,7 @@ def _convert_dashboard_fields(content: str, inject_dropdowns: bool = True) -> st

# If no existing template variables exist, just insert our own
if "templating" not in dict_content:
dict_content["templating"] = {"list": [d for d in template_dropdowns]} # type: ignore
dict_content["templating"] = {"list": list(template_dropdowns)} # type: ignore
else:
# Otherwise, set a flag so we can go back later
existing_templates = True
Expand Down Expand Up @@ -830,18 +830,18 @@ def _modify_panel(panel: dict, topology: dict, transformer: "CosTool") -> dict:

if "datasource" not in panel.keys():
continue
else:
if type(panel["datasource"]) == str:
if panel["datasource"] not in known_datasources:
continue
querytype = known_datasources[panel["datasource"]]
elif type(panel["datasource"]) == dict:
if panel["datasource"]["uid"] not in known_datasources:
continue
querytype = known_datasources[panel["datasource"]["uid"]]
else:
logger.error("Unknown datasource format: skipping")

if type(panel["datasource"]) == str:
if panel["datasource"] not in known_datasources:
continue
querytype = known_datasources[panel["datasource"]]
elif type(panel["datasource"]) == dict:
if panel["datasource"]["uid"] not in known_datasources:
continue
querytype = known_datasources[panel["datasource"]["uid"]]
else:
logger.error("Unknown datasource format: skipping")
continue

# Capture all values inside `[]` into a list which we'll iterate over later to
# put them back in-order. Then apply the regex again and replace everything with
Expand Down Expand Up @@ -901,13 +901,12 @@ def _type_convert_stored(obj):
"""Convert Stored* to their appropriate types, recursively."""
if isinstance(obj, StoredList):
return list(map(_type_convert_stored, obj))
elif isinstance(obj, StoredDict):
if isinstance(obj, StoredDict):
rdict = {} # type: Dict[Any, Any]
for k in obj.keys():
rdict[k] = _type_convert_stored(obj[k])
return rdict
else:
return obj
return obj


class GrafanaDashboardsChanged(EventBase):
Expand Down Expand Up @@ -1251,7 +1250,7 @@ def _juju_topology(self) -> Dict:
@property
def dashboard_templates(self) -> List:
"""Return a list of the known dashboard templates."""
return [v for v in self._stored.dashboard_templates.values()] # type: ignore
return list(self._stored.dashboard_templates.values()) # type: ignore


class GrafanaDashboardConsumer(Object):
Expand Down Expand Up @@ -1305,7 +1304,7 @@ def __init__(
self._relation_name = relation_name
self._tranformer = CosTool(self._charm)

self._stored.set_default(dashboards=dict()) # type: ignore
self._stored.set_default(dashboards={}) # type: ignore

self.framework.observe(
self._charm.on[self._relation_name].relation_changed,
Expand Down Expand Up @@ -1495,19 +1494,18 @@ def _render_dashboards_and_signal_changed(self, relation: Relation) -> bool: #

# Dropping dashboards for a relation needs to be signalled
return True
else:
stored_data = rendered_dashboards
currently_stored_data = self._get_stored_dashboards(relation.id)

coerced_data = (
_type_convert_stored(currently_stored_data) if currently_stored_data else {}
)
stored_data = rendered_dashboards
currently_stored_data = self._get_stored_dashboards(relation.id)

if not coerced_data == stored_data:
stored_dashboards = self.get_peer_data("dashboards")
stored_dashboards[relation.id] = stored_data
self.set_peer_data("dashboards", stored_dashboards)
return True
coerced_data = _type_convert_stored(currently_stored_data) if currently_stored_data else {}

if not coerced_data == stored_data:
stored_dashboards = self.get_peer_data("dashboards")
stored_dashboards[relation.id] = stored_data
self.set_peer_data("dashboards", stored_dashboards)
return True
return None # type: ignore

def _manage_dashboard_uid(self, dashboard: str, template: dict) -> str:
"""Add an uid to the dashboard if it is not present."""
Expand Down
13 changes: 6 additions & 7 deletions lib/charms/grafana_k8s/v0/grafana_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def __init__(self, *args):

# 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

logger = logging.getLogger(__name__)

Expand All @@ -173,13 +173,12 @@ def _type_convert_stored(obj):
"""Convert Stored* to their appropriate types, recursively."""
if isinstance(obj, StoredList):
return list(map(_type_convert_stored, obj))
elif isinstance(obj, StoredDict):
if isinstance(obj, StoredDict):
rdict = {}
for k in obj.keys():
rdict[k] = _type_convert_stored(obj[k])
return rdict
else:
return obj
return obj


class RelationNotFoundError(Exception):
Expand Down Expand Up @@ -499,7 +498,7 @@ def __init__(
# We're stuck with this forever now so upgrades work, or until such point as we can
# break compatibility
self._stored.set_default( # type: ignore
sources=dict(),
sources={},
sources_to_delete=set(),
)

Expand Down Expand Up @@ -546,7 +545,7 @@ def _get_source_config(self, rel: Relation):
"""Generate configuration from data stored in relation data by providers."""
source_data = json.loads(rel.data[rel.app].get("grafana_source_data", "{}")) # type: ignore
if not source_data:
return
return None

data = []

Expand Down Expand Up @@ -700,7 +699,7 @@ def sources(self) -> List[dict]:
sources = []
stored_sources = self.get_peer_data("sources")
for source in stored_sources.values():
sources.extend([host for host in _type_convert_stored(source)])
sources.extend(list(_type_convert_stored(source)))

return sources

Expand Down
7 changes: 3 additions & 4 deletions lib/charms/observability_libs/v1/kubernetes_service_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def setUp(self, *unused):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 6
LIBPATCH = 7

ServiceType = Literal["ClusterIP", "LoadBalancer"]

Expand Down Expand Up @@ -310,9 +310,8 @@ def _is_patched(self, client: Client) -> bool:
except ApiError as e:
if e.status.code == 404 and self.service_name != self._app:
return False
else:
logger.error("Kubernetes service get failed: %s", str(e))
raise
logger.error("Kubernetes service get failed: %s", str(e))
raise

# Construct a list of expected ports, should the patch be applied
expected_ports = [(p.port, p.targetPort) for p in self.service.spec.ports]
Expand Down

0 comments on commit 7d71d2b

Please sign in to comment.