|
| 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 | +__author__ = 'Gunjan Sharma <gunjansharma@google.com>' |
| 18 | + |
| 19 | +import getopt |
| 20 | +import sys |
| 21 | +import gdata.apps.groups.client |
| 22 | +import gdata.apps.groups.data |
| 23 | + |
| 24 | +SCOPE = 'https://apps-apis.google.com/a/feeds/groups/' |
| 25 | +USER_AGENT = 'GroupsQuickStartExample' |
| 26 | + |
| 27 | + |
| 28 | +class GroupsQuickStartExample(object): |
| 29 | + """Demonstrates all the functions of Groups provisioning.""" |
| 30 | + |
| 31 | + def __init__(self, client_id, client_secret, domain): |
| 32 | + """Constructor for the GroupsQuickStartExample object. |
| 33 | +
|
| 34 | + Takes a client_id, client_secret and domain needed to create an object for |
| 35 | + groups provisioning. |
| 36 | +
|
| 37 | + Args: |
| 38 | + client_id: [string] The clientId of the developer. |
| 39 | + client_secret: [string] The clientSecret of the developer. |
| 40 | + domain: [string] The domain on which the functions are to be performed. |
| 41 | + """ |
| 42 | + self.client_id = client_id |
| 43 | + self.client_secret = client_secret |
| 44 | + self.domain = domain |
| 45 | + |
| 46 | + def CreateGroupsClient(self): |
| 47 | + """Creates a groups provisioning client using OAuth2.0 flow.""" |
| 48 | + token = gdata.gauth.OAuth2Token( |
| 49 | + client_id=self.client_id, client_secret=self.client_secret, |
| 50 | + scope=SCOPE, user_agent=USER_AGENT) |
| 51 | + uri = token.generate_authorize_url() |
| 52 | + print 'Please visit this URL to authorize the application:' |
| 53 | + print uri |
| 54 | + # Get the verification code from the standard input. |
| 55 | + code = raw_input('What is the verification code? ').strip() |
| 56 | + token.get_access_token(code) |
| 57 | + self.groups_client = gdata.apps.groups.client.GroupsProvisioningClient( |
| 58 | + domain=self.domain, auth_token=token) |
| 59 | + |
| 60 | + def _GetValidGroupId(self): |
| 61 | + """Takes a valid group email address as input. |
| 62 | +
|
| 63 | + Return: |
| 64 | + group_id: [string] a valid group email address |
| 65 | + """ |
| 66 | + group_id = '' |
| 67 | + while not group_id: |
| 68 | + group_id = raw_input('Enter a valid group email address' |
| 69 | + '(group@domain.com): ') |
| 70 | + return group_id |
| 71 | + |
| 72 | + def _GetValidMemberId(self): |
| 73 | + """Takes a valid member email address as input. |
| 74 | +
|
| 75 | + Return: |
| 76 | + member_id: [string] a valid member email address |
| 77 | + """ |
| 78 | + member_id = '' |
| 79 | + while not member_id: |
| 80 | + member_id = raw_input('Enter a valid member email address' |
| 81 | + '(username@domain.com): ') |
| 82 | + return member_id |
| 83 | + |
| 84 | + def _PrintGroupDetails(self, group_entry): |
| 85 | + """Print all the details of a group. |
| 86 | +
|
| 87 | + Args: |
| 88 | + group_entry: [GroupEntry] contains all the data about the group. |
| 89 | + """ |
| 90 | + print 'Group ID: ' + group_entry.group_id |
| 91 | + print 'Group Name: ' + group_entry.group_name |
| 92 | + print 'Description: ' + group_entry.description |
| 93 | + print 'Email Permissions: ' + group_entry.email_permission |
| 94 | + print '' |
| 95 | + |
| 96 | + def _PrintMemberDetails(self, member_entry): |
| 97 | + """Print all the details of a group member. |
| 98 | +
|
| 99 | + Args: |
| 100 | + member_entry: [GroupMemberEntry] contains all the data about the group member. |
| 101 | + """ |
| 102 | + print 'Member ID: ' + member_entry.member_id |
| 103 | + print 'Member Type: ' + member_entry.member_type |
| 104 | + print 'Is Direct Member: ' + member_entry.direct_member |
| 105 | + print '' |
| 106 | + |
| 107 | + def _TakeGroupData(self, function='create'): |
| 108 | + """Takes input data for _UpdateGroup and _CreateGroup functions. |
| 109 | +
|
| 110 | + Args: |
| 111 | + function: [string] representing the kind of function (create/update) |
| 112 | + from where this function was called. |
| 113 | +
|
| 114 | + Return: |
| 115 | + group_data: [gdata.apps.groups.data.GroupEntry] All data for a group. |
| 116 | + """ |
| 117 | + email_permission_options = ['Owner', 'Member', 'Domain', 'Anyone'] |
| 118 | + extra_stmt = '' |
| 119 | + if function == 'update': |
| 120 | + extra_stmt = '. Press enter to not update the field' |
| 121 | + group_data = gdata.apps.groups.data.GroupEntry() |
| 122 | + group_data.group_id = self._GetValidGroupId() |
| 123 | + while not group_data.group_name: |
| 124 | + group_data.group_name = raw_input('Enter name for the group%s: ' |
| 125 | + % extra_stmt) |
| 126 | + if function == 'update': |
| 127 | + break |
| 128 | + group_data.description = raw_input('Enter description for the group%s: ' |
| 129 | + % extra_stmt) |
| 130 | + |
| 131 | + print ('Choose an option for email permission%s:' |
| 132 | + % extra_stmt) |
| 133 | + i = 1 |
| 134 | + for option in email_permission_options: |
| 135 | + print '%d - %s' % (i, option) |
| 136 | + i += 1 |
| 137 | + choice = (raw_input()) |
| 138 | + if not choice: |
| 139 | + choice = -1 |
| 140 | + choice = int(choice) |
| 141 | + if choice > 0 and choice <= len(email_permission_options): |
| 142 | + group_data.email_permission = email_permission_options[choice-1] |
| 143 | + |
| 144 | + return group_data |
| 145 | + |
| 146 | + def _CreateGroup(self): |
| 147 | + """Creates a new group.""" |
| 148 | + group_data = self._TakeGroupData() |
| 149 | + self.groups_client.CreateGroup(group_data.group_id, |
| 150 | + group_data.group_name, |
| 151 | + group_data.description, |
| 152 | + group_data.email_permission) |
| 153 | + |
| 154 | + def _UpdateGroup(self): |
| 155 | + """Updates an existing group.""" |
| 156 | + group_data = self._TakeGroupData(function='update') |
| 157 | + group_entry = self.groups_client.RetrieveGroup(group_data.group_id) |
| 158 | + if group_data.group_name: |
| 159 | + group_entry.group_name = group_data.group_name |
| 160 | + if group_data.description: |
| 161 | + group_entry.description = group_data.description |
| 162 | + if group_data.email_permission: |
| 163 | + group_entry.email_permission = group_data.email_permission |
| 164 | + |
| 165 | + self.groups_client.UpdateGroup(group_data.GetGroupId(), group_entry) |
| 166 | + |
| 167 | + def _RetrieveSingleGroup(self): |
| 168 | + """Retrieves a single group.""" |
| 169 | + group_id = self._GetValidGroupId() |
| 170 | + group_entry = self.groups_client.RetrieveGroup(group_id) |
| 171 | + self._PrintGroupDetails(group_entry) |
| 172 | + |
| 173 | + def _RetrieveAllGroupsForMember(self): |
| 174 | + """Retrieves all the groups the user is a member of.""" |
| 175 | + member_id = self._GetValidMemberId() |
| 176 | + direct_only = raw_input('Write true/false for direct_only: ') |
| 177 | + if direct_only == 'true': |
| 178 | + direct_only = True |
| 179 | + else: |
| 180 | + direct_only = False |
| 181 | + groups_feed = self.groups_client.RetrieveGroups(member_id, direct_only) |
| 182 | + for entry in groups_feed.entry: |
| 183 | + self._PrintGroupDetails(entry) |
| 184 | + |
| 185 | + def _RetrieveAllGroups(self): |
| 186 | + """Retrieves all the groups in a domain.""" |
| 187 | + groups_feed = self.groups_client.RetrieveAllGroups() |
| 188 | + for entry in groups_feed.entry: |
| 189 | + self._PrintGroupDetails(entry) |
| 190 | + |
| 191 | + def _DeleteGroup(self): |
| 192 | + """Delete a group.""" |
| 193 | + group_id = self._GetValidGroupId() |
| 194 | + self.groups_client.DeleteGroup(group_id) |
| 195 | + |
| 196 | + def _AddMemberToGroup(self): |
| 197 | + """Add a member to a particular group.""" |
| 198 | + group_id = self._GetValidGroupId() |
| 199 | + member_id = self._GetValidMemberId() |
| 200 | + self.groups_client.AddMemberToGroup(group_id, member_id) |
| 201 | + |
| 202 | + def _RetrieveSingleMember(self): |
| 203 | + """Retrieve a single member from a group.""" |
| 204 | + group_id = self._GetValidGroupId() |
| 205 | + member_id = self._GetValidMemberId() |
| 206 | + member_entry = self.groups_client.RetrieveGroupMember(group_id, member_id) |
| 207 | + self._PrintMemberDetails(member_entry) |
| 208 | + |
| 209 | + def _RetrieveAllMembersOfGroup(self): |
| 210 | + """Retrieve all the members of a group.""" |
| 211 | + group_id = self._GetValidGroupId() |
| 212 | + members_feed = self.groups_client.RetrieveAllMembers(group_id) |
| 213 | + for entry in members_feed.entry: |
| 214 | + self._PrintMemberDetails(entry) |
| 215 | + |
| 216 | + def _RemoveMemberFromGroup(self): |
| 217 | + """Remove a member from a group.""" |
| 218 | + group_id = self._GetValidGroupId() |
| 219 | + member_id = self._GetValidMemberId() |
| 220 | + self.groups_client.RemoveMemberFromGroup(group_id, member_id) |
| 221 | + |
| 222 | + def Run(self): |
| 223 | + """Runs the sample by getting user input and takin appropriate action.""" |
| 224 | + #List of all the function and there description |
| 225 | + functions_list = [ |
| 226 | + {'function': self._CreateGroup, |
| 227 | + 'description': 'Create a Group'}, |
| 228 | + {'function': self._UpdateGroup, |
| 229 | + 'description': 'Updating a Group'}, |
| 230 | + {'function': self._RetrieveSingleGroup, |
| 231 | + 'description': 'Retrieve a single Group'}, |
| 232 | + {'function': self._RetrieveAllGroupsForMember, |
| 233 | + 'description': 'Retrieve all Groups for a member'}, |
| 234 | + {'function': self._RetrieveAllGroups, |
| 235 | + 'description': 'Retrieve all Groups in a domain'}, |
| 236 | + {'function': self._DeleteGroup, |
| 237 | + 'description': 'Deleting a Group from a domain'}, |
| 238 | + {'function': self._AddMemberToGroup, |
| 239 | + 'description': 'Add a member to a Group'}, |
| 240 | + {'function': self._RetrieveSingleMember, |
| 241 | + 'description': 'Retrieve a member of a group'}, |
| 242 | + {'function': self._RetrieveAllMembersOfGroup, |
| 243 | + 'description': 'Retrieve all members of a group'}, |
| 244 | + {'function': self._RemoveMemberFromGroup, |
| 245 | + 'description': 'Delete a member from a Group'} |
| 246 | + ] |
| 247 | + |
| 248 | + while True: |
| 249 | + print 'Choose an option:\n0 - to exit' |
| 250 | + for i in range (0, len(functions_list)): |
| 251 | + print '%d - %s' % ((i+1), functions_list[i]['description']) |
| 252 | + choice = int(raw_input()) |
| 253 | + if choice == 0: |
| 254 | + break |
| 255 | + if choice < 0 or choice > len(functions_list): |
| 256 | + print 'Not a valid option!' |
| 257 | + continue |
| 258 | + functions_list[choice-1]['function']() |
| 259 | + |
| 260 | + |
| 261 | +def main(): |
| 262 | + """Runs the sample using an instance of GroupsQuickStartExample.""" |
| 263 | + # Parse command line options |
| 264 | + try: |
| 265 | + opts, args = getopt.getopt(sys.argv[1:], '', ['client_id=', |
| 266 | + 'client_secret=', |
| 267 | + 'domain=']) |
| 268 | + except getopt.error, msg: |
| 269 | + print ('python groups_provisioning_quick_start_example.py' |
| 270 | + ' --client_id [clientId] --client_secret [clientSecret]' |
| 271 | + ' --domain [domain]') |
| 272 | + sys.exit(2) |
| 273 | + |
| 274 | + client_id = '' |
| 275 | + client_secret = '' |
| 276 | + domain = '' |
| 277 | + # Parse options |
| 278 | + for option, arg in opts: |
| 279 | + if option == '--client_id': |
| 280 | + client_id = arg |
| 281 | + elif option == '--client_secret': |
| 282 | + client_secret = arg |
| 283 | + elif option == '--domain': |
| 284 | + domain = arg |
| 285 | + |
| 286 | + while not client_id: |
| 287 | + client_id = raw_input('Please enter a clientId: ') |
| 288 | + while not client_secret: |
| 289 | + client_secret = raw_input('Please enter a clientSecret: ') |
| 290 | + while not domain: |
| 291 | + domain = raw_input('Please enter domain name (example.com): ') |
| 292 | + |
| 293 | + try: |
| 294 | + groups_quick_start_example = GroupsQuickStartExample( |
| 295 | + client_id, client_secret, domain) |
| 296 | + except gdata.service.BadAuthentication: |
| 297 | + print 'Invalid user credentials given.' |
| 298 | + return |
| 299 | + |
| 300 | + groups_quick_start_example.CreateGroupsClient() |
| 301 | + groups_quick_start_example.Run() |
| 302 | + |
| 303 | + |
| 304 | +if __name__ == '__main__': |
| 305 | + main() |
0 commit comments