Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
calculate expected hash by first unrolling json data, sorting by rela…
Browse files Browse the repository at this point in the history
…tion data keys, then sha
  • Loading branch information
addyess committed Mar 1, 2024
1 parent 764d448 commit 488a501
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
30 changes: 26 additions & 4 deletions ops/ops/interface_azure/requires.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ def send_instance_info(self, _):
"subscription-id": self.subscription_id,
"model-uuid": os.environ["JUJU_MODEL_UUID"],
}
log.info(
"%s is vm_id=%s (vm_name=%s) in vm-location=%s",
self.charm.unit.name,
self.vm_id,
self.vm_name,
self.vm_location,
)
self._request(info)

@cached_property
Expand Down Expand Up @@ -223,7 +230,17 @@ def is_ready(self):
"""
requested = self._to_publish.get("requested")
completed = json.loads(self._received.get("completed", "{}")).get(self.vm_id)
return bool(requested and requested == completed)
ready = bool(requested and requested == completed)
if not requested:
log.warning("Local end has yet to request integration")
if not completed:
log.warning("Remote end has yet to calculate a response")
elif not ready:
log.warning(
"Waiting for completed=%s to be requested=%s", completed, requested
)
return ready


def evaluate_relation(self, event) -> Optional[str]:
"""Determine if relation is ready."""
Expand All @@ -239,9 +256,14 @@ def evaluate_relation(self, event) -> Optional[str]:

@property
def _expected_hash(self):
return sha256(
json.dumps(dict(self._to_publish), sort_keys=True).encode("utf8")
).hexdigest()
def from_json(s: str):
try:
return json.loads(s)
except json.decoder.JSONDecodeError:
return s

to_sha = {key: from_json(val) for key, val in self._to_publish.items()}
return sha256(json.dumps(to_sha, sort_keys=True).encode()).hexdigest()

def _request(self, keyvals):
kwds = {key: json.dumps(val) for key, val in keyvals.items()}
Expand Down
2 changes: 1 addition & 1 deletion ops/tests/data/from_integrator.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
completed: '{"i-abcdefghijklmnopq": "dbac1ab58274947966dbf98cd1a86e36622b3fc3a5278f8cc06fa7bb1c48d69c"}'
completed: '{"i-abcdefghijklmnopq": "e0effe5f0504c3fb6ddad1bf44ddf57b682689b7cbb51634030e2109440e2869"}'
resource-group-location: rgl
vnet-name: vn
vnet-resource-group: vrg
Expand Down

0 comments on commit 488a501

Please sign in to comment.