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
4755volume_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+
5069class 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 :
0 commit comments