~soren/nova/lp658257

« back to all changes in this revision

Viewing changes to nova/api/ec2/cloud.py

  • Committer: Tarmac
  • Author(s): Michael Gundlach
  • Date: 2010-10-05 18:58:37 UTC
  • mfrom: (295.1.22 servers_api)
  • Revision ID: hudson@openstack.org-20101005185837-wb2sq7zy5swxa9q7
Replace model.Instance.ec2_id with an integer internal_id so that both APIs can represent the ID to external users.

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
    return {'private_key': private_key, 'fingerprint': fingerprint}
73
73
 
74
74
 
 
75
def ec2_id_to_internal_id(ec2_id):
 
76
    """Convert an ec2 ID (i-[base 36 number]) to an internal id (int)"""
 
77
    return int(ec2_id[2:], 36)
 
78
 
 
79
 
 
80
def internal_id_to_ec2_id(internal_id):
 
81
    """Convert an internal ID (int) to an ec2 ID (i-[base 36 number])"""
 
82
    digits = []
 
83
    while internal_id != 0:
 
84
        internal_id, remainder = divmod(internal_id, 36)
 
85
        digits.append('0123456789abcdefghijklmnopqrstuvwxyz'[remainder])
 
86
    return "i-%s" % ''.join(reversed(digits))
 
87
 
 
88
 
