~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/db/sqlalchemy/api.py

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-01-21 11:48:06 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20110121114806-v8fvnnl6az4m4ohv
Tags: upstream-2011.1~bzr597
ImportĀ upstreamĀ versionĀ 2011.1~bzr597

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
Implementation of SQLAlchemy backend.
20
20
"""
21
21
 
22
 
import random
23
22
import warnings
24
23
 
25
24
from nova import db
41
40
def is_admin_context(context):
42
41
    """Indicates if the request context is an administrator."""
43
42
    if not context:
44
 
        warnings.warn('Use of empty request context is deprecated',
 
43
        warnings.warn(_('Use of empty request context is deprecated'),
45
44
                      DeprecationWarning)
46
45
        raise Exception('die')
47
46
    return context.is_admin
130
129
                     first()
131
130
 
132
131
    if not result:
133
 
        raise exception.NotFound('No service for id %s' % service_id)
134
 
 
 
132
        raise exception.NotFound(_('No service for id %s') % service_id)
 
133
 
 
134
    return result
 
135
 
 
136
 
 
137
@require_admin_context
 
138
def service_get_all(context, session=None, disabled=False):
 
139
    if not session:
 
140
        session = get_session()
 
141
 
 
142
    result = session.query(models.Service).\
 
143
                   filter_by(deleted=can_read_deleted(context)).\
 
144
                   filter_by(disabled=disabled).\
 
145
                   all()
135
146
    return result
136
147
 
137
148
 
146
157
 
147
158
 
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).\
 
165
                   all()
 
166
 
 
167
 
 
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)).\
227
247
                     filter_by(deleted=can_read_deleted(context)).\
228
248
                     first()
229
249
    if not result:
230
 
        raise exception.NotFound('No service for %s, %s' % (host, binary))
 
250
        raise exception.NotFound(_('No service for %s, %s') % (host, binary))
231
251
 
232
252
    return result
233
253
 
236
256
def service_create(context, values):
237
257
    service_ref = models.Service()
238
258
    service_ref.update(values)
 
259
    if not FLAGS.enable_new_services:
 
260
        service_ref.disabled = True
239
261
    service_ref.save()
240
262
    return service_ref
241
263
 
252
274
###################
253
275
 
254
276
 
 
277
@require_admin_context
 
278
def certificate_get(context, certificate_id, session=None):
 
279
    if not session:
 
280
        session = get_session()
 
281
 
 
282
    result = session.query(models.Certificate).\
 
283
                     filter_by(id=certificate_id).\
 
284
                     filter_by(deleted=can_read_deleted(context)).\
 
285
                     first()
 
286
 
 
287
    if not result:
 
288
        raise exception.NotFound('No certificate for id %s' % certificate_id)
 
289
 
 
290
    return result
 
291
 
 
292
 
 
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
 
300
 
 
301
 
 
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,
 
307
                                          certificate_id,
 
308
                                          session=session)
 
309
        certificate_ref.delete(session=session)
 
310
 
 
311
 
 
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).\
 
318
                   all()
 
319
 
 
320
 
 
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).\
 
327
                   all()
 
328
 
 
329
 
 
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).\
 
337
                   all()
 
338
 
 
339
 
 
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,
 
345
                                          certificate_id,
 
346
                                          session=session)
 
347
        for (key, value) in values.iteritems():
 
348
            certificate_ref[key] = value
 
349
        certificate_ref.save(session=session)
 
350
 
 
351
 
 
352
###################
 
353
 
 
354
 
255
355
@require_context
256
356
def floating_ip_allocate_address(context, host, project_id):
257
357
    authorize_project_context(context, project_id)
385
485
        session = get_session()
386
486
 
387
487
    result = session.query(models.FloatingIp).\
 
488
                   options(joinedload_all('fixed_ip.network')).\
388
489
                     filter_by(address=address).\
389
490
                     filter_by(deleted=can_read_deleted(context)).\
390
491
                     first()
491
592
                     options(joinedload('instance')).\
492
593
                     first()
493
594
    if not result:
494
 
        raise exception.NotFound('No floating ip for address %s' % address)
 
595
        raise exception.NotFound(_('No floating ip for address %s') % address)
495
596
 
496
597
    if is_user_context(context):
497
598
        authorize_project_context(context, result.instance.project_id)
505
606
    return fixed_ip_ref.instance
506
607
 
507
608
 
 
609
@require_context
 
610
def fixed_ip_get_instance_v6(context, address):
 
611
    session = get_session()
 
612
    mac = utils.to_mac(address)
 
613
 
 
614
    result = session.query(models.Instance).\
 
615
                     filter_by(mac_address=mac).\
 
616
                     first()
 
617
    return result
 
618
 
 
619
 
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
###################
526
638
 
527
639
 
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.
531
640
@require_context
532
641
def instance_create(context, values):
533
642
    """Create a new Instance record in the database.
