diff --git a/tabcmd.py b/tabcmd.py index 1c987d62..55e7e634 100644 --- a/tabcmd.py +++ b/tabcmd.py @@ -1,4 +1,4 @@ -from src import tabcmd +from tabcmd import tabcmd if __name__ == "__main__": tabcmd.main() diff --git a/tabcmd/commands/site/list_command.py b/tabcmd/commands/site/list_command.py new file mode 100644 index 00000000..2eff8ec5 --- /dev/null +++ b/tabcmd/commands/site/list_command.py @@ -0,0 +1,45 @@ +import tableauserverclient as TSC + +from tabcmd.commands.auth.session import Session +from tabcmd.commands.constants import Errors +from tabcmd.commands.server import Server +from tabcmd.execution.global_options import * +from tabcmd.execution.localize import _ +from tabcmd.execution.logger_config import log + + +class ListCommand(Server): + """ + Command to return a list of content the user can access + """ + + name: str = "list" + description: str = "List content items of a specified type" + + @staticmethod + def define_args(list_parser): + list_parser.add_argument("content", choices=["projects", "workbooks", "datasources"], help="View content") + + @staticmethod + def run_command(args): + logger = log(__name__, args.logging_level) + logger.debug(_("tabcmd.launching")) + session = Session() + server = session.create_session(args) + content_type = args.content + + try: + if content_type == "projects": + items = server.projects.all() + elif content_type == "workbooks": + items = server.workbooks.all() + elif content_type == "datasources": + items = server.datasources.all() + + logger.info("===== Listing {0} content for user {1}...".format(content_type, session.username)) + for item in items: + print("NAME:".rjust(10), item.name) + print("ID:".rjust(10), item.id) + + except TSC.ServerResponseError as e: + Errors.exit_with_error(logger, e) diff --git a/tabcmd/commands/site/list_sites_command.py b/tabcmd/commands/site/list_sites_command.py index 418f6caf..8a371ccb 100644 --- a/tabcmd/commands/site/list_sites_command.py +++ b/tabcmd/commands/site/list_sites_command.py @@ -30,10 +30,9 @@ def run_command(args): sites, pagination = server.sites.get() logger.info(_("listsites.status").format(session.username)) for site in sites: - print("NAME:", site.name) - print("SITEID:", site.content_url) + print("NAME:".rjust(10), site.name) + print("SITEID:".rjust(10), site.content_url) if args.get_extract_encryption_mode: print("EXTRACTENCRYPTION:", site.extract_encryption_mode) - print("") except TSC.ServerResponseError as e: Errors.exit_with_error(logger, e) diff --git a/tabcmd/execution/map_of_commands.py b/tabcmd/execution/map_of_commands.py index 87ff0a1d..c6f7a679 100644 --- a/tabcmd/execution/map_of_commands.py +++ b/tabcmd/execution/map_of_commands.py @@ -23,6 +23,7 @@ from tabcmd.commands.user.add_users_command import * from tabcmd.commands.user.create_site_users import * from tabcmd.commands.user.delete_site_users_command import * +from tabcmd.commands.site.list_command import * # from tabcmd.commands.user.create_users import * from tabcmd.commands.user.remove_users_command import * @@ -51,6 +52,7 @@ class CommandsMap: GetUrl, HelpCommand, ListSiteCommand, + ListCommand, LoginCommand, LogoutCommand, PublishCommand, diff --git a/tests/e2e/online_tests.py b/tests/e2e/online_tests.py index 7aa69610..8906c9b8 100644 --- a/tests/e2e/online_tests.py +++ b/tests/e2e/online_tests.py @@ -106,6 +106,11 @@ def _delete_extract(self, wb_name): arguments = [command, "-w", wb_name] _test_command(arguments) + def _list(self, item_type: str): + command = "list" + arguments = [command, item_type] + _test_command(arguments) + # actual tests TWBX_FILE_WITH_EXTRACT = "extract-data-access.twbx" TWBX_WITH_EXTRACT_NAME = "WorkbookWithExtract" @@ -192,6 +197,10 @@ def test_create_projects(self): self._create_project(project_name, parent_path) time.sleep(indexing_sleep_time) + @pytest.mark.order(8) + def test_list_projects(self): + self._list("projects") + @pytest.mark.order(9) def test_delete_projects(self): self._delete_project("project_name_2", project_name) # project 2