75
89
class CloudController(object):
76
90
    """ CloudController provides the critical dispatch between
77
91
 inbound API calls through the endpoint and messages
144
158
                },
145
159
                'hostname': hostname,
146
160
                'instance-action': 'none',
147
 
                'instance-id': instance_ref['ec2_id'],
 
161
                'instance-id': internal_id_to_ec2_id(instance_ref['internal_id']),
148
162
                'instance-type': instance_ref['instance_type'],
149
163
                'local-hostname': hostname,
150
164
                'local-ipv4': address,
244
258
    def delete_security_group(self, context, group_name, **kwargs):
245
259
        return True
246
260
 
247
 
    def get_console_output(self, context, instance_id, **kwargs):
248
 
        # instance_id is passed in as a list of instances
249
 
        instance_ref = db.instance_get_by_ec2_id(context, instance_id[0])
 
261
    def get_console_output(self, context, ec2_id_list, **kwargs):
 
262
        # ec2_id_list is passed in as a list of instances
 
263
        ec2_id = ec2_id_list[0]
 
264
        internal_id = ec2_id_to_internal_id(ec2_id)
 
265
        instance_ref = db.instance_get_by_ec2_id(context, internal_id)
250
266
        return rpc.call('%s.%s' % (FLAGS.compute_topic,
251
267
                                   instance_ref['host']),
252
268
                        {"method": "get_console_output",
326
342
            raise exception.ApiError("Volume status must be available")
327
343
        if volume_ref['attach_status'] == "attached":
328
344
            raise exception.ApiError("Volume is already attached")
329
 
        instance_ref = db.instance_get_by_ec2_id(context, instance_id)
 
345
        internal_id = ec2_id_to_internal_id(instance_id)
 
346
        instance_ref = db.instance_get_by_internal_id(context, internal_id)
330
347
        host = instance_ref['host']
331
348
        rpc.cast(db.queue_get_for(context, FLAGS.compute_topic, host),
332
349
                                {"method": "attach_volume",
360
377
            # If the instance doesn't exist anymore,
361
378
            # then we need to call detach blind
362
379
            db.volume_detached(context)
 
380
        internal_id = instance_ref['internal_id']
 
381
        ec2_id = internal_id_to_ec2_id(internal_id)
363
382
        return {'attachTime': volume_ref['attach_time'],
364
383
                'device': volume_ref['mountpoint'],
365
 
                'instanceId': instance_ref['ec2_id'],
 
384
                'instanceId': internal_id,
366
385
                'requestId': context.request_id,
367
386
                'status': volume_ref['attach_status'],
368
387
                'volumeId': volume_ref['id']}
411
430
                if instance['image_id'] == FLAGS.vpn_image_id:
412
431
                    continue
413
432
            i = {}
414
 
            i['instanceId'] = instance['ec2_id']
 
433
            internal_id = instance['internal_id']
 
434
            ec2_id = internal_id_to_ec2_id(internal_id)
 
435
            i['instanceId'] = ec2_id
415
436
            i['imageId'] = instance['image_id']
416
437
            i['instanceState'] = {
417
438
                'code': instance['state'],
464
485
            instance_id = None
465
486
            if (floating_ip_ref['fixed_ip']
466
487
                and floating_ip_ref['fixed_ip']['instance']):
467
 
                instance_id = floating_ip_ref['fixed_ip']['instance']['ec2_id']
 
488
                internal_id = floating_ip_ref['fixed_ip']['instance']['ec2_id']
 
489
                ec2_id = internal_id_to_ec2_id(internal_id)
468
490
            address_rv = {'public_ip': address,
469
 
                          'instance_id': instance_id}
 
491
                          'instance_id': ec2_id}
470
492
            if context.user.is_admin():
471
493
                details = "%s (%s)" % (address_rv['instance_id'],
472
494
                                       floating_ip_ref['project_id'])
498
520
                           "floating_address": floating_ip_ref['address']}})
499
521
        return {'releaseResponse': ["Address released."]}
500
522
 
501
 
    def associate_address(self, context, instance_id, public_ip, **kwargs):
502
 
        instance_ref = db.instance_get_by_ec2_id(context, instance_id)
 
523
    def associate_address(self, context, ec2_id, public_ip, **kwargs):
 
524
        internal_id = ec2_id_to_internal_id(ec2_id)
 
525
        instance_ref = db.instance_get_by_internal_id(context, internal_id)
503
526
        fixed_address = db.instance_get_fixed_address(context,
504
527
                                                      instance_ref['id'])
505
528
        floating_ip_ref = db.floating_ip_get_by_address(context, public_ip)
610
633
            inst = {}
611
634
            inst['mac_address'] = utils.generate_mac()
612
635
            inst['launch_index'] = num
613
 
            inst['hostname'] = instance_ref['ec2_id']
 
636
            internal_id = instance_ref['internal_id']
 
637
            ec2_id = internal_id_to_ec2_id(internal_id)
 
638
            inst['hostname'] = ec2_id
614
639
            db.instance_update(context, inst_id, inst)
615
640
            address = self.network_manager.allocate_fixed_ip(context,
616
641
                                                             inst_id,
634
659
        return self._format_run_instances(context, reservation_id)
635
660
 
636
661
 
637
 
    def terminate_instances(self, context, instance_id, **kwargs):
 
662
    def terminate_instances(self, context, ec2_id_list, **kwargs):
638
663
        logging.debug("Going to start terminating instances")
639
 
        for id_str in instance_id:
 
664
        for id_str in ec2_id_list:
 
665
            internal_id = ec2_id_to_internal_id(id_str)
640
666
            logging.debug("Going to try and terminate %s" % id_str)
641
667
            try:
642
 
                instance_ref = db.instance_get_by_ec2_id(context, id_str)
 
668
                instance_ref = db.instance_get_by_internal_id(context, 
 
669
                                                              internal_id)
643
670
            except exception.NotFound:
644
671
                logging.warning("Instance %s was not found during terminate"
645
672
                                % id_str)
688
715
            cloud.reboot(id_str, context=context)
689
716
        return True
690
717
 
691
 
    def update_instance(self, context, instance_id, **kwargs):
 
718
    def update_instance(self, context, ec2_id, **kwargs):
692
719
        updatable_fields = ['display_name', 'display_description']
693
720
        changes = {}
694
721
        for field in updatable_fields:
696
723
                changes[field] = kwargs[field]
697
724
        if changes:
698
725
            db_context = {}
699
 
            inst = db.instance_get_by_ec2_id(db_context, instance_id)
 
726
            internal_id = ec2_id_to_internal_id(ec2_id)
 
727
            inst = db.instance_get_by_internal_id(db_context, internal_id)
700
728
            db.instance_update(db_context, inst['id'], kwargs)
701
729
        return True
702
730