Ejemplo n.º 1
0
def _cloud_format_block_device_mapping(context,
                                       bdm,
                                       root_device_name=None,
                                       snapshot_ids=None,
                                       project_id=None):
    """Construct BlockDeviceMappingItemType."""
    keys = (('deviceName', 'device_name'), ('virtualName', 'virtual_name'))
    item = {name: bdm[k] for name, k in keys if k in bdm}
    if bdm.get('no_device'):
        item['noDevice'] = True
    if bdm.get('boot_index') == 0 and root_device_name:
        item['deviceName'] = root_device_name
    if ('snapshot_id' in bdm) or ('volume_id' in bdm):
        ebs_keys = (('volumeSize', 'volume_size'), ('deleteOnTermination',
                                                    'delete_on_termination'))
        ebs = {name: bdm[k] for name, k in ebs_keys if bdm.get(k) is not None}
        if bdm.get('snapshot_id'):
            ebs['snapshotId'] = ec2utils.os_id_to_ec2_id(
                context,
                'snap',
                bdm['snapshot_id'],
                ids_by_os_id=snapshot_ids,
                project_id=project_id)
        # NOTE(ft): Openstack extension, AWS-incompability
        elif bdm.get('volume_id'):
            ebs['snapshotId'] = ec2utils.os_id_to_ec2_id(context,
                                                         'vol',
                                                         bdm['volume_id'],
                                                         project_id=project_id)
        assert 'snapshotId' in ebs
        item['ebs'] = ebs
    return item
Ejemplo n.º 2
0
def _format_volume_mapping(context, bdm, snapshot_ids=None, project_id=None):
    ebs = {'deleteOnTermination': bdm['delete_on_termination']}
    # TODO(ft): set default volumeSize from the source
    if bdm.get('volume_size') is not None:
        ebs['volumeSize'] = bdm['volume_size']
    if bdm.get('source_type') == 'snapshot':
        if bdm.get('snapshot_id'):
            ebs['snapshotId'] = ec2utils.os_id_to_ec2_id(
                context,
                'snap',
                bdm['snapshot_id'],
                ids_by_os_id=snapshot_ids,
                project_id=project_id)
    # NOTE(ft): Openstack extension, AWS-incompability
    elif bdm.get('source_type') == 'volume':
        if bdm.get('volume_id'):
            ebs['snapshotId'] = ec2utils.os_id_to_ec2_id(context,
                                                         'vol',
                                                         bdm['volume_id'],
                                                         project_id=project_id)
    # NOTE(ft): extension, AWS-incompability
    elif bdm.get('source_type') == 'image':
        if bdm.get('image_id'):
            ebs['snapshotId'] = ec2utils.os_id_to_ec2_id(
                context, 'ami', bdm['image_id'])
    if ebs.get('snapshotId') or bdm.get('source_type') == 'blank':
        return ebs
Ejemplo n.º 3
0
 def auto_update_db(self, image, os_image):
     if not image:
         kind = _get_os_image_kind(os_image)
         if self.context.project_id == os_image.owner:
             if os_image.properties.get('ec2_id') in self.pending_images:
                 # NOTE(ft): the image is being creating, Glance had created
                 # image, but creating thread doesn't yet update db item
                 image = self.pending_images[os_image.properties['ec2_id']]
                 image['os_id'] = os_image.id
                 image['is_public'] = os_image.is_public
                 db_api.update_item(self.context, image)
             else:
                 image = ec2utils.get_db_item_by_os_id(
                     self.context, kind, os_image.id, self.items_dict,
                     os_image=os_image)
         else:
             image_id = ec2utils.os_id_to_ec2_id(
                 self.context, kind, os_image.id,
                 items_by_os_id=self.items_dict, ids_by_os_id=self.ids_dict)
             image = {'id': image_id,
                      'os_id': os_image.id}
     elif (self.context.project_id == os_image.owner and
             image.get('is_public') != os_image.is_public):
         image['is_public'] = os_image.is_public
         if image['id'] in self.local_images_os_ids:
             db_api.update_item(self.context, image)
         else:
             # TODO(ft): currently update_item can not update id mapping,
             # because its project_id is None. Instead of upgrade db_api,
             # we use add_item. But its execution leads to a needless
             # DB call. This should be reworked in the future.
             kind = ec2utils.get_ec2_id_kind(image['id'])
             db_api.add_item(self.context, kind, image)
     return image
