41
41
def is_admin_context(context):
42
42
"""Indicates if the request context is an administrator."""
44
warnings.warn('Use of empty request context is deprecated',
44
warnings.warn(_('Use of empty request context is deprecated'),
45
45
DeprecationWarning)
46
46
raise Exception('die')
47
47
return context.is_admin
227
227
filter_by(deleted=can_read_deleted(context)).\
230
raise exception.NotFound('No service for %s, %s' % (host, binary))
230
raise exception.NotFound(_('No service for %s, %s') % (host, binary))
252
252
###################
255
@require_admin_context
256
def certificate_get(context, certificate_id, session=None):
258
session = get_session()
260
result = session.query(models.Certificate).\
261
filter_by(id=certificate_id).\
262
filter_by(deleted=can_read_deleted(context)).\
266
raise exception.NotFound('No certificate for id %s' % certificate_id)
271
@require_admin_context
272
def certificate_create(context, values):
273
certificate_ref = models.Certificate()
274
for (key, value) in values.iteritems():
275
certificate_ref[key] = value
276
certificate_ref.save()
277
return certificate_ref
280
@require_admin_context
281
def certificate_destroy(context, certificate_id):
282
session = get_session()
283
with session.begin():
284
certificate_ref = certificate_get(context,
287
certificate_ref.delete(session=session)
290
@require_admin_context
291
def certificate_get_all_by_project(context, project_id):
292
session = get_session()
293
return session.query(models.Certificate).\
294
filter_by(project_id=project_id).\
295
filter_by(deleted=False).\
299
@require_admin_context
300
def certificate_get_all_by_user(context, user_id):
301
session = get_session()
302
return session.query(models.Certificate).\
303
filter_by(user_id=user_id).\
304
filter_by(deleted=False).\
308
@require_admin_context
309
def certificate_get_all_by_user_and_project(_context, user_id, project_id):
310
session = get_session()
311
return session.query(models.Certificate).\
312
filter_by(user_id=user_id).\
313
filter_by(project_id=project_id).\
314
filter_by(deleted=False).\
318
@require_admin_context
319
def certificate_update(context, certificate_id, values):
320
session = get_session()
321
with session.begin():
322
certificate_ref = certificate_get(context,
325
for (key, value) in values.iteritems():
326
certificate_ref[key] = value
327
certificate_ref.save(session=session)
256
334
def floating_ip_allocate_address(context, host, project_id):
257
335
authorize_project_context(context, project_id)
491
570
options(joinedload('instance')).\
494
raise exception.NotFound('No floating ip for address %s' % address)
573
raise exception.NotFound(_('No floating ip for address %s') % address)
496
575
if is_user_context(context):
497
576
authorize_project_context(context, result.instance.project_id)
528
607
#TODO(gundlach): instance_create and volume_create are nearly identical
529
608
#and should be refactored. I expect there are other copy-and-paste
530
609
#functions between the two of them as well.
532
613
def instance_create(context, values):
533
614
"""Create a new Instance record in the database.
580
661
if is_admin_context(context):
581
662
result = session.query(models.Instance).\
663
options(joinedload_all('fixed_ip.floating_ips')).\
582
664
options(joinedload('security_groups')).\
583
665
options(joinedload_all('security_groups.rules')).\
666
options(joinedload('volumes')).\
584
667
filter_by(id=instance_id).\
585
668
filter_by(deleted=can_read_deleted(context)).\
587
670
elif is_user_context(context):
588
671
result = session.query(models.Instance).\
672
options(joinedload_all('fixed_ip.floating_ips')).\
589
673
options(joinedload('security_groups')).\
590
674
options(joinedload_all('security_groups.rules')).\
675
options(joinedload('volumes')).\
591
676
filter_by(project_id=context.project_id).\
592
677
filter_by(id=instance_id).\
593
678
filter_by(deleted=False).\
596
raise exception.NotFound('No instance for id %s' % instance_id)
681
raise exception.NotFound(_('No instance for id %s') % instance_id)
741
@require_admin_context
742
def instance_get_project_vpn(context, project_id):
743
session = get_session()
744
return session.query(models.Instance).\
745
options(joinedload_all('fixed_ip.floating_ips')).\
746
options(joinedload('security_groups')).\
747
filter_by(project_id=project_id).\
748
filter_by(image_id=FLAGS.vpn_image_id).\
749
filter_by(deleted=can_read_deleted(context)).\
657
754
def instance_get_by_internal_id(context, internal_id):
658
755
session = get_session()
749
846
instance_ref.save(session=session)
850
def instance_action_create(context, values):
851
"""Create an instance action from the values dictionary."""
852
action_ref = models.InstanceActions()
853
action_ref.update(values)
855
session = get_session()
856
with session.begin():
857
action_ref.save(session=session)
861
@require_admin_context
862
def instance_get_actions(context, instance_id):
863
"""Return the actions associated to the given instance id"""
864
session = get_session()
866
for action in session.query(models.InstanceActions).\
867
filter_by(instance_id=instance_id).\
869
actions[action.action] = action.error
752
873
###################
915
1036
# NOTE(vish): pylint complains because of the long method name, but
916
1037
# it fits with the names of the rest of the methods
917
1038
# pylint: disable-msg=C0103
918
1041
@require_admin_context
919
1042
def network_get_associated_fixed_ips(context, network_id):
920
1043
session = get_session()
963
1086
with_lockmode('update').\
965
1088
if not network_ref:
966
raise exception.NotFound('No network for id %s' % network_id)
1089
raise exception.NotFound(_('No network for id %s') % network_id)
968
1091
# NOTE(vish): if with_lockmode isn't supported, as in sqlite,
969
1092
# then this has concurrency issues
989
1112
@require_context
990
def project_get_network(context, project_id):
1113
def project_get_network(context, project_id, associate=True):
991
1114
session = get_session()
992
rv = session.query(models.Network).\
993
filter_by(project_id=project_id).\
994
filter_by(deleted=False).\
1115
result = session.query(models.Network).\
1116
filter_by(project_id=project_id).\
1117
filter_by(deleted=False).\
998
1123
return network_associate(context, project_id)
999
1124
except IntegrityError:
1000
1125
# NOTE(vish): We hit this if there is a race and two
1001
1126
# processes are attempting to allocate the
1002
1127
# network at the same time
1003
rv = session.query(models.Network).\
1004
filter_by(project_id=project_id).\
1005
filter_by(deleted=False).\
1128
result = session.query(models.Network).\
1129
filter_by(project_id=project_id).\
1130
filter_by(deleted=False).\
1010
1135
###################
1064
1189
###################
1192
@require_admin_context
1067
1193
def auth_destroy_token(_context, token):
1068
1194
session = get_session()
1069
1195
session.delete(token)
1198
@require_admin_context
1072
1199
def auth_get_token(_context, token_hash):
1073
1200
session = get_session()
1074
1201
tk = session.query(models.AuthToken).\
1075
1202
filter_by(token_hash=token_hash).\
1078
raise exception.NotFound('Token %s does not exist' % token_hash)
1205
raise exception.NotFound(_('Token %s does not exist') % token_hash)
1209
@require_admin_context
1082
1210
def auth_create_token(_context, token):
1083
1211
tk = models.AuthToken()
1084
1212
tk.update(token)
1346
1474
filter_by(volume_id=volume_id).\
1349
raise exception.NotFound('No export device found for volume %s' %
1477
raise exception.NotFound(_('No export device found for volume %s') %
1352
1480
return (result.shelf_id, result.blade_id)