def main(): parser = argparse.ArgumentParser( description='Get all of the refresh tasks available on a server') parser.add_argument('--server', '-s', required=True, help='server address') parser.add_argument('--username', '-u', required=True, help='username to sign into server') parser.add_argument('--site', '-S', default=None) parser.add_argument('-p', default=None, help='password') parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', help='desired logging level (set to error by default)') args = parser.parse_args() if args.p is None: password = getpass.getpass("Password: ") else: password = args.p # Set logging level based on user input, or error by default logging_level = getattr(logging, args.logging_level.upper()) logging.basicConfig(level=logging_level) tableau_auth = TSC.TableauAuth(args.username, password) server = TSC.Server(args.server) with server.auth.sign_in(tableau_auth): # Use highest Server REST API version available server.use_server_version() # Without parent_id specified, projects are created at the top level. top_level_project = TSC.ProjectItem(name='Top Level Project') top_level_project = create_project(server, top_level_project) # Specifying parent_id creates a nested projects. child_project = TSC.ProjectItem(name='Child Project', parent_id=top_level_project.id) child_project = create_project(server, child_project) # Projects can be nested at any level. grand_child_project = TSC.ProjectItem(name='Grand Child Project', parent_id=child_project.id) grand_child_project = create_project(server, grand_child_project)
def test_delete_permission(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_group = TSC.GroupItem('Group1') single_group._id = 'c8f2773a-c83a-11e8-8c8f-33e6d787b506' single_project = TSC.ProjectItem('Project3') single_project._id = '0448d2ed-590d-4fa0-b272-a2a8a24555b5' self.server.projects.populate_permissions(single_project) permissions = single_project.permissions capabilities = {} for permission in permissions: if permission.grantee.tag_name == "group": if permission.grantee.id == single_group._id: capabilities = permission.capabilities rules = TSC.PermissionsRule( grantee=single_group, capabilities=capabilities ) endpoint = '{}/permissions/groups/{}'.format(single_project._id, single_group._id) m.delete('{}/{}/Read/Allow'.format(self.baseurl, endpoint), status_code=204) m.delete('{}/{}/Write/Allow'.format(self.baseurl, endpoint), status_code=204) self.server.projects.delete_permission(item=single_project, rules=rules)
def _create_project(): with SERVER.auth.sign_in(AUTH): project = SERVER.projects.create( TSC.ProjectItem(name=PROJECT_NAME, description='This is description', content_permissions='ManagedByOwner')) return project
def test_update_datasource_default_permission(self): response_xml = read_xml_asset(UPDATE_DATASOURCE_DEFAULT_PERMISSIONS_XML) with requests_mock.mock() as m: m.put(self.baseurl + '/b4065286-80f0-11ea-af1b-cb7191f48e45/default-permissions/datasources', text=response_xml) project = TSC.ProjectItem('test-project') project._id = 'b4065286-80f0-11ea-af1b-cb7191f48e45' group = TSC.GroupItem('test-group') group._id = 'b4488bce-80f0-11ea-af1c-976d0c1dab39' capabilities = {TSC.Permission.Capability.ExportXml: TSC.Permission.Mode.Deny} rules = [TSC.PermissionsRule( grantee=group, capabilities=capabilities )] new_rules = self.server.projects.update_datasource_default_permissions(project, rules) self.assertEquals('b4488bce-80f0-11ea-af1c-976d0c1dab39', new_rules[0].grantee.id) updated_capabilities = new_rules[0].capabilities self.assertEquals(4, len(updated_capabilities)) self.assertEquals('Deny', updated_capabilities['ExportXml']) self.assertEquals('Allow', updated_capabilities['Read']) self.assertEquals('Allow', updated_capabilities['Write']) self.assertEquals('Allow', updated_capabilities['Connect'])
def test_populate_workbooks(self): response_xml = read_xml_asset(POPULATE_WORKBOOK_DEFAULT_PERMISSIONS_XML) with requests_mock.mock() as m: m.get(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/default-permissions/workbooks', text=response_xml) single_project = TSC.ProjectItem('test', '1d0304cd-3796-429f-b815-7258370b9b74') single_project.owner_id = 'dd2239f6-ddf1-4107-981a-4cf94e415794' single_project._id = '9dbd2263-16b5-46e1-9c43-a76bb8ab65fb' self.server.projects.populate_workbook_default_permissions(single_project) permissions = single_project.default_workbook_permissions rule1 = permissions.pop() self.assertEqual('c8f2773a-c83a-11e8-8c8f-33e6d787b506', rule1.grantee.id) self.assertEqual('group', rule1.grantee.tag_name) self.assertDictEqual(rule1.capabilities, { TSC.Permission.Capability.Write: TSC.Permission.Mode.Allow, TSC.Permission.Capability.Read: TSC.Permission.Mode.Allow, TSC.Permission.Capability.Filter: TSC.Permission.Mode.Allow, TSC.Permission.Capability.ChangePermissions: TSC.Permission.Mode.Allow, TSC.Permission.Capability.WebAuthoring: TSC.Permission.Mode.Allow, TSC.Permission.Capability.ExportData: TSC.Permission.Mode.Allow, TSC.Permission.Capability.ExportXml: TSC.Permission.Mode.Allow, TSC.Permission.Capability.ExportImage: TSC.Permission.Mode.Allow, TSC.Permission.Capability.Delete: TSC.Permission.Mode.Deny, TSC.Permission.Capability.ShareView: TSC.Permission.Mode.Allow, TSC.Permission.Capability.ViewUnderlyingData: TSC.Permission.Mode.Deny, TSC.Permission.Capability.ViewComments: TSC.Permission.Mode.Allow, TSC.Permission.Capability.AddComment: TSC.Permission.Mode.Allow, TSC.Permission.Capability.ChangeHierarchy: TSC.Permission.Mode.Allow, })
def create_project(self, project_name, parent_id=None): """ Create a new project on the current site or a specified site. PARAMETERS ----- project_name : STRING Name of the new project. parent_id : STRING *optional ID of the Parent project if there is one RETURNS ----- PROJECT_ITEM """ switched_sites = False if self.is_current_site is False: switched_sites = True self.set_as_current_site() new_project = tsc.ProjectItem(name=project_name, parent_id=parent_id) new_project = self.__server.item.projects.create(new_project) new_project = projectItem.Project_Item(self.__server, new_project) if switched_sites is True: self.__server.switch_current_site(self.__server.previous_site) return new_project
def test_delete_permission(self) -> None: 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_group = TSC.GroupItem("Group1") single_group._id = "c8f2773a-c83a-11e8-8c8f-33e6d787b506" single_project = TSC.ProjectItem("Project3") single_project._id = "0448d2ed-590d-4fa0-b272-a2a8a24555b5" self.server.projects.populate_permissions(single_project) permissions = single_project.permissions capabilities = {} for permission in permissions: if permission.grantee.tag_name == "group": if permission.grantee.id == single_group._id: capabilities = permission.capabilities rules = TSC.PermissionsRule(grantee=single_group.to_reference(), capabilities=capabilities) endpoint = "{}/permissions/groups/{}".format( single_project._id, single_group._id) m.delete("{}/{}/Read/Allow".format(self.baseurl, endpoint), status_code=204) m.delete("{}/{}/Write/Allow".format(self.baseurl, endpoint), status_code=204) self.server.projects.delete_permission(item=single_project, rules=rules)
def test_delete_favorite_project(self) -> None: self.server.version = "3.1" baseurl = self.server.favorites.baseurl project = TSC.ProjectItem("Tableau") project._id = "1d0304cd-3796-429f-b815-7258370b9b74" with requests_mock.mock() as m: m.delete("{0}/{1}/projects/{2}".format(baseurl, self.user.id, project.id)) self.server.favorites.delete_favorite_project(self.user, project)
def test_delete_favorite_project(self): self.server.version = '3.1' baseurl = self.server.favorites.baseurl project = TSC.ProjectItem('Tableau') project._id = '1d0304cd-3796-429f-b815-7258370b9b74' with requests_mock.mock() as m: m.delete('{0}/{1}/projects/{2}'.format(baseurl, self.user.id, project.id)) self.server.favorites.delete_favorite_project(self.user, project)
def test_add_favorite_project(self) -> None: self.server.version = "3.1" baseurl = self.server.favorites.baseurl response_xml = read_xml_asset(ADD_FAVORITE_PROJECT_XML) project = TSC.ProjectItem("Tableau") project._id = "1d0304cd-3796-429f-b815-7258370b9b74" with requests_mock.mock() as m: m.put("{0}/{1}".format(baseurl, self.user.id), text=response_xml) self.server.favorites.add_favorite_project(self.user, project)
def test_invalid_name(self): self.assertRaises(ValueError, TSC.ProjectItem, None) self.assertRaises(ValueError, TSC.ProjectItem, "") project = TSC.ProjectItem("proj") with self.assertRaises(ValueError): project.name = None with self.assertRaises(ValueError): project.name = ""
def create_example_project(name='Example Project', content_permissions='LockedToProject', description='Project created for testing', server=None): new_project = TSC.ProjectItem(name=name, content_permissions=content_permissions, description=description) try: server.projects.create(new_project) print('Created a new project called: %s' % name) except TSC.ServerResponseError: print('We have already created this resource: %s' % name)
def test_update_copy_fields(self): with open(UPDATE_XML, 'rb') as f: response_xml = f.read().decode('utf-8') with requests_mock.mock() as m: m.put(self.baseurl + '/1d0304cd-3796-429f-b815-7258370b9b74', text=response_xml) single_project = TSC.ProjectItem('test') single_project._id = '1d0304cd-3796-429f-b815-7258370b9b74' single_project._permissions = 'Test to check if permissions copied over.' updated_project = self.server.projects.update(single_project) self.assertEqual(single_project._permissions, updated_project._permissions)
def create_project(parent_id=None, project_name='', description='', content_permissions='LockedToProject', **kwargs): with server.auth.sign_in(tableau_auth): new_project = TSC.ProjectItem(name=project_name, content_permissions=content_permissions, parent_id=parent_id, description=description) # # create the project new_project = server.projects.create(new_project) logger.info(f'{new_project.name, new_project.id} was created') return new_project
def main(): parser = argparse.ArgumentParser(description='Create new projects.') # Common options; please keep those in sync across all samples parser.add_argument('--server', '-s', required=True, help='server address') parser.add_argument('--site', '-S', help='site name') parser.add_argument('--token-name', '-p', required=True, help='name of the personal access token used to sign into the server') parser.add_argument('--token-value', '-v', required=True, help='value of the personal access token used to sign into the server') parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', help='desired logging level (set to error by default)') # Options specific to this sample # This sample has no additional options, yet. If you add some, please add them here args = parser.parse_args() # Set logging level based on user input, or error by default logging_level = getattr(logging, args.logging_level.upper()) logging.basicConfig(level=logging_level) tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site) server = TSC.Server(args.server, use_server_version=True) with server.auth.sign_in(tableau_auth): # Use highest Server REST API version available server.use_server_version() # Without parent_id specified, projects are created at the top level. top_level_project = TSC.ProjectItem(name='Top Level Project') top_level_project = create_project(server, top_level_project) # Specifying parent_id creates a nested projects. child_project = TSC.ProjectItem(name='Child Project', parent_id=top_level_project.id) child_project = create_project(server, child_project) # Projects can be nested at any level. grand_child_project = TSC.ProjectItem(name='Grand Child Project', parent_id=child_project.id) grand_child_project = create_project(server, grand_child_project)
def test_create(self): with open(CREATE_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_project = TSC.ProjectItem(name='Test Project', description='Project created for testing') new_project.content_permissions = 'ManagedByOwner' new_project.parent_id = '9a8f2265-70f3-4494-96c5-e5949d7a1120' new_project = self.server.projects.create(new_project) self.assertEqual('ccbea03f-77c4-4209-8774-f67bc59c3cef', new_project.id) self.assertEqual('Test Project', new_project.name) self.assertEqual('Project created for testing', new_project.description) self.assertEqual('ManagedByOwner', new_project.content_permissions) self.assertEqual('9a8f2265-70f3-4494-96c5-e5949d7a1120', new_project.parent_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_project = TSC.ProjectItem('Project3') single_project._id = '0448d2ed-590d-4fa0-b272-a2a8a24555b5' self.server.projects.populate_permissions(single_project) permissions = single_project.permissions self.assertEqual(permissions[0].grantee.tag_name, 'group') self.assertEqual(permissions[0].grantee.id, 'c8f2773a-c83a-11e8-8c8f-33e6d787b506') self.assertDictEqual(permissions[0].capabilities, { TSC.Permission.Capability.Write: TSC.Permission.Mode.Allow, TSC.Permission.Capability.Read: TSC.Permission.Mode.Allow, })
def test_update(self): with open(UPDATE_XML, 'rb') as f: response_xml = f.read().decode('utf-8') with requests_mock.mock() as m: m.put(self.baseurl + '/1d0304cd-3796-429f-b815-7258370b9b74', text=response_xml) single_project = TSC.ProjectItem(name='Test Project', content_permissions='LockedToProject', description='Project created for testing', parent_id='9a8f2265-70f3-4494-96c5-e5949d7a1120') single_project._id = '1d0304cd-3796-429f-b815-7258370b9b74' single_project = self.server.projects.update(single_project) self.assertEqual('1d0304cd-3796-429f-b815-7258370b9b74', single_project.id) self.assertEqual('Test Project', single_project.name) self.assertEqual('Project created for testing', single_project.description) self.assertEqual('LockedToProject', single_project.content_permissions) self.assertEqual('9a8f2265-70f3-4494-96c5-e5949d7a1120', single_project.parent_id)
def Projectupgrade(groupname): # 分配项目权限 # import tableauserverclient as TSC # server = TSC.Server('https://MY-SERVER') # sign in, etc locked_true = TSC.ProjectItem.ContentPermissions.LockedToProject print(locked_true) # prints 'LockedToProject' by_owner = TSC.ProjectItem.ContentPermissions.ManagedByOwner print(by_owner) # prints 'ManagedByOwner' # pass the content_permissions to new instance of the project item. new_project = TSC.ProjectItem(name='My Project', content_permissions=by_owner, description='Project example')
def run_command(args): logger = log(__class__.__name__, args.logging_level) logger.debug(_("tabcmd.launching")) session = Session() server = session.create_session(args) parent_id = None readable_name = args.project_name if args.parent_project_path: try: logger.info( _("tabcmd.find.parent_project").format( args.parent_project_path)) parent = Server.get_project_by_name_and_parent_path( logger, server, None, args.parent_project_path) except TSC.ServerResponseError as exc: Errors.exit_with_error( logger, _("publish.errors.server_resource_not_found"), exc) readable_name = "{0}/{1}".format(args.parent_project_path, args.project_name) parent_id = parent.id logger.debug("parent project = `{0}`, id = {1}".format( args.parent_project_path, parent_id)) logger.info(_("createproject.status").format(readable_name)) new_project = TSC.ProjectItem(args.project_name, args.description, None, parent_id) try: project_item = server.projects.create(new_project) logger.info(_("common.output.succeeded")) return project_item except TSC.ServerResponseError as e: if Errors.is_resource_conflict(e): if args.continue_if_exists: logger.info( _("tabcmd.result.already_exists").format( args.project_name)) return else: Errors.exit_with_error( logger, _("tabcmd.result.already_exists").format( args.project_name)) Errors.exit_with_error( logger, _("publish.errors.unexpected_server_response"), e)
def test_create(self) -> None: with open(CREATE_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_project = TSC.ProjectItem( name="Test Project", description="Project created for testing") new_project.content_permissions = "ManagedByOwner" new_project.parent_id = "9a8f2265-70f3-4494-96c5-e5949d7a1120" new_project = self.server.projects.create(new_project) self.assertEqual("ccbea03f-77c4-4209-8774-f67bc59c3cef", new_project.id) self.assertEqual("Test Project", new_project.name) self.assertEqual("Project created for testing", new_project.description) self.assertEqual("ManagedByOwner", new_project.content_permissions) self.assertEqual("9a8f2265-70f3-4494-96c5-e5949d7a1120", new_project.parent_id)
def create(project_name, description=None, content_permissions=None, server_url=None, username=None, password=None, server=None): """ Create a project. Authetication happens by either passing the credentials (username, pass- word and server_url) or the server object when previosly authenticated. Parameters: project_name -- name of the project the resource is stored in or name of the project to delete description -- description for the project content_permission -- 'LockedToProject'/'ManagedByOwner' default is 'ManagedByOwner' 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 created project """ # 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) new_project = TSC.ProjectItem(project_name, content_permissions=content_permissions, description=description) # create the project new_project = server.projects.create(new_project) if sign_out is True: # sign out from server server.auth.sign_out() return (new_project.id)
def test_content_permission_locked_to_project_without_nested(self): with open(SET_CONTENT_PERMISSIONS_XML, 'rb') as f: response_xml = f.read().decode('utf-8') with requests_mock.mock() as m: m.put(self.baseurl + '/cb3759e5-da4a-4ade-b916-7e2b4ea7ec86', text=response_xml) project_item = TSC.ProjectItem( name='Test Project Permissions', content_permissions='LockedToProjectWithoutNested', description='Project created for testing', parent_id='7687bc43-a543-42f3-b86f-80caed03a813') project_item._id = 'cb3759e5-da4a-4ade-b916-7e2b4ea7ec86' project_item = self.server.projects.update(project_item) self.assertEqual('cb3759e5-da4a-4ade-b916-7e2b4ea7ec86', project_item.id) self.assertEqual('Test Project Permissions', project_item.name) self.assertEqual('Project created for testing', project_item.description) self.assertEqual('LockedToProjectWithoutNested', project_item.content_permissions) self.assertEqual('7687bc43-a543-42f3-b86f-80caed03a813', project_item.parent_id)
def test_content_permission_locked_to_project_without_nested(self) -> None: with open(SET_CONTENT_PERMISSIONS_XML, "rb") as f: response_xml = f.read().decode("utf-8") with requests_mock.mock() as m: m.put(self.baseurl + "/cb3759e5-da4a-4ade-b916-7e2b4ea7ec86", text=response_xml) project_item = TSC.ProjectItem( name="Test Project Permissions", content_permissions="LockedToProjectWithoutNested", description="Project created for testing", parent_id="7687bc43-a543-42f3-b86f-80caed03a813", ) project_item._id = "cb3759e5-da4a-4ade-b916-7e2b4ea7ec86" project_item = self.server.projects.update(project_item) self.assertEqual("cb3759e5-da4a-4ade-b916-7e2b4ea7ec86", project_item.id) self.assertEqual("Test Project Permissions", project_item.name) self.assertEqual("Project created for testing", project_item.description) self.assertEqual("LockedToProjectWithoutNested", project_item.content_permissions) self.assertEqual("7687bc43-a543-42f3-b86f-80caed03a813", project_item.parent_id)
def test_populate_permissions(self) -> None: 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_project = TSC.ProjectItem("Project3") single_project._id = "0448d2ed-590d-4fa0-b272-a2a8a24555b5" self.server.projects.populate_permissions(single_project) permissions = single_project.permissions self.assertEqual(permissions[0].grantee.tag_name, "group") self.assertEqual(permissions[0].grantee.id, "c8f2773a-c83a-11e8-8c8f-33e6d787b506") self.assertDictEqual( permissions[0].capabilities, { TSC.Permission.Capability.Write: TSC.Permission.Mode.Allow, TSC.Permission.Capability.Read: TSC.Permission.Mode.Allow, }, )
def test_update(self) -> None: with open(UPDATE_XML, "rb") as f: response_xml = f.read().decode("utf-8") with requests_mock.mock() as m: m.put(self.baseurl + "/1d0304cd-3796-429f-b815-7258370b9b74", text=response_xml) single_project = TSC.ProjectItem( name="Test Project", content_permissions="LockedToProject", description="Project created for testing", parent_id="9a8f2265-70f3-4494-96c5-e5949d7a1120", ) single_project._id = "1d0304cd-3796-429f-b815-7258370b9b74" single_project = self.server.projects.update(single_project) self.assertEqual("1d0304cd-3796-429f-b815-7258370b9b74", single_project.id) self.assertEqual("Test Project", single_project.name) self.assertEqual("Project created for testing", single_project.description) self.assertEqual("LockedToProject", single_project.content_permissions) self.assertEqual("9a8f2265-70f3-4494-96c5-e5949d7a1120", single_project.parent_id)
def test_update_datasource_default_permission(self) -> None: response_xml = read_xml_asset( UPDATE_DATASOURCE_DEFAULT_PERMISSIONS_XML) with requests_mock.mock() as m: m.put( self.baseurl + "/b4065286-80f0-11ea-af1b-cb7191f48e45/default-permissions/datasources", text=response_xml, ) project = TSC.ProjectItem("test-project") project._id = "b4065286-80f0-11ea-af1b-cb7191f48e45" group = TSC.GroupItem("test-group") group._id = "b4488bce-80f0-11ea-af1c-976d0c1dab39" capabilities = { TSC.Permission.Capability.ExportXml: TSC.Permission.Mode.Deny } rules = [ TSC.PermissionsRule(grantee=group.to_reference(), capabilities=capabilities) ] new_rules = self.server.projects.update_datasource_default_permissions( project, rules) self.assertEqual("b4488bce-80f0-11ea-af1c-976d0c1dab39", new_rules[0].grantee.id) updated_capabilities = new_rules[0].capabilities self.assertEqual(4, len(updated_capabilities)) self.assertEqual("Deny", updated_capabilities["ExportXml"]) self.assertEqual("Allow", updated_capabilities["Read"]) self.assertEqual("Allow", updated_capabilities["Write"]) self.assertEqual("Allow", updated_capabilities["Connect"])
def test_update_missing_id(self): single_project = TSC.ProjectItem('test') self.assertRaises(TSC.MissingRequiredFieldError, self.server.projects.update, single_project)
def test_owner_id(self): project = TSC.ProjectItem("proj") with self.assertRaises(NotImplementedError): project.owner_id = "new_owner"
def test_parent_id(self): project = TSC.ProjectItem("proj") project.parent_id = "foo" self.assertEqual(project.parent_id, "foo")