~morita-kazutaka/nova/clone-volume

« back to all changes in this revision

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

  • Committer: MORITA Kazutaka
  • Date: 2011-05-13 12:32:05 UTC
  • mfrom: (1005.1.1 snapshot-volume)
  • Revision ID: morita.kazutaka@gmail.com-20110513123205-8cfzxtrw9uw5a844
MergeĀ lp:~morita-kazutaka/nova/snapshot-volume

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
 
50
50
LOG = logging.getLogger("nova.api.cloud")
51
51
 
52
 
InvalidInputException = exception.InvalidInputException
53
 
 
54
52
 
55
53
def _gen_key(context, user_id, key_name):
56
54
    """Generate a key
61
59
    #             creation before creating key_pair
62
60
    try:
63
61
        db.key_pair_get(context, user_id, key_name)
64
 
        raise exception.Duplicate(_("The key_pair %s already exists")
65
 
                                  % key_name)
 
62
        raise exception.KeyPairExists(key_name=key_name)
66
63
    except exception.NotFound:
67
64
        pass
68
65
    private_key, public_key, fingerprint = crypto.generate_key_pair()
159
156
        floating_ip = db.instance_get_floating_address(ctxt,
160
157
                                                       instance_ref['id'])
161
158
        ec2_id = ec2utils.id_to_ec2_id(instance_ref['id'])
162
 
        image_ec2_id = self._image_ec2_id(instance_ref['image_id'], 'ami')
 
159
        image_ec2_id = self.image_ec2_id(instance_ref['image_id'])
163
160
        data = {
164
161
            'user-data': base64.b64decode(instance_ref['user_data']),
165
162
            'meta-data': {
187
184
                'mpi': mpi}}
188
185
 
189
186
        for image_type in ['kernel', 'ramdisk']:
190
 
            if '%s_id' % image_type in instance_ref:
191
 
                ec2_id = self._image_ec2_id(instance_ref['%s_id' % image_type],
192
 
                                            self._image_type(image_type))
 
187
            if instance_ref.get('%s_id' % image_type):
 
188
                ec2_id = self.image_ec2_id(instance_ref['%s_id' % image_type],
 
189
                                           self._image_type(image_type))
193
190
                data['meta-data']['%s-id' % image_type] = ec2_id
194
191
 
195
192
        if False:  # TODO(vish): store ancestor ids
431
428
            ip_protocol = str(ip_protocol)
432
429
 
433
430
            if ip_protocol.upper() not in ['TCP', 'UDP', 'ICMP']:
434
 
                raise InvalidInputException(_('%s is not a valid ipProtocol') %
435
 
                                            (ip_protocol,))
 
431
                raise exception.InvalidIpProtocol(protocol=ip_protocol)
436
432
            if ((min(from_port, to_port) < -1) or
437
433
                (max(from_port, to_port) > 65535)):
438
 
                raise InvalidInputException(_('Invalid port range'))
 
434
                raise exception.InvalidPortRange(from_port=from_port,
 
435
                                                 to_port=to_port)
439
436
 
440
437
            values['protocol'] = ip_protocol
441
438
            values['from_port'] = from_port
657
654
        # TODO(vish): Instance should be None at db layer instead of
658
655
        #             trying to lazy load, but for now we turn it into
659
656
        #             a dict to avoid an error.
660
 
        return {'volumeSet': [self._format_volume(context, dict(volume))]}
 
657
        return self._format_volume(context, dict(volume))
661
658
 
662
659
    def delete_volume(self, context, volume_id, **kwargs):
663
660
        volume_id = ec2utils.ec2_id_to_id(volume_id)
747
744
            instances = self.compute_api.get_all(context, **kwargs)
748
745
        for instance in instances:
749
746
            if not context.is_admin:
750
 
                if instance['image_id'] == FLAGS.vpn_image_id:
 
747
                if instance['image_id'] == str(FLAGS.vpn_image_id):
751
748
                    continue
752
749
            i = {}
753
750
            instance_id = instance['id']
754
751
            ec2_id = ec2utils.id_to_ec2_id(instance_id)
755
752
            i['instanceId'] = ec2_id
756
 
            i['imageId'] = self._image_ec2_id(instance['image_id'])
 
753
            i['imageId'] = self.image_ec2_id(instance['image_id'])
757
754
            i['instanceState'] = {
758
755
                'code': instance['state'],
759
756
                'name': instance['state_description']}
770
767
                        instance['mac_address'])
771
768
 
772
769
            i['privateDnsName'] = fixed_addr
 
770
            i['privateIpAddress'] = fixed_addr
773
771
            i['publicDnsName'] = floating_addr
 
772
            i['ipAddress'] = floating_addr or fixed_addr
774
773
            i['dnsName'] = i['publicDnsName'] or i['privateDnsName']
775
774
            i['keyName'] = instance['key_name']
776
775
 
942
941
        return image_type
943
942
 
944
943
    @staticmethod
945
 
    def _image_ec2_id(image_id, image_type='ami'):
 
944
    def image_ec2_id(image_id, image_type='ami'):
946
945
        """Returns image ec2_id using id and three letter type."""
947
946
        template = image_type + '-%08x'
948
947
        return ec2utils.id_to_ec2_id(int(image_id), template=template)
951
950
        try:
952
951
            internal_id = ec2utils.ec2_id_to_id(ec2_id)
953
952
            return self.image_service.show(context, internal_id)
954
 
        except exception.NotFound:
 
953
        except (exception.InvalidEc2Id, exception.ImageNotFound):
955
954
            try:
956
955
                return self.image_service.show_by_name(context, ec2_id)
957
956
            except exception.NotFound:
958
 
                raise exception.NotFound(_('Image %s not found') % ec2_id)
 
957
                raise exception.ImageNotFound(image_id=ec2_id)
959
958
 
960
959
    def _format_image(self, image):
961
960
        """Convert from format defined by BaseImageService to S3 format."""
962
961
        i = {}
963
962
        image_type = self._image_type(image.get('container_format'))
964
 
        ec2_id = self._image_ec2_id(image.get('id'), image_type)
 
963
        ec2_id = self.image_ec2_id(image.get('id'), image_type)
965
964
        name = image.get('name')
966
965
        i['imageId'] = ec2_id
967
966
        kernel_id = image['properties'].get('kernel_id')
968
967
        if kernel_id:
969
 
            i['kernelId'] = self._image_ec2_id(kernel_id, 'aki')
 
968
            i['kernelId'] = self.image_ec2_id(kernel_id, 'aki')
970
969
        ramdisk_id = image['properties'].get('ramdisk_id')
971
970
        if ramdisk_id:
972
 
            i['ramdiskId'] = self._image_ec2_id(ramdisk_id, 'ari')
 
971
            i['ramdiskId'] = self.image_ec2_id(ramdisk_id, 'ari')
973
972
        i['imageOwnerId'] = image['properties'].get('owner_id')
974
973
        if name:
975
974
            i['imageLocation'] = "%s (%s)" % (image['properties'].
999
998
                try:
1000
999
                    image = self._get_image(context, ec2_id)
1001
1000
                except exception.NotFound:
1002
 
                    raise exception.NotFound(_('Image %s not found') %
1003
 
                                             ec2_id)
 
1001
                    raise exception.ImageNotFound(image_id=ec2_id)
1004
1002
                images.append(image)
1005
1003
        else:
1006
1004
            images = self.image_service.detail(context)
1020
1018
        metadata = {'properties': {'image_location': image_location}}
1021
1019
        image = self.image_service.create(context, metadata)
1022
1020
        image_type = self._image_type(image.get('container_format'))
1023
 
        image_id = self._image_ec2_id(image['id'],
1024
 
                                      image_type)
 
1021
        image_id = self.image_ec2_id(image['id'],
 
1022
                                     image_type)
1025
1023
        msg = _("Registered image %(image_location)s with"
1026
1024
                " id %(image_id)s") % locals()
1027
1025
        LOG.audit(msg, context=context)
1034
1032
        try:
1035
1033
            image = self._get_image(context, image_id)
1036
1034
        except exception.NotFound:
1037
 
            raise exception.NotFound(_('Image %s not found') % image_id)
 
1035
            raise exception.ImageNotFound(image_id=image_id)
1038
1036
        result = {'imageId': image_id, 'launchPermission': []}
1039
1037
        if image['is_public']:
1040
1038
            result['launchPermission'].append({'group': 'all'})
1057
1055
        try:
1058
1056
            image = self._get_image(context, image_id)
1059
1057
        except exception.NotFound:
1060
 
            raise exception.NotFound(_('Image %s not found') % image_id)
 
1058
            raise exception.ImageNotFound(image_id=image_id)
1061
1059
        internal_id = image['id']
1062
1060
        del(image['id'])
1063
1061