def check_shadow_table(migrate_engine, table_name): """This method checks that table with ``table_name`` and corresponding shadow table have same columns. """ meta = MetaData() meta.bind = migrate_engine table = Table(table_name, meta, autoload=True) shadow_table = Table(db._SHADOW_TABLE_PREFIX + table_name, meta, autoload=True) columns = dict([(c.name, c) for c in table.columns]) shadow_columns = dict([(c.name, c) for c in shadow_table.columns]) for name, column in columns.iteritems(): if name not in shadow_columns: raise exception.RackException( _("Missing column %(table)s.%(column)s in shadow table") % {'column': name, 'table': shadow_table.name}) shadow_column = shadow_columns[name] if not isinstance(shadow_column.type, type(column.type)): raise exception.RackException( _("Different types in %(table)s.%(column)s and shadow table: " "%(c_type)s %(shadow_c_type)s") % {'column': name, 'table': table.name, 'c_type': column.type, 'shadow_c_type': shadow_column.type}) for name, column in shadow_columns.iteritems(): if name not in columns: raise exception.RackException( _("Extra column %(table)s.%(column)s in shadow table") % {'column': name, 'table': shadow_table.name}) return True
def _get_not_supported_column(col_name_col_instance, column_name): try: column = col_name_col_instance[column_name] except Exception: msg = _("Please specify column %s in col_name_col_instance " "param. It is required because column has unsupported " "type by sqlite).") raise exception.RackException(msg % column_name) if not isinstance(column, Column): msg = _("col_name_col_instance param has wrong type of " "column instance for column %s It should be instance " "of sqlalchemy.Column.") raise exception.RackException(msg % column_name) return column
def test_create_exception_db_securitygroup_create(self): self.stubs.Set(db, "group_get_by_gid", fake_group_get_by_id) self.mox.StubOutWithMock(manager.ResourceOperator, "securitygroup_create") name = "test_securitygroup" securty_group = { "securitygroup": { "securitygroup_id": SECURITYGROUP_ID, "neutron_securitygroup_id": "fake_id", "gid": GID, "display_name": name, "is_default": True, } } manager.ResourceOperator.securitygroup_create( IsA(context.RequestContext), name, IsA(list)).AndReturn(securty_group) self.mox.StubOutWithMock(db, "securitygroup_create") db.securitygroup_create(IsA(context.RequestContext), IsA(dict))\ .AndRaise(exception.RackException()) self.mox.ReplayAll() request_body = { "securitygroup": { "name": name, "is_default": "true", } } url = get_base_url(GID) req = get_request(url, 'POST', request_body) res = req.get_response(self.app) self.assertEqual(res.status_code, 500)
def create_shadow_table(migrate_engine, table_name=None, table=None, **col_name_col_instance): """This method create shadow table for table with name ``table_name`` or table instance ``table``. :param table_name: Autoload table with this name and create shadow table :param table: Autoloaded table, so just create corresponding shadow table. :param col_name_col_instance: contains pair column_name=column_instance. column_instance is instance of Column. These params are required only for columns that have unsupported types by sqlite. For example BigInteger. :returns: The created shadow_table object. """ meta = MetaData(bind=migrate_engine) if table_name is None and table is None: raise exception.RackException(_("Specify `table_name` or `table` " "param")) if not (table_name is None or table is None): raise exception.RackException(_("Specify only one param `table_name` " "`table`")) if table is None: table = Table(table_name, meta, autoload=True) columns = [] for column in table.columns: if isinstance(column.type, NullType): new_column = _get_not_supported_column(col_name_col_instance, column.name) columns.append(new_column) else: columns.append(column.copy()) shadow_table_name = db._SHADOW_TABLE_PREFIX + table.name shadow_table = Table(shadow_table_name, meta, *columns, mysql_engine='InnoDB') try: shadow_table.create() return shadow_table except (OperationalError, ProgrammingError): LOG.info(repr(shadow_table)) LOG.exception(_('Exception while creating table.')) raise exception.ShadowTableExists(name=shadow_table_name) except Exception: LOG.info(repr(shadow_table)) LOG.exception(_('Exception while creating table.'))
def convert_version_to_int(version): try: if isinstance(version, six.string_types): version = convert_version_to_tuple(version) if isinstance(version, tuple): return reduce(lambda x, y: (x * 1000) + y, version) except Exception: raise exception.RackException(message="Hypervisor version invalid.")
def get_my_linklocal(interface): try: if_str = execute('ip', '-f', 'inet6', '-o', 'addr', 'show', interface) condition = '\s+inet6\s+([0-9a-f:]+)/\d+\s+scope\s+link' links = [re.search(condition, x) for x in if_str[0].split('\n')] address = [w.group(1) for w in links if w is not None] if address[0] is not None: return address[0] else: msg = _('Link Local address is not found.:%s') % if_str raise exception.RackException(msg) except Exception as ex: msg = _("Couldn't get Link Local IP of %(interface)s" " :%(ex)s") % { 'interface': interface, 'ex': ex } raise exception.RackException(msg)
def db_sync(version=None): if version is not None: try: version = int(version) except ValueError: raise exception.RackException(_("version should be an integer")) current_version = db_version() repository = _find_migrate_repo() if version is None or version > current_version: return versioning_api.upgrade(get_engine(), repository, version) else: return versioning_api.downgrade(get_engine(), repository, version)
def test_index_raise_exception_by_manager(self): self.stubs.Set(db, "securitygroup_get_all", fake_securitygroup_get_all) self.mox.StubOutWithMock(manager.ResourceOperator, "securitygroup_list") manager.ResourceOperator.securitygroup_list( IsA(context.RequestContext), IsA(list)).AndRaise(exception.RackException()) self.mox.ReplayAll() url = get_base_url(GID) req = get_request(url, 'GET') res = req.get_response(self.app) self.assertEqual(res.status_code, 500)
def _get_ipv4_address_for_interface(iface): """Run ip addr show for an interface and grab its ipv4 addresses """ try: out = execute('ip', '-f', 'inet', '-o', 'addr', 'show', iface) regexp_address = re.compile('inet\s*' '(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})') address = [ m.group(1) for m in regexp_address.finditer(out[0]) if m.group(1) != '127.0.0.1' ] if address: return address[0] else: msg = _('IPv4 address is not found.: %s') % out[0] raise exception.RackException(msg) except Exception as ex: msg = _("Couldn't get IPv4 of %(interface)s" " : %(ex)s") % { 'interface': iface, 'ex': ex } LOG.error(msg) raise exception.RackException(msg)
def db_version(): repository = _find_migrate_repo() try: return versioning_api.db_version(get_engine(), repository) except versioning_exceptions.DatabaseNotControlledError: meta = sqlalchemy.MetaData() engine = get_engine() meta.reflect(bind=engine) tables = meta.tables if len(tables) == 0: db_version_control(INIT_VERSION) return versioning_api.db_version(get_engine(), repository) else: # Some pre-Essex DB's may not be version controlled. # Require them to upgrade using Essex first. raise exception.RackException( _("Upgrade DB using Essex release first."))
def __get_backend(self): if not self.__backend: if self.__config_group is None: backend_name = CONF[self.__pivot] else: backend_name = CONF[self.__config_group][self.__pivot] if backend_name not in self.__backends: msg = _('Invalid backend: %s') % backend_name raise exception.RackException(msg) backend = self.__backends[backend_name] if isinstance(backend, tuple): name = backend[0] fromlist = backend[1] else: name = backend fromlist = backend self.__backend = __import__(name, None, None, fromlist) return self.__backend
def test_delete_exeption_manager_securitygroup_delete(self): self.mox.StubOutWithMock(db, "securitygroup_get_by_securitygroup_id") db.securitygroup_get_by_securitygroup_id( IsA(context.RequestContext), GID, SECURITYGROUP_ID)\ .AndReturn({"processes": [], "neutron_securitygroup_id": "fake_id"}) self.mox.StubOutWithMock(manager.ResourceOperator, "securitygroup_delete") manager.ResourceOperator.securitygroup_delete( IsA(context.RequestContext), "fake_id")\ .AndRaise(exception.RackException()) self.mox.ReplayAll() url = get_base_url(GID) + "/" + SECURITYGROUP_ID req = get_request(url, "DELETE") res = req.get_response(self.app) self.assertEqual(res.status_code, 500)
def test_create_exception_manager_securitygroup_create(self): self.stubs.Set(db, "group_get_by_gid", fake_group_get_by_id) self.mox.StubOutWithMock(manager.ResourceOperator, "securitygroup_create") name = "test_securitygroup" manager.ResourceOperator.securitygroup_create( IsA(context.RequestContext), name, IsA(list)).AndRaise(exception.RackException()) self.mox.ReplayAll() request_body = { "securitygroup": { "name": name, "is_default": "true", } } url = get_base_url(GID) req = get_request(url, 'POST', request_body) res = req.get_response(self.app) self.assertEqual(res.status_code, 500)
def _get_default_deleted_value(table): if isinstance(table.c.id.type, Integer): return 0 if isinstance(table.c.id.type, String): return "" raise exception.RackException(_("Unsupported id columns type"))