@@ -320,7 +320,7 @@ def _on_topic_requested(self, event: TopicRequestedEvent):
320
320
321
321
# Increment this PATCH version before using `charmcraft publish-lib` or reset
322
322
# to 0 if you are raising the major API version
323
- LIBPATCH = 22
323
+ LIBPATCH = 23
324
324
325
325
PYDEPS = ["ops>=2.0.0" ]
326
326
@@ -807,6 +807,9 @@ def _fetch_relation_data_without_secrets(
807
807
This is used typically when the Provides side wants to read the Requires side's data,
808
808
or when the Requires side may want to read its own data.
809
809
"""
810
+ if app not in relation .data or not relation .data [app ]:
811
+ return {}
812
+
810
813
if fields :
811
814
return {k : relation .data [app ][k ] for k in fields if k in relation .data [app ]}
812
815
else :
@@ -830,6 +833,9 @@ def _fetch_relation_data_with_secrets(
830
833
normal_fields = []
831
834
832
835
if not fields :
836
+ if app not in relation .data or not relation .data [app ]:
837
+ return {}
838
+
833
839
all_fields = list (relation .data [app ].keys ())
834
840
normal_fields = [field for field in all_fields if not self ._is_secret_field (field )]
835
841
@@ -853,8 +859,11 @@ def _fetch_relation_data_with_secrets(
853
859
854
860
def _update_relation_data_without_secrets (
855
861
self , app : Application , relation : Relation , data : Dict [str , str ]
856
- ):
862
+ ) -> None :
857
863
"""Updating databag contents when no secrets are involved."""
864
+ if app not in relation .data or relation .data [app ] is None :
865
+ return
866
+
858
867
if any (self ._is_secret_field (key ) for key in data .keys ()):
859
868
raise SecretsIllegalUpdateError ("Can't update secret {key}." )
860
869
@@ -865,8 +874,19 @@ def _delete_relation_data_without_secrets(
865
874
self , app : Application , relation : Relation , fields : List [str ]
866
875
) -> None :
867
876
"""Remove databag fields 'fields' from Relation."""
877
+ if app not in relation .data or not relation .data [app ]:
878
+ return
879
+
868
880
for field in fields :
869
- relation .data [app ].pop (field )
881
+ try :
882
+ relation .data [app ].pop (field )
883
+ except KeyError :
884
+ logger .debug (
885
+ "Non-existing field was attempted to be removed from the databag %s, %s" ,
886
+ str (relation .id ),
887
+ str (field ),
888
+ )
889
+ pass
870
890
871
891
# Public interface methods
872
892
# Handling Relation Fields seamlessly, regardless if in databag or a Juju Secret
@@ -880,9 +900,6 @@ def get_relation(self, relation_name, relation_id) -> Relation:
880
900
"Relation %s %s couldn't be retrieved" , relation_name , relation_id
881
901
)
882
902
883
- if not relation .app :
884
- raise DataInterfacesError ("Relation's application missing" )
885
-
886
903
return relation
887
904
888
905
def fetch_relation_data (
@@ -1089,7 +1106,10 @@ def _delete_relation_secret(
1089
1106
# Remove secret from the relation if it's fully gone
1090
1107
if not new_content :
1091
1108
field = self ._generate_secret_field_name (group )
1092
- relation .data [self .local_app ].pop (field )
1109
+ try :
1110
+ relation .data [self .local_app ].pop (field )
1111
+ except KeyError :
1112
+ pass
1093
1113
1094
1114
# Return the content that was removed
1095
1115
return True
0 commit comments