Ejemplo n.º 4
0
def change_os_id_to_ec2_id(context,text,object_name,kind):
    try:
        root=xml_tree.fromstring(text)
    except xml_tree.ParseError:
        return text
    temp=root.tag.split('}')
    ns=temp[0]
    ns=ns+"}"
    if ns!="{"+CONF.sbs_name_space+"}":
        LOG.debug("Found different namespace for sbs api response. Not processing.")
        return text
    # the namespace and CONF.sbs_name_space should be exactly similar.
    # or else this will fail. This code was written assuming they will be same.
    for elem in root.iter(ns+object_name):
        elem_text = elem.text
        split_id = elem_text.split(ns)
        os_instance_id = split_id[0]
        instance_ec2_id = ec2utils.os_id_to_ec2_id(
                       context,kind,os_instance_id,
                      project_id=context.project_id)

        if instance_ec2_id is None:
            continue
        elem.text=str(instance_ec2_id)
    text=xml_tree.tostring(root)
    return text
Ejemplo n.º 5
0
 def auto_update_db(self, image, os_image):
     if not image:
         kind = _get_os_image_kind(os_image)
         if self.context.project_id == os_image.owner:
             image = ec2utils.get_db_item_by_os_id(self.context,
                                                   kind,
                                                   os_image.id,
                                                   self.items_dict,
                                                   os_image=os_image)
         else:
             image_id = ec2utils.os_id_to_ec2_id(
                 self.context,
                 kind,
                 os_image.id,
                 items_by_os_id=self.items_dict,
                 ids_by_os_id=self.ids_dict)
             image = {'id': image_id, 'os_id': os_image.id}
     elif (self.context.project_id == os_image.owner
           and image.get('is_public') != os_image.is_public):
         image['is_public'] = os_image.is_public
         if image['id'] in self.local_images_os_ids:
             db_api.update_item(self.context, image)
         else:
             # TODO(ft): currently update_item can not update id mapping,
             # because its project_id is None. Instead of upgrade db_api,
             # we use add_item. But its execution leads to a needless
             # DB call. This should be reworked in the future.
             kind = ec2utils.get_ec2_id_kind(image['id'])
             db_api.add_item(self.context, kind, image)
     return image
Ejemplo n.º 6
0
def format_delete_on_termination_response(response, context):
    if 'instance' in response._info:
        instance_ec2_id = ec2utils.os_id_to_ec2_id(
            context,
            'i',
            response._info['instance'],
            project_id=context.project_id)
        response._info.pop('instance')
        response._info['instanceId'] = instance_ec2_id
    if 'id' in response._info:
        volume_id = response._info.pop('id')
        response._info['volumeId'] = volume_id
Ejemplo n.º 7
0
def _format_volume_mapping(context, bdm, snapshot_ids=None, project_id=None):
    ebs = {'deleteOnTermination': bdm['delete_on_termination']}
    # TODO(ft): set default volumeSize from the source
    if bdm.get('volume_size') is not None:
        ebs['volumeSize'] = bdm['volume_size']
    if bdm.get('source_type') == 'snapshot':
        if bdm.get('snapshot_id'):
            ebs['snapshotId'] = ec2utils.os_id_to_ec2_id(
                context, 'snap', bdm['snapshot_id'],
                ids_by_os_id=snapshot_ids, project_id=project_id)
    # NOTE(ft): Openstack extension, AWS-incompability
    elif bdm.get('source_type') == 'volume':
        if bdm.get('volume_id'):
            ebs['snapshotId'] = ec2utils.os_id_to_ec2_id(
                context, 'vol', bdm['volume_id'], project_id=project_id)
    # NOTE(ft): extension, AWS-incompability
    elif bdm.get('source_type') == 'image':
        if bdm.get('image_id'):
            ebs['snapshotId'] = ec2utils.os_id_to_ec2_id(
                context, 'ami', bdm['image_id'])
    if ebs.get('snapshotId') or bdm.get('source_type') == 'blank':
        return ebs
