~justin-fathomdb/nova/bug744150

« back to all changes in this revision

Viewing changes to nova/virt/xenapi/fake.py

  • Committer: Andy Smith
  • Date: 2011-01-18 23:51:13 UTC
  • mfrom: (579 nova)
  • mto: This revision was merged to the branch mainline in revision 581.
  • Revision ID: code@term.ie-20110118235113-ivu21efg3h9z6niq
merge from upstream and fix small issues

Show diffs side-by-side

added added

removed removed

Lines of Context:
76
76
    for c in _CLASSES:
77
77
        _db_content[c] = {}
78
78
    create_host('fake')
 
79
    create_vm('fake', 'Running', is_a_template=False, is_control_domain=True)
79
80
 
80
81
 
81
82
def create_host(name_label):
136
137
 
137
138
 
138
139
def create_vbd(vm_ref, vdi_ref):
139
 
    vbd_rec = {'VM': vm_ref, 'VDI': vdi_ref}
 
140
    vbd_rec = {
 
141
        'VM': vm_ref,
 
142
        'VDI': vdi_ref,
 
143
        'currently_attached': False,
 
144
        }
140
145
    vbd_ref = _create_object('VBD', vbd_rec)
141
146
    after_VBD_create(vbd_ref, vbd_rec)
142
147
    return vbd_ref
143
148
 
144
149
 
145
150
def after_VBD_create(vbd_ref, vbd_rec):
146
 
    """Create backref from VM to VBD when VBD is created"""
 
151
    """Create read-only fields and backref from VM to VBD when VBD is
 
152
    created."""
 
153
    vbd_rec['currently_attached'] = False
 
154
    vbd_rec['device'] = ''
147
155
    vm_ref = vbd_rec['VM']
148
156
    vm_rec = _db_content['VM'][vm_ref]
149
157
    vm_rec['VBDs'] = [vbd_ref]
152
160
    vbd_rec['vm_name_label'] = vm_name_label
153
161
 
154
162
 
155
 
def create_pbd(config, sr_ref, attached):
 
163
def create_pbd(config, host_ref, sr_ref, attached):
156
164
    return _create_object('PBD', {
157
165
        'device-config': config,
 
166
        'host': host_ref,
158
167
        'SR': sr_ref,
159
168
        'currently-attached': attached,
160
169
        })
167
176
        })
168
177
 
169
178
 
 
179
def create_local_srs():
 
180
    """Create an SR that looks like the one created on the local disk by
 
181
    default by the XenServer installer.  Do this one per host."""
 
182
    for host_ref in _db_content['host'].keys():
 
183
        _create_local_sr(host_ref)
 
184
 
 
185
 
 
186
def _create_local_sr(host_ref):
 
187
    sr_ref = _create_object('SR', {
 
188
        'name_label': 'Local storage',
 
189
        'type': 'lvm',
 
190
        'content_type': 'user',
 
191
        'shared': False,
 
192
        'physical_size': str(1 << 30),
 
193
        'physical_utilisation': str(0),
 
194
        'virtual_allocation': str(0),
 
195
        'other_config': {
 
196
            'i18n-original-value-name_label': 'Local storage',
 
197
            'i18n-key': 'local-storage',
 
198
            },
 
199
        'VDIs': []
 
200
        })
 
201
    pbd_ref = create_pbd('', host_ref, sr_ref, True)
 
202
    _db_content['SR'][sr_ref]['PBDs'] = [pbd_ref]
 
203
    return sr_ref
 
204
 
 
205
 
170
206
def _create_object(table, obj):
171
207
    ref = str(uuid.uuid4())
172
208
    obj['uuid'] = str(uuid.uuid4())
179
215
    # Forces fake to support iscsi only
180
216
    if sr_type != 'iscsi':
181
217
        raise Failure(['SR_UNKNOWN_DRIVER', sr_type])
 
218
    host_ref = _db_content['host'].keys()[0]
182
219
    sr_ref = _create_object(table, obj[2])
183
220
    vdi_ref = create_vdi('', False, sr_ref, False)
184
 
    pbd_ref = create_pbd('', sr_ref, True)
 
221
    pbd_ref = create_pbd('', host_ref, sr_ref, True)
185
222
    _db_content['SR'][sr_ref]['VDIs'] = [vdi_ref]
186
223
    _db_content['SR'][sr_ref]['PBDs'] = [pbd_ref]
187
224
    _db_content['VDI'][vdi_ref]['SR'] = sr_ref
233
270
    def __init__(self, uri):
234
271
        self._session = None
235
272
 
 
273
    def VBD_plug(self, _1, ref):
 
274
        rec = get_record('VBD', ref)
 
275
        if rec['currently_attached']:
 
276
            raise Failure(['DEVICE_ALREADY_ATTACHED', ref])
 
277
        rec['currently_attached'] = True
 
278
        rec['device'] = rec['userdevice']
 
279
 
 
280
    def VBD_unplug(self, _1, ref):
 
281
        rec = get_record('VBD', ref)
 
282
        if not rec['currently_attached']:
 
283
            raise Failure(['DEVICE_ALREADY_DETACHED', ref])
 
284
        rec['currently_attached'] = False
 
285
        rec['device'] = ''
 
286
 
236
287
    def xenapi_request(self, methodname, params):
237
288
        if methodname.startswith('login'):
238
289
            self._login(methodname, params)
289
340
            return lambda *params: self._getter(name, params)
290
341
        elif self._is_create(name):
291
342
            return lambda *params: self._create(name, params)
 
343
        elif self._is_destroy(name):
 
344
            return lambda *params: self._destroy(name, params)
292
345
        else:
293
346
            return None
294
347
 
299
352
                bits[1].startswith(getter and 'get_' or 'set_'))
300
353
 
301
354
    def _is_create(self, name):
 
355
        return self._is_method(name, 'create')
 
356
 
 
357
    def _is_destroy(self, name):
 
358
        return self._is_method(name, 'destroy')
 
359
 
 
360
    def _is_method(self, name, meth):
302
361
        bits = name.split('.')
303
362
        return (len(bits) == 2 and
304
363
                bits[0] in _CLASSES and
305
 
                bits[1] == 'create')
 
364
                bits[1] == meth)
306
365
 
307
366
    def _getter(self, name, params):
308
367
        self._check_session(params)
370
429
            _create_sr(cls, params) or _create_object(cls, params[1])
371
430
 
372
431
        # Call hook to provide any fixups needed (ex. creating backrefs)
373
 
        try:
374
 
            globals()["after_%s_create" % cls](ref, params[1])
375
 
        except KeyError:
376
 
            pass
 
432
        after_hook = 'after_%s_create' % cls
 
433
        if after_hook in globals():
 
434
            globals()[after_hook](ref, params[1])
377
435
 
378
436
        obj = get_record(cls, ref)
379
437
 
383
441
 
384
442
        return ref
385
443
 
 
444
    def _destroy(self, name, params):
 
445
        self._check_session(params)
 
446
        self._check_arg_count(params, 2)
 
447
        table, _ = name.split('.')
 
448
        ref = params[1]
 
449
        if ref not in _db_content[table]:
 
450
            raise Failure(['HANDLE_INVALID', table, ref])
 
451
        del _db_content[table][ref]
 
452
 
386
453
    def _async(self, name, params):
387
454
        task_ref = create_task(name)
388
455
        task = _db_content['task'][task_ref]
420
487
            try:
421
488
                return result[0]
422
489
            except IndexError:
423
 
                return None
 
490
                raise Failure(['UUID_INVALID', v, result, recs, k])
424
491
 
425
492
        return result
426
493