Skip to content

Commit 804934e

Browse files
authored
misc: update libs and onboard linting rules (#644)
* please new linting rules and libs bump * missing one * add placeholder function for followup PR * update parameters
1 parent 29437d3 commit 804934e

40 files changed

+978
-770
lines changed

lib/charms/mysql/v0/async_replication.py

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
1414
MySQLPromoteClusterToPrimaryError,
1515
MySQLRejoinClusterError,
1616
)
17+
from constants import (
18+
BACKUPS_PASSWORD_KEY,
19+
BACKUPS_USERNAME,
20+
CLUSTER_ADMIN_PASSWORD_KEY,
21+
CLUSTER_ADMIN_USERNAME,
22+
MONITORING_PASSWORD_KEY,
23+
MONITORING_USERNAME,
24+
PEER,
25+
ROOT_PASSWORD_KEY,
26+
ROOT_USERNAME,
27+
SERVER_CONFIG_PASSWORD_KEY,
28+
SERVER_CONFIG_USERNAME,
29+
)
1730
from ops import (
1831
ActionEvent,
1932
ActiveStatus,
@@ -30,21 +43,6 @@
3043
)
3144
from ops.framework import Object
3245
from tenacity import RetryError, Retrying, stop_after_attempt, wait_fixed
33-
from typing_extensions import Optional
34-
35-
from constants import (
36-
BACKUPS_PASSWORD_KEY,
37-
BACKUPS_USERNAME,
38-
CLUSTER_ADMIN_PASSWORD_KEY,
39-
CLUSTER_ADMIN_USERNAME,
40-
MONITORING_PASSWORD_KEY,
41-
MONITORING_USERNAME,
42-
PEER,
43-
ROOT_PASSWORD_KEY,
44-
ROOT_USERNAME,
45-
SERVER_CONFIG_PASSWORD_KEY,
46-
SERVER_CONFIG_USERNAME,
47-
)
4846

4947
if typing.TYPE_CHECKING:
5048
from charm import MySQLOperatorCharm
@@ -54,7 +52,7 @@
5452
# The unique Charmhub library identifier, never change it
5553
LIBID = "4de21f1a022c4e2c87ac8e672ec16f6a"
5654
LIBAPI = 0
57-
LIBPATCH = 7
55+
LIBPATCH = 10
5856

5957
RELATION_OFFER = "replication-offer"
6058
RELATION_CONSUMER = "replication"
@@ -126,28 +124,31 @@ def cluster_set_name(self) -> str:
126124
return self._charm.app_peer_data["cluster-set-domain-name"]
127125

128126
@property
129-
def relation(self) -> Optional[Relation]:
127+
def relation(self) -> Relation | None:
130128
"""Relation."""
131129
return self.model.get_relation(RELATION_OFFER) or self.model.get_relation(
132130
RELATION_CONSUMER
133131
)
134132

135133
@property
136-
def relation_data(self) -> Optional[RelationDataContent]:
134+
def relation_data(self) -> RelationDataContent | None:
137135
"""Relation data."""
138136
if not self.relation:
139137
return
140138
return self.relation.data[self.model.app]
141139

142140
@property
143-
def remote_relation_data(self) -> Optional[RelationDataContent]:
141+
def remote_relation_data(self) -> RelationDataContent | None:
144142
"""Remote relation data."""
145143
if not self.relation or not self.relation.app:
146144
return
147145
return self.relation.data[self.relation.app]
148146

149147
def _on_promote_to_primary(self, event: ActionEvent) -> None:
150148
"""Promote a standby cluster to primary."""
149+
if event.params.get("scope") != "cluster":
150+
return
151+
151152
if not self._charm.unit.is_leader():
152153
event.fail("Only the leader unit can promote a standby cluster")
153154
return
@@ -359,7 +360,7 @@ def __init__(self, charm: "MySQLOperatorCharm"):
359360
self.framework.observe(self._charm.on.secret_changed, self._on_secret_change)
360361

