148
159
@require_admin_context
160
def service_get_all_by_host(context, host):
161
session = get_session()
162
return session.query(models.Service).\
163
filter_by(deleted=False).\
164
filter_by(host=host).\
168
@require_admin_context
149
169
def _service_get_all_topic_subquery(context, session, topic, subq, label):
150
170
sort_value = getattr(subq.c, label)
151
171
return session.query(models.Service, func.coalesce(sort_value, 0)).\
252
274
###################
277
@require_admin_context
278
def certificate_get(context, certificate_id, session=None):
280
session = get_session()
282
result = session.query(models.Certificate).\
283
filter_by(id=certificate_id).\
284
filter_by(deleted=can_read_deleted(context)).\
288
raise exception.NotFound('No certificate for id %s' % certificate_id)
293
@require_admin_context
294
def certificate_create(context, values):
295
certificate_ref = models.Certificate()
296
for (key, value) in values.iteritems():
297
certificate_ref[key] = value
298
certificate_ref.save()
299
return certificate_ref
302
@require_admin_context
303
def certificate_destroy(context, certificate_id):
304
session = get_session()
305
with session.begin():
306
certificate_ref = certificate_get(context,
309
certificate_ref.delete(session=session)
312
@require_admin_context
313
def certificate_get_all_by_project(context, project_id):
314
session = get_session()
315
return session.query(models.Certificate).\
316
filter_by(project_id=project_id).\
317
filter_by(deleted=False).\
321
@require_admin_context
322
def certificate_get_all_by_user(context, user_id):
323
session = get_session()
324
return session.query(models.Certificate).\
325
filter_by(user_id=user_id).\
326
filter_by(deleted=False).\
330
@require_admin_context
331
def certificate_get_all_by_user_and_project(_context, user_id, project_id):
332
session = get_session()
333
return session.query(models.Certificate).\
334
filter_by(user_id=user_id).\
335
filter_by(project_id=project_id).\
336
filter_by(deleted=False).\
340
@require_admin_context
341
def certificate_update(context, certificate_id, values):
342
session = get_session()
343
with session.begin():
344
certificate_ref = certificate_get(context,
347
for (key, value) in values.iteritems():
348
certificate_ref[key] = value
349
certificate_ref.save(session=session)
256
356
def floating_ip_allocate_address(context, host, project_id):
257
357
authorize_project_context(context, project_id)
505
606
return fixed_ip_ref.instance
610
def fixed_ip_get_instance_v6(context, address):
611
session = get_session()
612
mac = utils.to_mac(address)
614
result = session.query(models.Instance).\
615
filter_by(mac_address=mac).\
508
620
@require_admin_context
509
621
def fixed_ip_get_network(context, address):
510
622
fixed_ip_ref = fixed_ip_get_by_address(context, address)
525
637
###################
528
#TODO(gundlach): instance_create and volume_create are nearly identical
529
#and should be refactored. I expect there are other copy-and-paste
530
#functions between the two of them as well.
532
641
def instance_create(context, values):
533
642
"""Create a new Instance record in the database.
535
644
context - request context object
536
645
values - dict containing column values.
537
'internal_id' is auto-generated and should not be specified.
539
647
instance_ref = models.Instance()
540
648
instance_ref.update(values)
542
650
session = get_session()
543
651
with session.begin():
544
while instance_ref.internal_id == None:
545
# Instances have integer internal ids.
546
internal_id = random.randint(0, 2 ** 31 - 1)
547
if not instance_internal_id_exists(context, internal_id,
549
instance_ref.internal_id = internal_id
550
652
instance_ref.save(session=session)
551
653
return instance_ref
580
682
if is_admin_context(context):
581
683
result = session.query(models.Instance).\
582
options(joinedload('security_groups')).\
684
options(joinedload_all('fixed_ip.floating_ips')).\
685
options(joinedload_all('security_groups.rules')).\
686
options(joinedload('volumes')).\
583
687
filter_by(id=instance_id).\
584
688
filter_by(deleted=can_read_deleted(context)).\
586
690
elif is_user_context(context):
587
691
result = session.query(models.Instance).\
588
options(joinedload('security_groups')).\
692
options(joinedload_all('fixed_ip.floating_ips')).\
693
options(joinedload_all('security_groups.rules')).\
694
options(joinedload('volumes')).\
589
695
filter_by(project_id=context.project_id).\
590
696
filter_by(id=instance_id).\
591
697
filter_by(deleted=False).\
594
raise exception.NotFound('No instance for id %s' % instance_id)
700
raise exception.NotFound(_('No instance for id %s') % instance_id)
760
@require_admin_context
761
def instance_get_project_vpn(context, project_id):
762
session = get_session()
763
return session.query(models.Instance).\
764
options(joinedload_all('fixed_ip.floating_ips')).\
765
options(joinedload('security_groups')).\
766
filter_by(project_id=project_id).\
767
filter_by(image_id=FLAGS.vpn_image_id).\
768
filter_by(deleted=can_read_deleted(context)).\
655
def instance_get_by_internal_id(context, internal_id):
773
def instance_get_by_id(context, instance_id):
656
774
session = get_session()
658
776
if is_admin_context(context):
659
777
result = session.query(models.Instance).\
778
options(joinedload_all('fixed_ip.floating_ips')).\
660
779
options(joinedload('security_groups')).\
661
filter_by(internal_id=internal_id).\
780
options(joinedload_all('fixed_ip.network')).\
781
filter_by(id=instance_id).\
662
782
filter_by(deleted=can_read_deleted(context)).\
664
784
elif is_user_context(context):
665
785
result = session.query(models.Instance).\
666
786
options(joinedload('security_groups')).\
787
options(joinedload_all('fixed_ip.floating_ips')).\
788
options(joinedload_all('fixed_ip.network')).\
667
789
filter_by(project_id=context.project_id).\
668
filter_by(internal_id=internal_id).\
790
filter_by(id=instance_id).\
669
791
filter_by(deleted=False).\
672
raise exception.NotFound('Instance %s not found' % (internal_id))
794
raise exception.NotFound(_('Instance %s not found') % (instance_id))
678
def instance_internal_id_exists(context, internal_id, session=None):
680
session = get_session()
681
return session.query(exists().\
682
where(models.Instance.internal_id == internal_id)).\
687
800
def instance_get_fixed_address(context, instance_id):
688
801
session = get_session()
689
802
with session.begin():
810
def instance_get_fixed_address_v6(context, instance_id):
811
session = get_session()
812
with session.begin():
813
instance_ref = instance_get(context, instance_id, session=session)
814
network_ref = network_get_by_instance(context, instance_id)
815
prefix = network_ref.cidr_v6
816
mac = instance_ref.mac_address
817
return utils.to_global_ipv6(prefix, mac)
697
821
def instance_get_floating_address(context, instance_id):
698
822
session = get_session()
699
823
with session.begin():
747
871
instance_ref.save(session=session)
875
def instance_action_create(context, values):
876
"""Create an instance action from the values dictionary."""
877
action_ref = models.InstanceActions()
878
action_ref.update(values)
880
session = get_session()
881
with session.begin():
882
action_ref.save(session=session)
886
@require_admin_context
887
def instance_get_actions(context, instance_id):
888
"""Return the actions associated to the given instance id"""
889
session = get_session()
890
return session.query(models.InstanceActions).\
891
filter_by(instance_id=instance_id).\
750
895
###################
987
1134
@require_context
988
def project_get_network(context, project_id):
1135
def project_get_network(context, project_id, associate=True):
989
1136
session = get_session()
990
rv = session.query(models.Network).\
991
filter_by(project_id=project_id).\
992
filter_by(deleted=False).\
1137
result = session.query(models.Network).\
1138
filter_by(project_id=project_id).\
1139
filter_by(deleted=False).\
996
1145
return network_associate(context, project_id)
997
1146
except IntegrityError:
998
1147
# NOTE(vish): We hit this if there is a race and two
999
1148
# processes are attempting to allocate the
1000
1149
# network at the same time
1001
rv = session.query(models.Network).\
1002
filter_by(project_id=project_id).\
1003
filter_by(deleted=False).\
1150
result = session.query(models.Network).\
1151
filter_by(project_id=project_id).\
1152
filter_by(deleted=False).\
1158
def project_get_network_v6(context, project_id):
1159
return project_get_network(context, project_id)
1008
1162
###################
1062
1216
###################
1219
@require_admin_context
1065
1220
def auth_destroy_token(_context, token):
1066
1221
session = get_session()
1067
1222
session.delete(token)
1225
@require_admin_context
1070
1226
def auth_get_token(_context, token_hash):
1071
1227
session = get_session()
1072
1228
tk = session.query(models.AuthToken).\
1073
1229
filter_by(token_hash=token_hash).\
1076
raise exception.NotFound('Token %s does not exist' % token_hash)
1232
raise exception.NotFound(_('Token %s does not exist') % token_hash)
1236
@require_admin_context
1080
1237
def auth_create_token(_context, token):
1081
1238
tk = models.AuthToken()
1082
1239
tk.update(token)
1292
def volume_get_by_ec2_id(context, ec2_id):
1293
session = get_session()
1296
if is_admin_context(context):
1297
result = session.query(models.Volume).\
1298
filter_by(ec2_id=ec2_id).\
1299
filter_by(deleted=can_read_deleted(context)).\
1301
elif is_user_context(context):
1302
result = session.query(models.Volume).\
1303
filter_by(project_id=context.project_id).\
1304
filter_by(ec2_id=ec2_id).\
1305
filter_by(deleted=False).\
1308
raise exception.NotAuthorized()
1311
raise exception.NotFound('Volume %s not found' % ec2_id)
1317
def volume_ec2_id_exists(context, ec2_id, session=None):
1319
session = get_session()
1321
return session.query(exists().\
1322
where(models.Volume.id == ec2_id)).\
1326
1444
@require_admin_context
1327
1445
def volume_get_instance(context, volume_id):
1328
1446
session = get_session()
1507
1625
filter_by(id=security_group_rule_id).\
1510
raise exception.NotFound("No secuity group rule with id %s" %
1628
raise exception.NotFound(_("No secuity group rule with id %s") %
1511
1629
security_group_rule_id)
1515
1633
@require_context
1634
def security_group_rule_get_by_security_group(context, security_group_id,
1637
session = get_session()
1638
if is_admin_context(context):
1639
result = session.query(models.SecurityGroupIngressRule).\
1640
filter_by(deleted=can_read_deleted(context)).\
1641
filter_by(parent_group_id=security_group_id).\
1644
# TODO(vish): Join to group and check for project_id
1645
result = session.query(models.SecurityGroupIngressRule).\
1646
filter_by(deleted=False).\
1647
filter_by(parent_group_id=security_group_id).\
1653
def security_group_rule_get_by_security_group_grantee(context,
1657
session = get_session()
1658
if is_admin_context(context):
1659
result = session.query(models.SecurityGroupIngressRule).\
1660
filter_by(deleted=can_read_deleted(context)).\
1661
filter_by(group_id=security_group_id).\
1664
result = session.query(models.SecurityGroupIngressRule).\
1665
filter_by(deleted=False).\
1666
filter_by(group_id=security_group_id).\
1516
1672
def security_group_rule_create(context, values):
1517
1673
security_group_rule_ref = models.SecurityGroupIngressRule()
1518
1674
security_group_rule_ref.update(values)
1747
1903
filter_by(deleted=False).\
1748
1904
filter_by(host=host).\
1911
def console_pool_create(context, values):
1912
pool = models.ConsolePool()
1918
def console_pool_get(context, pool_id):
1919
session = get_session()
1920
result = session.query(models.ConsolePool).\
1921
filter_by(deleted=False).\
1922
filter_by(id=pool_id).\
1925
raise exception.NotFound(_("No console pool with id %(pool_id)s") %
1926
{'pool_id': pool_id})
1931
def console_pool_get_by_host_type(context, compute_host, host,
1933
session = get_session()
1934
result = session.query(models.ConsolePool).\
1935
filter_by(host=host).\
1936
filter_by(console_type=console_type).\
1937
filter_by(compute_host=compute_host).\
1938
filter_by(deleted=False).\
1939
options(joinedload('consoles')).\
1942
raise exception.NotFound(_('No console pool of type %(type)s '
1943
'for compute host %(compute_host)s '
1944
'on proxy host %(host)s') %
1945
{'type': console_type,
1946
'compute_host': compute_host,
1951
def console_pool_get_all_by_host_type(context, host, console_type):
1952
session = get_session()
1953
return session.query(models.ConsolePool).\
1954
filter_by(host=host).\
1955
filter_by(console_type=console_type).\
1956
filter_by(deleted=False).\
1957
options(joinedload('consoles')).\
1961
def console_create(context, values):
1962
console = models.Console()
1963
console.update(values)
1968
def console_delete(context, console_id):
1969
session = get_session()
1970
with session.begin():
1971
# consoles are meant to be transient. (mdragon)
1972
session.execute('delete from consoles '
1973
'where id=:id', {'id': console_id})
1976
def console_get_by_pool_instance(context, pool_id, instance_id):
1977
session = get_session()
1978
result = session.query(models.Console).\
1979
filter_by(pool_id=pool_id).\
1980
filter_by(instance_id=instance_id).\
1981
options(joinedload('pool')).\
1984
raise exception.NotFound(_('No console for instance %(instance_id)s '
1985
'in pool %(pool_id)s') %
1986
{'instance_id': instance_id,
1987
'pool_id': pool_id})
1991
def console_get_all_by_instance(context, instance_id):
1992
session = get_session()
1993
results = session.query(models.Console).\
1994
filter_by(instance_id=instance_id).\
1995
options(joinedload('pool')).\
2000
def console_get(context, console_id, instance_id=None):
2001
session = get_session()
2002
query = session.query(models.Console).\
2003
filter_by(id=console_id)
2005
query = query.filter_by(instance_id=instance_id)
2006
result = query.options(joinedload('pool')).first()
2008
idesc = (_("on instance %s") % instance_id) if instance_id else ""
2009
raise exception.NotFound(_("No console with id %(console_id)s"
2012
'console_id': console_id})