Skip to content

Commit ebc20a3

Browse files
committed
Use a private method to acquire Application facade instances
This change allows us to easily mock the facade in tests.
1 parent ad3c449 commit ebc20a3

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

juju/application.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class Application(model.ModelEntity):
3131
def _unit_match_pattern(self):
3232
return r'^{}.*$'.format(self.entity_id)
3333

34+
def _facade(self):
35+
return client.ApplicationFacade.from_connection(self.connection)
36+
3437
def on_unit_add(self, callable_):
3538
"""Add a "unit added" observer to this entity, which will be called
3639
whenever a unit is added to this application.
@@ -123,7 +126,7 @@ async def add_unit(self, count=1, to=None):
123126
If None, a new machine is provisioned.
124127
125128
"""
126-
app_facade = client.ApplicationFacade.from_connection(self.connection)
129+
app_facade = self._facade()
127130

128131
log.debug(
129132
'Adding %s unit%s to %s',
@@ -152,7 +155,7 @@ async def scale(self, scale=None, scale_change=None):
152155
:param int scale_change: Amount by which to adjust the scale of this
153156
application (can be positive or negative).
154157
"""
155-
app_facade = client.ApplicationFacade.from_connection(self.connection)
158+
app_facade = self._facade()
156159

157160
if (scale, scale_change) == (None, None):
158161
raise ValueError('Must provide either scale or scale_change')
@@ -202,7 +205,7 @@ async def destroy_relation(self, local_relation, remote_relation):
202205
if ':' not in local_relation:
203206
local_relation = '{}:{}'.format(self.name, local_relation)
204207

205-
app_facade = client.ApplicationFacade.from_connection(self.connection)
208+
app_facade = self._facade()
206209

207210
log.debug(
208211
'Destroying relation %s <-> %s', local_relation, remote_relation)
@@ -222,7 +225,7 @@ async def destroy(self):
222225
"""Remove this application from the model.
223226
224227
"""
225-
app_facade = client.ApplicationFacade.from_connection(self.connection)
228+
app_facade = self._facade()
226229

227230
log.debug(
228231
'Destroying %s', self.name)
@@ -234,7 +237,7 @@ async def expose(self):
234237
"""Make this application publicly available over the network.
235238
236239
"""
237-
app_facade = client.ApplicationFacade.from_connection(self.connection)
240+
app_facade = self._facade()
238241

239242
log.debug(
240243
'Exposing %s', self.name)
@@ -245,7 +248,7 @@ async def get_config(self):
245248
"""Return the configuration settings dict for this application.
246249
247250
"""
248-
app_facade = client.ApplicationFacade.from_connection(self.connection)
251+
app_facade = self._facade()
249252

250253
log.debug(
251254
'Getting config for %s', self.name)
@@ -259,7 +262,7 @@ async def get_trusted(self):
259262
if self.model.info.agent_version < client.Number.from_json('2.4.0'):
260263
raise NotImplementedError("trusted is not supported on model version {}".format(self.model.info.agent_version))
261264

262-
app_facade = client.ApplicationFacade.from_connection(self.connection)
265+
app_facade = self._facade()
263266

264267
log.debug(
265268
'Getting config for %s', self.name)
@@ -281,7 +284,7 @@ async def set_trusted(self, trust):
281284

282285
# clamp trust to exactly the value juju expects, rather than allowing
283286
# anything in the config.
284-
app_facade = client.ApplicationFacade.from_connection(self.connection)
287+
app_facade = self._facade()
285288

286289
config = {'trust': json.dumps(True if trust is True else False)}
287290
log.debug(
@@ -296,7 +299,7 @@ async def get_constraints(self):
296299
"""Return the machine constraints dict for this application.
297300
298301
"""
299-
app_facade = client.ApplicationFacade.from_connection(self.connection)
302+
app_facade = self._facade()
300303

301304
log.debug(
302305
'Getting constraints for %s', self.name)
@@ -384,7 +387,7 @@ async def set_config(self, config):
384387
385388
:param config: Dict of configuration to set
386389
"""
387-
app_facade = client.ApplicationFacade.from_connection(self.connection)
390+
app_facade = self._facade()
388391

389392
log.debug(
390393
'Setting config for %s: %s', self.name, config)
@@ -398,7 +401,7 @@ async def reset_config(self, to_default):
398401
:param list to_default: A list of config options to be reset to their
399402
default value.
400403
"""
401-
app_facade = client.ApplicationFacade.from_connection(self.connection)
404+
app_facade = self._facade()
402405

403406
log.debug(
404407
'Restoring default config for %s: %s', self.name, to_default)
@@ -411,7 +414,7 @@ async def set_constraints(self, constraints):
411414
:param dict constraints: Dict of machine constraints
412415
413416
"""
414-
app_facade = client.ApplicationFacade.from_connection(self.connection)
417+
app_facade = self._facade()
415418

416419
log.debug(
417420
'Setting constraints for %s: %s', self.name, constraints)
@@ -439,7 +442,7 @@ async def unexpose(self):
439442
"""Remove public availability over the network for this application.
440443
441444
"""
442-
app_facade = client.ApplicationFacade.from_connection(self.connection)
445+
app_facade = self._facade()
443446

444447
log.debug(
445448
'Unexposing %s', self.name)
@@ -483,7 +486,7 @@ async def upgrade_charm(
483486
client_facade = client.ClientFacade.from_connection(self.connection)
484487
resources_facade = client.ResourcesFacade.from_connection(
485488
self.connection)
486-
app_facade = client.ApplicationFacade.from_connection(self.connection)
489+
app_facade = self._facade()
487490

488491
charmstore = self.model.charmstore
489492
charmstore_entity = None

0 commit comments

Comments
 (0)