361362
@property
362-
def state(self) -> Optional[States]:
363+
def state(self) -> States | None:
363364
"""State of the relation, on primary side."""
364365
if not self.relation:
365366
return States.UNINITIALIZED
@@ -405,12 +406,10 @@ def idle(self) -> bool:
405406
# transitional state between relation created and setup_action
406407
return False
407408

408-
if self.state not in [States.READY, States.UNINITIALIZED]:
409-
return False
410-
return True
409+
return self.state in [States.READY, States.UNINITIALIZED]
411410

412411
@property
413-
def secret(self) -> Optional[Secret]:
412+
def secret(self) -> Secret | None:
414413
"""Return the async replication secret."""
415414
if not self.relation:
416415
return
@@ -428,7 +427,7 @@ def _get_secret(self) -> Secret:
428427

429428
def _on_create_replication(self, event: ActionEvent):
430429
"""Promote the offer side to primary on initial setup."""
431-
if not self._charm.app_peer_data.get("async-ready") == "true":
430+
if self._charm.app_peer_data.get("async-ready") != "true":
432431
event.fail("Relation created but not ready")
433432
return
434433

@@ -489,10 +488,8 @@ def _on_offer_created(self, event: RelationCreatedEvent):
489488
):
490489
# Test for a broken relation on the primary side
491490
logger.error(
492-
(
493-
"Cannot setup async relation with primary cluster in blocked/read-only state\n"
494-
"Remove the relation."
495-
)
491+
"Cannot setup async relation with primary cluster in blocked/read-only state\n"
492+
"Remove the relation."
496493
)
497494
message = f"Cluster is in a blocked state. Remove {RELATION_OFFER} relation"
498495
self._charm.unit.status = BlockedStatus(message)
@@ -501,10 +498,8 @@ def _on_offer_created(self, event: RelationCreatedEvent):
501498
if not self.model.get_relation(RELATION_OFFER):
502499
# safeguard against a deferred event a previous relation.
503500
logger.error(
504-
(
505-
"Relation created running against removed relation.\n"
506-
f"Remove {RELATION_OFFER} relation and retry."
507-
)
501+
"Relation created running against removed relation.\n"
502+
f"Remove {RELATION_OFFER} relation and retry."
508503
)
509504
self._charm.unit.status = BlockedStatus(f"Remove {RELATION_OFFER} relation and retry")
510505
return
@@ -639,7 +634,7 @@ def __init__(self, charm: "MySQLOperatorCharm"):
639634
self.framework.observe(self._charm.on.secret_changed, self._on_secret_change)
640635

641636
@property
642-
def state(self) -> Optional[States]:
637+
def state(self) -> States | None:
643638
"""State of the relation, on consumer side."""
644639
if not self.relation:
645640
return None
@@ -716,7 +711,7 @@ def _async_replication_credentials(self) -> dict[str, str]:
716711
secret = self._obtain_secret()
717712
return secret.peek_content()
718713

