Skip to content

Commit fe871a5

Browse files
committed
Add support for default project_id and user_id
The user_id and project_id fields for our volumes and snapshots had a very poor default value, they were defaulting to None. And the only way we had to change them was to provide an override for these fields when creating the resource. This patch adds a better default for these values ("cinderlib") and also supports setting a different default on the setup process passing project_id and user_id parameters to the "setup" method.
1 parent 76cca47 commit fe871a5

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

HISTORY.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ History
99

1010
- Modify fields on connect method.
1111
- Support setting custom root_helper.
12+
- Setting default project_id and user_id.
1213

1314
- Bug fixes:
1415

cinderlib/cinderlib.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,31 @@
4141

4242

4343
__all__ = ['setup', 'load', 'json', 'jsons', 'Backend', 'Volume', 'Snapshot',
44-
'Connection']
44+
'Connection', 'DEFAULT_PROJECT_ID', 'DEFAULT_USER_ID']
45+
46+
47+
DEFAULT_PROJECT_ID = 'cinderlib'
48+
DEFAULT_USER_ID = 'cinderlib'
49+
CONTEXT = context.RequestContext(user_id=DEFAULT_USER_ID,
50+
project_id=DEFAULT_PROJECT_ID,
51+
is_admin=True,
52+
overwrite=False)
4553

4654

4755
volume_cmd.objects.register_all()
4856

4957

58+
def set_context(project_id=None, user_id=None):
59+
global CONTEXT
60+
project_id = project_id or DEFAULT_PROJECT_ID
61+
user_id = user_id or DEFAULT_USER_ID
62+
if project_id != CONTEXT.project_id or user_id != CONTEXT.user_id:
63+
CONTEXT.user_id = user_id
64+
CONTEXT.project_id = project_id
65+
Volume.DEFAULT_FIELDS_VALUES['user_id'] = user_id
66+
Volume.DEFAULT_FIELDS_VALUES['project_id'] = project_id
67+
68+
5069
class Backend(object):
5170
"""Representation of a Cinder Driver.
5271
@@ -63,7 +82,6 @@ class Backend(object):
6382
"""
6483
backends = {}
6584
global_initialization = False
66-
context = context.get_admin_context()
6785

6886
def __init__(self, volume_backend_name, **driver_cfg):
6987
if not self.global_initialization:
@@ -79,7 +97,7 @@ def __init__(self, volume_backend_name, **driver_cfg):
7997
host=volume_cmd.CONF.host,
8098
cluster_name=None, # No clusters for now: volume_cmd.CONF.cluster,
8199
active_backend_id=None) # No failover for now
82-
self.driver.do_setup(self.context)
100+
self.driver.do_setup(CONTEXT)
83101
self.driver.check_for_setup_error()
84102
self.driver.init_capabilities()
85103
self.driver.set_throttle()
@@ -156,11 +174,13 @@ def validate_connector(self, connector_dict):
156174
def global_setup(cls, file_locks_path=None, root_helper='sudo',
157175
suppress_requests_ssl_warnings=True, disable_logs=True,
158176
non_uuid_ids=False, output_all_backend_info=False,
159-
**log_params):
177+
project_id=None, user_id=None, **log_params):
160178
# Global setup can only be set once
161179
if cls.global_initialization:
162180
raise Exception('Already setup')
163181

182+
set_context(project_id, user_id)
183+
164184
# Prevent driver dynamic loading clearing configuration options
165185
volume_cmd.CONF._ConfigOpts__cache = MyDict()
166186

@@ -271,7 +291,6 @@ class Object(object):
271291
"""Base class for our resource representation objects."""
272292
DEFAULT_FIELDS_VALUES = {}
273293
objects = collections.defaultdict(dict)
274-
context = context.get_admin_context()
275294

276295
def __init__(self, backend, **fields_data):
277296
self.backend = backend
@@ -310,7 +329,7 @@ def _create_ovo(self, **fields_data):
310329
elif field.nullable:
311330
fields_values.setdefault(field_name, None)
312331

313-
return self.OVO_CLASS(context=self.context, **fields_values)
332+
return self.OVO_CLASS(context=CONTEXT, **fields_values)
314333

315334
@property
316335
def json(self):
@@ -342,7 +361,7 @@ def load(cls, json_src):
342361
backend = Backend(**json_src['backend'])
343362

344363
ovo = cinder_base_ovo.CinderObject.obj_from_primitive(json_src['ovo'],
345-
cls.context)
364+
CONTEXT)
346365
return cls._load(backend, ovo)
347366

348367
def _replace_ovo(self, ovo):
@@ -362,8 +381,8 @@ class Volume(Object):
362381
OVO_CLASS = volume_cmd.objects.Volume
363382
DEFAULT_FIELDS_VALUES = {
364383
'size': 1,
365-
'user_id': Object.context.user_id,
366-
'project_id': Object.context.project_id,
384+
'user_id': CONTEXT.user_id,
385+
'project_id': CONTEXT.project_id,
367386
'host': volume_cmd.CONF.host,
368387
'status': 'creating',
369388
'attach_status': 'detached',
@@ -390,10 +409,10 @@ def __init__(self, backend_or_vol, **kwargs):
390409
kwargs['display_name'] = kwargs.pop('name')
391410
kwargs.setdefault(
392411
'volume_attachment',
393-
volume_cmd.objects.VolumeAttachmentList(context=self.context))
412+
volume_cmd.objects.VolumeAttachmentList(context=CONTEXT))
394413
kwargs.setdefault(
395414
'snapshots',
396-
volume_cmd.objects.SnapshotList(context=self.context))
415+
volume_cmd.objects.SnapshotList(context=CONTEXT))
397416

398417
super(Volume, self).__init__(backend_or_vol, **kwargs)
399418
self.snapshots = set()
@@ -556,7 +575,7 @@ def detach(self, force=False, ignore_errors=False):
556575

557576
def connect(self, connector_dict, **ovo_fields):
558577
if not self.exported:
559-
model_update = self.backend.driver.create_export(self.context,
578+
model_update = self.backend.driver.create_export(CONTEXT,
560579
self._ovo,
561580
connector_dict)
562581
if model_update:

docs/topics/initialization.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The method definition is as follows:
2323
def global_setup(cls, file_locks_path=None, root_helpers='sudo',
2424
suppress_requests_ssl_warnings=True, disable_logs=True,
2525
non_uuid_ids=False, output_all_backend_info=False,
26-
**log_params):
26+
project_id=None, user_id=None, **log_params):
2727
2828
2929
The meaning of the library's configuration options are:
@@ -102,6 +102,26 @@ default.
102102

103103
Defaults to `True`.
104104

105+
project_id
106+
----------
107+
108+
*Cinder* is a multi-tenant service, and when resources are created they belong
109+
to a specific tenant/project. With this parameter we can define, using a
110+
string, an identifier for our project that will be assigned to the resources we
111+
create.
112+
113+
Defaults to `cinderlib`.
114+
115+
user_id
116+
-------
117+
118+
Within each project/tenant the *Cinder* project supports multiple users, so
119+
when it creates a resource a reference to the user that created it is stored
120+
in the resource. Using this this parameter we can define, using a string, an
121+
identifier for the user of cinderlib to be recorded in the resources.
122+
123+
Defaults to `cinderlib`.
124+
105125
other keyword arguments
106126
-----------------------
107127

0 commit comments

Comments
 (0)