8000 277 update group feature by sotnich · Pull Request #279 · tableau/server-client-python · GitHub
[go: up one dir, main page]

Skip to content

277 update group feature #279

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 6 commits into from
Apr 19, 2018
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
46 changes: 46 additions & 0 deletions docs/docs/api-ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,52 @@ Error | Description
<br>
<br>

#### groups.update

```py
groups.update(group_item, default_site_role=UserItem.Roles.Unlicensed)
```

Updates the group on the site.
If domain_name = 'local' then update only the name of the group.
If not - update group from the Active Directory with domain_name.

REST API: [Update Group](http://onlinehelp.tableau.com/current/api/rest_api/en-us/help.htm#REST/rest_api_ref.htm#Update_Group%3FTocPath%3DAPI%2520Reference%7C_____95){:target="_blank"}


**Parameters**

Name | Description
:--- | :---
`group_item` | the group_item specifies the group to update.
`default_site_role` | if group updates from Active Directory then this is the default role for the new users.


**Exceptions**

Error | Description
:--- | :---
`Group item missing ID` | Raises an exception if a valid `group_item.id` is not provided.


**Example**

```py
# Update a group

# import tableauserverclient as TSC
# tableau_auth = TSC.TableauAuth('USERNAME', 'PASSWORD')
# server = TSC.Server('http://SERVERURL')

with server.auth.sign_in(tableau_auth):
all_groups, pagination_item = server.groups.get()

for group in all_groups:
server.groups.update(group)
```
<br>
<br>

#### groups.get

```py
Expand Down
14 changes: 14 additions & 0 deletions tableauserverclient/server/endpoint/groups_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

logger = logging.getLogger('tableau.endpoint.groups')

UNLICENSED_USER = UserItem.Roles.Unlicensed


class Groups(Endpoint):
@property
Expand Down Expand Up @@ -55,6 +57,18 @@ def delete(self, group_id):
self.delete_request(url)
logger.info('Deleted single group (ID: {0})'.format(group_id))

@api(version="2.0")
def update(self, group_item, default_site_role=UNLICENSED_USER):
if not group_item.id:
error = "Group item missing ID."
raise MissingRequiredFieldError(error)
url = "{0}/{1}".format(self.baseurl, group_item.id)
update_req = RequestFactory.Group.update_req(group_item, default_site_role)
server_response = self.put_request(url, update_req)
logger.info('Updated group item (ID: {0})'.format(group_item.id))
updated_group = GroupItem.from_response(server_response.content, self.parent_srv.namespace)[0]
return updated_group

# Create a 'local' Tableau group
@api(version="2.0")
def create(self, group_item):
Expand Down
11 changes: 11 additions & 0 deletions tableauserverclient/server/request_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ def create_req(self, group_item):
group_element.attrib['name'] = group_item.name
return ET.tostring(xml_request)

def update_req(self, group_item, default_site_role):
xml_request = ET.Element('tsRequest')
group_element = ET.SubElement(xml_request, 'group')
group_element.attrib['name'] = group_item.name
if group_item.domain_name != 'local':
project_element = ET.SubElement(group_element, 'import')
project_element.attrib['source'] = "ActiveDirectory"
project_element.attrib['domainName'] = group_item.domain_name
project_element.attrib['siteRole'] = default_site_role
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_update.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="ef8b19c0-43b6-11e6-af50-63f5805dbe3c" name="Group updated name" />
</tsResponse>
14 changes: 14 additions & 0 deletions test/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ADD_USER_POPULATE = os.path.join(TEST_ASSET_DIR, 'group_users_added.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')
UPDATE_XML = os.path.join(TEST_ASSET_DIR, 'group_update.xml')


class GroupTests(unittest.TestCase):
Expand Down Expand Up @@ -183,3 +184,16 @@ def test_create_group(self):
group = self.server.groups.create(group_to_create)
self.assertEqual(group.name, u'試供品')
self.assertEqual(group.id, '3e4a9ea0-a07a-4fe6-b50f-c345c8c81034')

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 + '/ef8b19c0-43b6-11e6-af50-63f5805dbe3c', text=response_xml)
group = TSC.GroupItem(name='Test Group')
group._domain_name = 'local'
group._id = 'ef8b19c0-43b6-11e6-af50-63f5805dbe3c'
group = self.server.groups.update(group)

self.assertEqual('ef8b19c0-43b6-11e6-af50-63f5805dbe3c', group.id)
self.assertEqual('Group updated name', group.name)
0