def create(self, req, body): """Creates a migration process.""" ctxt = req.environ['guts.context'] authorize(ctxt) migration = body['migration'] name = migration.get('name', None) source_instance_id = migration.get('source_instance_id') description = migration.get('description') if description is not None: utils.check_string_length(description, 'Migration description', min_length=0, max_length=255) try: migration = migrations.create(ctxt, name, source_instance_id, description=description) req.cache_resource(migration, name='migrations') self._notify_migration_info( ctxt, 'migration.create', migration) except exception.MigrationExists as err: self._notify_migration_error( ctxt, 'migration.create', err, migration=migration) raise webob.exc.HTTPConflict(explanation=six.text_type(err)) except exception.MigrationNotFoundByName as err: self._notify_migration_error( ctxt, 'migration_.create', err, name=name) raise webob.exc.HTTPNotFound(explanation=err.msg) return self._view_builder.show(req, migration)
def _is_valid_as_reason(self, reason): if not reason: return False try: utils.check_string_length(reason.strip(), "Disabled reason", min_length=1, max_length=255) except exception.InvalidInput: return False return True
def _is_valid_as_reason(self, reason): if not reason: return False try: utils.check_string_length(reason.strip(), 'Disabled reason', min_length=1, max_length=255) except exception.InvalidInput: return False return True
def update(self, req, id, body): """Updates given source type.""" context = req.environ['guts.context'] authorize(context) stype = body['source_type'] name = stype.get('name', None) driver = stype.get('driver', None) description = stype.get('description', None) if not (name or driver or description): raise exception.Invalid('No attributes to update.') if driver and len(driver.strip()) == 0: msg = "Source type driver can not be empty." raise webob.exc.HTTPBadRequest(explanation=msg) utils.check_string_length(driver, 'Source Type driver', min_length=1, max_length=255) validate_type_driver(driver) if description is not None: utils.check_string_length(description, 'Type description', min_length=0, max_length=255) try: types.update(context, id, name, driver, description) modified_stype = types.get_source_type(context, id) req.cache_resource(modified_stype, name='types') self._notify_source_type_info(context, 'source_type.update', modified_stype) except exception.SourceTypeExists as err: self._notify_source_type_error(context, 'source_type.update', err, source_type=modified_stype) raise webob.exc.HTTPConflict(explanation=six.text_type(err)) except exception.SourceTypeNotFoundByName as err: self._notify_source_type_error(context, 'source_type.update', err, name=name) raise webob.exc.HTTPNotFound(explanation=err.msg) return self._view_builder.show(req, modified_stype)
def validate_string_length(value, entity_name, min_length=0, max_length=None, remove_whitespaces=False): """Check the length of specified string. :param value: the value of the string :param entity_name: the name of the string :param min_length: the min_length of the string :param max_length: the max_length of the string :param remove_whitespaces: True if trimming whitespaces is needed else False """ if isinstance(value, six.string_types) and remove_whitespaces: value = value.strip() try: utils.check_string_length(value, entity_name, min_length=min_length, max_length=max_length) except exception.InvalidInput as error: raise webob.exc.HTTPBadRequest(explanation=error.msg)
def validate_name_and_description(body): name = body.get("name") if name is not None: if isinstance(name, six.string_types): body["name"] = name.strip() try: utils.check_string_length(body["name"], "Name", min_length=0, max_length=255) except exception.InvalidInput as error: raise webob.exc.HTTPBadRequest(explanation=error.msg) description = body.get("description") if description is not None: try: utils.check_string_length(description, "Description", min_length=0, max_length=255) except exception.InvalidInput as error: raise webob.exc.HTTPBadRequest(explanation=error.msg)
def create(self, req, body): """Creates a new source hypervisor type.""" ctxt = req.environ['guts.context'] authorize(ctxt) stype = body['source_type'] name = stype.get('name', None) driver = stype.get('driver') description = stype.get('description') if driver is None or len(driver.strip()) == 0: msg = "Source type driver can not be empty." raise webob.exc.HTTPBadRequest(explanation=msg) utils.check_string_length(driver, 'Source Type driver', min_length=1, max_length=255) validate_type_driver(driver) if description is not None: utils.check_string_length(description, 'Type description', min_length=0, max_length=255) try: stype = types.create(ctxt, name, driver, description=description) req.cache_resource(stype, name='types') self._notify_source_type_info(ctxt, 'source_type.create', stype) except exception.SourceTypeExists as err: self._notify_source_type_error(ctxt, 'source_type.create', err, source_type=stype) raise webob.exc.HTTPConflict(explanation=six.text_type(err)) except exception.SourceTypeNotFoundByName as err: self._notify_source_type_error(ctxt, 'source_type.create', err, name=name) raise webob.exc.HTTPNotFound(explanation=err.msg) return self._view_builder.show(req, stype)
def update(self, req, id, body): """Updates given source type.""" context = req.environ['guts.context'] authorize(context) stype = body['source_type'] name = stype.get('name', None) driver = stype.get('driver', None) description = stype.get('description', None) if not (name or driver or description): raise exception.Invalid('No attributes to update.') if driver and len(driver.strip()) == 0: msg = "Source type driver can not be empty." raise webob.exc.HTTPBadRequest(explanation=msg) utils.check_string_length(driver, 'Source Type driver', min_length=1, max_length=255) validate_type_driver(driver) if description is not None: utils.check_string_length(description, 'Type description', min_length=0, max_length=255) try: types.update(context, id, name, driver, description) modified_stype = types.get_source_type(context, id) req.cache_resource(modified_stype, name='types') self._notify_source_type_info( context, 'source_type.update', modified_stype) except exception.SourceTypeExists as err: self._notify_source_type_error( context, 'source_type.update', err, source_type=modified_stype) raise webob.exc.HTTPConflict(explanation=six.text_type(err)) except exception.SourceTypeNotFoundByName as err: self._notify_source_type_error( context, 'source_type.update', err, name=name) raise webob.exc.HTTPNotFound(explanation=err.msg) return self._view_builder.show(req, modified_stype)
def create(self, req, body): """Creates a new source hypervisor type.""" ctxt = req.environ['guts.context'] authorize(ctxt) stype = body['source_type'] name = stype.get('name', None) driver = stype.get('driver') description = stype.get('description') if driver is None or len(driver.strip()) == 0: msg = "Source type driver can not be empty." raise webob.exc.HTTPBadRequest(explanation=msg) utils.check_string_length(driver, 'Source Type driver', min_length=1, max_length=255) validate_type_driver(driver) if description is not None: utils.check_string_length(description, 'Type description', min_length=0, max_length=255) try: stype = types.create(ctxt, name, driver, description=description) req.cache_resource(stype, name='types') self._notify_source_type_info( ctxt, 'source_type.create', stype) except exception.SourceTypeExists as err: self._notify_source_type_error( ctxt, 'source_type.create', err, source_type=stype) raise webob.exc.HTTPConflict(explanation=six.text_type(err)) except exception.SourceTypeNotFoundByName as err: self._notify_source_type_error( ctxt, 'source_type.create', err, name=name) raise webob.exc.HTTPNotFound(explanation=err.msg) return self._view_builder.show(req, stype)
def create(self, req, body): """Creates a new source hypervisor.""" ctxt = req.environ['guts.context'] authorize(ctxt) source = body['source'] name = source.get('name', None) stype = source.get('stype') connection_params = source.get('connection_params') description = source.get('description') if name is None or len(name.strip()) == 0: msg = "Source name can not be empty." raise webob.exc.HTTPBadRequest(explanation=msg) if connection_params is None or len(connection_params.strip()) == 0: msg = "Source connection params can not be empty." raise webob.exc.HTTPBadRequest(explanation=msg) utils.check_string_length(name, 'Hypervisor name', min_length=1, max_length=255) utils.check_string_length(connection_params, 'Source connection parameters', min_length=1, max_length=255) if description is not None: utils.check_string_length(description, 'Source description', min_length=0, max_length=255) try: sources.create(ctxt, name, stype, connection_params, description=description) source = sources.get_source_by_name(ctxt, name) req.cache_resource(source, name='sources') self._notify_source_info( ctxt, 'source.create', source) except exception.SourceExists as err: self._notify_source_error( ctxt, 'source.create', err, source=source) raise webob.exc.HTTPConflict(explanation=six.text_type(err)) except exception.SourceNotFoundByName as err: self._notify_source_error( ctxt, 'source_.create', err, name=name) raise webob.exc.HTTPNotFound(explanation=err.msg) vms.fetch_vms(ctxt, source.get('id')) return self._view_builder.show(req, ctxt, source)
def create(self, req, body): """Creates a new source hypervisor.""" ctxt = req.environ['guts.context'] authorize(ctxt) source = body['source'] name = source.get('name', None) stype = source.get('stype') connection_params = source.get('connection_params') description = source.get('description') if name is None or len(name.strip()) == 0: msg = "Source name can not be empty." raise webob.exc.HTTPBadRequest(explanation=msg) if connection_params is None or len(connection_params.strip()) == 0: msg = "Source connection params can not be empty." raise webob.exc.HTTPBadRequest(explanation=msg) utils.check_string_length(name, 'Hypervisor name', min_length=1, max_length=255) utils.check_string_length(connection_params, 'Source connection parameters', min_length=1, max_length=255) if description is not None: utils.check_string_length(description, 'Source description', min_length=0, max_length=255) try: source = sources.create(ctxt, name, stype, connection_params, description=description) req.cache_resource(source, name='sources') self._notify_source_info( ctxt, 'source.create', source) except exception.SourceExists as err: self._notify_source_error( ctxt, 'source.create', err, source=source) raise webob.exc.HTTPConflict(explanation=six.text_type(err)) except exception.SourceNotFoundByName as err: self._notify_source_error( ctxt, 'source_.create', err, name=name) raise webob.exc.HTTPNotFound(explanation=err.msg) vms.fetch_vms(ctxt, source.get('id')) return self._view_builder.show(req, ctxt, source)