diff --git a/juju/model.py b/juju/model.py index d90d3d30..37e8cd6a 100644 --- a/juju/model.py +++ b/juju/model.py @@ -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 @@ -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. @@ -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. diff --git a/tests/integration/test_model.py b/tests/integration/test_model.py index 10f939e6..1cba79a2 100644 --- a/tests/integration/test_model.py +++ b/tests/integration/test_model.py @@ -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)