8000 Bugfix 66 add create group basic by graysonarts · Pull Request #69 · tableau/server-client-python · GitHub
[go: up one dir, main page]

Skip to content

Bugfix 66 add create group basic #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions samples/create_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
####
# This script demonstrates how to create groups using the Tableau
# Server Client.
#
# To run the script, you must have installed Python 2.7.9 or later.
####


import argparse
import getpass
import logging

from datetime import time

import tableauserverclient as TSC


def main():

parser = argparse.ArgumentParser(description='Creates sample schedules for each type of frequency.')
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('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
help='desired logging level (set to error by default)')
args = parser.parse_args()

password = getpass.getpass("Password: ")

# 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):
group = TSC.GroupItem('test')
group = server.groups.create(group)
print(group)


if __name__ == '__main__':
main()
6 changes: 6 additions & 0 deletions tableauserverclient/server/endpoint/groups_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def delete(self, group_id):
self.delete_request(url)
logger.info('Deleted single group (ID: {0})'.format(group_id))

def create(self, group_item):
url = self.baseurl
create_req = RequestFactory.Group.create_req(group_item)
server_response = self.post_request(url, create_req)
return GroupItem.from_response(server_response.content)[0]

# Removes 1 user from 1 group
def remove_user(self, group_item, user_id):
self._remove_user(group_item, user_id)
Expand Down
6 changes: 6 additions & 0 deletions tableauserverclient/server/request_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def add_user_req(self, user_id):
user_element.attrib['id'] = user_id
return ET.tostring(xml_request)

def create_req(self, group_item):
xml_request = ET.Element('tsRequest')
group_element = ET.SubElement(xml_request, 'group')
group_element.attrib['name'] = group_item.name
return ET.tostring(xml_request)


class PermissionRequest(object):
def _add_capability(self, parent_element, capability_set, mode):
Expand Down
6 changes: 6 additions & 0 deletions test/assets/group_create.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="http://tableau.com/api"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.3.xsd">
<group id="3e4a9ea0-a07a-4fe6-b50f-c345c8c81034" name="試供品" />
</tsResponse>
7 changes: 7 additions & 0 deletions test/assets/group_create_async.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="http://tableau.com/api"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.3.xsd">
<job id="job-id" mode="Asynchronous" type="GroupImport"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is there for future tests? We don't currently let people trigger an async import.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that's for a future test when I implement import() (see comment above about AD import)

progress="0" createdAt="time-job-created" />
</tsResponse>
13 changes: 13 additions & 0 deletions test/test_group.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding=utf-8
import unittest
import os
import requests_mock
Expand All @@ -8,6 +9,8 @@
GET_XML = os.path.join(TEST_ASSET_DIR, 'group_get.xml')
POPULATE_USERS = os.path.join(TEST_ASSET_DIR, 'group_populate_users.xml')
ADD_USER = os.path.join(TEST_ASSET_DIR, 'group_add_user.xml')
CREATE_GROUP = os.path.join(TEST_ASSET_DIR, 'group_create.xml')
CREATE_GROUP_ASYNC = os.path.join(TEST_ASSET_DIR, 'group_create_async.xml')


class GroupTests(unittest.TestCase):
Expand Down Expand Up @@ -155,3 +158,13 @@ def test_remove_user_missing_group_id(self):
single_group._users = []
self.assertRaises(TSC.MissingRequiredFieldError, self.server.groups.remove_user, single_group,
'5de011f8-5aa9-4d5b-b991-f462c8dd6bb7')

def test_create_group(self):
with open(CREATE_GROUP, 'rb') as f:
response_xml = f.read().decode('utf-8')
with requests_mock.mock() as m:
m.post(self.baseurl, text=response_xml)
group_to_create = TSC.GroupItem(u'試供品')
group = self.server.groups.create(group_to_create)
self.assertEqual(group.name, u'試供品')
self.assertEqual(group.id, '3e4a9ea0-a07a-4fe6-b50f-c345c8c81034')
0