Ejemplo n.º 8
0
def _get_ec2_instance_and_reservation(context, os_instance_id):
    instance_id = ec2utils.os_id_to_ec2_id(context, 'i', os_instance_id)
    try:
        ec2_reservations = instance_api.describe_instances(
                context, [instance_id])
    except exception.InvalidInstanceIDNotFound:
        ec2_reservations = instance_api.describe_instances(
                context, filter=[{'name': 'instance-id',
                                  'value': [instance_id]}])
    if (len(ec2_reservations['reservationSet']) != 1 or
            len(ec2_reservations['reservationSet'][0]['instancesSet']) != 1):
        LOG.error(_('Failed to get metadata for instance id: %s'),
                  os_instance_id)
        raise exception.EC2MetadataNotFound()

    ec2_reservation = ec2_reservations['reservationSet'][0]
    ec2_instance = ec2_reservation['instancesSet'][0]

    return ec2_instance, ec2_reservation
Ejemplo n.º 9
0
 def auto_update_db(self, image, os_image):
     if not image:
         kind = _get_os_image_kind(os_image)
         if self.context.project_id == os_image.owner:
             if getattr(os_image, 'ec2_id', None) in self.pending_images:
                 # NOTE(ft): the image is being creating, Glance had created
                 # image, but creating thread doesn't yet update db item
                 image = self.pending_images[os_image.ec2_id]
                 image['os_id'] = os_image.id
                 image['is_public'] = os_image.visibility == 'public'
                 db_api.update_item(self.context, image)
             else:
                 image = ec2utils.get_db_item_by_os_id(self.context,
                                                       kind,
                                                       os_image.id,
                                                       self.items_dict,
                                                       os_image=os_image)
         else:
             image_id = ec2utils.os_id_to_ec2_id(
                 self.context,
                 kind,
                 os_image.id,
                 items_by_os_id=self.items_dict,
                 ids_by_os_id=self.ids_dict)
             image = {'id': image_id, 'os_id': os_image.id}
     elif (self.context.project_id == os_image.owner
           and image.get('is_public') != os_image.visibility == 'public'):
         image['is_public'] = os_image.visibility == 'public'
         if image['id'] in self.local_images_os_ids:
             db_api.update_item(self.context, image)
         else:
             # TODO(ft): currently update_item can not update id mapping,
             # because its project_id is None. Instead of upgrade db_api,
             # we use add_item. But its execution leads to a needless
             # DB call. This should be reworked in the future.
             kind = ec2utils.get_ec2_id_kind(image['id'])
             db_api.add_item(self.context, kind, image)
     return image
