Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring forward support for nested assumes expressions on 3x #943

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 21 additions & 10 deletions juju/client/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,26 @@ async def rpc(self, msg):

@classmethod
def from_json(cls, data):
def _parse_nested_list_entry(expr, result_dict):
if isinstance(expr, str):
if '>' in expr or '>=' in expr:
# something like juju >= 2.9.31
i = expr.index('>')
_key = expr[:i].strip()
_value = expr[i:].strip()
result_dict[_key] = _value
else:
# this is a simple entry
result_dict[expr] = ''
elif isinstance(expr, dict):
for _, v in expr.items():
_parse_nested_list_entry(v, result_dict)
elif isinstance(expr, list):
for v in expr:
_parse_nested_list_entry(v, result_dict)
else:
raise TypeError(f"Unexpected type of entry in assumes expression: {expr}")

if isinstance(data, cls):
return data
if isinstance(data, str):
Expand All @@ -680,16 +700,7 @@ def from_json(cls, data):
# check: https://juju.is/docs/sdk/assumes
# assumes are in the form of a list
d = {}
for entry in data:
if '>' in entry or '>=' in entry:
# something like juju >= 2.9.31
i = entry.index('>')
key = entry[:i].strip()
value = entry[i:].strip()
d[key] = value
else:
# this is a simple entry
d[entry] = ''
_parse_nested_list_entry(data, d)
return cls(**d)
return None

Expand Down
9 changes: 5 additions & 4 deletions juju/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,13 @@ def should_upgrade_resource(available_resource, existing_resources):
"""
# should upgrade resource?
res_name = available_resource.get('Name', available_resource.get('name'))
# no, if it's upload
if existing_resources[res_name].origin == 'upload':
return False

# no, if we already have it (and upstream doesn't have a newer res available)
# do we have it already?
if res_name in existing_resources:
# no upgrade, if it's upload
if existing_resources[res_name].origin == 'upload':
return False
# no upgrade, if upstream doesn't have a newer revision of the resource available
available_rev = available_resource.get('Revision', available_resource.get('revision', -1))
u_fields = existing_resources[res_name].unknown_fields
existing_rev = u_fields.get('Revision', u_fields.get('revision', -1))
Expand Down