def test_choose_best_location(self):
        self.config(location_strategy='location_order')

        original_locs = [{'url': 'loc1'}, {'url': 'loc2'}]
        best_loc = location_strategy.choose_best_location(original_locs)

        # Deep copy protect original location.
        self.assertNotEqual(id(original_locs), id(best_loc))
        self.assertEqual(original_locs[0], best_loc)
Exemple #2
0
    def _format_image(self, image):
        def _get_image_locations(image):
            try:
                return list(image.locations)
            except exception.Forbidden:
                return []

        try:
            image_view = dict(image.extra_properties)
            attributes = [
                'name', 'disk_format', 'container_format', 'visibility',
                'size', 'virtual_size', 'status', 'checksum', 'protected',
                'min_ram', 'min_disk', 'owner'
            ]
            for key in attributes:
                image_view[key] = getattr(image, key)
            image_view['id'] = image.image_id
            image_view['created_at'] = timeutils.isotime(image.created_at)
            image_view['updated_at'] = timeutils.isotime(image.updated_at)

            if CONF.show_multiple_locations:
                locations = _get_image_locations(image)
                if locations:
                    image_view['locations'] = []
                    for loc in locations:
                        tmp = dict(loc)
                        tmp.pop('id', None)
                        tmp.pop('status', None)
                        image_view['locations'].append(tmp)
                else:
                    # NOTE (flwang): We will still show "locations": [] if
                    # image.locations is None to indicate it's allowed to show
                    # locations but it's just non-existent.
                    image_view['locations'] = []
                    LOG.debug(
                        "There is not available location "
                        "for image %s", image.image_id)

            if CONF.show_image_direct_url:
                locations = _get_image_locations(image)
                if locations:
                    # Choose best location configured strategy
                    l = location_strategy.choose_best_location(locations)
                    image_view['direct_url'] = l['url']
                else:
                    LOG.debug(
                        "There is not available location "
                        "for image %s", image.image_id)

            image_view['tags'] = list(image.tags)
            image_view['self'] = self._get_image_href(image)
            image_view['file'] = self._get_image_href(image, 'file')
            image_view['schema'] = '/v2/schemas/image'
            image_view = self.schema.filter(image_view)  # domain
            return image_view
        except exception.Forbidden as e:
            raise webob.exc.HTTPForbidden(explanation=e.msg)
    def test_choose_best_location(self):
        self.config(location_strategy='location_order')

        original_locs = [{'url': 'loc1'}, {'url': 'loc2'}]
        best_loc = location_strategy.choose_best_location(original_locs)

        # Deep copy protect original location.
        self.assertNotEqual(id(original_locs), id(best_loc))
        self.assertEqual(original_locs[0], best_loc)
Exemple #4
0
    def _format_image(self, image):
        image_view = dict()
        try:
            image_view = dict(image.extra_properties)
            attributes = [
                "name",
                "disk_format",
                "container_format",
                "visibility",
                "size",
                "virtual_size",
                "status",
                "checksum",
                "protected",
                "min_ram",
                "min_disk",
                "owner",
            ]
            for key in attributes:
                image_view[key] = getattr(image, key)
            image_view["id"] = image.image_id
            image_view["created_at"] = timeutils.isotime(image.created_at)
            image_view["updated_at"] = timeutils.isotime(image.updated_at)

            if CONF.show_multiple_locations:
                locations = list(image.locations)
                if locations:
                    image_view["locations"] = []
                    for loc in locations:
                        tmp = dict(loc)
                        tmp.pop("id", None)
                        tmp.pop("status", None)
                        image_view["locations"].append(tmp)
                else:
                    # NOTE (flwang): We will still show "locations": [] if
                    # image.locations is None to indicate it's allowed to show
                    # locations but it's just non-existent.
                    image_view["locations"] = []
                    LOG.debug("There is not available location " "for image %s", image.image_id)

            if CONF.show_image_direct_url:
                if image.locations:
                    # Choose best location configured strategy
                    l = location_strategy.choose_best_location(image.locations)
                    image_view["direct_url"] = l["url"]
                else:
                    LOG.debug("There is not available location " "for image %s", image.image_id)

            image_view["tags"] = list(image.tags)
            image_view["self"] = self._get_image_href(image)
            image_view["file"] = self._get_image_href(image, "file")
            image_view["schema"] = "/v2/schemas/image"
            image_view = self.schema.filter(image_view)  # domain
        except exception.Forbidden as e:
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        return image_view
Exemple #5
0
    def _format_image(self, image):

        def _get_image_locations(image):
            try:
                return list(image.locations)
            except exception.Forbidden:
                return []

        try:
            image_view = dict(image.extra_properties)
            attributes = ['name', 'disk_format', 'container_format',
                          'visibility', 'size', 'virtual_size', 'status',
                          'checksum', 'protected', 'min_ram', 'min_disk',
                          'owner']
            for key in attributes:
                image_view[key] = getattr(image, key)
            image_view['id'] = image.image_id
            image_view['created_at'] = timeutils.isotime(image.created_at)
            image_view['updated_at'] = timeutils.isotime(image.updated_at)

            if CONF.show_multiple_locations:
                locations = _get_image_locations(image)
                if locations:
                    image_view['locations'] = []
                    for loc in locations:
                        tmp = dict(loc)
                        tmp.pop('id', None)
                        tmp.pop('status', None)
                        image_view['locations'].append(tmp)
                else:
                    # NOTE (flwang): We will still show "locations": [] if
                    # image.locations is None to indicate it's allowed to show
                    # locations but it's just non-existent.
                    image_view['locations'] = []
                    LOG.debug("There is not available location "
                              "for image %s", image.image_id)

            if CONF.show_image_direct_url:
                locations = _get_image_locations(image)
                if locations:
                    # Choose best location configured strategy
                    l = location_strategy.choose_best_location(locations)
                    image_view['direct_url'] = l['url']
                else:
                    LOG.debug("There is not available location "
                              "for image %s", image.image_id)

            image_view['tags'] = list(image.tags)
            image_view['self'] = self._get_image_href(image)
            image_view['file'] = self._get_image_href(image, 'file')
            image_view['schema'] = '/v2/schemas/image'
            image_view = self.schema.filter(image_view)  # domain
            return image_view
        except exception.Forbidden as e:
            raise webob.exc.HTTPForbidden(explanation=e.msg)
