Skip to content

Commit 55ea0e2

Browse files
committed
Model name can now be accessed through model.name
This resovles #693
1 parent 34981c5 commit 55ea0e2

5 files changed

Lines changed: 42 additions & 16 deletions

File tree

juju/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def __init__(self, entity_name, entity_types=None):
6363
super().__init__("Entity not found: {}".format(entity_name))
6464

6565

66+
class JujuModelError(JujuError):
67+
pass
68+
69+
6670
class JujuMachineError(JujuError):
6771
pass
6872

juju/model.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from .controller import Controller, ConnectedController
3131
from .delta import get_entity_class, get_entity_delta
3232
from .errors import JujuAPIError, JujuError, JujuModelConfigError, JujuBackupError
33-
from .errors import JujuAppError, JujuUnitError, JujuAgentError, JujuMachineError, PylibjujuError
33+
from .errors import JujuModelError, JujuAppError, JujuUnitError, JujuAgentError, JujuMachineError, PylibjujuError
3434
from .exceptions import DeadEntityException
3535
from .names import is_valid_application
3636
from .offerendpoints import ParseError as OfferParseError
@@ -982,6 +982,15 @@ def charmhub(self):
982982
def charmstore(self):
983983
return self._charmstore
984984

985+
@property
986+
def name(self):
987+
"""Return the name of this model
988+
989+
"""
990+
if self._info is None:
991+
raise JujuModelError("Model is not connected")
992+
return self._info.name
993+
985994
@property
986995
def info(self):
987996
"""Return the cached client.ModelInfo object for this Model.
@@ -2270,7 +2279,7 @@ async def list_offers(self):
22702279
shared and who is connected.
22712280
"""
22722281
async with ConnectedController(self.connection()) as controller:
2273-
return await controller.list_offers(self.info.name)
2282+
return await controller.list_offers(self.name)
22742283

