Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def make_test_model(foo_unit=True, foo_status='idle'):
juju_model = Model()
state = juju_model.state
state.apply_delta(ApplicationDelta(
('application', 'type2', {'name': 'foo'})))
if foo_unit:
state.apply_delta(UnitDelta(('unit', 'type1', {
'name': 'steve',
'application': 'foo',
# Add some status to fake out chaos's "wait 'til the
# cloud is idle" check.
'agent-status': {'current': foo_status}
})))
return juju_model
async def _set_model():
model = Model()
await model.connect_current()
self.model = model
async def run_in_model(model_name):
"""Context manager for executing code inside a libjuju model.
Example of using run_in_model:
async with run_in_model(model_name) as model:
model.do_something()
:param model_name: Name of model to run function in
:type model_name: str
:returns: The juju Model object correcsponding to model_name
:rtype: Iterator[:class:'juju.Model()']
"""
model = Model()
if not model_name:
model_name = await async_get_juju_model()
await model.connect_model(model_name)
try:
await yield_(model)
finally:
await model.disconnect()
async def connect_model():
""" Connect to the selected model.
"""
if app.provider.controller is None:
raise Exception("No controller selected")
if app.provider.model is None:
raise Exception("No model selected.")
app.juju.client = Model(app.loop)
model_name = '{}:{}'.format(app.provider.controller,
app.provider.model)
await app.juju.client.connect(model_name)
events.ModelConnected.set()
async def add_model(self, context):
if self.model:
if context.juju_model:
return
log.info("Connecting to model %s", self.model)
context.juju_model = juju.model.Model(loop=self.loop)
await context.juju_model.connect_model(self.model)
else:
# work-around for: https://bugs.launchpad.net/juju/+bug/1652171
credential = await self._get_credential(context)
name = "{}-{}".format(
context.config.model_prefix,
petname.Generate(2, '-')
)
log.info("Creating model %s", name)
context.juju_model = await context.juju_controller.add_model(
name, credential_name=credential,
cloud_name=context.config.cloud)
self.bus.dispatch(
origin="matrix",
payload=context.juju_model,
kind="model.new",
async def deployed():
"""List deployed applications."""
# Create a Model instance. We need to connect our Model to a Juju api
# server before we can use it.
model = Model()
# Connect to the currently active Juju model
await model.connect_current()
try:
# list currently deploeyd services
return list(model.applications.keys())
finally:
# Disconnect from the api server and cleanup.
await model.disconnect()