def get_item(self, id_, params, context=None): """Retrieve item with id id_ from model. Args: id_: The ID of the item to retrieve params: A dict-like object containing parameters from the request query string and body. context: Key-values providing frame of reference of request Returns: The matching item or None if item with id_ does not exist. """ caller, source_id = api_utils.get_id_from_context(context, self.datasource_mgr) table = context.get('table_id') args = {'source_id': source_id} try: schema = self.invoke_rpc(caller, 'get_datasource_schema', args) except exception.CongressException as e: raise webservice.DataModelException(e.code, str(e), http_status_code=e.code) # request to see the schema for one table if table: if table not in schema: raise webservice.DataModelException( 404, ("Table '{}' for datasource '{}' has no " "schema ".format(id_, source_id)), http_status_code=404) return api_utils.create_table_dict(table, schema) tables = [api_utils.create_table_dict(table_, schema) for table_ in schema] return {'tables': tables}
def get_item(self, id_, params, context=None): """Retrieve item with id id_ from model. Args: id_: The ID of the item to retrieve params: A dict-like object containing parameters from the request query string and body. context: Key-values providing frame of reference of request Returns: The matching item or None if item with id_ does not exist. """ datasource = context.get('ds_id') table = context.get('table_id') try: schema = self.rpc(self.datasource_mgr, 'get_datasource_schema', datasource) except (datasource_manager.DatasourceNotFound, datasource_manager.DriverNotFound) as e: raise webservice.DataModelException(e.code, e.message, http_status_code=e.code) # request to see the schema for one table if table: if table not in schema: raise webservice.DataModelException( 404, ("Table '{}' for datasource '{}' has no " "schema ".format(id_, datasource)), http_status_code=404) return api_utils.create_table_dict(table, schema) tables = [api_utils.create_table_dict(table_, schema) for table_ in schema] return {'tables': tables}
def get_item(self, id_, params, context=None): """Retrieve item with id id_ from model. Args: id_: The ID of the item to retrieve params: A dict-like object containing parameters from the request query string and body. context: Key-values providing frame of reference of request Returns: The matching item or None if item with id_ does not exist. """ datasource = context.get('driver_id') try: driver = self.bus.get_driver_info(datasource) schema = self.bus.get_driver_schema(datasource) except exception.DriverNotFound as e: raise webservice.DataModelException(e.code, str(e), http_status_code=e.code) tables = [ api_utils.create_table_dict(table_, schema) for table_ in schema ] driver['tables'] = tables return driver
def test_get_item_table(self): context = {'ds_id': self.data.service_id, 'table_id': 'fake_table'} fake_schema = self.data.get_schema() fake_table = api_utils.create_table_dict( "fake_table", fake_schema) table = self.schema_model.get_item(None, {}, context=context) self.assertEqual(fake_table, table)
def get_item(self, id_, params, context=None): """Retrieve item with id id_ from model. Args: id_: The ID of the item to retrieve params: A dict-like object containing parameters from the request query string and body. context: Key-values providing frame of reference of request Returns: The matching item or None if item with id_ does not exist. """ datasource = context.get('driver_id') try: schema = self.rpc(self.datasource_mgr, 'get_driver_schema', datasource) driver = self.rpc(self.datasource_mgr, 'get_driver_info', datasource) except datasource_manager.DriverNotFound as e: raise webservice.DataModelException(e.code, e.message, http_status_code=e.code) tables = [api_utils.create_table_dict(table_, schema) for table_ in schema] driver['tables'] = tables return driver
def test_get_item_all_table(self): context = {'ds_id': self.data.service_id} schema = self.data.get_schema() fake_tables = {'tables': [api_utils.create_table_dict( table_, schema) for table_ in schema]} tables = self.schema_model.get_item(None, {}, context=context) self.assertEqual(fake_tables, tables)
def test_create_table_dict(self): table_name = 'fake_table' schema = {'fake_table': ('id', 'name')} expected = {'table_id': table_name, 'columns': [{'name': 'id', 'description': 'None'}, {'name': 'name', 'description': 'None'}]} result = api_utils.create_table_dict(table_name, schema) self.assertEqual(expected, result)
def test_get_item_all_table(self): context = {'ds_id': self.data.service_id} schema = self.data.get_schema() fake_tables = { 'tables': [api_utils.create_table_dict(table_, schema) for table_ in schema] } tables = self.schema_model.get_item(None, {}, context=context) self.assertEqual(fake_tables, tables)
def get_item(self, id_, params, context=None): """Retrieve item with id id_ from model. Args: id_: The ID of the item to retrieve params: A dict-like object containing parameters from the request query string and body. context: Key-values providing frame of reference of request Returns: The matching item or None if item with id_ does not exist. """ # Note(thread-safety): blocking call caller, source_id = api_utils.get_id_from_context(context) # FIXME(threod-safety): in DSE2, the returned caller can be a # datasource name. But the datasource name may now refer to a new, # unrelated datasource. Causing the rest of this code to operate on # an unintended datasource. # Fix: check UUID of datasource before operating. Abort if mismatch table = context.get('table_id') args = {'source_id': source_id} try: # Note(thread-safety): blocking call schema = self.invoke_rpc(caller, 'get_datasource_schema', args) except exception.CongressException as e: raise webservice.DataModelException(e.code, str(e), http_status_code=e.code) # request to see the schema for one table if table: if table not in schema: raise webservice.DataModelException( 404, ("Table '{}' for datasource '{}' has no " "schema ".format(id_, source_id)), http_status_code=404) return api_utils.create_table_dict(table, schema) tables = [ api_utils.create_table_dict(table_, schema) for table_ in schema ] return {'tables': tables}
def test_get_item_table(self): context = {'ds_id': 'fake_datasource', 'table_id': 'fake_table'} fake_schema = fake_datasource.FakeDataSource.get_schema() fake_table = api_utils.create_table_dict( "fake_table", fake_schema) with mock.patch.object(self.schema_model.datasource_mgr, "get_datasource_schema", return_value=fake_schema): table = self.schema_model.get_item(None, {}, context=context) self.assertEqual(table, fake_table)
def test_get_item_all_table(self): context = {'ds_id': 'fake_datasource'} schema = fake_datasource.FakeDataSource.get_schema() fake_tables = {'tables': [api_utils.create_table_dict( table_, schema) for table_ in schema]} with mock.patch.object(self.schema_model.datasource_mgr, "get_datasource_schema", return_value=schema): tables = self.schema_model.get_item(None, {}, context=context) self.assertEqual(tables, fake_tables)
def get_item(self, id_, params, context=None): """Retrieve item with id id\_ from model. :param: id\_: The ID of the item to retrieve :param: params: A dict-like object containing parameters from the request query string and body. :param: context: Key-values providing frame of reference of request :returns: The matching item or None if item with id\_ does not exist. """ datasource = context.get('driver_id') try: driver = self.bus.get_driver_info(datasource) schema = self.bus.get_driver_schema(datasource) except exception.DriverNotFound as e: raise webservice.DataModelException(e.code, str(e), http_status_code=e.code) tables = [api_utils.create_table_dict(table_, schema) for table_ in schema] driver['tables'] = tables return driver
def test_create_table_dict(self): table_name = 'fake_table' schema = { 'fake_table': ({ 'name': 'id', 'desc': None }, { 'name': 'name', 'desc': None }) } expected = { 'table_id': table_name, 'columns': [{ 'name': 'id', 'description': None }, { 'name': 'name', 'description': None }] } result = api_utils.create_table_dict(table_name, schema) self.assertEqual(expected, result)
def test_get_item_table(self): context = {'ds_id': self.data.service_id, 'table_id': 'fake_table'} fake_schema = self.data.get_schema() fake_table = api_utils.create_table_dict("fake_table", fake_schema) table = self.schema_model.get_item(None, {}, context=context) self.assertEqual(fake_table, table)