diff --git a/climate/context.py b/climate/context.py index e0c56064..d3bbb410 100644 --- a/climate/context.py +++ b/climate/context.py @@ -43,6 +43,15 @@ class BaseContext(object): else: raise AttributeError(name) + def __setattr__(self, name, value): + # NOTE(yorik-sar): only the very first assignment for __values is + # allowed. All context arguments should be set at the time the context + # object is being created. + if not self.__dict__: + super(BaseContext, self).__setattr__(name, value) + else: + raise Exception(self.__dict__, name, value) + def __enter__(self): try: stack = self._context_stack.stack diff --git a/climate/tests/utils/test_trusts.py b/climate/tests/utils/test_trusts.py index a3c7f5eb..716d529d 100644 --- a/climate/tests/utils/test_trusts.py +++ b/climate/tests/utils/test_trusts.py @@ -51,12 +51,13 @@ class TestTrusts(tests.TestCase): def test_create_ctx_from_trust(self): fake_item = self.client().service_catalog.catalog.__getitem__() - fake_ctx_dict = {'_BaseContext__values': {}, - 'auth_token': self.client().auth_token, - 'service_catalog': fake_item, - 'tenant_id': self.client().tenant_id, - 'tenant_name': 'admin', - 'user_name': 'admin'} + fake_ctx_dict = {'_BaseContext__values': { + 'auth_token': self.client().auth_token, + 'service_catalog': fake_item, + 'tenant_id': self.client().tenant_id, + 'tenant_name': 'admin', + 'user_name': 'admin', + }} ctx = self.trusts.create_ctx_from_trust('1') diff --git a/climate/utils/trusts.py b/climate/utils/trusts.py index f134027f..b87117ff 100644 --- a/climate/utils/trusts.py +++ b/climate/utils/trusts.py @@ -48,9 +48,10 @@ def delete_trust(lease): def create_ctx_from_trust(trust_id): """Return context built from given trust.""" - ctx = context.ClimateContext() - ctx.user_name = CONF.os_admin_username - ctx.tenant_name = CONF.os_admin_tenant_name + ctx = context.ClimateContext( + user_name=CONF.os_admin_username, + tenant_name=CONF.os_admin_tenant_name, + ) auth_url = "%s://%s:%s/v3" % (CONF.os_auth_protocol, CONF.os_auth_host, CONF.os_auth_port) @@ -58,12 +59,13 @@ def create_ctx_from_trust(trust_id): password=CONF.os_admin_password, trust_id=trust_id, auth_url=auth_url, - ctx=ctx + ctx=ctx, ) - ctx.auth_token = client.auth_token - ctx.service_catalog = client.service_catalog.catalog['catalog'] - ctx.tenant_id = client.tenant_id - # use 'with ctx' statement in the place you need context from trust - return ctx + return context.ClimateContext( + ctx, + auth_token=client.auth_token, + service_catalog=client.service_catalog.catalog['catalog'], + tenant_id=client.tenant_id, + )