Skip to content

Commit fbffd4a

Browse files
author
Lucas Gameiro
authored
add retention time in days (#474)
1 parent 5ef9c53 commit fbffd4a

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

lib/charms/data_platform_libs/v0/s3.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def _on_credential_gone(self, event: CredentialsGoneEvent):
137137

138138
# Increment this PATCH version before using `charmcraft publish-lib` or reset
139139
# to 0 if you are raising the major API version
140-
LIBPATCH = 4
140+
LIBPATCH = 5
141141

142142
logger = logging.getLogger(__name__)
143143

@@ -212,7 +212,7 @@ class S3CredentialEvents(CharmEvents):
212212
class S3Provider(Object):
213213
"""A provider handler for communicating S3 credentials to consumers."""
214214

215-
on = S3CredentialEvents() # pyright: ignore [reportGeneralTypeIssues]
215+
on = S3CredentialEvents() # pyright: ignore [reportAssignmentType]
216216

217217
def __init__(
218218
self,
@@ -481,6 +481,18 @@ def set_s3_api_version(self, relation_id: int, s3_api_version: str) -> None:
481481
"""
482482
self.update_connection_info(relation_id, {"s3-api-version": s3_api_version})
483483

484+
def set_delete_older_than_days(self, relation_id: int, days: int) -> None:
485+
"""Sets the retention days for full backups in application databag.
486+
487+
This function writes in the application data bag, therefore,
488+
only the leader unit can call it.
489+
490+
Args:
491+
relation_id: the identifier for a particular relation.
492+
days: the value.
493+
"""
494+
self.update_connection_info(relation_id, {"delete-older-than-days": str(days)})
495+
484496
def set_attributes(self, relation_id: int, attributes: List[str]) -> None:
485497
"""Sets the connection attributes in application databag.
486498
@@ -580,6 +592,17 @@ def s3_api_version(self) -> Optional[str]:
580592

581593
return self.relation.data[self.relation.app].get("s3-api-version")
582594

595+
@property
596+
def delete_older_than_days(self) -> Optional[int]:
597+
"""Returns the retention days for full backups."""
598+
if not self.relation.app:
599+
return None
600+
601+
days = self.relation.data[self.relation.app].get("delete-older-than-days")
602+
if days is None:
603+
return None
604+
return int(days)
605+
583606
@property
584607
def attributes(self) -> Optional[List[str]]:
585608
"""Returns the attributes."""
@@ -613,7 +636,7 @@ class S3CredentialRequiresEvents(ObjectEvents):
613636
class S3Requirer(Object):
614637
"""Requires-side of the s3 relation."""
615638

616-
on = S3CredentialRequiresEvents() # pyright: ignore[reportGeneralTypeIssues]
639+
on = S3CredentialRequiresEvents() # pyright: ignore[reportAssignmentType]
617640

618641
def __init__(
619642
self, charm: ops.charm.CharmBase, relation_name: str, bucket_name: Optional[str] = None

src/backups.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ def _render_pgbackrest_conf_file(self) -> bool:
832832
stanza=self.stanza_name,
833833
storage_path=self.charm._storage_path,
834834
user=BACKUP_USER,
835+
retention_full=s3_parameters["delete-older-than-days"],
835836
)
836837
# Render pgBackRest config file.
837838
self.charm._patroni.render_file(f"{PGBACKREST_CONF_PATH}/pgbackrest.conf", rendered, 0o644)
@@ -866,6 +867,7 @@ def _retrieve_s3_parameters(self) -> Tuple[Dict, List[str]]:
866867
s3_parameters.setdefault("region")
867868
s3_parameters.setdefault("path", "")
868869
s3_parameters.setdefault("s3-uri-style", "host")
870+
s3_parameters.setdefault("delete-older-than-days", "9999999")
869871

870872
# Strip whitespaces from all parameters.
871873
for key, value in s3_parameters.items():

templates/pgbackrest.conf.j2

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
backup-standby=y
33
lock-path=/tmp
44
log-path={{ log_path }}
5-
repo1-retention-full=9999999
5+
repo1-retention-full-type=time
6+
repo1-retention-full={{ retention_full }}
67
repo1-type=s3
78
repo1-path={{ path }}
89
repo1-s3-region={{ region }}

tests/unit/test_backups.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,7 @@ def test_render_pgbackrest_conf_file(harness):
14531453
"path": "test-path/",
14541454
"region": "us-east-1",
14551455
"s3-uri-style": "path",
1456+
"delete-older-than-days": "30",
14561457
},
14571458
[],
14581459
)
@@ -1476,6 +1477,7 @@ def test_render_pgbackrest_conf_file(harness):
14761477
stanza=harness.charm.backup.stanza_name,
14771478
storage_path=harness.charm._storage_path,
14781479
user="backup",
1480+
retention_full=30,
14791481
)
14801482

14811483
# Patch the `open` method with our mock.
@@ -1539,6 +1541,7 @@ def test_retrieve_s3_parameters(harness):
15391541
{
15401542
"access-key": "test-access-key",
15411543
"bucket": "test-bucket",
1544+
"delete-older-than-days": "9999999",
15421545
"endpoint": "https://s3.amazonaws.com",
15431546
"path": "/",
15441547
"region": None,
@@ -1558,6 +1561,7 @@ def test_retrieve_s3_parameters(harness):
15581561
"path": " test-path/ ",
15591562
"region": " us-east-1 ",
15601563
"s3-uri-style": " path ",
1564+
"delete-older-than-days": "30",
15611565
}
15621566
tc.assertEqual(
15631567
harness.charm.backup._retrieve_s3_parameters(),
@@ -1570,6 +1574,7 @@ def test_retrieve_s3_parameters(harness):
15701574
"region": "us-east-1",
15711575
"s3-uri-style": "path",
15721576
"secret-key": "test-secret-key",
1577+
"delete-older-than-days": "30",
15731578
},
15741579
[],
15751580
),

0 commit comments

Comments
 (0)