def create(self, req, body): """Creates a new volume.""" if not self.is_valid_body(body, 'volume'): raise exc.HTTPUnprocessableEntity() context = req.environ['cinder.context'] volume = body['volume'] kwargs = {} req_volume_type = volume.get('volume_type', None) if req_volume_type: try: kwargs['volume_type'] = volume_types.get_volume_type_by_name( context, req_volume_type) except exception.VolumeTypeNotFound: explanation = 'Volume type not found.' raise exc.HTTPNotFound(explanation=explanation) kwargs['metadata'] = volume.get('metadata', None) snapshot_id = volume.get('snapshot_id') if snapshot_id is not None: kwargs['snapshot'] = self.volume_api.get_snapshot(context, snapshot_id) else: kwargs['snapshot'] = None size = volume.get('size', None) if size is None and kwargs['snapshot'] is not None: size = kwargs['snapshot']['volume_size'] LOG.audit(_("Create volume of %s GB"), size, context=context) image_href = None image_uuid = None if self.ext_mgr.is_loaded('os-image-create'): image_href = volume.get('imageRef') if snapshot_id and image_href: msg = _("Snapshot and image cannot be specified together.") raise exc.HTTPBadRequest(explanation=msg) if image_href: image_uuid = self._image_uuid_from_href(image_href) kwargs['image_id'] = image_uuid kwargs['availability_zone'] = volume.get('availability_zone', None) new_volume = self.volume_api.create(context, size, volume.get('display_name'), volume.get('display_description'), **kwargs) # TODO(vish): Instance should be None at db layer instead of # trying to lazy load, but for now we turn it into # a dict to avoid an error. retval = _translate_volume_detail_view(context, dict(new_volume), image_uuid) return {'volume': retval}
def create(self, req, body): """Creates a new volume.""" if not self.is_valid_body(body, 'volume'): raise exc.HTTPUnprocessableEntity() context = req.environ['cinder.context'] volume = body['volume'] kwargs = {} req_volume_type = volume.get('volume_type', None) if req_volume_type: try: kwargs['volume_type'] = volume_types.get_volume_type_by_name( context, req_volume_type) except exception.NotFound: raise exc.HTTPNotFound() kwargs['metadata'] = volume.get('metadata', None) snapshot_id = volume.get('snapshot_id') if snapshot_id is not None: kwargs['snapshot'] = self.volume_api.get_snapshot( context, snapshot_id) else: kwargs['snapshot'] = None size = volume.get('size', None) if size is None and kwargs['snapshot'] is not None: size = kwargs['snapshot']['volume_size'] LOG.audit(_("Create volume of %s GB"), size, context=context) image_href = None image_uuid = None if self.ext_mgr.is_loaded('os-image-create'): image_href = volume.get('imageRef') if snapshot_id and image_href: msg = _("Snapshot and image cannot be specified together.") raise exc.HTTPBadRequest(explanation=msg) if image_href: image_uuid = self._image_uuid_from_href(image_href) kwargs['image_id'] = image_uuid kwargs['availability_zone'] = volume.get('availability_zone', None) new_volume = self.volume_api.create(context, size, volume.get('display_name'), volume.get('display_description'), **kwargs) # TODO(vish): Instance should be None at db layer instead of # trying to lazy load, but for now we turn it into # a dict to avoid an error. retval = _translate_volume_detail_view(context, dict(new_volume), image_uuid) return {'volume': retval}
def _add_visible_admin_metadata(self, context, volume): if context is None: return visible_admin_meta = {} volume_tmp = (volume if context.is_admin else self.volume_api.get( context.elevated(), volume['id'])) if volume_tmp.get('volume_admin_metadata'): for item in volume_tmp['volume_admin_metadata']: if item['key'] in self._visible_admin_metadata_keys: visible_admin_meta[item['key']] = item['value'] # avoid circular ref when volume is a Volume instance elif (volume_tmp.get('admin_metadata') and isinstance(volume_tmp.get('admin_metadata'), dict)): for key in self._visible_admin_metadata_keys: if key in volume_tmp['admin_metadata'].keys(): visible_admin_meta[key] = volume_tmp['admin_metadata'][key] if not visible_admin_meta: return # NOTE(zhiyan): update visible administration metadata to # volume metadata, administration metadata will rewrite existing key. if volume.get('volume_metadata'): orig_meta = volume.get('volume_metadata') for item in orig_meta: if item['key'] in visible_admin_meta.keys(): item['value'] = visible_admin_meta.pop(item['key']) for key, value in visible_admin_meta.iteritems(): orig_meta.append({'key': key, 'value': value}) # avoid circular ref when vol is a Volume instance elif (volume.get('metadata') and isinstance(volume.get('metadata'), dict)): volume['metadata'].update(visible_admin_meta) else: volume['metadata'] = visible_admin_meta
def _add_visible_admin_metadata(self, context, volume): if context is None: return visible_admin_meta = {} volume_tmp = (volume if context.is_admin else self.volume_api.get(context.elevated(), volume['id'])) if volume_tmp.get('volume_admin_metadata'): for item in volume_tmp['volume_admin_metadata']: if item['key'] in self._visible_admin_metadata_keys: visible_admin_meta[item['key']] = item['value'] # avoid circular ref when volume is a Volume instance elif (volume_tmp.get('admin_metadata') and isinstance(volume_tmp.get('admin_metadata'), dict)): for key in self._visible_admin_metadata_keys: if key in volume_tmp['admin_metadata'].keys(): visible_admin_meta[key] = volume_tmp['admin_metadata'][key] if not visible_admin_meta: return # NOTE(zhiyan): update visible administration metadata to # volume metadata, administration metadata will rewrite existing key. if volume.get('volume_metadata'): orig_meta = volume.get('volume_metadata') for item in orig_meta: if item['key'] in visible_admin_meta.keys(): item['value'] = visible_admin_meta.pop(item['key']) for key, value in visible_admin_meta.iteritems(): orig_meta.append({'key': key, 'value': value}) # avoid circular ref when vol is a Volume instance elif (volume.get('metadata') and isinstance(volume.get('metadata'), dict)): volume['metadata'].update(visible_admin_meta) else: volume['metadata'] = visible_admin_meta
def create(self, req, body): """Creates a new volume.""" context = req.environ['cinder.context'] if not body: raise exc.HTTPUnprocessableEntity() volume = body['volume'] def as_int(s): try: return int(s) except ValueError: return s # NOTE(eglynn): we're tolerant of non-int sizes here, as type # integrity is enforced later in the creation codepath size = as_int(volume['size']) LOG.audit(_("Create volume of %s GB"), size, context=context) kwargs = {} req_volume_type = volume.get('volume_type', None) if req_volume_type: try: kwargs['volume_type'] = volume_types.get_volume_type_by_name( context, req_volume_type) except exception.NotFound: raise exc.HTTPNotFound() kwargs['metadata'] = volume.get('metadata', None) snapshot_id = volume.get('snapshot_id') if snapshot_id is not None: kwargs['snapshot'] = self.volume_api.get_snapshot(context, snapshot_id) else: kwargs['snapshot'] = None kwargs['availability_zone'] = volume.get('availability_zone', None) new_volume = self.volume_api.create(context, size, volume.get('display_name'), volume.get('display_description'), **kwargs) # TODO(vish): Instance should be None at db layer instead of # trying to lazy load, but for now we turn it into # a dict to avoid an error. retval = _translate_volume_detail_view(context, dict(new_volume)) return {'volume': retval}
def create(self, req, body): """Creates a new volume.""" context = req.environ["cinder.context"] if not body: raise exc.HTTPUnprocessableEntity() volume = body["volume"] size = volume["size"] LOG.audit(_("Create volume of %s GB"), size, context=context) kwargs = {} req_volume_type = volume.get("volume_type", None) if req_volume_type: try: kwargs["volume_type"] = volume_types.get_volume_type_by_name(context, req_volume_type) except exception.NotFound: raise exc.HTTPNotFound() kwargs["metadata"] = volume.get("metadata", None) snapshot_id = volume.get("snapshot_id") if snapshot_id is not None: kwargs["snapshot"] = self.volume_api.get_snapshot(context, snapshot_id) else: kwargs["snapshot"] = None kwargs["availability_zone"] = volume.get("availability_zone", None) new_volume = self.volume_api.create( context, size, volume.get("display_name"), volume.get("display_description"), **kwargs ) # TODO(vish): Instance should be None at db layer instead of # trying to lazy load, but for now we turn it into # a dict to avoid an error. retval = _translate_volume_detail_view(context, dict(new_volume)) return {"volume": retval}
def create(self, req, body): """Creates a new volume.""" if not self.is_valid_body(body, 'volume'): raise exc.HTTPBadRequest() LOG.debug('Create volume request body: %s', body) context = req.environ['cinder.context'] volume = body['volume'] kwargs = {} # NOTE(thingee): v2 API allows name instead of display_name if volume.get('name'): volume['display_name'] = volume.get('name') del volume['name'] # NOTE(thingee): v2 API allows description instead of description if volume.get('description'): volume['display_description'] = volume.get('description') del volume['description'] req_volume_type = volume.get('volume_type', None) if req_volume_type: try: kwargs['volume_type'] = volume_types.get_volume_type( context, req_volume_type) except exception.VolumeTypeNotFound: explanation = 'Volume type not found.' raise exc.HTTPNotFound(explanation=explanation) kwargs['metadata'] = volume.get('metadata', None) snapshot_id = volume.get('snapshot_id') if snapshot_id is not None: kwargs['snapshot'] = self.volume_api.get_snapshot( context, snapshot_id) else: kwargs['snapshot'] = None source_volid = volume.get('source_volid') if source_volid is not None: kwargs['source_volume'] = self.volume_api.get_volume( context, source_volid) else: kwargs['source_volume'] = None size = volume.get('size', None) if size is None and kwargs['snapshot'] is not None: size = kwargs['snapshot']['volume_size'] elif size is None and kwargs['source_volume'] is not None: size = kwargs['source_volume']['size'] LOG.audit(_("Create volume of %s GB"), size, context=context) image_href = None image_uuid = None if self.ext_mgr.is_loaded('os-image-create'): image_href = volume.get('imageRef') if image_href: image_uuid = self._image_uuid_from_href(image_href) kwargs['image_id'] = image_uuid kwargs['availability_zone'] = volume.get('availability_zone', None) kwargs['scheduler_hints'] = volume.get('scheduler_hints', None) new_volume = self.volume_api.create(context, size, volume.get('display_name'), volume.get('display_description'), **kwargs) # TODO(vish): Instance should be None at db layer instead of # trying to lazy load, but for now we turn it into # a dict to avoid an error. retval = self._view_builder.summary(req, dict(new_volume.iteritems())) return retval
def _get_image_metadata_list(self, body): return [ volume['volume_image_metadata'] for volume in jsonutils.loads(body)['volumes'] if volume.get('volume_image_metadata') ]
def create(self, req, body): """Creates a new volume.""" if not self.is_valid_body(body, 'volume'): raise exc.HTTPBadRequest() context = req.environ['cinder.context'] volume = body['volume'] kwargs = {} # NOTE(thingee): v2 API allows name instead of display_name if volume.get('name'): volume['display_name'] = volume.get('name') del volume['name'] # NOTE(thingee): v2 API allows description instead of description if volume.get('description'): volume['display_description'] = volume.get('description') del volume['description'] req_volume_type = volume.get('volume_type', None) if req_volume_type: try: kwargs['volume_type'] = volume_types.get_volume_type( context, req_volume_type) except exception.VolumeTypeNotFound: explanation = 'Volume type not found.' raise exc.HTTPNotFound(explanation=explanation) kwargs['metadata'] = volume.get('metadata', None) snapshot_id = volume.get('snapshot_id') if snapshot_id is not None: kwargs['snapshot'] = self.volume_api.get_snapshot(context, snapshot_id) else: kwargs['snapshot'] = None source_volid = volume.get('source_volid') if source_volid is not None: kwargs['source_volume'] = self.volume_api.get_volume(context, source_volid) else: kwargs['source_volume'] = None size = volume.get('size', None) if size is None and kwargs['snapshot'] is not None: size = kwargs['snapshot']['volume_size'] elif size is None and kwargs['source_volume'] is not None: size = kwargs['source_volume']['size'] LOG.audit(_("Create volume of %s GB"), size, context=context) image_href = None image_uuid = None if self.ext_mgr.is_loaded('os-image-create'): image_href = volume.get('imageRef') if image_href: image_uuid = self._image_uuid_from_href(image_href) kwargs['image_id'] = image_uuid kwargs['availability_zone'] = volume.get('availability_zone', None) new_volume = self.volume_api.create(context, size, volume.get('display_name'), volume.get('display_description'), **kwargs) # TODO(vish): Instance should be None at db layer instead of # trying to lazy load, but for now we turn it into # a dict to avoid an error. retval = self._view_builder.summary(req, dict(new_volume.iteritems())) return retval
def create(self, req, body): """Creates a new volume.""" if not self.is_valid_body(body, "volume"): msg = _("Missing required element '%s' in request body") % "volume" raise exc.HTTPBadRequest(explanation=msg) LOG.debug("Create volume request body: %s", body) context = req.environ["cinder.context"] volume = body["volume"] kwargs = {} # NOTE(thingee): v2 API allows name instead of display_name if volume.get("name"): volume["display_name"] = volume.get("name") del volume["name"] # NOTE(thingee): v2 API allows description instead of description if volume.get("description"): volume["display_description"] = volume.get("description") del volume["description"] req_volume_type = volume.get("volume_type", None) if req_volume_type: try: kwargs["volume_type"] = volume_types.get_volume_type(context, req_volume_type) except exception.VolumeTypeNotFound: msg = _("Volume type not found") raise exc.HTTPNotFound(explanation=msg) kwargs["metadata"] = volume.get("metadata", None) snapshot_id = volume.get("snapshot_id") if snapshot_id is not None: kwargs["snapshot"] = self.volume_api.get_snapshot(context, snapshot_id) else: kwargs["snapshot"] = None source_volid = volume.get("source_volid") if source_volid is not None: kwargs["source_volume"] = self.volume_api.get_volume(context, source_volid) else: kwargs["source_volume"] = None size = volume.get("size", None) if size is None and kwargs["snapshot"] is not None: size = kwargs["snapshot"]["volume_size"] elif size is None and kwargs["source_volume"] is not None: size = kwargs["source_volume"]["size"] LOG.audit(_("Create volume of %s GB"), size, context=context) image_href = None image_uuid = None if self.ext_mgr.is_loaded("os-image-create"): image_href = volume.get("imageRef") if image_href: image_uuid = self._image_uuid_from_href(image_href) kwargs["image_id"] = image_uuid kwargs["availability_zone"] = volume.get("availability_zone", None) kwargs["scheduler_hints"] = volume.get("scheduler_hints", None) new_volume = self.volume_api.create( context, size, volume.get("display_name"), volume.get("display_description"), **kwargs ) # TODO(vish): Instance should be None at db layer instead of # trying to lazy load, but for now we turn it into # a dict to avoid an error. retval = self._view_builder.summary(req, dict(new_volume.iteritems())) return retval
def create(self, req, body): """Creates a new volume.""" if not self.is_valid_body(body, "volume"): raise exc.HTTPUnprocessableEntity() LOG.debug("Create volume request body: %s", body) context = req.environ["cinder.context"] volume = body["volume"] kwargs = {} req_volume_type = volume.get("volume_type", None) if req_volume_type: if not uuidutils.is_uuid_like(req_volume_type): try: kwargs["volume_type"] = volume_types.get_volume_type_by_name(context, req_volume_type) except exception.VolumeTypeNotFound: explanation = "Volume type not found." raise exc.HTTPNotFound(explanation=explanation) else: try: kwargs["volume_type"] = volume_types.get_volume_type(context, req_volume_type) except exception.VolumeTypeNotFound: explanation = "Volume type not found." raise exc.HTTPNotFound(explanation=explanation) kwargs["metadata"] = volume.get("metadata", None) snapshot_id = volume.get("snapshot_id") if snapshot_id is not None: kwargs["snapshot"] = self.volume_api.get_snapshot(context, snapshot_id) else: kwargs["snapshot"] = None source_volid = volume.get("source_volid") if source_volid is not None: kwargs["source_volume"] = self.volume_api.get_volume(context, source_volid) else: kwargs["source_volume"] = None size = volume.get("size", None) if size is None and kwargs["snapshot"] is not None: size = kwargs["snapshot"]["volume_size"] elif size is None and kwargs["source_volume"] is not None: size = kwargs["source_volume"]["size"] LOG.audit(_("Create volume of %s GB"), size, context=context) image_href = None image_uuid = None if self.ext_mgr.is_loaded("os-image-create"): image_href = volume.get("imageRef") if image_href: image_uuid = self._image_uuid_from_href(image_href) kwargs["image_id"] = image_uuid kwargs["availability_zone"] = volume.get("availability_zone", None) new_volume = self.volume_api.create( context, size, volume.get("display_name"), volume.get("display_description"), **kwargs ) # TODO(vish): Instance should be None at db layer instead of # trying to lazy load, but for now we turn it into # a dict to avoid an error. retval = _translate_volume_detail_view(context, dict(new_volume.iteritems()), image_uuid) return {"volume": retval}
def create(self, req, body): """Creates a new volume.""" if not self.is_valid_body(body, 'volume'): raise exc.HTTPUnprocessableEntity() LOG.debug('Create volume request body: %s', body) context = req.environ['cinder.context'] volume = body['volume'] kwargs = {} req_volume_type = volume.get('volume_type', None) if req_volume_type: if not uuidutils.is_uuid_like(req_volume_type): try: kwargs['volume_type'] = \ volume_types.get_volume_type_by_name( context, req_volume_type) except exception.VolumeTypeNotFound: explanation = 'Volume type not found.' raise exc.HTTPNotFound(explanation=explanation) else: try: kwargs['volume_type'] = volume_types.get_volume_type( context, req_volume_type) except exception.VolumeTypeNotFound: explanation = 'Volume type not found.' raise exc.HTTPNotFound(explanation=explanation) kwargs['metadata'] = volume.get('metadata', None) snapshot_id = volume.get('snapshot_id') if snapshot_id is not None: kwargs['snapshot'] = self.volume_api.get_snapshot(context, snapshot_id) else: kwargs['snapshot'] = None source_volid = volume.get('source_volid') if source_volid is not None: kwargs['source_volume'] = self.volume_api.get_volume(context, source_volid) else: kwargs['source_volume'] = None size = volume.get('size', None) if size is None and kwargs['snapshot'] is not None: size = kwargs['snapshot']['volume_size'] elif size is None and kwargs['source_volume'] is not None: size = kwargs['source_volume']['size'] LOG.audit(_("Create volume of %s GB"), size, context=context) image_href = None image_uuid = None if self.ext_mgr.is_loaded('os-image-create'): # NOTE(jdg): misleading name "imageRef" as it's an image-id image_href = volume.get('imageRef') if image_href: image_uuid = self._image_uuid_from_href(image_href) kwargs['image_id'] = image_uuid kwargs['availability_zone'] = volume.get('availability_zone', None) new_volume = self.volume_api.create(context, size, volume.get('display_name'), volume.get('display_description'), **kwargs) # TODO(vish): Instance should be None at db layer instead of # trying to lazy load, but for now we turn it into # a dict to avoid an error. new_volume = dict(new_volume.iteritems()) self._add_visible_admin_metadata(context, new_volume) retval = _translate_volume_detail_view(context, new_volume, image_uuid) return {'volume': retval}
def create(self, req, body): """Creates a new volume.""" if not self.is_valid_body(body, 'volume'): raise exc.HTTPUnprocessableEntity() LOG.debug('Create volume request body: %s', body) context = req.environ['cinder.context'] volume = body['volume'] kwargs = {} req_volume_type = volume.get('volume_type', None) if req_volume_type: if not uuidutils.is_uuid_like(req_volume_type): try: kwargs['volume_type'] = \ volume_types.get_volume_type_by_name( context, req_volume_type) except exception.VolumeTypeNotFound: explanation = 'Volume type not found.' raise exc.HTTPNotFound(explanation=explanation) else: try: kwargs['volume_type'] = volume_types.get_volume_type( context, req_volume_type) except exception.VolumeTypeNotFound: explanation = 'Volume type not found.' raise exc.HTTPNotFound(explanation=explanation) kwargs['metadata'] = volume.get('metadata', None) snapshot_id = volume.get('snapshot_id') if snapshot_id is not None: kwargs['snapshot'] = self.volume_api.get_snapshot( context, snapshot_id) else: kwargs['snapshot'] = None source_volid = volume.get('source_volid') if source_volid is not None: kwargs['source_volume'] = self.volume_api.get_volume( context, source_volid) else: kwargs['source_volume'] = None size = volume.get('size', None) if size is None and kwargs['snapshot'] is not None: size = kwargs['snapshot']['volume_size'] elif size is None and kwargs['source_volume'] is not None: size = kwargs['source_volume']['size'] LOG.audit(_("Create volume of %s GB"), size, context=context) image_href = None image_uuid = None if self.ext_mgr.is_loaded('os-image-create'): # NOTE(jdg): misleading name "imageRef" as it's an image-id image_href = volume.get('imageRef') if image_href: image_uuid = self._image_uuid_from_href(image_href) kwargs['image_id'] = image_uuid kwargs['availability_zone'] = volume.get('availability_zone', None) new_volume = self.volume_api.create(context, size, volume.get('display_name'), volume.get('display_description'), **kwargs) # TODO(vish): Instance should be None at db layer instead of # trying to lazy load, but for now we turn it into # a dict to avoid an error. retval = _translate_volume_detail_view(context, dict(new_volume.iteritems()), image_uuid) return {'volume': retval}