Skip to content

Commit

Permalink
Update libraries (#25)
Browse files Browse the repository at this point in the history
* Update libraries

* Update unit tests
  • Loading branch information
rbarry82 authored Jan 25, 2023
1 parent 72291b9 commit 38cef28
Show file tree
Hide file tree
Showing 3 changed files with 578 additions and 453 deletions.
49 changes: 20 additions & 29 deletions lib/charms/observability_libs/v0/juju_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,15 @@
```
"""

import re
from collections import OrderedDict
from typing import Dict, List, Optional
from uuid import UUID

# The unique Charmhub library identifier, never change it
LIBID = "bced1658f20f49d28b88f61f83c2d232"

LIBAPI = 0
LIBPATCH = 2
LIBPATCH = 4


class InvalidUUIDError(Exception):
Expand All @@ -95,8 +94,8 @@ def __init__(
model: str,
model_uuid: str,
application: str,
unit: str = None,
charm_name: str = None,
unit: Optional[str] = None,
charm_name: Optional[str] = None,
):
"""Build a JujuTopology object.
Expand Down Expand Up @@ -126,29 +125,18 @@ def __init__(
self._unit = unit

def is_valid_uuid(self, uuid):
"""Validate the supplied UUID against the Juju Model UUID pattern."""
# TODO:
# Harness is harcoding an UUID that is v1 not v4: f2c1b2a6-e006-11eb-ba80-0242ac130004
# See: https://github.com/canonical/operator/issues/779
#
# >>> uuid.UUID("f2c1b2a6-e006-11eb-ba80-0242ac130004").version
# 1
#
# we changed the validation of the 3ed UUID block: 4[a-f0-9]{3} -> [a-f0-9]{4}
# See: https://github.com/canonical/operator/blob/main/ops/testing.py#L1094
#
# Juju in fact generates a UUID v4: https://github.com/juju/utils/blob/master/uuid.go#L62
# but does not validate it is actually v4:
# See:
# - https://github.com/juju/utils/blob/master/uuid.go#L22
# - https://github.com/juju/schema/blob/master/strings.go#L79
#
# Once Harness fixes this, we should remove this comment and refactor the regex or
# the entire method using the uuid module to validate UUIDs
regex = re.compile(
"^[a-f0-9]{8}-?[a-f0-9]{4}-?[a-f0-9]{4}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}$"
)
return bool(regex.match(uuid))
"""Validate the supplied UUID against the Juju Model UUID pattern.
Args:
uuid: string that needs to be checked if it is valid v4 UUID.
Returns:
True if parameter is a valid v4 UUID, False otherwise.
"""
try:
return str(UUID(uuid, version=4)) == uuid
except (ValueError, TypeError):
return False

@classmethod
def from_charm(cls, charm):
Expand Down Expand Up @@ -193,7 +181,10 @@ def from_dict(cls, data: dict):
)

def as_dict(
self, *, remapped_keys: Dict[str, str] = None, excluded_keys: List[str] = None
self,
*,
remapped_keys: Optional[Dict[str, str]] = None,
excluded_keys: Optional[List[str]] = None,
) -> OrderedDict:
"""Format the topology information into an ordered dict.
Expand Down
Loading

0 comments on commit 38cef28

Please sign in to comment.