719-
def _get_endpoint(self) -> Optional[str]:
714+
def _get_endpoint(self) -> str | None:
720715
"""Get endpoint to be used by the primary cluster.
721716
722717
This is the address in which the unit must be reachable from the primary cluster.
@@ -885,7 +880,7 @@ def _on_consumer_non_leader_created(self, _):
885880
# set waiting state to inhibit auto recovery, only when not already set
886881
if self._charm.unit.is_leader():
887882
return
888-
if not self._charm.unit_peer_data.get("member-state") == "waiting":
883+
if self._charm.unit_peer_data.get("member-state") != "waiting":
889884
self._charm.unit_peer_data["member-state"] = "waiting"
890885
self._charm.unit.status = WaitingStatus("waiting replica cluster be configured")
891886

lib/charms/mysql/v0/backups.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def is_unit_blocked(self) -> bool:
5050
import pathlib
5151
import re
5252
import typing
53-
from typing import Dict, List, Optional, Tuple
5453

5554
from charms.data_platform_libs.v0.s3 import (
5655
CredentialsChangedEvent,
@@ -88,17 +87,16 @@ def is_unit_blocked(self) -> bool:
8887
list_backups_in_s3_path,
8988
upload_content_to_s3,
9089
)
91-
from ops.charm import ActionEvent
92-
from ops.framework import Object
93-
from ops.jujuversion import JujuVersion
94-
from ops.model import BlockedStatus, MaintenanceStatus
95-
9690
from constants import (
9791
MYSQL_DATA_DIR,
9892
PEER,
9993
SERVER_CONFIG_PASSWORD_KEY,
10094
SERVER_CONFIG_USERNAME,
10195
)
96+
from ops.charm import ActionEvent
97+
from ops.framework import Object
98+
from ops.jujuversion import JujuVersion
99+
from ops.model import BlockedStatus, MaintenanceStatus
102100

103101
logger = logging.getLogger(__name__)
104102

@@ -113,7 +111,7 @@ def is_unit_blocked(self) -> bool:
113111

114112
# Increment this PATCH version before using `charmcraft publish-lib` or reset
115113
# to 0 if you are raising the major API version
116-
LIBPATCH = 14
114+
LIBPATCH = 16
117115

118116
ANOTHER_S3_CLUSTER_REPOSITORY_ERROR_MESSAGE = "S3 repository claimed by another cluster"
119117
MOVE_RESTORED_CLUSTER_TO_ANOTHER_S3_REPOSITORY_ERROR = (
@@ -150,7 +148,7 @@ def _s3_integrator_relation_exists(self) -> bool:
150148
"""Returns whether a relation with the s3-integrator exists."""
151149
return bool(self.model.get_relation(S3_INTEGRATOR_RELATION_NAME))
152150

153-
def _retrieve_s3_parameters(self) -> Tuple[Dict[str, str], List[str]]:
151+
def _retrieve_s3_parameters(self) -> tuple[dict[str, str], list[str]]:
154152
"""Retrieve S3 parameters from the S3 integrator relation.
155153
156154
Returns: tuple of (s3_parameters, missing_required_parameters)
@@ -196,7 +194,7 @@ def _upload_logs_to_s3(
196194
stdout: str,
197195
stderr: str,
198196
log_filename: str,
199-
s3_parameters: Dict[str, str],
197+
s3_parameters: dict[str, str],
200198
) -> bool:
201199
"""Upload logs to S3 at the specified location.
202200
@@ -219,7 +217,7 @@ def _upload_logs_to_s3(
219217
# ------------------ List Backups ------------------
220218

221219
@staticmethod
222-
def _format_backups_list(backup_list: List[Tuple[str, str]]) -> str:
220+
def _format_backups_list(backup_list: list[tuple[str, str]]) -> str:
223221
"""Formats the provided list of backups as a table."""
224222
backups = [f"{'backup-id':<21} | {'backup-type':<12} | backup-status"]
225223

@@ -250,9 +248,7 @@ def _on_list_backups(self, event: ActionEvent) -> None:
250248
event.set_results({"backups": self._format_backups_list(backups)})
251249
except Exception as e:
252250
error_message = (
253-
getattr(e, "message")
254-
if hasattr(e, "message")
255-
else "Failed to retrieve backup ids from S3"
251+
e.message if hasattr(e, "message") else "Failed to retrieve backup ids from S3"
256252
)
257253
logger.error(error_message)
258254
event.fail(error_message)
@@ -313,7 +309,7 @@ def _on_create_backup(self, event: ActionEvent) -> None:
313309
f"Model Name: {self.model.name}\n"
314310
f"Application Name: {self.model.app.name}\n"
315311
f"Unit Name: {self.charm.unit.name}\n"
316-
f"Juju Version: {str(juju_version)}\n"
312+
f"Juju Version: {juju_version!s}\n"
317313
)
318314

319315
if not upload_content_to_s3(metadata, f"{backup_path}.metadata", s3_parameters):
@@ -359,7 +355,7 @@ def _on_create_backup(self, event: ActionEvent) -> None:
359355
})
360356
self.charm._on_update_status(None)
361357

362-
def _can_unit_perform_backup(self) -> Tuple[bool, Optional[str]]:
358+
def _can_unit_perform_backup(self) -> tuple[bool, str | None]:
363359
"""Validates whether this unit can perform a backup.
364360
365361
Returns: tuple of (success, error_message)
@@ -390,7 +386,7 @@ def _can_unit_perform_backup(self) -> Tuple[bool, Optional[str]]:
390386

