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

[MISC] Use ruff for formatting #241

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
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
889 changes: 598 additions & 291 deletions lib/charms/data_platform_libs/v0/data_interfaces.py

Large diffs are not rendered by default.

27 changes: 20 additions & 7 deletions lib/charms/data_platform_libs/v0/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def restart(self, event) -> None:

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

PYDEPS = ["pydantic>=1.10,<2", "poetry-core"]

Expand Down Expand Up @@ -501,7 +501,7 @@ class DataUpgrade(Object, ABC):

STATES = ["recovery", "failed", "idle", "ready", "upgrading", "completed"]

on = UpgradeEvents() # pyright: ignore [reportGeneralTypeIssues]
on = UpgradeEvents() # pyright: ignore [reportAssignmentType]

def __init__(
self,
Expand Down Expand Up @@ -606,6 +606,21 @@ def upgrade_stack(self, stack: List[int]) -> None:
self.peer_relation.data[self.charm.app].update({"upgrade-stack": json.dumps(stack)})
self._upgrade_stack = stack

@property
def other_unit_states(self) -> list:
"""Current upgrade state for other units.

Returns:
Unsorted list of upgrade states for other units.
"""
if not self.peer_relation:
return []

return [
self.peer_relation.data[unit].get("state", "")
for unit in list(self.peer_relation.units)
]

@property
def unit_states(self) -> list:
"""Current upgrade state for all units.
Expand Down Expand Up @@ -926,11 +941,8 @@ def on_upgrade_changed(self, event: EventBase) -> None:
return

if self.substrate == "vm" and self.cluster_state == "recovery":
# Only defer for vm, that will set unit states to "ready" on upgrade-charm
# on k8s only the upgrading unit will receive the upgrade-charm event
# and deferring will prevent the upgrade stack from being popped
logger.debug("Cluster in recovery, deferring...")
event.defer()
# skip run while in recovery. The event will be retrigged when the cluster is ready
logger.debug("Cluster in recovery, skip...")
return

# if all units completed, mark as complete
Expand Down Expand Up @@ -981,6 +993,7 @@ def on_upgrade_changed(self, event: EventBase) -> None:
self.charm.unit == top_unit
and top_state in ["ready", "upgrading"]
and self.cluster_state == "ready"
and "upgrading" not in self.other_unit_states
):
logger.debug(
f"{top_unit.name} is next to upgrade, emitting `upgrade_granted` event and upgrading..."
Expand Down
126 changes: 20 additions & 106 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ cosl = "*"
optional = true

[tool.poetry.group.format.dependencies]
black = "^24.2.0"
ruff = "^0.3.1"
ruff = "^0.3.2"

[tool.poetry.group.lint]
optional = true
Expand Down
19 changes: 8 additions & 11 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

"""Charmed PgBouncer connection pooler."""


import json
import logging
import math
Expand Down Expand Up @@ -352,13 +351,11 @@ def _pgbouncer_layer(self) -> Layer:
"startup": "enabled",
"override": "replace",
}
return Layer(
{
"summary": "pgbouncer layer",
"description": "pebble config layer for pgbouncer",
"services": pebble_services,
}
)
return Layer({
"summary": "pgbouncer layer",
"description": "pebble config layer for pgbouncer",
"services": pebble_services,
})

def _on_update_status(self, _) -> None:
"""Update Status hook.
Expand Down Expand Up @@ -445,9 +442,9 @@ def _generate_monitoring_service(self, enabled: bool = True) -> Dict[str, str]:

def toggle_monitoring_layer(self, enabled: bool) -> None:
"""Starts or stops the monitoring service."""
pebble_layer = Layer(
{"services": {self._metrics_service: self._generate_monitoring_service(enabled)}}
)
pebble_layer = Layer({
"services": {self._metrics_service: self._generate_monitoring_service(enabled)}
})
pgb_container = self.unit.get_container(PGB)
pgb_container.add_layer(PGB, pebble_layer, combine=True)
if enabled:
Expand Down
6 changes: 3 additions & 3 deletions src/relations/backend_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,9 @@ def _on_relation_departed(self, event: RelationDepartedEvent):

if event.departing_unit == self.charm.unit:
# This should only occur when the relation is being removed, not on scale-down
self.charm.peers.unit_databag.update(
{f"{BACKEND_RELATION_NAME}_{event.relation.id}_departing": "true"}
)
self.charm.peers.unit_databag.update({
f"{BACKEND_RELATION_NAME}_{event.relation.id}_departing": "true"
})
logger.warning("added relation-departing flag to peer databag")
return

Expand Down
18 changes: 8 additions & 10 deletions src/relations/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,9 @@ def _on_relation_departed(self, departed_event: RelationDepartedEvent):
# Neither peer relation data nor stored state are good solutions,
# just a temporary solution.
if departed_event.departing_unit == self.charm.unit:
self.charm.peers.unit_databag.update(
{self._depart_flag(departed_event.relation): "True"}
)
self.charm.peers.unit_databag.update({
self._depart_flag(departed_event.relation): "True"
})
# Just run the rest of the logic for departing of remote units.
return

Expand Down Expand Up @@ -493,13 +493,11 @@ def _comma_split(string) -> Iterable[str]:
def get_allowed_units(self, relation: Relation) -> str:
"""Gets the external units from this relation that can be allowed into the network."""
return ",".join(
sorted(
[
unit.name
for unit in relation.data
if isinstance(unit, Unit) and unit.app != self.charm.app
]
)
sorted([
unit.name
for unit in relation.data
if isinstance(unit, Unit) and unit.app != self.charm.app
])
)

def get_external_app(self, relation):
Expand Down
1 change: 0 additions & 1 deletion src/relations/pgbouncer_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
└──────────────────┴───────────────────────────────────────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────────────────────────────────────────┘
""" # noqa: W505


import logging

from charms.data_platform_libs.v0.data_interfaces import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ async def test_an_application_can_request_multiple_databases(ops_test: OpsTest):
@pytest.mark.group(1)
async def test_legacy_relation_compatibility(ops_test: OpsTest):
finos = "finos-waltz-k8s"
await ops_test.model.deploy(finos, application_name=finos, channel="edge"),
(await ops_test.model.deploy(finos, application_name=finos, channel="edge"),)
finos_relation = await ops_test.model.add_relation(f"{PGB}:db", f"{finos}:db")
wait_for_relation_joined_between(ops_test, PGB, finos)
async with ops_test.fast_forward():
Expand Down Expand Up @@ -388,7 +388,7 @@ async def test_multiple_pgb_can_connect_to_one_backend(ops_test: OpsTest, pgb_ch
series=CHARM_SERIES,
)
async with ops_test.fast_forward():
await ops_test.model.wait_for_idle(apps=[pgb_secondary], status="blocked"),
(await ops_test.model.wait_for_idle(apps=[pgb_secondary], status="blocked"),)

await ops_test.model.add_relation(f"{pgb_secondary}:{BACKEND_RELATION_NAME}", f"{PG}:database")
wait_for_relation_joined_between(ops_test, PG, pgb_secondary)
Expand Down
Loading
Loading