Skip to content

Commit

Permalink
Merge pull request #943 from cderici/nested-assumes-on-3x
Browse files Browse the repository at this point in the history
#943

#### Description

This forward ports the fix for nested assume expressions from #940 into the master branch.

Fixes #938 for `3.x`.

#### QA Steps

Same QA steps can be followed from #940, so copy/pasting the manual QA steps below:

----- 

Unfortunately it's not trivial to write an integration test for this, as the code that's been changed is part of the facade api. So we'll use the `mysql-k8s` charm that's been used in #938.

```
 $ juju version
3.x
 $ juju bootstrap microk8s micro28
 $ juju add-model assumes-test
```

```python
 $ python -m asyncio
asyncio REPL 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> from juju import model;m=model.Model();await m.connect();await m.deploy("mysql-k8s", channel="8.0/edge", trust=True)
<Application entity_id="mysql-k8s">
>>>
exiting asyncio REPL...
```

#### Notes & Discussion

JUJU-4562
  • Loading branch information
jujubot authored Sep 6, 2023
2 parents 0f577e2 + ca6dffb commit dc8499c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
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

0 comments on commit dc8499c

Please sign in to comment.