391387
return True, None
392388

393-
def _pre_backup(self) -> Tuple[bool, Optional[str]]:
389+
def _pre_backup(self) -> tuple[bool, str | None]:
394390
"""Runs operations required before performing a backup.
395391
396392
Returns: tuple of (success, error_message)
@@ -415,7 +411,7 @@ def _pre_backup(self) -> Tuple[bool, Optional[str]]:
415411

416412
return True, None
417413

418-
def _backup(self, backup_path: str, s3_parameters: Dict) -> Tuple[bool, Optional[str]]:
414+
def _backup(self, backup_path: str, s3_parameters: dict) -> tuple[bool, str | None]:
419415
"""Runs the backup operations.
420416
421417
Args:
@@ -450,7 +446,7 @@ def _backup(self, backup_path: str, s3_parameters: Dict) -> Tuple[bool, Optional
450446

451447
return True, None
452448

453-
def _post_backup(self) -> Tuple[bool, Optional[str]]:
449+
def _post_backup(self) -> tuple[bool, str | None]:
454450
"""Runs operations required after performing a backup.
455451
456452
Returns: tuple of (success, error_message)
@@ -613,7 +609,7 @@ def _on_restore(self, event: ActionEvent) -> None: # noqa: C901
613609
# update status as soon as possible
614610
self.charm._on_update_status(None)
615611

616-
def _pre_restore(self) -> Tuple[bool, str]:
612+
def _pre_restore(self) -> tuple[bool, str]:
617613
"""Perform operations that need to be done before performing a restore.
618614
619615
Returns: tuple of (success, error_message)
@@ -635,7 +631,7 @@ def _pre_restore(self) -> Tuple[bool, str]:
635631

636632
return True, ""
637633

638-
def _restore(self, backup_id: str, s3_parameters: Dict[str, str]) -> Tuple[bool, bool, str]:
634+
def _restore(self, backup_id: str, s3_parameters: dict[str, str]) -> tuple[bool, bool, str]:
639635
"""Run the restore operations.
640636
641637
Args:
@@ -687,7 +683,7 @@ def _restore(self, backup_id: str, s3_parameters: Dict[str, str]) -> Tuple[bool,
687683

688684
return True, True, ""
689685

690-
def _clean_data_dir_and_start_mysqld(self) -> Tuple[bool, str]:
686+
def _clean_data_dir_and_start_mysqld(self) -> tuple[bool, str]:
691687
"""Run idempotent operations run after restoring a backup.
692688
693689
Returns tuple of (success, error_message)
@@ -711,8 +707,8 @@ def _clean_data_dir_and_start_mysqld(self) -> Tuple[bool, str]:
711707
return True, ""
712708

713709
def _pitr_restore(
714-
self, restore_to_time: str, s3_parameters: Dict[str, str]
715-
) -> Tuple[bool, str]:
710+
self, restore_to_time: str, s3_parameters: dict[str, str]
711+
) -> tuple[bool, str]:
716712
try:
717713
logger.info("Restoring point-in-time-recovery")
718714
stdout, stderr = self.charm._mysql.restore_pitr(
@@ -728,7 +724,7 @@ def _pitr_restore(
728724
return False, f"Failed to restore point-in-time-recovery to the {restore_to_time}"
729725
return True, ""
730726

731-
def _post_restore(self) -> Tuple[bool, str]:
727+
def _post_restore(self) -> tuple[bool, str]:
732728
"""Run operations required after restoring a backup.
733729
734730
Returns: tuple of (success, error_message)
@@ -836,7 +832,7 @@ def _on_s3_credentials_gone(self, event: CredentialsGoneEvent) -> None:
836832
"Exception is occurred when trying to stop binlogs collecting after S3 relation depart. It may be a leader departure"
837833
)
838834

839-
def get_binlogs_collector_config(self) -> Dict[str, str]:
835+
def get_binlogs_collector_config(self) -> dict[str, str]:
840836
"""Return binlogs collector service config file.
841837
842838
Returns: dict of binlogs collector service config

0 commit comments

Comments
 (0)