22752284
async def remove_offer(self, endpoint, force=False):
22762285
"""

tests/integration/test_controller.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ async def test_list_models(event_loop):
130130
async with base.CleanController() as controller:
131131
async with base.CleanModel() as model:
132132
result = await controller.list_models()
133-
assert model.info.name in result
133+
assert model.name in result
134134

135135

136136
@base.bootstrapped
@@ -164,9 +164,9 @@ async def test_get_model(event_loop):
164164
try:
165165
by_name = await controller.get_model(model_name)
166166
by_uuid = await controller.get_model(model_uuid)
167-
assert by_name.info.name == model_name
167+
assert by_name.name == model_name
168168
assert by_name.info.uuid == model_uuid
169-
assert by_uuid.info.name == model_name
169+
assert by_uuid.name == model_name
170170
assert by_uuid.info.uuid == model_uuid
171171
finally:
172172
if by_name:

tests/integration/test_crossmodel.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async def test_offer(event_loop):
2525
await model.block_until(
2626
lambda: all(offer.application_name == 'ubuntu'
2727
for offer in offers.results))
28-
await model.remove_offer("admin/{}.ubuntu".format(model.info.name), force=True)
28+
await model.remove_offer("admin/{}.ubuntu".format(model.name), force=True)
2929

3030

3131
@base.bootstrapped
@@ -49,13 +49,13 @@ async def test_consume(event_loop):
4949

5050
# farm off a new model to test the consumption
5151
async with base.CleanModel() as model_2:
52-
await model_2.consume("admin/{}.ubuntu".format(model_1.info.name))
52+
await model_2.consume("admin/{}.ubuntu".format(model_1.name))
5353

5454
status = await model_2.get_status()
5555
if 'ubuntu' not in status.remote_applications:
5656
raise Exception("Expected ubuntu in saas")
5757

58-
await model_1.remove_offer("admin/{}.ubuntu".format(model_1.info.name), force=True)
58+
await model_1.remove_offer("admin/{}.ubuntu".format(model_1.name), force=True)
5959

6060

6161
@base.bootstrapped
@@ -79,7 +79,7 @@ async def test_remove_saas(event_loop):
7979

8080
# farm off a new model to test the consumption
8181
async with base.CleanModel() as model_2:
82-
await model_2.consume("admin/{}.ubuntu".format(model_1.info.name))
82+
await model_2.consume("admin/{}.ubuntu".format(model_1.name))
8383

8484
await model_2.remove_saas('ubuntu')
8585
await jasyncio.sleep(5)
@@ -88,7 +88,7 @@ async def test_remove_saas(event_loop):
8888
if 'ubuntu' in status.remote_applications:
8989
raise Exception("Expected ubuntu not to be in saas")
9090

91-
await model_1.remove_offer("admin/{}.ubuntu".format(model_1.info.name), force=True)
91+
await model_1.remove_offer("admin/{}.ubuntu".format(model_1.name), force=True)
9292

9393

9494
@base.bootstrapped
@@ -123,7 +123,7 @@ async def test_relate_with_offer(event_loop):
123123
lambda: all(unit.agent_status == 'idle'
124124
for unit in application.units))
125125

126-
await model_2.relate("mediawiki:db", "admin/{}.mysql".format(model_1.info.name))
126+
await model_2.relate("mediawiki:db", "admin/{}.mysql".format(model_1.name))
127127
status = await model_2.get_status()
128128
if 'mysql' not in status.remote_applications:
129129
raise Exception("Expected mysql in saas")
@@ -135,7 +135,7 @@ async def test_relate_with_offer(event_loop):
135135
if 'mysql' in status.remote_applications:
136136
raise Exception("Expected mysql not to be in saas")
137137

138-
await model_1.remove_offer("admin/{}.mysql".format(model_1.info.name), force=True)
138+
await model_1.remove_offer("admin/{}.mysql".format(model_1.name), force=True)
139139

140140

141141
@base.bootstrapped
@@ -160,7 +160,7 @@ async def test_add_bundle(event_loop):
160160
try:
161161
tmp_path = str(Path(dirpath) / 'bundle.yaml')
162162
with open(tmp_path, "w") as file:
163-
file.write(file_contents.format(model_1.info.name))
163+
file.write(file_contents.format(model_1.name))
164164
except IOError:
165165
raise
166166

@@ -186,4 +186,4 @@ async def test_add_bundle(event_loop):
186186
await model_2.deploy('local:{}'.format(tmp_path))
187187
await model_2.wait_for_idle(status="active")
188188

189-
await model_1.remove_offer("admin/{}.influxdb".format(model_1.info.name), force=True)
189+
await model_1.remove_offer("admin/{}.influxdb".format(model_1.name), force=True)

tests/integration/test_model.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import pytest
1515
from juju import jasyncio
1616
from juju.client import client
17-
from juju.errors import JujuError, JujuUnitError, JujuConnectionError
17+
from juju.errors import JujuError, JujuModelError, JujuUnitError, JujuConnectionError
1818
from juju.model import Model, ModelObserver
1919
from juju.utils import block_until, run_with_interrupt, wait_for_bundle
2020

@@ -28,6 +28,19 @@
2828
OVERLAYS_DIR = HERE_DIR / 'bundle' / 'test-overlays'
2929

3030

31+
@base.bootstrapped
32+
@pytest.mark.asyncio
33+
async def test_model_name(event_loop):
34+
model = Model()
35+
with pytest.raises(JujuModelError):
36+
model.name
37+
38+
async with base.CleanModel() as new_model:
39+
await model.connect(new_model.name)
40+
assert model.name == new_model.name
41+
await model.disconnect()
42+
43+
3144
@base.bootstrapped
3245
@pytest.mark.asyncio
3346
async def test_deploy_local_bundle_dir(event_loop):
@@ -591,7 +604,7 @@ async def _deploy_in_loop(new_loop, model_name, jujudata):
591604
@pytest.mark.asyncio
592605
async def test_explicit_loop_threaded(event_loop):
593606
async with base.CleanModel() as model:
594-
model_name = model.info.name
607+
model_name = model.name
595608
new_loop = jasyncio.new_event_loop()
596609
with ThreadPoolExecutor(1) as executor:
597610
f = executor.submit(

0 commit comments

Comments
 (0)