blob: 0e91bac7da31f9c5f96d98efe49c45caa0358cc9 [file] [log] [blame]
Joe Gregorio53806d02011-11-23 09:08:31 -05001#!/usr/bin/python
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"""Sample for the Group Settings API demonstrates get and update method.
18
19Usage:
20 $ python groupsettings.py
21
22You can also get help on all the command-line flags the program understands
23by running:
24
25 $ python groupsettings.py --help
26"""
27
28__author__ = 'Shraddha Gupta <shraddhag@google.com>'
29
30from optparse import OptionParser
31import os
32
33import pprint
34import sys
35from apiclient.discovery import build
36import httplib2
37from oauth2client.client import flow_from_clientsecrets
38from oauth2client.file import Storage
39from oauth2client.tools import run
40
41
42# CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this
43# application, including client_id and client_secret, which are found
44# on the API Access tab on the Google APIs
45# Console <http://code.google.com/apis/console>
46CLIENT_SECRETS = 'client_secrets.json'
47
48# Helpful message to display in the browser if the CLIENT_SECRETS file
49# is missing.
50MISSING_CLIENT_SECRETS_MESSAGE = """
51WARNING: Please configure OAuth 2.0
52
53To make this sample run you will need to populate the client_secrets.json file
54found at:
55
56 %s
57
58with information from the APIs Console <https://code.google.com/apis/console>.
59
60""" % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS)
61
62
63def access_settings(service, groupId, settings):
64 """Retrieves a group's settings and updates the access permissions to it.
65
66 Args:
67 service: object service for the Group Settings API.
68 groupId: string identifier of the group@domain.
69 settings: dictionary key-value pairs of properties of group.
70 """
71
72 # Get the resource 'group' from the set of resources of the API.
73 # The Group Settings API has only one resource 'group'.
74 group = service.groups()
75
76 # Retrieve the group properties
77 g = group.get(groupUniqueId=groupId).execute()
78 print '\nGroup properties for group %s\n' % g['name']
79 pprint.pprint(g)
80
81 # If dictionary is empty, return without updating the properties.
82 if not settings.keys():
83 print '\nGive access parameters to update group access permissions\n'
84 return
85
86 body = {}
87
88 # Settings might contain null value for some keys(properties).
89 # Extract the properties with values and add to dictionary body.
90 for key in settings.iterkeys():
91 if settings[key] is not None:
92 body[key] = settings[key]
93
94 # Update the properties of group
95 g1 = group.update(groupUniqueId=groupId, body=body).execute()
96
97 print '\nUpdated Access Permissions to the group\n'
98 pprint.pprint(g1)
99
100
101def main(argv):
102 """Demos the setting of the access properties by the Groups Settings API."""
103 usage = 'usage: %prog [options]'
104 parser = OptionParser(usage=usage)
105 parser.add_option('--groupId',
106 help='Group email address')
107 parser.add_option('--whoCanInvite',
108 help='Possible values: ALL_MANAGERS_CAN_INVITE, '
109 'ALL_MEMBERS_CAN_INVITE')
110 parser.add_option('--whoCanJoin',
111 help='Possible values: ALL_IN_DOMAIN_CAN_JOIN, '
112 'ANYONE_CAN_JOIN, CAN_REQUEST_TO_JOIN, '
113 'CAN_REQUEST_TO_JOIN')
114 parser.add_option('--whoCanPostMessage',
115 help='Possible values: ALL_IN_DOMAIN_CAN_POST, '
116 'ALL_MANAGERS_CAN_POST, ALL_MEMBERS_CAN_POST, '
117 'ANYONE_CAN_POST, NONE_CAN_POST')
118 parser.add_option('--whoCanViewGroup',
119 help='Possible values: ALL_IN_DOMAIN_CAN_VIEW, '
120 'ALL_MANAGERS_CAN_VIEW, ALL_MEMBERS_CAN_VIEW, '
121 'ANYONE_CAN_VIEW')
122 parser.add_option('--whoCanViewMembership',
123 help='Possible values: ALL_IN_DOMAIN_CAN_VIEW, '
124 'ALL_MANAGERS_CAN_VIEW, ALL_MEMBERS_CAN_VIEW, '
125 'ANYONE_CAN_VIEW')
126 (options, args) = parser.parse_args()
127
128 if options.groupId is None:
129 print 'Give the groupId for the group'
130 parser.print_help()
131 return
132
133 settings = {}
134
135 if (options.whoCanInvite or options.whoCanJoin or options.whoCanPostMessage
136 or options.whoCanPostMessage or options.whoCanViewMembership) is None:
137 print 'No access parameters given in input to update access permissions'
138 parser.print_help()
139 else:
140 settings = {'whoCanInvite': options.whoCanInvite,
141 'whoCanJoin': options.whoCanJoin,
142 'whoCanPostMessage': options.whoCanPostMessage,
143 'whoCanViewGroup': options.whoCanViewGroup,
144 'whoCanViewMembership': options.whoCanViewMembership}
145
146 # Set up a Flow object to be used if we need to authenticate.
147 FLOW = flow_from_clientsecrets(CLIENT_SECRETS,
148 scope='https://www.googleapis.com/auth/apps.groups.settings',
149 message=MISSING_CLIENT_SECRETS_MESSAGE)
150
151 storage = Storage('groupsettings.dat')
152 credentials = storage.get()
153
154 if credentials is None or credentials.invalid:
155 print 'invalid credentials'
156 # Save the credentials in storage to be used in subsequent runs.
157 credentials = run(FLOW, storage)
158
159 # Create an httplib2.Http object to handle our HTTP requests and authorize it
160 # with our good Credentials.
161 http = httplib2.Http()
162 http = credentials.authorize(http)
163
164 service = build('groupssettings', 'v1', http=http)
165
166 access_settings(service=service, groupId=options.groupId, settings=settings)
167
168if __name__ == '__main__':
169 main(sys.argv)