Skip to content

Commit

Permalink
Update OCI due to outadate pkgs (#178)
Browse files Browse the repository at this point in the history
* Update OCI due to outadate pkgs

* Bump charm libs

* Update to latest OCI
  • Loading branch information
dragomirp authored Jun 7, 2023
1 parent db3118a commit baf1b95
Show file tree
Hide file tree
Showing 4 changed files with 489 additions and 37 deletions.
46 changes: 24 additions & 22 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 = 30
LIBPATCH = 31

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -955,7 +955,7 @@ def restore(self, snapshot):
"""Restore grafana source information."""
self.error_message = snapshot["error_message"]
self.valid = snapshot["valid"]
self.errors = json.loads(snapshot["errors"])
self.errors = json.loads(str(snapshot["errors"]))


class GrafanaProviderEvents(ObjectEvents):
Expand All @@ -968,7 +968,7 @@ class GrafanaDashboardProvider(Object):
"""An API to provide Grafana dashboards to a Grafana charm."""

_stored = StoredState()
on = GrafanaProviderEvents()
on = GrafanaProviderEvents() # pyright: ignore

def __init__(
self,
Expand Down Expand Up @@ -1072,7 +1072,7 @@ def add_dashboard(self, content: str, inject_dropdowns: bool = True) -> None:
"""
# Update of storage must be done irrespective of leadership, so
# that the stored state is there when this unit becomes leader.
stored_dashboard_templates = self._stored.dashboard_templates # type: Any
stored_dashboard_templates: Any = self._stored.dashboard_templates # pyright: ignore

encoded_dashboard = _encode_dashboard_content(content)

Expand All @@ -1093,7 +1093,7 @@ def remove_non_builtin_dashboards(self) -> None:
"""Remove all dashboards to the relation added via :method:`add_dashboard`."""
# Update of storage must be done irrespective of leadership, so
# that the stored state is there when this unit becomes leader.
stored_dashboard_templates = self._stored.dashboard_templates # type: Any
stored_dashboard_templates: Any = self._stored.dashboard_templates # pyright: ignore

for dashboard_id in list(stored_dashboard_templates.keys()):
if dashboard_id.startswith("prog:"):
Expand All @@ -1120,7 +1120,7 @@ def _update_all_dashboards_from_dir(
# Ensure we do not leave outdated dashboards by removing from stored all
# the encoded dashboards that start with "file/".
if self._dashboards_path:
stored_dashboard_templates = self._stored.dashboard_templates # type: Any
stored_dashboard_templates: Any = self._stored.dashboard_templates # pyright: ignore

for dashboard_id in list(stored_dashboard_templates.keys()):
if dashboard_id.startswith("file:"):
Expand Down Expand Up @@ -1174,7 +1174,7 @@ def _reinitialize_dashboard_data(self, inject_dropdowns: bool = True) -> None:
e.grafana_dashboards_absolute_path,
e.message,
)
stored_dashboard_templates = self._stored.dashboard_templates # type: Any
stored_dashboard_templates: Any = self._stored.dashboard_templates # pyright: ignore

for dashboard_id in list(stored_dashboard_templates.keys()):
if dashboard_id.startswith("file:"):
Expand Down Expand Up @@ -1212,16 +1212,18 @@ def _on_grafana_dashboard_relation_changed(self, event: RelationChangedEvent) ->
valid = bool(data.get("valid", True))
errors = data.get("errors", [])
if valid and not errors:
self.on.dashboard_status_changed.emit(valid=valid)
self.on.dashboard_status_changed.emit(valid=valid) # pyright: ignore
else:
self.on.dashboard_status_changed.emit(valid=valid, errors=errors)
self.on.dashboard_status_changed.emit( # pyright: ignore
valid=valid, errors=errors
)

def _upset_dashboards_on_relation(self, relation: Relation) -> None:
"""Update the dashboards in the relation data bucket."""
# It's completely ridiculous to add a UUID, but if we don't have some
# pseudo-random value, this never makes it across 'juju set-state'
stored_data = {
"templates": _type_convert_stored(self._stored.dashboard_templates),
"templates": _type_convert_stored(self._stored.dashboard_templates), # pyright: ignore
"uuid": str(uuid.uuid4()),
}

Expand Down Expand Up @@ -1256,7 +1258,7 @@ def dashboard_templates(self) -> List:
class GrafanaDashboardConsumer(Object):
"""A consumer object for working with Grafana Dashboards."""

on = GrafanaDashboardEvents()
on = GrafanaDashboardEvents() # pyright: ignore
_stored = StoredState()

def __init__(
Expand Down Expand Up @@ -1348,13 +1350,13 @@ def _on_grafana_dashboard_relation_changed(self, event: RelationChangedEvent) ->
changes = self._render_dashboards_and_signal_changed(event.relation)

if changes:
self.on.dashboards_changed.emit()
self.on.dashboards_changed.emit() # pyright: ignore

def _on_grafana_peer_changed(self, _: RelationChangedEvent) -> None:
"""Emit dashboard events on peer events so secondary charm data updates."""
if self._charm.unit.is_leader():
return
self.on.dashboards_changed.emit()
self.on.dashboards_changed.emit() # pyright: ignore

def update_dashboards(self, relation: Optional[Relation] = None) -> None:
"""Re-establish dashboards on one or more relations.
Expand Down Expand Up @@ -1401,7 +1403,7 @@ def _render_dashboards_and_signal_changed(self, relation: Relation) -> bool: #
"""
other_app = relation.app

raw_data = relation.data[other_app].get("dashboards", {}) # type: ignore
raw_data = relation.data[other_app].get("dashboards", "") # pyright: ignore

if not raw_data:
logger.warning(
Expand Down Expand Up @@ -1509,20 +1511,20 @@ def _render_dashboards_and_signal_changed(self, relation: Relation) -> bool: #

def _manage_dashboard_uid(self, dashboard: str, template: dict) -> str:
"""Add an uid to the dashboard if it is not present."""
dashboard = json.loads(dashboard)
dashboard_dict = json.loads(dashboard)

if not dashboard.get("uid", None) and "dashboard_alt_uid" in template:
dashboard["uid"] = template["dashboard_alt_uid"]
if not dashboard_dict.get("uid", None) and "dashboard_alt_uid" in template:
dashboard_dict["uid"] = template["dashboard_alt_uid"]

return json.dumps(dashboard)
return json.dumps(dashboard_dict)

def _remove_all_dashboards_for_relation(self, relation: Relation) -> None:
"""If an errored dashboard is in stored data, remove it and trigger a deletion."""
if self._get_stored_dashboards(relation.id):
stored_dashboards = self.get_peer_data("dashboards")
stored_dashboards.pop(str(relation.id))
self.set_peer_data("dashboards", stored_dashboards)
self.on.dashboards_changed.emit()
self.on.dashboards_changed.emit() # pyright: ignore

def _to_external_object(self, relation_id, dashboard):
return {
Expand Down Expand Up @@ -1604,7 +1606,7 @@ class GrafanaDashboardAggregator(Object):
"""

_stored = StoredState()
on = GrafanaProviderEvents()
on = GrafanaProviderEvents() # pyright: ignore

def __init__(
self,
Expand Down Expand Up @@ -1669,7 +1671,7 @@ def _update_remote_grafana(self, _: Optional[RelationEvent] = None) -> None:
"""Push dashboards to the downstream Grafana relation."""
# It's still ridiculous to add a UUID here, but needed
stored_data = {
"templates": _type_convert_stored(self._stored.dashboard_templates),
"templates": _type_convert_stored(self._stored.dashboard_templates), # pyright: ignore
"uuid": str(uuid.uuid4()),
}

Expand All @@ -1690,7 +1692,7 @@ def remove_dashboards(self, event: RelationBrokenEvent) -> None:
del self._stored.dashboard_templates[id] # type: ignore

stored_data = {
"templates": _type_convert_stored(self._stored.dashboard_templates),
"templates": _type_convert_stored(self._stored.dashboard_templates), # pyright: ignore
"uuid": str(uuid.uuid4()),
}

Expand Down
2 changes: 1 addition & 1 deletion metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ resources:
postgresql-image:
type: oci-image
description: OCI image for PostgreSQL
upstream-source: ghcr.io/canonical/charmed-postgresql@sha256:0c03dd7143a71d276600cffbe1e321de30d5604ea21f6af5bb1e87876fb3d8a2
upstream-source: ghcr.io/canonical/charmed-postgresql@sha256:c1f77c6b95a487d9e1de59dbb0eac6ec61a6493901d9d33355f98ccce9d74486

peers:
database-peers:
Expand Down
Loading

0 comments on commit baf1b95

Please sign in to comment.