Ejemplo n.º 10
0
    def test_os_id_to_ec2_id(self, db_api):
        fake_context = base.create_context()
        fake_id = fakes.random_ec2_id('fake')
        fake_os_id = fakes.random_os_id()

        # no cache, item is found
        db_api.get_items_ids.return_value = [(fake_id, fake_os_id)]
        item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id)
        self.assertEqual(fake_id, item_id)
        db_api.get_items_ids.assert_called_once_with(
            fake_context, 'fake', item_ids=None, item_os_ids=(fake_os_id,))
        self.assertFalse(db_api.add_item_id.called)

        # no cache, item isn't found
        db_api.get_items_ids.return_value = []
        db_api.add_item_id.return_value = fake_id
        item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id)
        self.assertEqual(fake_id, item_id)
        db_api.add_item_id.assert_called_once_with(
            fake_context, 'fake', fake_os_id, None)

        # no item in cache, item isn't found
        db_api.reset_mock()
        ids_cache = {fakes.random_os_id(): fakes.random_ec2_id('fake')}
        item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id,
                                           ids_by_os_id=ids_cache)
        self.assertEqual(fake_id, item_id)
        self.assertIn(fake_os_id, ids_cache)
        self.assertEqual(fake_id, ids_cache[fake_os_id])
        db_api.add_item_id.assert_called_once_with(
            fake_context, 'fake', fake_os_id, None)

        # no item in cache, item is found
        db_api.reset_mock()
        db_api.get_items_ids.return_value = [(fake_id, fake_os_id)]
        ids_cache = {}
        item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id,
                                           ids_by_os_id=ids_cache)
        self.assertEqual(fake_id, item_id)
        self.assertEqual({fake_os_id: fake_id}, ids_cache)
        self.assertFalse(db_api.add_item_id.called)

        # item in cache
        db_api.reset_mock()
        ids_cache = {fake_os_id: fake_id}
        item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id,
                                           ids_by_os_id=ids_cache)
        self.assertEqual(fake_id, item_id)
        self.assertEqual({fake_os_id: fake_id}, ids_cache)
        self.assertFalse(db_api.get_items_ids.called)
        self.assertFalse(db_api.add_item_id.called)

        # item in items dict
        items_dict = {fake_os_id: {'id': fake_id,
                                   'os_id': fake_os_id}}
        ids_cache = {}
        item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id,
                                           items_by_os_id=items_dict,
                                           ids_by_os_id=ids_cache)
        self.assertEqual(fake_id, item_id)
        self.assertFalse(db_api.get_items_ids.called)
        self.assertFalse(db_api.add_item_id.called)
        self.assertEqual({}, ids_cache)

        # item not in items dict, item is found
        items_dict = {fake_os_id: {'id': fake_id,
                                   'os_id': fake_os_id}}
        db_api.get_items_ids.return_value = [(fake_id, fake_os_id)]
        item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id,
                                           items_by_os_id=items_dict)
        self.assertEqual(fake_id, item_id)
        self.assertFalse(db_api.add_item_id.called)
    def test_os_id_to_ec2_id(self, db_api):
        fake_context = mock.Mock(service_catalog=[{'type': 'fake'}])
        fake_id = fakes.random_ec2_id('fake')
        fake_os_id = fakes.random_os_id()

        # no cache, item is found
        db_api.get_items_ids.return_value = [(fake_id, fake_os_id)]
        item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id)
        self.assertEqual(fake_id, item_id)
        db_api.get_items_ids.assert_called_once_with(
            fake_context, 'fake', item_ids=None, item_os_ids=(fake_os_id,))
        self.assertFalse(db_api.add_item_id.called)

        # no cache, item isn't found
        db_api.get_items_ids.return_value = []
        db_api.add_item_id.return_value = fake_id
        item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id)
        self.assertEqual(fake_id, item_id)
        db_api.add_item_id.assert_called_once_with(
            fake_context, 'fake', fake_os_id, project_id=None)

        # no item in cache, item isn't found
        db_api.reset_mock()
        ids_cache = {fakes.random_os_id(): fakes.random_ec2_id('fake')}
        item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id,
                                           ids_by_os_id=ids_cache)
        self.assertEqual(fake_id, item_id)
        self.assertIn(fake_os_id, ids_cache)
        self.assertEqual(fake_id, ids_cache[fake_os_id])
        db_api.add_item_id.assert_called_once_with(
            fake_context, 'fake', fake_os_id, project_id=None)

        # no item in cache, item is found
        db_api.reset_mock()
        db_api.get_items_ids.return_value = [(fake_id, fake_os_id)]
        ids_cache = {}
        item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id,
                                           ids_by_os_id=ids_cache)
        self.assertEqual(fake_id, item_id)
        self.assertEqual({fake_os_id: fake_id}, ids_cache)
        self.assertFalse(db_api.add_item_id.called)

        # item in cache
        db_api.reset_mock()
        ids_cache = {fake_os_id: fake_id}
        item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id,
                                           ids_by_os_id=ids_cache)
        self.assertEqual(fake_id, item_id)
        self.assertEqual({fake_os_id: fake_id}, ids_cache)
        self.assertFalse(db_api.get_items_ids.called)
        self.assertFalse(db_api.add_item_id.called)

        # item in items dict
        items_dict = {fake_os_id: {'id': fake_id,
                                   'os_id': fake_os_id}}
        ids_cache = {}
        item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id,
                                           items_by_os_id=items_dict,
                                           ids_by_os_id=ids_cache)
        self.assertEqual(fake_id, item_id)
        self.assertFalse(db_api.get_items_ids.called)
        self.assertFalse(db_api.add_item_id.called)
        self.assertEqual({}, ids_cache)

        # item not in items dict, item is found
        items_dict = {fake_os_id: {'id': fake_id,
                                   'os_id': fake_os_id}}
        db_api.get_items_ids.return_value = [(fake_id, fake_os_id)]
        item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id,
                                           items_by_os_id=items_dict)
        self.assertEqual(fake_id, item_id)
        self.assertFalse(db_api.add_item_id.called)
