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

Implementation of ops requires side of aws interface #17

Merged
merged 8 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 19 additions & 6 deletions ops/ops/interface_aws/requires.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,22 @@ def _joined(self, _):
self.instance_id,
self.region,
)
self._to_publish["instance-id"] = self.instance_id
self._to_publish["region"] = self.region
self._request({"instance-id": self.instance_id, "region": self.region})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure this stuff gets encoded in json rather than just left to be a string.


@property
def is_ready(self):
completed = json.loads(self._received.get("completed", "{}"))
response_hash = completed.get(self.instance_id)
return response_hash == self._expected_hash
ready = response_hash == self._expected_hash
if not response_hash:
log.warning("Remote end is yet to calculate a response")
elif not ready:
log.warning(
"Waiting for response_hash=%s to be self._expected_hash=%s",
response_hash,
self._expected_hash,
)
Comment on lines +139 to +147
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some handy logging to figure out of the requires side has seen the correct hash yet.

return ready

def evaluate_relation(self, event) -> Optional[str]:
"""Determine if relation is ready."""
Expand Down Expand Up @@ -168,9 +176,14 @@ def region(self):

@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

Comment on lines +179 to +184
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mateoflorido @eaudetcobello
the reactive relation handler automatically encoded/decoded relation data as json if it was a complex object (dict, list ...etc). Since ops doesn't do this -- I have to manually handle data that may or may not be json data when calculating the hash to make sure the far end integrator and remote end ops client come up with the same hash for the data.

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/aws_recv.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
completed: '{"i-abcdefghijklmnopq": "1fca8013323a5aaa67a9a816ab1b910a2402165c198f8502c69d7c0b2a0546cf"}'
completed: '{"i-abcdefghijklmnopq": "3577342edb3a0a9a4b6861b7d2f580f35db2709e92f3ddd17bb027eb85a2f670"}'
Loading