534
643
 
535
644
    context - request context object
536
645
    values - dict containing column values.
537
 
             'internal_id' is auto-generated and should not be specified.
538
646
    """
539
647
    instance_ref = models.Instance()
540
648
    instance_ref.update(values)
541
649
 
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,
548
 
                                               session=session):
549
 
                instance_ref.internal_id = internal_id
550
652
        instance_ref.save(session=session)
551
653
    return instance_ref
552
654
 
579
681
 
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)).\
585
689
                         first()
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).\
592
698
                         first()
593
699
    if not result:
594
 
        raise exception.NotFound('No instance for id %s' % instance_id)
 
700
        raise exception.NotFound(_('No instance for id %s') % instance_id)
595
701
 
596
702
    return result
597
703
 
651
757
                       all()
652
758
 
653
759
 
 
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)).\
 
769
                   first()
 
770
 
 
771
 
654
772
@require_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()
657
775
 
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)).\
663
783
                         first()
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).\
670
792
                         first()
671
793
    if not result:
672
 
        raise exception.NotFound('Instance %s not found' % (internal_id))
 
794
        raise exception.NotFound(_('Instance %s not found') % (instance_id))
673
795
 
674
796
    return result
675
797
 
676
798
 
677
799
@require_context
678
 
def instance_internal_id_exists(context, internal_id, session=None):
679
 
    if not session:
680
 
        session = get_session()
681
 
    return session.query(exists().\
682
 
                         where(models.Instance.internal_id == internal_id)).\
683
 
                   one()[0]
684
 
 
685
 
 
686
 
@require_context
687
800
def instance_get_fixed_address(context, instance_id):
688
801
    session = get_session()
689
802
    with session.begin():
694
807
 
695
808
 
696
809
@require_context
 
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)
 
818
 
 
819
 
 
820
@require_context
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)
748
872
 
749
873
 
 
874
@require_context
 
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)
 
879
 
 
880
    session = get_session()
 
881
    with session.begin():
 
882
        action_ref.save(session=session)
 
883
    return action_ref
 
884
 
 
885
 
 
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).\
 
892
        all()
 
893
 
 
894
 
750
895
###################
751
896
 
752
897
 
790
935
                     filter_by(deleted=can_read_deleted(context)).\
791
936
                     first()
792
937
    if not result:
793
 
        raise exception.NotFound('no keypair for user %s, name %s' %
 
938
        raise exception.NotFound(_('no keypair for user %s, name %s') %
794
939
                                 (user_id, name))
795
940
    return result
796
941
 
905
1050
                         filter_by(deleted=False).\
906
1051
                         first()
907
1052
    if not result:
908
 
        raise exception.NotFound('No network for id %s' % network_id)
 
1053
        raise exception.NotFound(_('No network for id %s') % network_id)
909
1054
 
910
1055
    return result
911
1056
 
913
1058
# NOTE(vish): pylint complains because of the long method name, but
914
1059
#             it fits with the names of the rest of the methods
915
1060
# pylint: disable-msg=C0103
 
1061
 
 
1062
 
916
1063
@require_admin_context
917
1064
def network_get_associated_fixed_ips(context, network_id):
918
1065
    session = get_session()
933
1080
                 first()
934
1081
 
935
1082
    if not result:
936
 
        raise exception.NotFound('No network for bridge %s' % bridge)
 
1083
        raise exception.NotFound(_('No network for bridge %s') % bridge)
937
1084
    return result
938
1085
 
939
1086
 
947
1094
                 filter_by(deleted=False).\
948
1095
                 first()
949
1096
    if not rv:
950
 
        raise exception.NotFound('No network for instance %s' % instance_id)
 
1097
        raise exception.NotFound(_('No network for instance %s') % instance_id)
951
1098
    return rv
952
1099
 
953
1100
 
961
1108
                              with_lockmode('update').\
962
1109
                              first()
963
1110
        if not network_ref:
964
 
            raise exception.NotFound('No network for id %s' % network_id)
 
1111
            raise exception.NotFound(_('No network for id %s') % network_id)
965
1112
 
966
1113
        # NOTE(vish): if with_lockmode isn't supported, as in sqlite,
967
1114
        #             then this has concurrency issues
985
1132
 
986
1133
 
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).\
993
 
                 first()
994
 
    if not rv:
 
1137
    result = session.query(models.Network).\
 
1138
                     filter_by(project_id=project_id).\
 
1139
                     filter_by(deleted=False).\
 
1140
                     first()
 
1141
    if not result:
 
1142
        if not associate:
 
1143
            return None
995
1144
        try:
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).\
1004
 
                         first()
1005
 
    return rv
 
1150
            result = session.query(models.Network).\
 
1151
                             filter_by(project_id=project_id).\
 
1152
                             filter_by(deleted=False).\
 
1153
                             first()
 
1154
    return result
 
1155
 
 
1156
 
 
1157
@require_context
 
1158
def project_get_network_v6(context, project_id):
 
1159
    return project_get_network(context, project_id)
1006
1160
 
1007
1161
 
1008
1162
###################
1062
1216
###################
1063
1217
 
1064
1218
 
 
1219
@require_admin_context
1065
1220
def auth_destroy_token(_context, token):
1066
1221
    session = get_session()
1067
1222
    session.delete(token)
1068
1223
 
1069
1224
 
 
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).\
1074
1230
                  first()
1075
1231
    if not tk:
1076
 
        raise exception.NotFound('Token %s does not exist' % token_hash)
 
1232
        raise exception.NotFound(_('Token %s does not exist') % token_hash)
1077
1233
    return tk
1078
1234
 
1079
1235
 
 
1236
@require_admin_context
1080
1237
def auth_create_token(_context, token):
1081
1238
    tk = models.AuthToken()
1082
1239
    tk.update(token)
1097
1254
                     filter_by(deleted=can_read_deleted(context)).\
1098
1255
                     first()
1099
1256
    if not result:
1100
 
        raise exception.NotFound('No quota for project_id %s' % project_id)
 
1257
        raise exception.NotFound(_('No quota for project_id %s') % project_id)
1101
1258
 
1102
1259
    return result
1103
1260
 
1187
1344
 
1188
1345
    session = get_session()
1189
1346
    with session.begin():
1190
 
        while volume_ref.ec2_id == None:
1191
 
            ec2_id = utils.generate_uid('vol')
1192
 
            if not volume_ec2_id_exists(context, ec2_id, session=session):
1193
 
                volume_ref.ec2_id = ec2_id
1194
1347
        volume_ref.save(session=session)
1195
1348
    return volume_ref
1196
1349
 
1252
1405
                         filter_by(deleted=False).\
1253
1406
                         first()
1254
1407
    if not result:
1255
 
        raise exception.NotFound('No volume for id %s' % volume_id)
 
1408
        raise exception.NotFound(_('No volume for id %s') % volume_id)
1256
1409
 
1257
1410
    return result
1258
1411
 
1288
1441
                   all()
1289
1442
 
1290
1443
 
1291
 
@require_context
1292
 
def volume_get_by_ec2_id(context, ec2_id):
1293
 
    session = get_session()
1294
 
    result = None
1295
 
 
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)).\
1300
 
                         first()
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).\
1306
 
                         first()
1307
 
    else:
1308
 
        raise exception.NotAuthorized()
1309
 
 
1310
 
    if not result:
1311
 
        raise exception.NotFound('Volume %s not found' % ec2_id)
1312
 
 
1313
 
    return result
1314
 
 
1315
 
 
1316
 
@require_context
1317
 
def volume_ec2_id_exists(context, ec2_id, session=None):
1318
 
    if not session:
1319
 
        session = get_session()
1320
 
 
1321
 
    return session.query(exists().\
1322
 
                   where(models.Volume.id == ec2_id)).\
1323
 
                   one()[0]
1324
 
 
1325
 
 
1326
1444
@require_admin_context
1327
1445
def volume_get_instance(context, volume_id):
1328
1446
    session = get_session()
1332
1450
                     options(joinedload('instance')).\
1333
1451
                     first()
1334
1452
    if not result:
1335
 
        raise exception.NotFound('Volume %s not found' % ec2_id)
 
1453
        raise exception.NotFound(_('Volume %s not found') % ec2_id)
1336
1454
 
1337
1455
    return result.instance
1338
1456
 
1344
1462
                     filter_by(volume_id=volume_id).\
1345
1463
                     first()
1346
1464
    if not result:
1347
 
        raise exception.NotFound('No export device found for volume %s' %
 
1465
        raise exception.NotFound(_('No export device found for volume %s') %
1348
1466
                                 volume_id)
1349
1467
 
1350
1468
    return (result.shelf_id, result.blade_id)
1357
1475
                     filter_by(volume_id=volume_id).\
1358
1476
                     first()
1359
1477
    if not result:
1360
 
        raise exception.NotFound('No target id found for volume %s' %
 
1478
        raise exception.NotFound(_('No target id found for volume %s') %
1361
1479
                                 volume_id)
1362
1480
 
1363
1481
    return result.target_num
1402
1520
                         options(joinedload_all('rules')).\
1403
1521
                         first()
1404
1522
    if not result:
1405
 
        raise exception.NotFound("No secuity group with id %s" %
 
1523
        raise exception.NotFound(_("No security group with id %s") %
1406
1524
                                 security_group_id)
1407
1525
    return result
1408
1526
 
1419
1537
                        first()
1420
1538
    if not result:
1421
1539
        raise exception.NotFound(
1422
 
            'No security group named %s for project: %s' \
 
1540
            _('No security group named %s for project: %s')
1423
1541
             % (group_name, project_id))
1424
1542
    return result
1425
1543
 
1507
1625
                         filter_by(id=security_group_rule_id).\
1508
1626
                         first()
1509
1627
    if not result:
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)
1512
1630
    return result
1513
1631
 
1514
1632
 
1515
1633
@require_context
 
1634
def security_group_rule_get_by_security_group(context, security_group_id,
 
1635
                                              session=None):
 
1636
    if not session:
 
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).\
 
1642
                         all()
 
1643
    else:
 
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).\
 
1648
                         all()
 
1649
    return result
 
1650
 
 
1651
 
 
1652
@require_context
 
1653
def security_group_rule_get_by_security_group_grantee(context,
 
1654
                                                      security_group_id,
 
1655
                                                      session=None):
 
1656
    if not session:
 
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).\
 
1662
                         all()
 
1663
    else:
 
1664
        result = session.query(models.SecurityGroupIngressRule).\
 
1665
                         filter_by(deleted=False).\
 
1666
                         filter_by(group_id=security_group_id).\
 
1667
                         all()
 
1668
    return result
 
1669
 
 
1670
 
 
1671
@require_context
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)
1543
1699
                     first()
1544
1700
 
1545
1701
    if not result:
1546
 
        raise exception.NotFound('No user for id %s' % id)
 
1702
        raise exception.NotFound(_('No user for id %s') % id)
1547
1703
 
1548
1704
    return result
1549
1705
 
1559
1715
                   first()
1560
1716
 
1561
1717
    if not result:
1562
 
        raise exception.NotFound('No user for access key %s' % access_key)
 
1718
        raise exception.NotFound(_('No user for access key %s') % access_key)
1563
1719
 
1564
1720
    return result
1565
1721
 
1621
1777
                     first()
1622
1778
 
1623
1779
    if not result:
1624
 
        raise exception.NotFound("No project with id %s" % id)
 
1780
        raise exception.NotFound(_("No project with id %s") % id)
1625
1781
 
1626
1782
    return result
1627
1783
 
1747
1903
                       filter_by(deleted=False).\
1748
1904
                       filter_by(host=host).\
1749
1905
                       all()
 
1906
 
 
1907
 
 
1908
##################
 
1909
 
 
1910
 
 
1911
def console_pool_create(context, values):
 
1912
    pool = models.ConsolePool()
 
1913
    pool.update(values)
 
1914
    pool.save()
 
1915
    return pool
 
1916
 
 
1917
 
 
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).\
 
1923
                     first()
 
1924
    if not result:
 
1925
        raise exception.NotFound(_("No console pool with id %(pool_id)s") %
 
1926
                                 {'pool_id': pool_id})
 
1927
 
 
1928
    return result
 
1929
 
 
1930
 
 
1931
def console_pool_get_by_host_type(context, compute_host, host,
 
1932
                                  console_type):
 
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')).\
 
1940
                   first()
 
1941
    if not result:
 
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,
 
1947
                                    'host': host})
 
1948
    return result
 
1949
 
 
1950
 
 
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')).\
 
1958
                   all()
 
1959
 
 
1960
 
 
1961
def console_create(context, values):
 
1962
    console = models.Console()
 
1963
    console.update(values)
 
1964
    console.save()
 
1965
    return console
 
1966
 
 
1967
 
 
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})
 
1974
 
 
1975
 
 
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')).\
 
1982
                   first()
 
1983
    if not result:
 
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})
 
1988
    return result
 
1989
 
 
1990
 
 
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')).\
 
1996
                   all()
 
1997
    return results
 
1998
 
 
1999
 
 
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)
 
2004
    if instance_id:
 
2005
        query = query.filter_by(instance_id=instance_id)
 
2006
    result = query.options(joinedload('pool')).first()
 
2007
    if not result:
 
2008
        idesc = (_("on instance %s") % instance_id)  if instance_id else ""
 
2009
        raise exception.NotFound(_("No console with id %(console_id)s"
 
2010
                                   " %(instance)s") %
 
2011
                                  {'instance': idesc,
 
2012
                                  'console_id': console_id})
 
2013
    return result