Skip to content

Commit 1a411cc

Browse files
author
Shraddha
committed
Added a sample to search users with a given pattern and and move them to an organization
1 parent 84b2946 commit 1a411cc

1 file changed

Lines changed: 130 additions & 0 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/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+
"""Search users with a given pattern and move to a new organization.
18+
19+
Sample to move users to a new organization based on a pattern using
20+
the User Provisioning and Organization Provisioning APIs.
21+
22+
Usage:
23+
$ python search_organize_users.py
24+
"""
25+
26+
__author__ = 'Shraddha Gupta <shraddhag@google.com>'
27+
28+
from optparse import OptionParser
29+
import re
30+
from gdata.apps.client import AppsClient
31+
from gdata.apps.organization.client import OrganizationUnitProvisioningClient
32+
import gdata.gauth
33+
34+
BATCH_SIZE = 25
35+
SCOPES = ('https://apps-apis.google.com/a/feeds/user/ '
36+
'https://apps-apis.google.com/a/feeds/policies/')
37+
USER_AGENT = 'SearchAndOrganizeUsers'
38+
39+
40+
class SearchAndOrganizeUsers(object):
41+
"""Search users with a pattern and move them to organization."""
42+
43+
def __init__(self, client_id, client_secret, domain):
44+
"""Create a new SearchAndOrganizeUsers object configured for a domain.
45+
46+
Args:
47+
client_id: [string] The clientId of the developer.
48+
client_secret: [string] The clientSecret of the developer.
49+
domain: [string] The domain on which the functions are to be performed.
50+
"""
51+
self.client_id = client_id
52+
self.client_secret = client_secret
53+
self.domain = domain
54+
55+
def AuthorizeClient(self):
56+
"""Authorize the clients for making API requests."""
57+
self.token = gdata.gauth.OAuth2Token(
58+
client_id=self.client_id, client_secret=self.client_secret,
59+
scope=SCOPES, user_agent=USER_AGENT)
60+
uri = self.token.generate_authorize_url()
61+
print 'Please visit this URL to authorize the application:'
62+
print uri
63+
# Get the verification code from the standard input.
64+
code = raw_input('What is the verification code? ').strip()
65+
self.token.get_access_token(code)
66+
self.user_client = AppsClient(domain=self.domain, auth_token=self.token)
67+
self.org_client = OrganizationUnitProvisioningClient(
68+
domain=self.domain, auth_token=self.token)
69+
70+
def OrganizeUsers(self, customer_id, org_unit_path, pattern):
71+
"""Find users with given pattern and move to an organization in batches.
72+
73+
Args:
74+
customer_id: [string] customer_id to make calls to Organization API.
75+
org_unit_path: [string] path of organization unit where users are moved
76+
pattern: [regex object] regex to match with users
77+
"""
78+
users = self.user_client.RetrieveAllUsers()
79+
matched_users = []
80+
# Search the users that match given pattern
81+
for user in users.entry:
82+
if (pattern.search(user.login.user_name) or
83+
pattern.search(user.name.given_name) or
84+
pattern.search(user.name.family_name)):
85+
user_email = '%s@%s' % (user.login.user_name, self.domain)
86+
matched_users.append(user_email)
87+
# Maximum BATCH_SIZE users can be moved at one time
88+
# Split users into batches of BATCH_SIZE and move in batches
89+
for i in xrange(0, len(matched_users), BATCH_SIZE):
90+
batch_to_move = matched_users[i: i + BATCH_SIZE]
91+
self.org_client.MoveUserToOrgUnit(customer_id,
92+
org_unit_path, batch_to_move)
93+
print 'Number of users moved = %d' % len(matched_users)
94+
95+
def Run(self, org_unit_path, regex):
96+
self.AuthorizeClient()
97+
customer_id_entry = self.org_client.RetrieveCustomerId()
98+
customer_id = customer_id_entry.customer_id
99+
pattern = re.compile(regex)
100+
print 'Moving Users with the pattern %s' % regex
101+
self.OrganizeUsers(customer_id, org_unit_path, pattern)
102+
103+
104+
def main():
105+
usage = 'Usage: %prog [options]'
106+
parser = OptionParser(usage=usage)
107+
parser.add_option('--DOMAIN',
108+
help='Google Apps Domain, e.g. "domain.com".')
109+
parser.add_option('--CLIENT_ID',
110+
help='Registered CLIENT_ID of Domain.')
111+
parser.add_option('--CLIENT_SECRET',
112+
help='Registered CLIENT_SECRET of Domain.')
113+
parser.add_option('--ORG_UNIT_PATH',
114+
help='Orgunit path of organization where to move users.')
115+
parser.add_option('--PATTERN',
116+
help='Pattern to search in users')
117+
(options, args) = parser.parse_args()
118+
119+
if not (options.DOMAIN and options.CLIENT_ID and options.CLIENT_SECRET
120+
and options.ORG_UNIT_PATH and options.PATTERN):
121+
parser.print_help()
122+
return
123+
124+
sample = SearchAndOrganizeUsers(options.CLIENT_ID, options.CLIENT_SECRET,
125+
options.DOMAIN)
126+
sample.Run(options.ORG_UNIT_PATH, options.PATTERN)
127+
128+
129+
if __name__ == '__main__':
130+
main()

0 commit comments

Comments
 (0)