Exemple #6
0
    def _format_image(self, image):
        image_view = dict()
        try:
            image_view = dict(image.extra_properties)
            attributes = [
                'name', 'disk_format', 'container_format', 'visibility',
                'size', 'virtual_size', 'status', 'checksum', 'protected',
                'min_ram', 'min_disk', 'owner'
            ]
            for key in attributes:
                image_view[key] = getattr(image, key)
            image_view['id'] = image.image_id
            image_view['created_at'] = timeutils.isotime(image.created_at)
            image_view['updated_at'] = timeutils.isotime(image.updated_at)

            if CONF.show_multiple_locations:
                if image.locations:
                    image_view['locations'] = list(image.locations)
                else:
                    # NOTE (flwang): We will still show "locations": [] if
                    # image.locations is None to indicate it's allowed to show
                    # locations but it's just non-existent.
                    image_view['locations'] = []

            if CONF.show_image_direct_url and image.locations:
                # Choose best location configured strategy
                best_location = (location_strategy.choose_best_location(
                    image.locations))
                image_view['direct_url'] = best_location['url']

            image_view['tags'] = list(image.tags)
            image_view['self'] = self._get_image_href(image)
            image_view['file'] = self._get_image_href(image, 'file')
            image_view['schema'] = '/v2/schemas/image'
            image_view = self.schema.filter(image_view)  # domain
        except exception.Forbidden as e:
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        return image_view
Exemple #7
0
    def _format_image(self, image):
        image_view = dict()
        try:
            image_view = dict(image.extra_properties)
            attributes = ['name', 'disk_format', 'container_format',
                          'visibility', 'size', 'virtual_size', 'status',
                          'checksum', 'protected', 'min_ram', 'min_disk',
                          'owner']
            for key in attributes:
                image_view[key] = getattr(image, key)
            image_view['id'] = image.image_id
            image_view['created_at'] = timeutils.isotime(image.created_at)
            image_view['updated_at'] = timeutils.isotime(image.updated_at)

            if CONF.show_multiple_locations:
                if image.locations:
                    image_view['locations'] = list(image.locations)
                else:
                    # NOTE (flwang): We will still show "locations": [] if
                    # image.locations is None to indicate it's allowed to show
                    # locations but it's just non-existent.
                    image_view['locations'] = []

            if CONF.show_image_direct_url and image.locations:
                # Choose best location configured strategy
                best_location = (
                    location_strategy.choose_best_location(image.locations))
                image_view['direct_url'] = best_location['url']

            image_view['tags'] = list(image.tags)
            image_view['self'] = self._get_image_href(image)
            image_view['file'] = self._get_image_href(image, 'file')
            image_view['schema'] = '/v2/schemas/image'
            image_view = self.schema.filter(image_view)  # domain
        except exception.Forbidden as e:
            raise webob.exc.HTTPForbidden(unicode(e))
        return image_view
 def test_choose_best_location_with_none_or_empty_locations(self):
     self.assertIsNone(location_strategy.choose_best_location(None))
     self.assertIsNone(location_strategy.choose_best_location([]))
 def test_choose_best_location_with_none_or_empty_locations(self):
     self.assertIsNone(location_strategy.choose_best_location(None))
     self.assertIsNone(location_strategy.choose_best_location([]))
 def test_choose_best_location_with_none_or_empty_locations(self):
     self.assertEqual(location_strategy.choose_best_location(None), None)
     self.assertEqual(location_strategy.choose_best_location([]), None)