Ejemplo n.º 12
0
def _format_image(context,
                  image,
                  os_image,
                  images_dict,
                  ids_dict,
                  snapshot_ids=None):
    ec2_image = {
        'imageId': image['id'],
        'imageOwnerId': os_image.owner,
        'imageType': IMAGE_TYPES[ec2utils.get_ec2_id_kind(image['id'])],
        'isPublic': os_image.is_public,
        'architecture': os_image.properties.get('architecture'),
        'creationDate': os_image.created_at
    }
    if 'description' in image:
        ec2_image['description'] = image['description']
    if 'state' in image:
        state = image['state']
    else:
        state = GLANCE_STATUS_TO_EC2.get(os_image.status, 'error')
    if state in ('available', 'pending'):
        state = _s3_image_state_map.get(os_image.properties.get('image_state'),
                                        state)
    ec2_image['imageState'] = state

    kernel_id = os_image.properties.get('kernel_id')
    if kernel_id:
        ec2_image['kernelId'] = ec2utils.os_id_to_ec2_id(
            context,
            'aki',
            kernel_id,
            items_by_os_id=images_dict,
            ids_by_os_id=ids_dict)
    ramdisk_id = os_image.properties.get('ramdisk_id')
    if ramdisk_id:
        ec2_image['ramdiskId'] = ec2utils.os_id_to_ec2_id(
            context,
            'ari',
            ramdisk_id,
            items_by_os_id=images_dict,
            ids_by_os_id=ids_dict)

    name = os_image.name
    img_loc = os_image.properties.get('image_location')
    if img_loc:
        ec2_image['imageLocation'] = img_loc
    else:
        ec2_image['imageLocation'] = "%s (%s)" % (img_loc, name)
    if not name and img_loc:
        # This should only occur for images registered with ec2 api
        # prior to that api populating the glance name
        ec2_image['name'] = img_loc
    else:
        ec2_image['name'] = name

    properties = ec2utils.deserialize_os_image_properties(os_image)
    root_device_name = (
        ec2utils.block_device_properties_root_device_name(properties))
    mappings = _format_mappings(context, properties, root_device_name,
                                snapshot_ids, os_image.owner)
    if mappings:
        ec2_image['blockDeviceMapping'] = mappings

    root_device_type = 'instance-store'
    if root_device_name:
        ec2_image['rootDeviceName'] = root_device_name

        short_root_device_name = ec2utils.block_device_strip_dev(
            root_device_name)
        if any((short_root_device_name == ec2utils.block_device_strip_dev(
                bdm.get('deviceName'))) for bdm in mappings):
            root_device_type = 'ebs'
    ec2_image['rootDeviceType'] = root_device_type

    return ec2_image
