<s> """<STR_LIT>""" <EOL> import pprint <EOL> import textwrap <EOL> import time <EOL> from apiclient import errors as apiclient_errors <EOL> from apiclient . discovery import build <EOL> from utils import admin_api_tool_errors <EOL> from utils import http_utils <EOL> from utils import log_utils <EOL> _MISSING_FIELD_STUB = '<STR_LIT>' <EOL> def GetFieldFromUser ( user , field_name , sub_field_name = None ) : <EOL> """<STR_LIT>""" <EOL> if field_name not in user : <EOL> return _MISSING_FIELD_STUB % field_name <EOL> field_value = user . get ( field_name ) <EOL> if sub_field_name : <EOL> if not field_value or sub_field_name not in field_value : <EOL> return _MISSING_FIELD_STUB % ( '<STR_LIT>' % ( field_name , sub_field_name ) ) <EOL> return field_value . get ( sub_field_name ) <EOL> return field_value <EOL> class UsersApiWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , http ) : <EOL> """<STR_LIT>""" <EOL> self . _service = build ( serviceName = '<STR_LIT>' , version = '<STR_LIT>' , <EOL> http = http ) <EOL> self . _users = self . _service . users ( ) <EOL> @ staticmethod <EOL> def _ShowAllUserFields ( user ) : <EOL> """<STR_LIT>""" <EOL> return user <EOL> @ staticmethod <EOL> def _ShowBasicUserFields ( user ) : <EOL> """<STR_LIT>""" <EOL> return ( GetFieldFromUser ( user , '<STR_LIT>' ) , <EOL> GetFieldFromUser ( user , '<STR_LIT:id>' ) , <EOL> GetFieldFromUser ( user , '<STR_LIT:name>' , sub_field_name = '<STR_LIT>' ) ) <EOL> @ staticmethod <EOL> def _ProcessCustomerId ( user ) : <EOL> """<STR_LIT>""" <EOL> return GetFieldFromUser ( user , '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def _PrintCustomerId ( user ) : <EOL> """<STR_LIT>""" <EOL> print '<STR_LIT>' % GetFieldFromUser ( user , '<STR_LIT>' ) . split ( <EOL> '<STR_LIT:@>' ) [ <NUM_LIT:1> ] <EOL> print GetFieldFromUser ( user , '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def _PrintOneLine ( email , user_id , full_name ) : <EOL> """<STR_LIT>""" <EOL> print '<STR_LIT>' % ( user_id , email , full_name ) <EOL> @ staticmethod <EOL> def _PrintUserHeader ( ) : <EOL> """<STR_LIT>""" <EOL> UsersApiWrapper . _PrintOneLine ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def _PrintOneUser ( user , long_list = False ) : <EOL> """<STR_LIT>""" <EOL> UsersApiWrapper . _PrintOneLine ( GetFieldFromUser ( user , '<STR_LIT>' ) , <EOL> GetFieldFromUser ( user , '<STR_LIT:id>' ) , <EOL> GetFieldFromUser ( user , '<STR_LIT:name>' , <EOL> sub_field_name = '<STR_LIT>' ) ) <EOL> if long_list : <EOL> for field in sorted ( user . keys ( ) ) : <EOL> if field in ( '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT>' ) : <EOL> continue <EOL> formatted_text = textwrap . fill ( <EOL> pprint . pformat ( GetFieldFromUser ( user , field ) ) , <EOL> subsequent_indent = ( <NUM_LIT:7> + len ( field ) ) * '<STR_LIT:U+0020>' , break_on_hyphens = False ) <EOL> print '<STR_LIT>' % ( field , formatted_text ) <EOL> def _ProcessUserListPage ( self , apps_domain , max_page , next_page_token = None , <EOL> query_filter = None ) : <EOL> """<STR_LIT>""" <EOL> request = self . _users . list ( domain = apps_domain , maxResults = max_page , <EOL> pageToken = next_page_token , <EOL> query = query_filter ) <EOL> backoff = http_utils . Backoff ( ) <EOL> while backoff . Loop ( ) : <EOL> try : <EOL> users_list = request . execute ( ) <EOL> return users_list <EOL> except apiclient_errors . HttpError as e : <EOL> if e . resp . status not in http_utils . RETRY_RESPONSE_CODES : <EOL> raise admin_api_tool_errors . AdminAPIToolUserError ( <EOL> '<STR_LIT>' % ( <EOL> http_utils . ParseHttpResult ( e . uri , e . resp , e . content ) , <EOL> apps_domain ) ) <EOL> log_utils . LogInfo ( <EOL> '<STR_LIT>' % e . resp . status ) <EOL> backoff . Fail ( ) <EOL> def _ProcessDomainUsers ( self , apps_domain , process_fn , max_results = None , <EOL> max_page = <NUM_LIT:100> , query_filter = None ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> retrieved_count = <NUM_LIT:0> <EOL> next_page_token = None <EOL> if max_page < <NUM_LIT:1> or max_page > <NUM_LIT> : <EOL> max_page = <NUM_LIT:100> <EOL> if max_results is not None and max_results < max_page : <EOL> max_page = max_results <EOL> users_list = self . _ProcessUserListPage ( apps_domain = apps_domain , <EOL> max_page = max_page , <EOL> query_filter = query_filter ) <EOL> while True : <EOL> for user in users_list . get ( '<STR_LIT>' , [ ] ) : <EOL> result = process_fn ( user ) <EOL> if result : <EOL> results . append ( result ) <EOL> retrieved_count += <NUM_LIT:1> <EOL> if max_results is not None and retrieved_count >= max_results : <EOL> return results , retrieved_count <EOL> next_page_token = users_list . get ( '<STR_LIT>' ) <EOL> if not next_page_token : <EOL> return results , retrieved_count <EOL> users_list = self . _ProcessUserListPage ( apps_domain = apps_domain , <EOL> max_page = max_page , <EOL> query_filter = query_filter , <EOL> next_page_token = next_page_token ) <EOL> def GetCustomerId ( self , apps_domain ) : <EOL> """<STR_LIT>""" <EOL> results , _ = self . _ProcessDomainUsers ( apps_domain = apps_domain , <EOL> process_fn = self . _ProcessCustomerId , <EOL> max_results = <NUM_LIT:1> ) <EOL> if results : <EOL> return results [ <NUM_LIT:0> ] <EOL> return None <EOL> def PrintCustomerId ( self , apps_domain ) : <EOL> """<STR_LIT>""" <EOL> self . _ProcessDomainUsers ( apps_domain = apps_domain , <EOL> process_fn = self . _PrintCustomerId , <EOL> max_results = <NUM_LIT:1> ) <EOL> def GetDomainUsers ( self , apps_domain , basic = True , max_results = None , <EOL> max_page = <NUM_LIT> , query_filter = None ) : <EOL> """<STR_LIT>""" <EOL> log_utils . LogDebug ( '<STR_LIT>' % max_results ) <EOL> if basic : <EOL> user_attribute_filter_fn = self . _ShowBasicUserFields <EOL> else : <EOL> user_attribute_filter_fn = self . _ShowAllUserFields <EOL> results , _ = self . _ProcessDomainUsers ( apps_domain = apps_domain , <EOL> max_results = max_results , <EOL> max_page = max_page , <EOL> query_filter = query_filter , <EOL> process_fn = user_attribute_filter_fn ) <EOL> return results <EOL> def PrintDomainUsers ( self , apps_domain , max_results = None , max_page = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> print '<STR_LIT>' % apps_domain <EOL> self . _PrintUserHeader ( ) <EOL> _ , count = self . _ProcessDomainUsers ( apps_domain = apps_domain , <EOL> process_fn = self . _PrintOneUser , <EOL> max_results = max_results , <EOL> max_page = max_page ) <EOL> print '<STR_LIT>' % count <EOL> def GetDomainUser ( self , user_mail ) : <EOL> """<STR_LIT>""" <EOL> log_utils . LogDebug ( '<STR_LIT>' % user_mail ) <EOL> request = self . _users . get ( userKey = user_mail ) <EOL> backoff = http_utils . Backoff ( ) <EOL> while backoff . Loop ( ) : <EOL> try : <EOL> return request . execute ( ) <EOL> except apiclient_errors . HttpError as e : <EOL> if e . resp . status not in http_utils . RETRY_RESPONSE_CODES : <EOL> error_text = http_utils . ParseHttpResult ( e . uri , e . resp , e . content ) <EOL> if error_text . startswith ( '<STR_LIT>' ) : <EOL> return None <EOL> raise admin_api_tool_errors . AdminAPIToolUserError ( <EOL> '<STR_LIT>' % ( user_mail , error_text ) ) <EOL> log_utils . LogInfo ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( user_mail , e . resp . status ) ) <EOL> backoff . Fail ( ) <EOL> def IsDomainUser ( self , user_mail ) : <EOL> """<STR_LIT>""" <EOL> log_utils . LogDebug ( '<STR_LIT>' % user_mail ) <EOL> return self . GetDomainUser ( user_mail ) is not None <EOL> def PrintDomainUser ( self , user_mail , long_list = False ) : <EOL> """<STR_LIT>""" <EOL> user = self . GetDomainUser ( user_mail ) <EOL> if user : <EOL> self . _PrintUserHeader ( ) <EOL> self . _PrintOneUser ( user = user , long_list = long_list ) <EOL> else : <EOL> print '<STR_LIT>' % user_mail <EOL> def AddDomainUser ( self , first_name , last_name , user_mail , new_password , <EOL> verify = False ) : <EOL> """<STR_LIT>""" <EOL> log_utils . LogDebug ( '<STR_LIT>' % user_mail ) <EOL> if self . IsDomainUser ( user_mail ) : <EOL> raise admin_api_tool_errors . AdminAPIToolUserError ( <EOL> '<STR_LIT>' % user_mail ) <EOL> body = { <EOL> '<STR_LIT>' : user_mail , <EOL> '<STR_LIT:name>' : { '<STR_LIT>' : first_name , '<STR_LIT>' : last_name } , <EOL> '<STR_LIT:password>' : new_password <EOL> } <EOL> backoff = http_utils . Backoff ( ) <EOL> while backoff . Loop ( ) : <EOL> try : <EOL> self . _users . insert ( body = body ) . execute ( ) <EOL> if verify : <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> if not self . IsDomainUser ( user_mail ) : <EOL> raise admin_api_tool_errors . AdminAPIToolUserError ( <EOL> '<STR_LIT>' % user_mail ) <EOL> return <EOL> except apiclient_errors . HttpError as e : <EOL> if e . resp . status not in http_utils . RETRY_RESPONSE_CODES : <EOL> raise admin_api_tool_errors . AdminAPIToolUserError ( <EOL> http_utils . ParseHttpResult ( e . uri , e . resp , e . content ) ) <EOL> log_utils . LogInfo ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( user_mail , e . resp . status ) ) <EOL> backoff . Fail ( ) <EOL> def DeleteDomainUser ( self , user_mail , verify = False ) : <EOL> """<STR_LIT>""" <EOL> log_utils . LogDebug ( '<STR_LIT>' % user_mail ) <EOL> if not self . IsDomainUser ( user_mail ) : <EOL> raise admin_api_tool_errors . AdminAPIToolUserError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( <EOL> user_mail ) ) <EOL> backoff = http_utils . Backoff ( ) <EOL> while backoff . Loop ( ) : <EOL> try : <EOL> self . _users . delete ( userKey = user_mail ) . execute ( ) <EOL> if verify : <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> if self . IsDomainUser ( user_mail ) : <EOL> raise admin_api_tool_errors . AdminAPIToolUserError ( <EOL> '<STR_LIT>' % user_mail ) <EOL> return <EOL> except apiclient_errors . HttpError as e : <EOL> if e . resp . status not in http_utils . RETRY_RESPONSE_CODES : <EOL> raise admin_api_tool_errors . AdminAPIToolUserError ( <EOL> http_utils . ParseHttpResult ( e . uri , e . resp , e . content ) ) <EOL> log_utils . LogInfo ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( user_mail , e . resp . status ) ) <EOL> backoff . Fail ( ) </s>
