Skip to content

Commit

Permalink
Implement set/get model constraints (juju#253)
Browse files Browse the repository at this point in the history
* Implement set/get model constraints

Add methods for setting and getting model constraints.

* Fix model constraint name in tests
  • Loading branch information
Liam Young authored and johnsca committed Jul 23, 2018
1 parent c6b4ab4 commit 26c86c8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
32 changes: 26 additions & 6 deletions juju/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from . import tag, utils
from .client import client, connector
from .client.client import ConfigValue
from .client.client import Value
from .constraints import parse as parse_constraints
from .constraints import normalize_key
from .delta import get_entity_class, get_entity_delta
Expand Down Expand Up @@ -1461,11 +1462,28 @@ async def get_config(self):
config[key] = ConfigValue.from_json(value)
return config

def get_constraints(self):
async def get_constraints(self):
"""Return the machine constraints for this model.
:returns: A ``dict`` of constraints.
"""
raise NotImplementedError()
constraints = {}
client_facade = client.ClientFacade.from_connection(self.connection())
result = await client_facade.GetModelConstraints()

# GetModelConstraints returns GetConstraintsResults which has a 'constraints'
# attribute. If no constraints have been set GetConstraintsResults.constraints
# is None. Otherwise GetConstraintsResults.constraints has an attribute for each
# possible constraint, each of these in turn will be None if they have not been
# set.
if result.constraints:
constraint_types = [a for a in dir(result.constraints)
if a in Value._toSchema.keys()]
for constraint in constraint_types:
value = getattr(result.constraints, constraint)
if value is not None:
constraints[constraint] = getattr(result.constraints, constraint)
return constraints

def import_ssh_key(self, identity):
"""Add a public SSH key from a trusted indentity source to this model.
Expand Down Expand Up @@ -1624,13 +1642,15 @@ async def set_config(self, config):
config[key] = value.value
await config_facade.ModelSet(config)

def set_constraints(self, constraints):
async def set_constraints(self, constraints):
"""Set machine constraints on this model.
:param :class:`juju.Constraints` constraints: Machine constraints
:param dict config: Mapping of model constraints
"""
raise NotImplementedError()
client_facade = client.ClientFacade.from_connection(self.connection())
await client_facade.SetModelConstraints(
application='',
constraints=constraints)

async def get_action_output(self, action_uuid, wait=None):
"""Get the results of an action by ID.
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,14 @@ async def test_config(event_loop):
assert result['extra-info'].source == 'model'
assert result['extra-info'].value == 'booyah'

@base.bootstrapped
@pytest.mark.asyncio
async def test_set_constraints(event_loop):
async with base.CleanModel() as model:
await model.set_constraints({'cpu-power': 1})
cons = await model.get_constraints()
assert cons['cpu_power'] == 1

# @base.bootstrapped
# @pytest.mark.asyncio
# async def test_grant(event_loop)
Expand Down

0 comments on commit 26c86c8

Please sign in to comment.