def test_publish_a_non_packaged_file_object(self): response_xml = read_xml_asset(PUBLISH_XML) with requests_mock.mock() as m: m.post(self.baseurl, text=response_xml) new_datasource = TSC.DatasourceItem( 'ee8c6e70-43b6-11e6-af4f-f7b0d8e20760', 'SampleDS') publish_mode = self.server.PublishMode.CreateNew with open(asset('SampleDS.tds'), 'rb') as file_object: new_datasource = self.server.datasources.publish( new_datasource, file_object, mode=publish_mode) self.assertEqual('e76a1461-3b1d-4588-bf1b-17551a879ad9', new_datasource.id) self.assertEqual('SampleDS', new_datasource.name) self.assertEqual('SampleDS', new_datasource.content_url) self.assertEqual('dataengine', new_datasource.datasource_type) self.assertEqual('2016-08-11T21:22:40Z', format_datetime(new_datasource.created_at)) self.assertEqual('2016-08-17T23:37:08Z', format_datetime(new_datasource.updated_at)) self.assertEqual('ee8c6e70-43b6-11e6-af4f-f7b0d8e20760', new_datasource.project_id) self.assertEqual('default', new_datasource.project_name) self.assertEqual('5de011f8-5aa9-4d5b-b991-f462c8dd6bb7', new_datasource.owner_id)
def publishDataSource(filePath, projectId): tableauAuth = TSC.TableauAuth(tableauUserId, tableauPassword) server = TSC.Server(tableauServerAddress) if tableauIgnoreSslCert: server.add_http_options({'verify': False}) server.auth.sign_in(tableauAuth) datasourceCredentials = TSC.ConnectionCredentials(name=pgUser, password=pgPassword) datasource = TSC.DatasourceItem(projectId) if useCustomSQL: datasource.name = dataSourceName datasource = server.datasources.publish(datasource, filePath, 'CreateNew', datasourceCredentials) if verbose: print('Published datasource {} to Tableau Server running at {}'.format( datasource.name, tableauServerAddress)) lineClear() server.auth.sign_out()
def publish_to_server(site_name, server_address, project_name, tdsx_name, tableau_token_name, tableau_token): '''Publishes updated, local .tdsx to Tableau, overwriting the original file.''' # Creates the auth object based on the config file. tableau_auth = TSC.PersonalAccessTokenAuth( token_name=tableau_token_name, personal_access_token=tableau_token, site_id=site_name) server = TSC.Server(server_address) print(f"Signing into to site: {site_name}.") # Signs in and find the specified project. with server.auth.sign_in(tableau_auth): all_projects, pagination_item = server.projects.get() for project in TSC.Pager(server.projects): if project.name == project_name: project_id = project.id if project_id == None: message = "Could not find project. Please update the config file." sys.exit(message) print(f"Publishing to {project_name}.") # Publishes the data source. overwrite_true = TSC.Server.PublishMode.Overwrite datasource = TSC.DatasourceItem(project_id) file_path = os.path.join(os.getcwd(), tdsx_name) datasource = server.datasources.publish(datasource, file_path, overwrite_true) print(f"Publishing of datasource '{tdsx_name}' complete.")
def publishExtract(extract_file=None, server_address=None, site=None, project=None, username=None, password=None): tableau_auth = TSC.TableauAuth(username, password) server = TSC.Server(server_address) server.use_highest_version() with server.auth.sign_in(tableau_auth): # Query projects for use when demonstrating publishing and updating all_projects, pagination_item = server.projects.get() default_project = next( (project for project in all_projects if project.is_default()), None) if default_project is not None: new_datasource = TSC.DatasourceItem(default_project.id) new_datasource = server.datasources.publish( new_datasource, args.publish, TSC.Server.PublishMode.Overwrite) print("Datasource published. ID: {}".format(new_datasource.id)) else: print("Publish failed. Could not find the default project.")
def test_publish_unnamed_file_object(self): new_datasource = TSC.DatasourceItem('test') publish_mode = self.server.PublishMode.CreateNew with open(asset('SampleDS.tds'), 'rb') as file_object: self.assertRaises(ValueError, self.server.datasources.publish, new_datasource, file_object, publish_mode)
def test_publish_a_packaged_file_object(self) -> None: response_xml = read_xml_asset(PUBLISH_XML) with requests_mock.mock() as m: m.post(self.baseurl, text=response_xml) new_datasource = TSC.DatasourceItem( "ee8c6e70-43b6-11e6-af4f-f7b0d8e20760", "SampleDS") publish_mode = self.server.PublishMode.CreateNew # Create a dummy tdsx file in memory with BytesIO() as zip_archive: with ZipFile(zip_archive, "w") as zf: zf.write(asset("SampleDS.tds")) zip_archive.seek(0) new_datasource = self.server.datasources.publish( new_datasource, zip_archive, mode=publish_mode) self.assertEqual("e76a1461-3b1d-4588-bf1b-17551a879ad9", new_datasource.id) self.assertEqual("SampleDS", new_datasource.name) self.assertEqual("SampleDS", new_datasource.content_url) self.assertEqual("dataengine", new_datasource.datasource_type) self.assertEqual("2016-08-11T21:22:40Z", format_datetime(new_datasource.created_at)) self.assertEqual("2016-08-17T23:37:08Z", format_datetime(new_datasource.updated_at)) self.assertEqual("ee8c6e70-43b6-11e6-af4f-f7b0d8e20760", new_datasource.project_id) self.assertEqual("default", new_datasource.project_name) self.assertEqual("5de011f8-5aa9-4d5b-b991-f462c8dd6bb7", new_datasource.owner_id)
def test_publish_invalid_file_type(self): new_datasource = TSC.DatasourceItem( 'test', 'ee8c6e70-43b6-11e6-af4f-f7b0d8e20760') self.assertRaises(ValueError, self.server.datasources.publish, new_datasource, os.path.join(TEST_ASSET_DIR, 'SampleWB.twbx'), self.server.PublishMode.Append)
def test_delete_favorite_datasource(self) -> None: datasource = TSC.DatasourceItem("ee8c6e70-43b6-11e6-af4f-f7b0d8e20760") datasource._id = "e76a1461-3b1d-4588-bf1b-17551a879ad9" datasource.name = "SampleDS" with requests_mock.mock() as m: m.delete("{0}/{1}/datasources/{2}".format(self.baseurl, self.user.id, datasource.id)) self.server.favorites.delete_favorite_datasource(self.user, datasource)
def _publish_hyper_file( self, path_to_database, tab_ds_name, publish_mode=TSC.Server.PublishMode.CreateNew, ): """ Publishes a Hyper file to Tableau Server path_to_database (string): Hyper file to publish tab_ds_name (string): Target datasource name publish_mode: One of TSC.Server.[Overwrite|CreateNew] (default=CreateNew) Returns the Datasource ID """ # This is used to create new datasources and to implement the APPEND action # # Create the datasource object with the project_id datasource = TSC.DatasourceItem(self.tableau_project_id, name=tab_ds_name) logger.info(f"Publishing {path_to_database} to {tab_ds_name}...") # Publish datasource lock = self._datasource_lock(tab_ds_name) with lock: datasource = self.tableau_server.datasources.publish( datasource, path_to_database, publish_mode) logger.info("Datasource published. Datasource ID: {0}".format( datasource.id)) return datasource.id
def test_publish_multi_connection(self) -> None: new_datasource = TSC.DatasourceItem( name="Sample", project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760") connection1 = TSC.ConnectionItem() connection1.server_address = "mysql.test.com" connection1.connection_credentials = TSC.ConnectionCredentials( "test", "secret", True) connection2 = TSC.ConnectionItem() connection2.server_address = "pgsql.test.com" connection2.connection_credentials = TSC.ConnectionCredentials( "test", "secret", True) response = RequestFactory.Datasource._generate_xml( new_datasource, connections=[connection1, connection2]) # Can't use ConnectionItem parser due to xml namespace problems connection_results = fromstring(response).findall(".//connection") self.assertEqual(connection_results[0].get("serverAddress", None), "mysql.test.com") self.assertEqual( connection_results[0].find("connectionCredentials").get( "name", None), "test") # type: ignore[union-attr] self.assertEqual(connection_results[1].get("serverAddress", None), "pgsql.test.com") self.assertEqual( connection_results[1].find("connectionCredentials").get( "password", None), "secret") # type: ignore[union-attr]
def publish_datasources_to_site(server_object, project, folder): path = folder + '/*.tds*' for fname in glob.glob(path): new_ds = TSC.DatasourceItem(project.id) new_ds = server_object.datasources.publish(new_ds, fname, server_object.PublishMode.Overwrite) print("Datasource published. ID: {0}".format(new_ds.id))
def test_publish(self): with open(PUBLISH_XML, 'rb') as f: response_xml = f.read().decode('utf-8') with requests_mock.mock() as m: m.post(self.baseurl, text=response_xml) new_datasource = TSC.DatasourceItem( 'SampleDS', 'ee8c6e70-43b6-11e6-af4f-f7b0d8e20760') new_datasource = self.server.datasources.publish( new_datasource, os.path.join(TEST_ASSET_DIR, 'SampleDS.tds'), mode=self.server.PublishMode.CreateNew) self.assertEqual('e76a1461-3b1d-4588-bf1b-17551a879ad9', new_datasource.id) self.assertEqual('SampleDS', new_datasource.name) self.assertEqual('SampleDS', new_datasource.content_url) self.assertEqual('dataengine', new_datasource.datasource_type) self.assertEqual('2016-08-11T21:22:40Z', format_datetime(new_datasource.created_at)) self.assertEqual('2016-08-17T23:37:08Z', format_datetime(new_datasource.updated_at)) self.assertEqual('ee8c6e70-43b6-11e6-af4f-f7b0d8e20760', new_datasource.project_id) self.assertEqual('default', new_datasource.project_name) self.assertEqual('5de011f8-5aa9-4d5b-b991-f462c8dd6bb7', new_datasource.owner_id)
def test_populate_permissions(self): with open(asset(POPULATE_PERMISSIONS_XML), 'rb') as f: response_xml = f.read().decode('utf-8') with requests_mock.mock() as m: m.get(self.baseurl + '/0448d2ed-590d-4fa0-b272-a2a8a24555b5/permissions', text=response_xml) single_datasource = TSC.DatasourceItem('test') single_datasource._id = '0448d2ed-590d-4fa0-b272-a2a8a24555b5' self.server.datasources.populate_permissions(single_datasource) permissions = single_datasource.permissions self.assertEqual(permissions[0].grantee.tag_name, 'group') self.assertEqual(permissions[0].grantee.id, '5e5e1978-71fa-11e4-87dd-7382f5c437af') self.assertDictEqual( permissions[0].capabilities, { TSC.Permission.Capability.Delete: TSC.Permission.Mode.Deny, TSC.Permission.Capability.ChangePermissions: TSC.Permission.Mode.Deny, TSC.Permission.Capability.Connect: TSC.Permission.Mode.Allow, TSC.Permission.Capability.Read: TSC.Permission.Mode.Allow, }) self.assertEqual(permissions[1].grantee.tag_name, 'user') self.assertEqual(permissions[1].grantee.id, '7c37ee24-c4b1-42b6-a154-eaeab7ee330a') self.assertDictEqual( permissions[1].capabilities, { TSC.Permission.Capability.Write: TSC.Permission.Mode.Allow, })
def test_publish_multi_connection(self): new_datasource = TSC.DatasourceItem( name='Sample', project_id='ee8c6e70-43b6-11e6-af4f-f7b0d8e20760') connection1 = TSC.ConnectionItem() connection1.server_address = 'mysql.test.com' connection1.connection_credentials = TSC.ConnectionCredentials( 'test', 'secret', True) connection2 = TSC.ConnectionItem() connection2.server_address = 'pgsql.test.com' connection2.connection_credentials = TSC.ConnectionCredentials( 'test', 'secret', True) response = RequestFactory.Datasource._generate_xml( new_datasource, connections=[connection1, connection2]) # Can't use ConnectionItem parser due to xml namespace problems connection_results = ET.fromstring(response).findall('.//connection') self.assertEqual(connection_results[0].get('serverAddress', None), 'mysql.test.com') self.assertEqual( connection_results[0].find('connectionCredentials').get( 'name', None), 'test') self.assertEqual(connection_results[1].get('serverAddress', None), 'pgsql.test.com') self.assertEqual( connection_results[1].find('connectionCredentials').get( 'password', None), 'secret')
def test_populate_connections(self): response_xml = read_xml_asset(POPULATE_CONNECTIONS_XML) with requests_mock.mock() as m: m.get(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/connections', text=response_xml) single_datasource = TSC.DatasourceItem( 'test', '1d0304cd-3796-429f-b815-7258370b9b74') single_datasource.owner_id = 'dd2239f6-ddf1-4107-981a-4cf94e415794' single_datasource._id = '9dbd2263-16b5-46e1-9c43-a76bb8ab65fb' self.server.datasources.populate_connections(single_datasource) self.assertEqual('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', single_datasource.id) connections = single_datasource.connections self.assertTrue(connections) ds1, ds2 = connections self.assertEqual('be786ae0-d2bf-4a4b-9b34-e2de8d2d4488', ds1.id) self.assertEqual('textscan', ds1.connection_type) self.assertEqual('forty-two.net', ds1.server_address) self.assertEqual('duo', ds1.username) self.assertEqual(True, ds1.embed_password) self.assertEqual('970e24bc-e200-4841-a3e9-66e7d122d77e', ds2.id) self.assertEqual('sqlserver', ds2.connection_type) self.assertEqual('database.com', ds2.server_address) self.assertEqual('heero', ds2.username) self.assertEqual(False, ds2.embed_password)
def test_update_connection(self): populate_xml, response_xml = read_xml_assets(POPULATE_CONNECTIONS_XML, UPDATE_CONNECTION_XML) with requests_mock.mock() as m: m.get(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/connections', text=populate_xml) m.put( self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/connections/be786ae0-d2bf-4a4b-9b34-e2de8d2d4488', text=response_xml) single_datasource = TSC.DatasourceItem( 'test', '1d0304cd-3796-429f-b815-7258370b9b74') single_datasource.owner_id = 'dd2239f6-ddf1-4107-981a-4cf94e415794' single_datasource._id = '9dbd2263-16b5-46e1-9c43-a76bb8ab65fb' self.server.datasources.populate_connections(single_datasource) connection = single_datasource.connections[0] connection.server_address = 'bar' connection.server_port = '9876' connection.username = '******' new_connection = self.server.datasources.update_connection( single_datasource, connection) self.assertEqual(connection.id, new_connection.id) self.assertEqual(connection.connection_type, new_connection.connection_type) self.assertEqual('bar', new_connection.server_address) self.assertEqual('9876', new_connection.server_port) self.assertEqual('foo', new_connection.username)
def test_update(self) -> None: response_xml = read_xml_asset(UPDATE_XML) with requests_mock.mock() as m: m.put(self.baseurl + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb", text=response_xml) single_datasource = TSC.DatasourceItem( "1d0304cd-3796-429f-b815-7258370b9b74", "Sample datasource") single_datasource.owner_id = "dd2239f6-ddf1-4107-981a-4cf94e415794" single_datasource._content_url = "Sampledatasource" single_datasource._id = "9dbd2263-16b5-46e1-9c43-a76bb8ab65fb" single_datasource.certified = True single_datasource.certification_note = "Warning, here be dragons." updated_datasource = self.server.datasources.update( single_datasource) self.assertEqual(updated_datasource.id, single_datasource.id) self.assertEqual(updated_datasource.name, single_datasource.name) self.assertEqual(updated_datasource.content_url, single_datasource.content_url) self.assertEqual(updated_datasource.project_id, single_datasource.project_id) self.assertEqual(updated_datasource.owner_id, single_datasource.owner_id) self.assertEqual(updated_datasource.certified, single_datasource.certified) self.assertEqual(updated_datasource.certification_note, single_datasource.certification_note)
def close(self): """ Perform any necessary cleanup """ self.e.close() tableau_auth = TSC.TableauAuth(self.username, self.password, site_id=self.site_id) server = TSC.Server(self.server_url, use_server_version=True) server.auth.sign_in(tableau_auth) try: if self.datasource: server.datasources.publish(self.datasource, self.output_file, 'Overwrite', connection_credentials=None) else: new_datasource = TSC.DatasourceItem(self.project.id, name=self.output_table) new_datasource = server.datasources.publish( new_datasource, self.output_file, 'CreateNew') except: raise finally: server.auth.sign_out() os.remove(self.output_file)
def publishToTableauServer(username, password, project, tabUrl, unique_filename, mode): tableau_auth = TSC.TableauAuth(username, password) server = TSC.Server(tabUrl) server.add_http_options({'verify': CA}) project_id = '' file_path = unique_filename with server.auth.sign_in(tableau_auth): all_project_items, pagination_item = server.projects.get() for proj in all_project_items: if proj.name == project: project_id = proj.id new_datasource = TSC.DatasourceItem(project_id) try: new_datasource = server.datasources.publish( new_datasource, file_path, mode) logger.debug(' DataSource Published') except ServerResponseError as e: server.auth.sign_out() logger.error(' DataSource failed to publish ') logger.error(e) raise Exception(e) sys.exit(1) server.auth.sign_out() if os.path.exists(unique_filename): os.remove(unique_filename) else: logger.warning("The file does not exist")
def test_revisions(self) -> None: datasource = TSC.DatasourceItem("project", "test") datasource._id = "06b944d2-959d-4604-9305-12323c95e70e" response_xml = read_xml_asset(REVISION_XML) with requests_mock.mock() as m: m.get("{0}/{1}/revisions".format(self.baseurl, datasource.id), text=response_xml) self.server.datasources.populate_revisions(datasource) revisions = datasource.revisions self.assertEqual(len(revisions), 3) self.assertEqual("2016-07-26T20:34:56Z", format_datetime(revisions[0].created_at)) self.assertEqual("2016-07-27T20:34:56Z", format_datetime(revisions[1].created_at)) self.assertEqual("2016-07-28T20:34:56Z", format_datetime(revisions[2].created_at)) self.assertEqual(False, revisions[0].deleted) self.assertEqual(False, revisions[0].current) self.assertEqual(False, revisions[1].deleted) self.assertEqual(False, revisions[1].current) self.assertEqual(False, revisions[2].deleted) self.assertEqual(True, revisions[2].current) self.assertEqual("Cassie", revisions[0].user_name) self.assertEqual("5de011f8-5aa9-4d5b-b991-f462c8dd6bb7", revisions[0].user_id) self.assertIsNone(revisions[1].user_name) self.assertIsNone(revisions[1].user_id) self.assertEqual("Cassie", revisions[2].user_name) self.assertEqual("5de011f8-5aa9-4d5b-b991-f462c8dd6bb7", revisions[2].user_id)
def test_publish_unnamed_file_object(self) -> None: new_datasource = TSC.DatasourceItem("test") publish_mode = self.server.PublishMode.CreateNew with open(asset("SampleDS.tds"), "rb") as file_object: self.assertRaises(ValueError, self.server.datasources.publish, new_datasource, file_object, publish_mode)
def test_publish_hyper_file_object_raises_exception(self) -> None: new_datasource = TSC.DatasourceItem( "ee8c6e70-43b6-11e6-af4f-f7b0d8e20760", "test") with open(asset("World Indicators.hyper"), "rb") as file_object: self.assertRaises(ValueError, self.server.datasources.publish, new_datasource, file_object, self.server.PublishMode.Append)
def publish_hyper(): """ Shows how to leverage the Tableau Server Client (TSC) to sign in and publish an extract directly to Tableau Online/Server """ # Sign in to server tableau_auth = TSC.PersonalAccessTokenAuth(token_name=token_name, personal_access_token=token_value, site_id=site_name) server = TSC.Server(server_address, use_server_version=True) print(f"Signing into {site_name} at {server_address}") with server.auth.sign_in(tableau_auth): # Define publish mode - Overwrite, Append, or CreateNew publish_mode = TSC.Server.PublishMode.Overwrite # Get project_id from project_name all_projects, pagination_item = server.projects.get() for project in TSC.Pager(server.projects): if project.name == project_name: project_id = project.id # Create the datasource object with the project_id datasource = TSC.DatasourceItem(project_id) print(f"Publishing {hyper_name} to {project_name}...") # Publish datasource datasource = server.datasources.publish(datasource, path_to_hyper, publish_mode) print("Datasource published. Datasource ID: {0}".format(datasource.id))
def test_update_connection(self) -> None: populate_xml, response_xml = read_xml_assets(POPULATE_CONNECTIONS_XML, UPDATE_CONNECTION_XML) with requests_mock.mock() as m: m.get(self.baseurl + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/connections", text=populate_xml) m.put( self.baseurl + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/connections/be786ae0-d2bf-4a4b-9b34-e2de8d2d4488", text=response_xml, ) single_datasource = TSC.DatasourceItem( "1d0304cd-3796-429f-b815-7258370b9b74") single_datasource.owner_id = "dd2239f6-ddf1-4107-981a-4cf94e415794" single_datasource._id = "9dbd2263-16b5-46e1-9c43-a76bb8ab65fb" self.server.datasources.populate_connections(single_datasource) connection = single_datasource.connections[ 0] # type: ignore[index] connection.server_address = "bar" connection.server_port = "9876" connection.username = "******" new_connection = self.server.datasources.update_connection( single_datasource, connection) self.assertEqual(connection.id, new_connection.id) self.assertEqual(connection.connection_type, new_connection.connection_type) self.assertEqual("bar", new_connection.server_address) self.assertEqual("9876", new_connection.server_port) self.assertEqual("foo", new_connection.username)
def test_populate_connections(self) -> None: response_xml = read_xml_asset(POPULATE_CONNECTIONS_XML) with requests_mock.mock() as m: m.get(self.baseurl + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/connections", text=response_xml) single_datasource = TSC.DatasourceItem( "1d0304cd-3796-429f-b815-7258370b9b74", "test") single_datasource.owner_id = "dd2239f6-ddf1-4107-981a-4cf94e415794" single_datasource._id = "9dbd2263-16b5-46e1-9c43-a76bb8ab65fb" self.server.datasources.populate_connections(single_datasource) self.assertEqual("9dbd2263-16b5-46e1-9c43-a76bb8ab65fb", single_datasource.id) connections = single_datasource.connections self.assertTrue(connections) ds1, ds2 = connections self.assertEqual("be786ae0-d2bf-4a4b-9b34-e2de8d2d4488", ds1.id) self.assertEqual("textscan", ds1.connection_type) self.assertEqual("forty-two.net", ds1.server_address) self.assertEqual("duo", ds1.username) self.assertEqual(True, ds1.embed_password) self.assertEqual("970e24bc-e200-4841-a3e9-66e7d122d77e", ds2.id) self.assertEqual("sqlserver", ds2.connection_type) self.assertEqual("database.com", ds2.server_address) self.assertEqual("heero", ds2.username) self.assertEqual(False, ds2.embed_password)
def test_update_tags(self): with open(ADD_TAGS_XML, 'rb') as f: add_tags_xml = f.read().decode('utf-8') with open(UPDATE_XML, 'rb') as f: update_xml = f.read().decode('utf-8') with requests_mock.mock() as m: m.put(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/tags', text=add_tags_xml) m.delete(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/tags/b', status_code=204) m.delete(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/tags/d', status_code=204) m.put(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', text=update_xml) single_datasource = TSC.DatasourceItem( '1d0304cd-3796-429f-b815-7258370b9b74') single_datasource._id = '9dbd2263-16b5-46e1-9c43-a76bb8ab65fb' single_datasource._initial_tags.update(['a', 'b', 'c', 'd']) single_datasource.tags.update(['a', 'c', 'e']) updated_datasource = self.server.datasources.update( single_datasource) self.assertEqual(single_datasource.tags, updated_datasource.tags) self.assertEqual(single_datasource._initial_tags, updated_datasource._initial_tags)
def test_delete_revision(self) -> None: datasource = TSC.DatasourceItem("project", "test") datasource._id = "06b944d2-959d-4604-9305-12323c95e70e" with requests_mock.mock() as m: m.delete("{0}/{1}/revisions/3".format(self.baseurl, datasource.id)) self.server.datasources.delete_revision(datasource.id, "3")
def publish(resource_type, project_name, path, mode, server_url=None, username=None, password=None, server=None): """ Publish a datasource or workbook. Authetication happens by either passing the credentials (username, pass- word and server_url) or the server object when previosly authenticated. Parameters: resource_type -- workbook or datasource resource_name -- name of the resource to publish project_name -- name of the project the resource is stored in path -- path of the resource to publish mode -- 'CreateNew'/'Overwrite'/'Append' server_url -- the url of the server to connect with username -- username of the user to authenticate with password -- password of the user to authenticate with server -- the server object if authenticated previosly Return value(s): resource_id -- ID of the published workbook Exception(s): NameError -- if resource_type is neither workbook nor datasource """ # check if the either all the necessary credentials or the server object # are there and authenticate if necessary server, sign_out = check_credentials_authenticate(username, password, server_url, server) # get project_id project_id, _ = get_project_id(project_name, server) # if resource is a datasource create new object and publish if resource_type == "datasource": # Use the project id to create new datsource_item new_resource = TSC.DatasourceItem(project_id) # publish data source (specified in file_path) new_resource = server.datasources.publish(new_resource, path, mode) # if resource is workbook create new object and publish elif resource_type == "workbook": # create new workbook new_resource = TSC.WorkbookItem(project_id) # publish workbook new_resource = server.workbooks.publish(new_resource, path, mode=mode, as_job=False) # raise error if resource_type is neither workbook nor datasource else: raise NameError("Invalid resource_type") if sign_out is True: # sign out from server server.auth.sign_out() return (new_resource.id)
def test_add_favorite_datasource(self) -> None: response_xml = read_xml_asset(ADD_FAVORITE_DATASOURCE_XML) datasource = TSC.DatasourceItem("ee8c6e70-43b6-11e6-af4f-f7b0d8e20760") datasource._id = "e76a1461-3b1d-4588-bf1b-17551a879ad9" datasource.name = "SampleDS" with requests_mock.mock() as m: m.put("{0}/{1}".format(self.baseurl, self.user.id), text=response_xml) self.server.favorites.add_favorite_datasource(self.user, datasource)
def test_generate_xml(self): datasource_item: TSC.DatasourceItem = TSC.DatasourceItem("name") datasource_item.name = "a ds" datasource_item.description = "described" datasource_item.use_remote_query_agent = False datasource_item.ask_data_enablement = DatasourceItem.AskDataEnablement.Enabled datasource_item.project_id = "testval" TSC_RF.RequestFactory.Datasource._generate_xml(datasource_item)