def update(self, req, id, body): """Update a snapshot.""" context = req.environ['cinder.context'] snapshot_body = body['snapshot'] self.validate_name_and_description(snapshot_body, check_length=False) if 'name' in snapshot_body: snapshot_body['display_name'] = snapshot_body.pop('name') if 'description' in snapshot_body: snapshot_body['display_description'] = snapshot_body.pop( 'description') # Not found exception will be handled at the wsgi level snapshot = self.volume_api.get_snapshot(context, id) volume_utils.notify_about_snapshot_usage(context, snapshot, 'update.start') self.volume_api.update_snapshot(context, snapshot, snapshot_body) snapshot.update(snapshot_body) req.cache_db_snapshot(snapshot) volume_utils.notify_about_snapshot_usage(context, snapshot, 'update.end') return self._view_builder.detail(req, snapshot)
def _notify_about_snapshot_usage(self, context, snapshot, event_suffix, extra_usage_info=None): volume_utils.notify_about_snapshot_usage( context, snapshot, event_suffix, extra_usage_info=extra_usage_info, host=self.host)
def update(self, req, id, body): """Update a snapshot.""" context = req.environ['cinder.context'] if not body: msg = _("Missing request body") raise exc.HTTPBadRequest(explanation=msg) if 'snapshot' not in body: msg = (_("Missing required element '%s' in request body") % 'snapshot') raise exc.HTTPBadRequest(explanation=msg) snapshot = body['snapshot'] update_dict = {} valid_update_keys = ( 'name', 'description', 'display_name', 'display_description', ) # NOTE(thingee): v2 API allows name instead of display_name if 'name' in snapshot: snapshot['display_name'] = snapshot['name'] del snapshot['name'] # NOTE(thingee): v2 API allows description instead of # display_description if 'description' in snapshot: snapshot['display_description'] = snapshot['description'] del snapshot['description'] for key in valid_update_keys: if key in snapshot: update_dict[key] = snapshot[key] try: snapshot = self.volume_api.get_snapshot(context, id) volume_utils.notify_about_snapshot_usage(context, snapshot, 'update.start') self.volume_api.update_snapshot(context, snapshot, update_dict) except exception.NotFound: msg = _("Snapshot could not be found") raise exc.HTTPNotFound(explanation=msg) snapshot.update(update_dict) req.cache_db_snapshot(snapshot) volume_utils.notify_about_snapshot_usage(context, snapshot, 'update.end') return {'snapshot': _translate_snapshot_detail_view(context, snapshot)}
def update(self, req, id, body): """Update a snapshot.""" context = req.environ['cinder.context'] if not body: msg = _("Missing request body") raise exc.HTTPBadRequest(explanation=msg) if 'snapshot' not in body: msg = (_("Missing required element '%s' in request body") % 'snapshot') raise exc.HTTPBadRequest(explanation=msg) snapshot = body['snapshot'] update_dict = {} valid_update_keys = ( 'name', 'description', 'display_name', 'display_description', ) # NOTE(thingee): v2 API allows name instead of display_name if 'name' in snapshot: snapshot['display_name'] = snapshot['name'] del snapshot['name'] # NOTE(thingee): v2 API allows description instead of # display_description if 'description' in snapshot: snapshot['display_description'] = snapshot['description'] del snapshot['description'] for key in valid_update_keys: if key in snapshot: update_dict[key] = snapshot[key] try: snapshot = self.volume_api.get_snapshot(context, id) volume_utils.notify_about_snapshot_usage(context, snapshot, 'update.start') self.volume_api.update_snapshot(context, snapshot, update_dict) except exception.SnapshotNotFound as error: raise exc.HTTPNotFound(explanation=error.msg) snapshot.update(update_dict) req.cache_db_snapshot(snapshot) volume_utils.notify_about_snapshot_usage(context, snapshot, 'update.end') return {'snapshot': _translate_snapshot_detail_view(context, snapshot)}
def execute(self, context, snapshot_ref): snapshot_id = snapshot_ref['id'] try: volume_utils.notify_about_snapshot_usage(context, snapshot_ref, self.event_suffix, host=self.host) except exception.CinderException: # If notification sending of snapshot database entry reading fails # then we shouldn't error out the whole workflow since this is # not always information that must be sent for snapshots to operate LOG.exception(_LE("Failed notifying about the snapshot " "action %(event)s for snapshot %(snp_id)s."), {'event': self.event_suffix, 'snp_id': snapshot_id})
def execute(self, context, snapshot_ref): snapshot_id = snapshot_ref['id'] try: volume_utils.notify_about_snapshot_usage(context, snapshot_ref, self.event_suffix, host=self.host) except exception.CinderException: # If notification sending of snapshot database entry reading fails # then we shouldn't error out the whole workflow since this is # not always information that must be sent for snapshots to operate LOG.exception("Failed notifying about the snapshot " "action %(event)s for snapshot %(snp_id)s.", {'event': self.event_suffix, 'snp_id': snapshot_id})
def update(self, req, id, body): """Update a snapshot.""" context = req.environ['cinder.context'] if not body: msg = _("Missing request body") raise exc.HTTPBadRequest(explanation=msg) if 'snapshot' not in body: msg = (_("Missing required element '%s' in request body") % 'snapshot') raise exc.HTTPBadRequest(explanation=msg) snapshot = body['snapshot'] update_dict = {} valid_update_keys = ( 'name', 'description', 'display_name', 'display_description', ) self.validate_name_and_description(snapshot) # NOTE(thingee): v2 API allows name instead of display_name if 'name' in snapshot: snapshot['display_name'] = snapshot.pop('name') # NOTE(thingee): v2 API allows description instead of # display_description if 'description' in snapshot: snapshot['display_description'] = snapshot.pop('description') for key in valid_update_keys: if key in snapshot: update_dict[key] = snapshot[key] # Not found exception will be handled at the wsgi level snapshot = self.volume_api.get_snapshot(context, id) volume_utils.notify_about_snapshot_usage(context, snapshot, 'update.start') self.volume_api.update_snapshot(context, snapshot, update_dict) snapshot.update(update_dict) req.cache_db_snapshot(snapshot) volume_utils.notify_about_snapshot_usage(context, snapshot, 'update.end') return self._view_builder.detail(req, snapshot)
def test_notify_about_snapshot_usage(self, mock_rpc, mock_conf, mock_usage): mock_conf.host = "host1" output = volume_utils.notify_about_snapshot_usage(mock.sentinel.context, mock.sentinel.snapshot, "test_suffix") self.assertIsNone(output) mock_usage.assert_called_once_with(mock.sentinel.snapshot) mock_rpc.get_notifier.assert_called_once_with("snapshot", "host1") mock_rpc.get_notifier.return_value.info.assert_called_once_with( mock.sentinel.context, "snapshot.test_suffix", mock_usage.return_value )
def update(self, req, id, body): """Update a snapshot.""" context = req.environ["cinder.context"] if not body: msg = _("Missing request body") raise exc.HTTPBadRequest(explanation=msg) if "snapshot" not in body: msg = _("Missing required element '%s' in request body") % "snapshot" raise exc.HTTPBadRequest(explanation=msg) snapshot = body["snapshot"] update_dict = {} valid_update_keys = ("name", "description", "display_name", "display_description") self.validate_name_and_description(snapshot) # NOTE(thingee): v2 API allows name instead of display_name if "name" in snapshot: snapshot["display_name"] = snapshot.pop("name") # NOTE(thingee): v2 API allows description instead of # display_description if "description" in snapshot: snapshot["display_description"] = snapshot.pop("description") for key in valid_update_keys: if key in snapshot: update_dict[key] = snapshot[key] try: snapshot = self.volume_api.get_snapshot(context, id) volume_utils.notify_about_snapshot_usage(context, snapshot, "update.start") self.volume_api.update_snapshot(context, snapshot, update_dict) except exception.SnapshotNotFound as error: raise exc.HTTPNotFound(explanation=error.msg) snapshot.update(update_dict) req.cache_db_snapshot(snapshot) volume_utils.notify_about_snapshot_usage(context, snapshot, "update.end") return self._view_builder.detail(req, snapshot)
def test_notify_about_snapshot_usage(self, mock_rpc, mock_conf, mock_usage): mock_conf.host = 'host1' output = volume_utils.notify_about_snapshot_usage( mock.sentinel.context, mock.sentinel.snapshot, 'test_suffix') self.assertIsNone(output) mock_usage.assert_called_once_with(mock.sentinel.snapshot) mock_rpc.get_notifier.assert_called_once_with('snapshot', 'host1') mock_rpc.get_notifier.return_value.info.assert_called_once_with( mock.sentinel.context, 'snapshot.test_suffix', mock_usage.return_value)
def test_notify_about_snapshot_usage_with_kwargs(self, mock_rpc, mock_conf, mock_usage): mock_conf.host = "host1" output = volume_utils.notify_about_snapshot_usage( mock.sentinel.context, mock.sentinel.snapshot, "test_suffix", extra_usage_info={"a": "b", "c": "d"}, host="host2", ) self.assertIsNone(output) mock_usage.assert_called_once_with(mock.sentinel.snapshot, a="b", c="d") mock_rpc.get_notifier.assert_called_once_with("snapshot", "host2") mock_rpc.get_notifier.return_value.info.assert_called_once_with( mock.sentinel.context, "snapshot.test_suffix", mock_usage.return_value )
def test_notify_about_snapshot_usage_with_kwargs(self, mock_rpc, mock_conf, mock_usage): mock_conf.host = 'host1' output = volume_utils.notify_about_snapshot_usage( mock.sentinel.context, mock.sentinel.snapshot, 'test_suffix', extra_usage_info={'a': 'b', 'c': 'd'}, host='host2') self.assertIsNone(output) mock_usage.assert_called_once_with(mock.sentinel.snapshot, a='b', c='d') mock_rpc.get_notifier.assert_called_once_with('snapshot', 'host2') mock_rpc.get_notifier.return_value.info.assert_called_once_with( mock.sentinel.context, 'snapshot.test_suffix', mock_usage.return_value)