Remove direct assignments of context attributes

Context attributes should be set by its __init__ only.

Change-Id: I7c5b0b21e27384adec7049a6f5bb8454fa81ffb8
This commit is contained in:
Yuriy Taraday 2014-02-11 18:56:15 +04:00
parent 573fb0cc1f
commit 485f8a1606
3 changed files with 27 additions and 15 deletions

View File

@ -43,6 +43,15 @@ class BaseContext(object):
else: else:
raise AttributeError(name) 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): def __enter__(self):
try: try:
stack = self._context_stack.stack stack = self._context_stack.stack

View File

@ -51,12 +51,13 @@ class TestTrusts(tests.TestCase):
def test_create_ctx_from_trust(self): def test_create_ctx_from_trust(self):
fake_item = self.client().service_catalog.catalog.__getitem__() fake_item = self.client().service_catalog.catalog.__getitem__()
fake_ctx_dict = {'_BaseContext__values': {}, fake_ctx_dict = {'_BaseContext__values': {
'auth_token': self.client().auth_token, 'auth_token': self.client().auth_token,
'service_catalog': fake_item, 'service_catalog': fake_item,
'tenant_id': self.client().tenant_id, 'tenant_id': self.client().tenant_id,
'tenant_name': 'admin', 'tenant_name': 'admin',
'user_name': 'admin'} 'user_name': 'admin',
}}
ctx = self.trusts.create_ctx_from_trust('1') ctx = self.trusts.create_ctx_from_trust('1')

View File

@ -48,9 +48,10 @@ def delete_trust(lease):
def create_ctx_from_trust(trust_id): def create_ctx_from_trust(trust_id):
"""Return context built from given trust.""" """Return context built from given trust."""
ctx = context.ClimateContext() ctx = context.ClimateContext(
ctx.user_name = CONF.os_admin_username user_name=CONF.os_admin_username,
ctx.tenant_name = CONF.os_admin_tenant_name tenant_name=CONF.os_admin_tenant_name,
)
auth_url = "%s://%s:%s/v3" % (CONF.os_auth_protocol, auth_url = "%s://%s:%s/v3" % (CONF.os_auth_protocol,
CONF.os_auth_host, CONF.os_auth_host,
CONF.os_auth_port) CONF.os_auth_port)
@ -58,12 +59,13 @@ def create_ctx_from_trust(trust_id):
password=CONF.os_admin_password, password=CONF.os_admin_password,
trust_id=trust_id, trust_id=trust_id,
auth_url=auth_url, 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 # 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,
)