def _get_marker(self, req): """Parse a marker query param into something usable.""" marker = req.params.get('marker', None) if marker and not uuidutils.is_uuid_like(marker): msg = _('Invalid marker format') raise exc.HTTPBadRequest(explanation=msg) return marker
def parse_uri(self, uri): valid_schema = 'sheepdog://' if not uri.startswith(valid_schema): raise exception.BadStoreUri(_("URI must start with %s://") % valid_schema) self.image = uri[len(valid_schema):] if not uuidutils.is_uuid_like(self.image): raise exception.BadStoreUri(_("URI must contains well-formated " "image id"))
def parse_uri(self, uri): if not uri.startswith('cinder://'): reason = _("URI must start with cinder://") LOG.error(reason) raise exception.BadStoreUri(uri, reason) self.scheme = 'cinder' self.volume_id = uri[9:] if not uuidutils.is_uuid_like(self.volume_id): reason = _("URI contains invalid volume ID: %s") % self.volume_id LOG.error(reason) raise exception.BadStoreUri(uri, reason)
def create(self, req, body): """Registers a new image with the registry. :param req: wsgi Request object :param body: Dictionary of information about the image :retval Returns the newly-created image information as a mapping, which will include the newly-created image's internal id in the 'id' field """ image_data = body['image'] # Ensure the image has a status set image_data.setdefault('status', 'active') # Set up the image owner if not req.context.is_admin or 'owner' not in image_data: image_data['owner'] = req.context.owner image_id = image_data.get('id') if image_id and not uuidutils.is_uuid_like(image_id): msg = _("Rejecting image creation request for invalid image " "id '%(bad_id)s'") LOG.info(msg % {'bad_id': image_id}) msg = _("Invalid image id format") return exc.HTTPBadRequest(explanation=msg) if 'location' in image_data: image_data['locations'] = [image_data.pop('location')] try: image_data = _normalize_image_location_for_db(image_data) image_data = self.db_api.image_create(req.context, image_data) msg = _("Successfully created image %(id)s") % {'id': image_id} LOG.info(msg) return dict(image=make_image_dict(image_data)) except exception.Duplicate: msg = _("Image with identifier %s already exists!") % image_id LOG.error(msg) return exc.HTTPConflict(msg) except exception.Invalid as e: msg = (_("Failed to add image metadata. " "Got error: %(e)s") % { 'e': e }) LOG.error(msg) return exc.HTTPBadRequest(msg)
def create(self, req, body): """ Registers a new image with the registry. :param req: wsgi Request object :param body: Dictionary of information about the image :retval Returns the newly-created image information as a mapping, which will include the newly-created image's internal id in the 'id' field """ image_data = body['image'] # Ensure the image has a status set image_data.setdefault('status', 'active') # Set up the image owner if not req.context.is_admin or 'owner' not in image_data: image_data['owner'] = req.context.owner image_id = image_data.get('id') if image_id and not uuidutils.is_uuid_like(image_id): msg = _("Rejecting image creation request for invalid image " "id '%(bad_id)s'") LOG.info(msg % {'bad_id': image_id}) msg = _("Invalid image id format") return exc.HTTPBadRequest(explanation=msg) if 'location' in image_data: image_data['locations'] = [image_data.pop('location')] try: image_data = _normalize_image_location_for_db(image_data) image_data = self.db_api.image_create(req.context, image_data) msg = _("Successfully created image %(id)s") LOG.info(msg % {'id': image_id}) return dict(image=make_image_dict(image_data)) except exception.Duplicate: msg = (_("Image with identifier %s already exists!") % image_id) LOG.error(msg) return exc.HTTPConflict(msg) except exception.Invalid as e: msg = (_("Failed to add image metadata. " "Got error: %(e)s") % locals()) LOG.error(msg) return exc.HTTPBadRequest(msg)
def _check_012(self, engine, test_data): images = get_table(engine, 'images') image_members = get_table(engine, 'image_members') image_properties = get_table(engine, 'image_properties') # Find kernel, ramdisk and normal images. Make sure id has been # changed to a uuid uuids = {} for name in ('kernel', 'ramdisk', 'normal'): image_name = '%s migration 012 test' % name rows = images.select()\ .where(images.c.name == image_name)\ .execute().fetchall() self.assertEquals(len(rows), 1) row = rows[0] print repr(dict(row)) self.assertTrue(uuidutils.is_uuid_like(row['id'])) uuids[name] = row['id'] # Find all image_members to ensure image_id has been updated results = image_members.select()\ .where(image_members.c.image_id == uuids['normal'])\ .execute().fetchall() self.assertEquals(len(results), 1) # Find all image_properties to ensure image_id has been updated # as well as ensure kernel_id and ramdisk_id values have been # updated too results = image_properties.select()\ .where(image_properties.c.image_id == uuids['normal'])\ .execute().fetchall() self.assertEquals(len(results), 2) for row in results: self.assertIn(row['name'], ('kernel_id', 'ramdisk_id')) if row['name'] == 'kernel_id': self.assertEqual(row['value'], uuids['kernel']) if row['name'] == 'ramdisk_id': self.assertEqual(row['value'], uuids['ramdisk'])
def _check_012(self, engine, test_data): images = get_table(engine, 'images') image_members = get_table(engine, 'image_members') image_properties = get_table(engine, 'image_properties') # Find kernel, ramdisk and normal images. Make sure id has been # changed to a uuid uuids = {} for name in ('kernel', 'ramdisk', 'normal'): image_name = '%s migration 012 test' % name rows = images.select()\ .where(images.c.name == image_name)\ .execute().fetchall() self.assertEquals(len(rows), 1) row = rows[0] print(repr(dict(row))) self.assertTrue(uuidutils.is_uuid_like(row['id'])) uuids[name] = row['id'] # Find all image_members to ensure image_id has been updated results = image_members.select()\ .where(image_members.c.image_id == uuids['normal'])\ .execute().fetchall() self.assertEquals(len(results), 1) # Find all image_properties to ensure image_id has been updated # as well as ensure kernel_id and ramdisk_id values have been # updated too results = image_properties.select()\ .where(image_properties.c.image_id == uuids['normal'])\ .execute().fetchall() self.assertEquals(len(results), 2) for row in results: self.assertIn(row['name'], ('kernel_id', 'ramdisk_id')) if row['name'] == 'kernel_id': self.assertEqual(row['value'], uuids['kernel']) if row['name'] == 'ramdisk_id': self.assertEqual(row['value'], uuids['ramdisk'])
def _walk_all_locations(self, remove=False): """Returns a list of image id and location tuple from scrub queue. :param remove: Whether remove location from queue or not after walk :retval a list of image image_id and location tuple from scrub queue """ if not os.path.exists(self.scrubber_datadir): LOG.info(_("%s directory does not exist.") % self.scrubber_datadir) return [] ret = [] for root, dirs, files in os.walk(self.scrubber_datadir): for image_id in files: if not uuidutils.is_uuid_like(image_id): continue with lockutils.lock("scrubber-%s" % image_id, lock_file_prefix='glance-', external=True): file_path = os.path.join(self.scrubber_datadir, image_id) uris, delete_times = self._read_queue_file(file_path) remove_record_idxs = [] skipped = False for (record_idx, delete_time) in enumerate(delete_times): if delete_time > time.time(): skipped = True continue else: ret.append((image_id, uris[record_idx])) remove_record_idxs.append(record_idx) if remove: if skipped: # NOTE(zhiyan): remove location records from # the queue file. self._update_queue_file(file_path, remove_record_idxs) else: utils.safe_remove(file_path) return ret
def _validate_marker(self, marker): if marker and not uuidutils.is_uuid_like(marker): msg = _('Invalid marker format') raise webob.exc.HTTPBadRequest(explanation=msg) return marker