Ejemplo n.º 13
0
 def _ramdisk_attribute(os_image, image, result):
     ramdisk_id = os_image.properties.get('ramdisk_id')
     if ramdisk_id:
         result['ramdisk'] = {
             'value': ec2utils.os_id_to_ec2_id(context, 'ari', ramdisk_id)
         }
Ejemplo n.º 14
0
 def _kernel_attribute(os_image, image, result):
     kernel_id = os_image.properties.get('kernel_id')
     if kernel_id:
         result['kernel'] = {
             'value': ec2utils.os_id_to_ec2_id(context, 'aki', kernel_id)
         }
Ejemplo n.º 15
0
 def _kernel_attribute(os_image, image, result):
     kernel_id = getattr(os_image, 'kernel_id', None)
     if kernel_id:
         result['kernel'] = {
             'value': ec2utils.os_id_to_ec2_id(context, 'aki', kernel_id)
         }
Ejemplo n.º 16
0
def _format_image(context,
                  image,
                  os_image,
                  images_dict,
                  ids_dict,
                  snapshot_ids=None):
    ec2_image = {
        'imageId': image['id'],
        'imageType': IMAGE_TYPES[ec2utils.get_ec2_id_kind(image['id'])],
        'isPublic': os_image.is_public,
        'architecture': os_image.properties.get('architecture'),
    }
    if 'description' in image:
        ec2_image['description'] = image['description']
    state = os_image.status
    # NOTE(vish): fallback status if image_state isn't set
    if state == 'active':
        state = 'available'
    ec2_image['imageState'] = os_image.properties.get('image_state', state)

    kernel_id = os_image.properties.get('kernel_id')
    if kernel_id:
        ec2_image['kernelId'] = ec2utils.os_id_to_ec2_id(
            context,
            'jki',
            kernel_id,
            items_by_os_id=images_dict,
            ids_by_os_id=ids_dict)
    ramdisk_id = os_image.properties.get('ramdisk_id')
    if ramdisk_id:
        ec2_image['ramdiskId'] = ec2utils.os_id_to_ec2_id(
            context,
            'jri',
            ramdisk_id,
            items_by_os_id=images_dict,
            ids_by_os_id=ids_dict)

    name = os_image.name
    # Commenting out the image location code as it is not required
    # in the first release
    # img_loc = os_image.properties.get('image_location')
    # if img_loc:
    #     ec2_image['imageLocation'] = img_loc
    # else:
    #     ec2_image['imageLocation'] = "%s (%s)" % (img_loc, name)

    # Glance allows image names to be empty
    if name:
        ec2_image['name'] = name
    else:
        # Corner case: no image name which is allowed in glance
        ec2_image['name'] = ""

    _prepare_mappings(os_image)
    properties = os_image.properties

    root_bdm = _prepare_root_bdm_for_image(os_image)
    ec2_image['blockDeviceMapping'] = root_bdm

    # Commenting the root device name and root device type from the response
    # Will remove any dead code in next iteration
    root_device_name = _block_device_properties_root_device_name(properties)
    if root_device_name:
        # ec2_image['rootDeviceName'] = root_device_name

        root_device_type = 'instance-store'
        short_root_device_name = instance_api._block_device_strip_dev(
            root_device_name)
        for bdm in properties.get('block_device_mapping', []):
            if (('snapshot_id' in bdm or 'volume_id' in bdm)
                    and not bdm.get('no_device')
                    and (bdm.get('boot_index') == 0 or short_root_device_name
                         == instance_api._block_device_strip_dev(
                             bdm.get('device_name')))):
                root_device_type = 'ebs'
                break
        # ec2_image['rootDeviceType'] = root_device_type

    _cloud_format_mappings(context, properties, ec2_image, root_device_name,
                           snapshot_ids, os_image.owner)

    return ec2_image
Ejemplo n.º 17
0
 def _ramdisk_attribute(os_image, image, result):
     ramdisk_id = getattr(os_image, 'ramdisk_id', None)
     if ramdisk_id:
         result['ramdisk'] = {
             'value': ec2utils.os_id_to_ec2_id(context, 'ari', ramdisk_id)
         }
