8000 277 update group feature (#279) · tableau/server-client-python@18372ba · GitHub
[go: up one dir, main page]

Skip to content

Commit 18372ba

Browse files
sotnicht8y8
authored andcommitted
277 update group feature (#279)
* Adding update method for groups * Add some docs * Add test for update group function
1 parent d28d5fc commit 18372ba

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

docs/docs/api-ref.md

Lines changed: 46 additions & 0 deletions
854
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,52 @@ Error | Description
849849
<br>
850850
<br>
851851

852+
#### groups.update
853+
854+
```py
855+
groups.update(group_item, default_site_role=UserItem.Roles.Unlicensed)
856+
```
857+
858+
Updates the group on the site.
859+
If domain_name = 'local' then update only the name of the group.
860+
If not - update group from the Active Directory with domain_name.
861+
862+
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"}
863+
864+
865+
**Parameters**
866+
867+
Name | Description
868+
:--- | :---
869+
`group_item` | the group_item specifies the group to update.
870+
`default_site_role` | if group updates from Active Directory then this is the default role for the new users.
871+
872+
873+
**Exceptions**
874+
875+
Error | Description
876+
:--- | :---
877+
`Group item missing ID` | Raises an exception if a valid `group_item.id` is not provided.
878+
879+
880+
**Example**
881+
882+
```py
883+
# Update a group
884+
885+
# import tableauserverclient as TSC
886+
# tableau_auth = TSC.TableauAuth('USERNAME', 'PASSWORD')
887+
# server = TSC.Server('http://SERVERURL')
888+
889+
with server.auth.sign_in(tableau_auth):
890+
all_groups, pagination_item = server.groups.get()
891+
892+
for group in all_groups:
893+
server.groups.update(group)
894+
```
895+
<br>
896+
<br>
897+
852898
#### groups.get
853899

900
```py

tableauserverclient/server/endpoint/groups_endpoint.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

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

10+
UNLICENSED_USER = UserItem.Roles.Unlicensed
11+
1012

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

60+
@api(version="2.0")
61+
def update(self, group_item, default_site_role=UNLICENSED_USER):
62+
if not group_item.id:
63+
error = "Group item missing ID."
64+
raise MissingRequiredFieldError(error)
65+
url = "{0}/{1}".format(self.baseurl, group_item.id)
66+
update_req = RequestFactory.Group.update_req(group_item, default_site_role)
67+
server_response = self.put_request(url, update_req)
68+
logger.info('Updated group item (ID: {0})'.format(group_item.id))
69+
updated_group = GroupItem.from_response(server_response.content, self.parent_srv.namespace)[0]
70+
return updated_group
71+
5872
# Create a 'local' Tableau group
5973
@api(version="2.0")
6074
def create(self, group_item):

tableauserverclient/server/request_factory.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ def create_req(self, group_item):
107107
group_element.attrib['name'] = group_item.name
108108
return ET.tostring(xml_request)
109109

110+
def update_req(self, group_item, default_site_role):
111+
xml_request = ET.Element('tsRequest')
112+
group_element = ET.SubElement(xml_request, 'group')
113+
group_element.attrib['name'] = group_item.name
114+
if group_item.domain_name != 'local':
115+
project_element = ET.SubElement(group_element, 'import')
116+
project_element.attrib['source'] = "ActiveDirectory"
117+
project_element.attrib['domainName'] = group_item.domain_name
118+
project_element.attrib['siteRole'] = default_site_role
119+
return ET.tostring(xml_request)
120+
110121

111122
class PermissionRequest(object):
112123
def _add_capability(self, parent_element, capability_set, mode):

test/assets/group_update.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version='1.0' encoding='UTF-8' 9E88 ?>
2+
<tsResponse xmlns="http://tableau.com/api"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.3.xsd">
5+
<group id="ef8b19c0-43b6-11e6-af50-63f5805dbe3c" name="Group updated name" />
6+
</tsResponse>

test/test_group.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
ADD_USER_POPULATE = os.path.join(TEST_ASSET_DIR, 'group_users_added.xml')
1515
CREATE_GROUP = os.path.join(TEST_ASSET_DIR, 'group_create.xml')
1616
CREATE_GROUP_ASYNC = os.path.join(TEST_ASSET_DIR, 'group_create_async.xml')
17+
UPDATE_XML = os.path.join(TEST_ASSET_DIR, 'group_update.xml')
1718

1819

1920
class GroupTests(unittest.TestCase):
@@ -183,3 +184,16 @@ def test_create_group(self):
183184
group = self.server.groups.create(group_to_create)
184185
self.assertEqual(group.name, u'試供品')
185186
self.assertEqual(group.id, '3e4a9ea0-a07a-4fe6-b50f-c345c8c81034')
187+
188+
def test_update(self):
189+
with open(UPDATE_XML, 'rb') as f:
190+
response_xml = f.read().decode('utf-8')
191+
with requests_mock.mock() as m:
192+
m.put(self.baseurl + '/ef8b19c0-43b6-11e6-af50-63f5805dbe3c', text=response_xml)
193+
group = TSC.GroupItem(name='Test Group')
194+
group._domain_name = 'local'
195+
group._id = 'ef8b19c0-43b6-11e6-af50-63f5805dbe3c'
196+
group = self.server.groups.update(group)
197+
198+
self.assertEqual('ef8b19c0-43b6-11e6-af50-63f5805dbe3c', group.id)
199+
self.assertEqual('Group updated name', group.name)

0 commit comments

Comments
 (0)
0