|
| 1 | +#!/usr/bin/python2.4 |
| 2 | +# |
| 3 | +# Copyright 2011 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""GroupsClient simplifies Groups Provisioning API calls. |
| 18 | +
|
| 19 | +GroupsClient extends gdata.client.GDClient to ease interaction |
| 20 | +with the Group Provisioning API. These interactions include the |
| 21 | +ability to create, retrieve, update and delete groups. |
| 22 | +""" |
| 23 | + |
| 24 | + |
| 25 | +__author__ = 'Shraddha gupta <shraddhag@google.com>' |
| 26 | + |
| 27 | + |
| 28 | +import urllib |
| 29 | +import gdata.apps.groups.data |
| 30 | +import gdata.client |
| 31 | + |
| 32 | + |
| 33 | +# Multidomain URI templates |
| 34 | +# The strings in this template are eventually replaced with the API version, |
| 35 | +# and Google Apps domain name respectively. |
| 36 | +GROUP_URI_TEMPLATE = '/a/feeds/group/%s/%s' |
| 37 | +GROUP_MEMBER = 'member' |
| 38 | + |
| 39 | + |
| 40 | +class GroupsProvisioningClient(gdata.client.GDClient): |
| 41 | + """Client extension for the Google Group Provisioning API service. |
| 42 | +
|
| 43 | + Attributes: |
| 44 | + host: string The hostname for the Group Provisioning API service. |
| 45 | + api_version: string The version of the MultiDomain Provisioning API. |
| 46 | + """ |
| 47 | + |
| 48 | + host = 'apps-apis.google.com' |
| 49 | + api_version = '2.0' |
| 50 | + auth_service = 'apps' |
| 51 | + auth_scopes = gdata.gauth.AUTH_SCOPES['apps'] |
| 52 | + ssl = True |
| 53 | + |
| 54 | + def __init__(self, domain, auth_token=None, **kwargs): |
| 55 | + """Constructs a new client for the Groups Provisioning API. |
| 56 | +
|
| 57 | + Args: |
| 58 | + domain: string The Google Apps domain with Group Provisioning. |
| 59 | + auth_token: (optional) gdata.gauth.ClientLoginToken, AuthSubToken, or |
| 60 | + OAuthToken which authorizes this client to edit the email settings. |
| 61 | + kwargs: The other parameters to pass to the gdata.client.GDClient |
| 62 | + constructor. |
| 63 | + """ |
| 64 | + gdata.client.GDClient.__init__(self, auth_token=auth_token, **kwargs) |
| 65 | + self.domain = domain |
| 66 | + |
| 67 | + def make_group_provisioning_uri( |
| 68 | + self, feed_type=None, group_id=None, member_id=None, params=None): |
| 69 | + |
| 70 | + """Creates a resource feed URI for the Groups Provisioning API. |
| 71 | +
|
| 72 | + Using this client's Google Apps domain, create a feed URI for group |
| 73 | + provisioning in that domain. If an email address is provided, return a |
| 74 | + URI for that specific resource. If params are provided, append them as GET |
| 75 | + params. |
| 76 | +
|
| 77 | + Args: |
| 78 | + feed_type: string groupmember for groupmember feed else None |
| 79 | + group_id: string (optional) The identifier of group for which to |
| 80 | + make a feed URI. |
| 81 | + member_id: string (optional) The identifier of group member for which to |
| 82 | + make a feed URI. |
| 83 | + params: dict (optional) key -> value params to append as GET vars to the |
| 84 | + URI. Example: params={'start': 'my-resource-id'} |
| 85 | +
|
| 86 | + Returns: |
| 87 | + A string giving the URI for group provisioning for this client's |
| 88 | + Google Apps domain. |
| 89 | + """ |
| 90 | + uri = GROUP_URI_TEMPLATE % (self.api_version, self.domain) |
| 91 | + if group_id: |
| 92 | + uri += '/' + group_id |
| 93 | + if feed_type is GROUP_MEMBER: |
| 94 | + uri += '/' + feed_type |
| 95 | + if member_id: |
| 96 | + uri += '/' + member_id |
| 97 | + if params: |
| 98 | + uri += '?' + urllib.urlencode(params) |
| 99 | + return uri |
| 100 | + |
| 101 | + MakeGroupProvisioningUri = make_group_provisioning_uri |
| 102 | + |
| 103 | + def make_group_member_uri(self, group_id, member_id=None, params=None): |
| 104 | + """Creates a resource feed URI for the Group Member Provisioning API.""" |
| 105 | + |
| 106 | + return self.make_group_provisioning_uri(GROUP_MEMBER, group_id=group_id, |
| 107 | + member_id=member_id, params=params) |
| 108 | + |
| 109 | + MakeGroupMembersUri = make_group_member_uri |
| 110 | + |
| 111 | + def retrieve_all_groups(self, **kwargs): |
| 112 | + """Retrieves all groups for the given domain. |
| 113 | +
|
| 114 | + Args: |
| 115 | + kwargs: The other parameters to pass to gdata.client.GDClient.GetFeed() |
| 116 | +
|
| 117 | + Returns: |
| 118 | + A gdata.apps.groups.data.GDFeed of the group users |
| 119 | + """ |
| 120 | + uri = self.MakeGroupProvisioningUri() |
| 121 | + return self.GetFeed(uri, |
| 122 | + desired_class=gdata.apps.groups.data.GroupFeed, **kwargs) |
| 123 | + |
| 124 | + RetrieveAllGroups = retrieve_all_groups |
| 125 | + |
| 126 | + def retrieve_group(self, group_id, **kwargs): |
| 127 | + """Retrieves a single group in the domain. |
| 128 | +
|
| 129 | + Args: |
| 130 | + group_id: string groupId of the group to be retrieved |
| 131 | + kwargs: other parameters to pass to gdata.client.GDClient.GetEntry() |
| 132 | +
|
| 133 | + Returns: |
| 134 | + A gdata.apps.groups.data.GroupEntry representing the group |
| 135 | + """ |
| 136 | + uri = self.MakeGroupProvisioningUri(group_id=group_id) |
| 137 | + print uri |
| 138 | + return self.GetEntry(uri, |
| 139 | + desired_class=gdata.apps.groups.data.GroupEntry, **kwargs) |
| 140 | + |
| 141 | + RetrieveGroup = retrieve_group |
| 142 | + |
| 143 | + def create_group(self, group_id, group_name, |
| 144 | + description=None, email_permission=None, **kwargs): |
| 145 | + """Creates a group in the domain with the given properties. |
| 146 | +
|
| 147 | + Args: |
| 148 | + group_id: string identifier of the group. |
| 149 | + group_name: string name of the group. |
| 150 | + description: string (optional) description of the group. |
| 151 | + email_permission: string (optional) email permission level for the group. |
| 152 | + kwargs: other parameters to pass to gdata.client.GDClient.post(). |
| 153 | +
|
| 154 | + Returns: |
| 155 | + A gdata.apps.groups.data.GroupEntry of the new group |
| 156 | + """ |
| 157 | + new_group = gdata.apps.groups.data.GroupEntry(group_id=group_id, |
| 158 | + group_name=group_name, description=description, |
| 159 | + email_permission=email_permission) |
| 160 | + return self.post(new_group, self.MakeGroupProvisioningUri(), |
| 161 | + **kwargs) |
| 162 | + |
| 163 | + CreateGroup = create_group |
| 164 | + |
| 165 | + def update_group(self, group_id, group_entry, **kwargs): |
| 166 | + """Updates the group with the given groupID. |
| 167 | +
|
| 168 | + Args: |
| 169 | + group_id: string identifier of the group. |
| 170 | + group_entry: GroupEntry The group entry with updated values. |
| 171 | + kwargs: The other parameters to pass to gdata.client.GDClient.put() |
| 172 | +
|
| 173 | + Returns: |
| 174 | + A gdata.apps.groups.data.GroupEntry representing the group |
| 175 | + """ |
| 176 | + return self.update(group_entry, |
| 177 | + uri=self.MakeGroupProvisioningUri(group_id=group_id), |
| 178 | + **kwargs) |
| 179 | + |
| 180 | + UpdateGroup = update_group |
| 181 | + |
| 182 | + def delete_group(self, group_id, **kwargs): |
| 183 | + """Deletes the group with the given groupId. |
| 184 | +
|
| 185 | + Args: |
| 186 | + group_id: string groupId of the group to delete. |
| 187 | + kwargs: The other parameters to pass to gdata.client.GDClient.delete() |
| 188 | + """ |
| 189 | + self.delete(self.MakeGroupProvisioningUri(group_id=group_id), **kwargs) |
| 190 | + |
| 191 | + DeleteGroup = delete_group |
| 192 | + |
| 193 | + def retrieve_all_members(self, group_id, **kwargs): |
| 194 | + """Retrieves group members of the group. |
| 195 | +
|
| 196 | + Args: |
| 197 | + group_id: string groupId of the group whose members are retrieved |
| 198 | + kwargs: The other parameters to pass to gdata.client.GDClient.GetFeed() |
| 199 | +
|
| 200 | + Returns: |
| 201 | + A gdata.apps.groups.data.GDFeed of the GroupMember entries |
| 202 | + """ |
| 203 | + uri = self.MakeGroupMembersUri(group_id=group_id) |
| 204 | + return self.GetFeed(uri, |
| 205 | + desired_class=gdata.apps.groups.data.GroupMemberFeed, **kwargs) |
| 206 | + |
| 207 | + RetrieveAllMembers = retrieve_all_members |
| 208 | + |
| 209 | + def retrieve_group_member(self, group_id, member_id, **kwargs): |
| 210 | + """Retrieves a group member with the given id from given group. |
| 211 | +
|
| 212 | + Args: |
| 213 | + group_id: string groupId of the group whose member is retrieved |
| 214 | + member_id: string memberId of the group member retrieved |
| 215 | + kwargs: The other parameters to pass to gdata.client.GDClient.GetEntry() |
| 216 | +
|
| 217 | + Returns: |
| 218 | + A gdata.apps.groups.data.GroupEntry representing the group member |
| 219 | + """ |
| 220 | + uri = self.MakeGroupMembersUri(group_id=group_id, member_id=member_id) |
| 221 | + return self.GetEntry(uri, |
| 222 | + desired_class=gdata.apps.groups.data.GroupMemberEntry, **kwargs) |
| 223 | + |
| 224 | + RetrieveGroupMember = retrieve_group_member |
| 225 | + |
| 226 | + def add_member_to_group(self, group_id, member_id, member_type=None, |
| 227 | + direct_member=None, **kwargs): |
| 228 | + """Adds a member with the given id to the group. |
| 229 | +
|
| 230 | + Args: |
| 231 | + group_id: string groupId of the group where member is added |
| 232 | + member_id: string memberId of the member added |
| 233 | + member_type: string (optional) type of member(user or group) |
| 234 | + direct_member: bool (optional) if member is a direct member |
| 235 | + kwargs: The other parameters to pass to gdata.client.GDClient.post(). |
| 236 | +
|
| 237 | + Returns: |
| 238 | + A gdata.apps.groups.data.GroupMemberEntry of the group member |
| 239 | + """ |
| 240 | + member = gdata.apps.groups.data.GroupMemberEntry(member_id=member_id, |
| 241 | + member_type=member_type, direct_member=direct_member) |
| 242 | + return self.post(member, self.MakeGroupMembersUri(group_id=group_id), |
| 243 | + **kwargs) |
| 244 | + |
| 245 | + AddMemberToGroup = add_member_to_group |
| 246 | + |
| 247 | + def remove_member_from_group(self, group_id, member_id, **kwargs): |
| 248 | + """Remove member from the given group. |
| 249 | +
|
| 250 | + Args: |
| 251 | + group_id: string groupId of the group |
| 252 | + member_id: string memberId of the member to be removed |
| 253 | + kwargs: The other parameters to pass to gdata.client.GDClient.delete() |
| 254 | + """ |
| 255 | + self.delete( |
| 256 | + self.MakeGroupMembersUri(group_id=group_id, member_id=member_id), |
| 257 | + **kwargs) |
| 258 | + |
| 259 | + RemoveMemberFromGroup = remove_member_from_group |
0 commit comments