Ejemplo n.º 18
0
 def _kernel_attribute(os_image, image, result):
     kernel_id = getattr(os_image, 'kernel_id', None)
     if kernel_id:
         result['kernel'] = {
             'value': ec2utils.os_id_to_ec2_id(context, 'aki', kernel_id)
         }
Ejemplo n.º 19
0
 def _ramdisk_attribute(os_image, image, result):
     ramdisk_id = getattr(os_image, 'ramdisk_id', None)
     if ramdisk_id:
         result['ramdisk'] = {
             'value': ec2utils.os_id_to_ec2_id(context, 'ari', ramdisk_id)
         }
Ejemplo n.º 20
0
 def _kernel_attribute(os_image, image, result):
     kernel_id = os_image.properties.get('kernel_id')
     if kernel_id:
         result['kernel'] = {
             'value': ec2utils.os_id_to_ec2_id(context, 'aki', kernel_id)
         }
Ejemplo n.º 21
0
def _format_image(context, image, os_image, images_dict, ids_dict,
                  snapshot_ids=None):
    ec2_image = {'imageId': image['id'],
                 'imageOwnerId': os_image.owner,
                 'imageType': IMAGE_TYPES[
                                   ec2utils.get_ec2_id_kind(image['id'])],
                 'isPublic': os_image.is_public,
                 'architecture': os_image.properties.get('architecture'),
                 'creationDate': os_image.created_at
                 }
    if 'description' in image:
        ec2_image['description'] = image['description']
    if 'state' in image:
        state = image['state']
    else:
        state = GLANCE_STATUS_TO_EC2.get(os_image.status, 'error')
    if state in ('available', 'pending'):
        state = _s3_image_state_map.get(os_image.properties.get('image_state'),
                                        state)
    ec2_image['imageState'] = state

    kernel_id = os_image.properties.get('kernel_id')
    if kernel_id:
        ec2_image['kernelId'] = ec2utils.os_id_to_ec2_id(
                context, 'aki', kernel_id,
                items_by_os_id=images_dict, ids_by_os_id=ids_dict)
    ramdisk_id = os_image.properties.get('ramdisk_id')
    if ramdisk_id:
        ec2_image['ramdiskId'] = ec2utils.os_id_to_ec2_id(
                context, 'ari', ramdisk_id,
                items_by_os_id=images_dict, ids_by_os_id=ids_dict)

    name = os_image.name
    img_loc = os_image.properties.get('image_location')
    if img_loc:
        ec2_image['imageLocation'] = img_loc
    else:
        ec2_image['imageLocation'] = "%s (%s)" % (img_loc, name)
    if not name and img_loc:
        # This should only occur for images registered with ec2 api
        # prior to that api populating the glance name
        ec2_image['name'] = img_loc
    else:
        ec2_image['name'] = name

    properties = ec2utils.deserialize_os_image_properties(os_image)
    root_device_name = (
        ec2utils.block_device_properties_root_device_name(properties))
    mappings = _format_mappings(context, properties, root_device_name,
                                snapshot_ids, os_image.owner)
    if mappings:
        ec2_image['blockDeviceMapping'] = mappings

    root_device_type = 'instance-store'
    if root_device_name:
        ec2_image['rootDeviceName'] = root_device_name

        short_root_device_name = ec2utils.block_device_strip_dev(
                root_device_name)
        if any((short_root_device_name ==
                ec2utils.block_device_strip_dev(bdm.get('deviceName')))
               for bdm in mappings):
            root_device_type = 'ebs'
    ec2_image['rootDeviceType'] = root_device_type

    return ec2_image
Ejemplo n.º 22
0
 def _ramdisk_attribute(os_image, image, result):
     ramdisk_id = os_image.properties.get('ramdisk_id')
     if ramdisk_id:
         result['ramdisk'] = {
             'value': ec2utils.os_id_to_ec2_id(context, 'ari', ramdisk_id)
         }