<s> from __builtin__ import False <EOL> DOCUMENTATION = '''<STR_LIT>''' <EOL> EXAMPLES = '''<STR_LIT>''' <EOL> from keystoneclient . v3 import client as v3client <EOL> from keystoneclient . auth . identity import v3 <EOL> from keystoneclient . auth import token_endpoint <EOL> from keystoneclient import session <EOL> from keystoneclient import exceptions <EOL> def _get_client ( auth_url = None , token = None , login_username = None , login_password = None , login_project_name = None , <EOL> login_project_domain_name = None , login_user_domain_name = None , login_domain_name = None , <EOL> insecure = False , ca_cert = None ) : <EOL> """<STR_LIT>""" <EOL> auth_plugin = None <EOL> if login_domain_name and login_project_name : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if token : <EOL> auth_plugin = token_endpoint . Token ( endpoint = auth_url , token = token ) <EOL> else : <EOL> auth_plugin = v3 . Password ( auth_url = auth_url , username = login_username , password = login_password , <EOL> project_name = login_project_name , project_domain_name = login_project_domain_name , <EOL> user_domain_name = login_user_domain_name , domain_name = login_domain_name ) <EOL> verify = not insecure <EOL> auth_session = session . Session ( <EOL> auth = auth_plugin , verify = verify , cert = ca_cert ) <EOL> return v3client . Client ( auth_url = auth_url , session = auth_session ) <EOL> def _find_domain ( ks_client , domain_name = None ) : <EOL> domains = ks_client . domains . list ( name = domain_name ) <EOL> return domains [ <NUM_LIT:0> ] if len ( domains ) else None <EOL> def _delete_domain ( ks_client , domain = None ) : <EOL> ks_client . domains . update ( domain , enabled = False ) <EOL> return ks_client . domains . delete ( domain ) <EOL> def _create_domain ( ks_client , domain_name = None , description = None ) : <EOL> return ks_client . domains . create ( name = domain_name , description = description ) <EOL> def _find_user ( ks_client , domain_name = None , user_name = None ) : <EOL> domain = _find_domain ( ks_client , domain_name = domain_name ) <EOL> if domain : <EOL> users = ks_client . users . list ( domain = domain , name = user_name ) <EOL> if len ( users ) : <EOL> return users [ <NUM_LIT:0> ] <EOL> def _create_user ( ks_client , user_name = None , user_password = None , domain_name = None , <EOL> email = None , description = None ) : <EOL> domain = _find_domain ( ks_client , domain_name ) <EOL> return ks_client . users . create ( name = user_name , password = user_password , <EOL> description = description , <EOL> email = email , domain = domain ) <EOL> def _delete_user ( ks_client , user = None ) : <EOL> ks_client . users . update ( user = user , enabled = False ) <EOL> ks_client . users . delete ( user = user ) <EOL> def _update_user ( ks_client , user = None , password = None ) : <EOL> ks_client . users . update ( user = user , password = password ) <EOL> def _find_project ( ks_client , domain_name = None , project_name = None ) : <EOL> domain = _find_domain ( ks_client , domain_name = domain_name ) <EOL> if domain : <EOL> projects = ks_client . projects . list ( domain = domain , name = project_name ) <EOL> if len ( projects ) : <EOL> return projects [ <NUM_LIT:0> ] <EOL> def _create_project ( ks_client , project_name = None , domain_name = None , <EOL> description = None ) : <EOL> domain = _find_domain ( ks_client , domain_name ) <EOL> return ks_client . projects . create ( name = project_name , <EOL> description = description , <EOL> domain = domain ) <EOL> def _delete_project ( ks_client , project = None ) : <EOL> ks_client . projects . update ( project = project , enabled = False ) <EOL> ks_client . projects . delete ( project = project ) <EOL> def _find_role ( ks_client , role_name = None ) : <EOL> roles = ks_client . roles . list ( name = role_name ) <EOL> return roles [ <NUM_LIT:0> ] if len ( roles ) else None <EOL> def _delete_role ( ks_client , role = None ) : <EOL> return ks_client . roles . delete ( role ) <EOL> def _create_role ( ks_client , role_name = None , description = None ) : <EOL> return ks_client . roles . create ( name = role_name , description = description ) <EOL> def _grant_roles ( ks_client , role = None , project = None , user = None , domain = None ) : <EOL> return ks_client . roles . grant ( role = role , project = project , user = user , domain = domain ) <EOL> def _revoke_roles ( ks_client , role = None , project = None , user = None , domain = None ) : <EOL> return ks_client . roles . revoke ( role = role , project = project , user = user , domain = domain ) <EOL> def _find_service ( ks_client , service_name = None , service_type = None ) : <EOL> services = ks_client . services . list ( name = service_name , <EOL> type = service_type ) <EOL> return services [ <NUM_LIT:0> ] if len ( services ) else None <EOL> def _create_service ( ks_client , service_name = None , service_type = None , <EOL> description = None ) : <EOL> return ks_client . services . create ( name = service_name , type = service_type , <EOL> description = description ) <EOL> def _delete_service ( ks_client , service_name = None ) : <EOL> return ks_client . services . delete ( name = service_name ) <EOL> def _find_endpoint ( ks_client , service = None , interface = None ) : <EOL> endpoints = ks_client . endpoints . list ( service = service , interface = interface ) <EOL> return endpoints [ <NUM_LIT:0> ] if len ( endpoints ) else None <EOL> def _create_endpoint ( ks_client , service = None , url = None , <EOL> interface = None , region = None ) : <EOL> return ks_client . endpoints . create ( service = service , url = url , <EOL> interface = interface , region = region ) <EOL> def find_domain ( ks_client , domain_name = None ) : <EOL> domain = _find_domain ( ks_client , domain_name = domain_name ) <EOL> result , domain = ( True , domain ) if ( domain ) else ( False , None ) <EOL> return result , domain <EOL> def create_domain ( ks_client , domain_name = None , description = None ) : <EOL> domain = _find_domain ( ks_client , domain_name = domain_name ) <EOL> if domain : <EOL> return ( False , domain ) <EOL> domain = _create_domain ( <EOL> ks_client , domain_name = domain_name , description = description ) <EOL> return ( True , domain ) <EOL> def delete_domain ( ks_client , domain_name = None ) : <EOL> domain = _find_domain ( ks_client , domain_name = domain_name ) <EOL> if domain : <EOL> _delete_domain ( ks_client , domain = domain ) <EOL> return ( True , domain ) <EOL> return ( False , None ) <EOL> def find_user ( ks_client , user_name = None , domain_name = None ) : <EOL> user = _find_user ( ks_client , domain_name = domain_name , user_name = user_name ) <EOL> result , user = ( True , user ) if user else ( False , None ) <EOL> return result , user <EOL> def create_user ( ks_client , user_name = None , user_password = None , domain_name = None , <EOL> email = None , description = None ) : <EOL> user = _find_user ( ks_client , user_name = user_name , domain_name = domain_name ) <EOL> if user : <EOL> return ( False , user ) <EOL> user = _create_user ( ks_client , user_name = user_name , <EOL> user_password = user_password , domain_name = domain_name , <EOL> email = email , description = description ) <EOL> return ( True , user ) <EOL> def delete_user ( ks_client , user_name = None , domain_name = None ) : <EOL> user = _find_user ( ks_client , domain_name = domain_name , user_name = user_name ) <EOL> if user : <EOL> _delete_user ( ks_client , user = user ) <EOL> return ( True , user ) <EOL> return ( False , None ) <EOL> def update_user ( ks_client , user_name = None , domain_name = None , user_password = None ) : <EOL> user = _find_user ( ks_client , domain_name = domain_name , user_name = user_name ) <EOL> if user : <EOL> _update_user ( ks_client , user = user , password = user_password ) <EOL> return ( True , user ) <EOL> return ( False , None ) <EOL> def find_project ( ks_client , project_name = None , domain_name = None ) : <EOL> project = _find_project ( <EOL> ks_client , domain_name = domain_name , project_name = project_name ) <EOL> result , project = ( True , project ) if project else ( False , None ) <EOL> return result , project <EOL> def create_project ( ks_client , project_name = None , domain_name = None , <EOL> description = None ) : <EOL> project = _find_project ( <EOL> ks_client , project_name = project_name , domain_name = domain_name ) <EOL> if project : <EOL> return ( False , project ) <EOL> project = _create_project ( ks_client , project_name = project_name , <EOL> domain_name = domain_name , description = description ) <EOL> return ( True , project ) <EOL> def delete_project ( ks_client , project_name = None , domain_name = None ) : <EOL> project = _find_project ( <EOL> ks_client , domain_name = domain_name , project_name = project_name ) <EOL> if project : <EOL> _delete_project ( ks_client , project = project ) <EOL> return ( True , project ) <EOL> return ( False , None ) <EOL> def find_role ( ks_client , role_name = None ) : <EOL> role = _find_role ( ks_client , role_name = role_name ) <EOL> result , role = ( True , role ) if ( role ) else ( False , None ) <EOL> return result , role <EOL> def create_role ( ks_client , role_name = None , description = None ) : <EOL> role = _find_role ( ks_client , role_name = role_name ) <EOL> if role : <EOL> return ( False , role ) <EOL> role = _create_role ( <EOL> ks_client , role_name = role_name , description = description ) <EOL> return ( True , role ) <EOL> def delete_role ( ks_client , role_name = None ) : <EOL> role = _find_role ( ks_client , role_name = role_name ) <EOL> if role : <EOL> _delete_role ( ks_client , role = role ) <EOL> return ( True , role ) <EOL> return ( False , None ) <EOL> def _find_project_role_assignment ( ks_client , role_name = None , user_name = None , project_name = None , <EOL> user_domain_name = None , project_domain_name = None ) : <EOL> role = _find_role ( ks_client , role_name = role_name ) <EOL> user = _find_user ( <EOL> ks_client , user_name = user_name , domain_name = user_domain_name ) <EOL> project = _find_project ( <EOL> ks_client , project_name = project_name , domain_name = project_domain_name ) <EOL> ret = False <EOL> if ( user and role and project ) : <EOL> try : <EOL> ret = ks_client . roles . check ( role = role , user = user , project = project ) <EOL> except Exception as e : <EOL> if not isinstance ( e , exceptions . NotFound ) : <EOL> raise <EOL> return ( ret , role , user , project ) <EOL> def _grant_or_revoke_project_role ( ks_client , role_name = None , user_name = None , project_name = None , <EOL> user_domain_name = None , project_domain_name = None , grant = True ) : <EOL> ( assignment_status , role , user , project ) = _find_project_role_assignment ( ks_client , role_name = role_name , <EOL> user_name = user_name , project_name = project_name , <EOL> user_domain_name = user_domain_name , project_domain_name = project_domain_name ) <EOL> if assignment_status and grant : <EOL> return ( False , "<STR_LIT>" ) <EOL> if not assignment_status and not grant : <EOL> return ( False , "<STR_LIT>" ) <EOL> if ( user and role and project ) : <EOL> if ( grant ) : <EOL> _grant_roles ( ks_client , role = role , project = project , user = user ) <EOL> else : <EOL> _revoke_roles ( ks_client , role = role , project = project , user = user ) <EOL> return ( True , "<STR_LIT:OK>" ) <EOL> return ( False , "<STR_LIT>" ) <EOL> def _find_domain_role_assignment ( ks_client , role_name = None , user_name = None , <EOL> user_domain_name = None , domain_name = None ) : <EOL> role = _find_role ( ks_client , role_name = role_name ) <EOL> user = _find_user ( <EOL> ks_client , user_name = user_name , domain_name = user_domain_name ) <EOL> domain = _find_domain ( ks_client , domain_name = domain_name ) <EOL> ret = False <EOL> if ( user and role and domain ) : <EOL> try : <EOL> ret = ks_client . roles . check ( role = role , user = user , domain = domain ) <EOL> except Exception as e : <EOL> if not isinstance ( e , exceptions . NotFound ) : <EOL> raise <EOL> return ( ret , role , user , domain ) <EOL> def _grant_or_revoke_domain_role ( ks_client , role_name = None , user_name = None , <EOL> user_domain_name = None , domain_name = None , grant = True ) : <EOL> ( assignment_status , role , user , domain ) = _find_domain_role_assignment ( ks_client , role_name = role_name , <EOL> user_name = user_name , user_domain_name = user_domain_name , domain_name = domain_name ) <EOL> if assignment_status and grant : <EOL> return ( False , "<STR_LIT>" ) <EOL> if not assignment_status and not grant : <EOL> return ( False , "<STR_LIT>" ) <EOL> if ( user and role and domain ) : <EOL> if ( grant ) : <EOL> _grant_roles ( ks_client , role = role , domain = domain , user = user ) <EOL> else : <EOL> _revoke_roles ( ks_client , role = role , domain = domain , user = user ) <EOL> return ( True , "<STR_LIT:OK>" ) <EOL> return ( False , "<STR_LIT>" ) <EOL> def grant_project_role ( ks_client , role_name = None , user_name = None , project_name = None , <EOL> user_domain_name = None , project_domain_name = None ) : <EOL> return _grant_or_revoke_project_role ( ks_client , role_name = role_name , user_name = user_name , <EOL> project_name = project_name , user_domain_name = user_domain_name , <EOL> project_domain_name = project_domain_name , grant = True ) <EOL> def revoke_project_role ( ks_client , role_name = None , user_name = None , project_name = None , <EOL> user_domain_name = None , project_domain_name = None ) : <EOL> return _grant_or_revoke_project_role ( ks_client , role_name = role_name , user_name = user_name , <EOL> project_name = project_name , user_domain_name = user_domain_name , <EOL> project_domain_name = project_domain_name , grant = False ) <EOL> def grant_domain_role ( ks_client , role_name = None , user_name = None , domain_name = None , <EOL> user_domain_name = None ) : <EOL> return _grant_or_revoke_domain_role ( ks_client , role_name = role_name , user_name = user_name , <EOL> user_domain_name = user_domain_name , <EOL> domain_name = domain_name , grant = True ) <EOL> def revoke_domain_role ( ks_client , role_name = None , user_name = None , domain_name = None , <EOL> user_domain_name = None ) : <EOL> return _grant_or_revoke_domain_role ( ks_client , role_name = role_name , user_name = user_name , <EOL> user_domain_name = user_domain_name , <EOL> domain_name = domain_name , grant = False ) <EOL> def create_service ( ks_client , service_name = None , service_type = None , <EOL> description = None ) : <EOL> service = _find_service ( ks_client , service_name = service_name , <EOL> service_type = service_type ) <EOL> if service : <EOL> return ( False , service ) <EOL> service = _create_service ( <EOL> ks_client , service_name = service_name , service_type = service_type , <EOL> description = description ) <EOL> return ( True , service ) <EOL> def create_endpoint ( ks_client , service_name = None , region = None , <EOL> admin_url = None , internal_url = None , <EOL> public_url = None ) : <EOL> service = _find_service ( ks_client , service_name = service_name ) <EOL> if not service : <EOL> raise Exception ( "<STR_LIT>" % ( service_name ) ) <EOL> endpoint = _find_endpoint ( ks_client , service = service , interface = "<STR_LIT>" ) <EOL> if endpoint : <EOL> return ( False , endpoint ) <EOL> if public_url : <EOL> _ = _create_endpoint ( <EOL> ks_client , service = service , interface = "<STR_LIT>" , region = region , <EOL> url = public_url ) <EOL> _ = _create_endpoint ( <EOL> ks_client , service = service , interface = "<STR_LIT>" , region = region , <EOL> url = internal_url ) <EOL> admin_endpoint = _create_endpoint ( <EOL> ks_client , service = service , interface = "<STR_LIT>" , region = region , <EOL> url = admin_url ) <EOL> return True , admin_endpoint <EOL> def get_login_token ( ks_client ) : <EOL> session = ks_client . session <EOL> auth_headers = session . get_auth_headers ( ) <EOL> token = auth_headers . get ( '<STR_LIT>' ) <EOL> return ( True , token ) <EOL> def process_params ( module ) : <EOL> user_name = module . params . get ( "<STR_LIT>" , None ) <EOL> user_password = module . params . get ( "<STR_LIT>" , None ) <EOL> email = module . params . get ( "<STR_LIT:email>" , None ) <EOL> description = module . params . get ( "<STR_LIT:description>" , None ) <EOL> user_domain_name = module . params . get ( "<STR_LIT>" , None ) <EOL> domain_name = module . params . get ( "<STR_LIT>" , None ) <EOL> project_name = module . params . get ( "<STR_LIT>" , None ) <EOL> project_domain_name = module . params . get ( "<STR_LIT>" , None ) <EOL> role_name = module . params . get ( "<STR_LIT>" , None ) <EOL> service_name = module . params . get ( "<STR_LIT>" , None ) <EOL> service_type = module . params . get ( "<STR_LIT>" , None ) <EOL> region = module . params . get ( "<STR_LIT>" , None ) <EOL> admin_url = module . params . get ( "<STR_LIT>" , None ) <EOL> internal_url = module . params . get ( "<STR_LIT>" , None ) <EOL> public_url = module . params . get ( "<STR_LIT>" , None ) <EOL> action = module . params [ "<STR_LIT:action>" ] <EOL> if ( action == "<STR_LIT>" ) : <EOL> kwargs = dict ( ) <EOL> elif ( action == "<STR_LIT>" or action == "<STR_LIT>" ) : <EOL> kwargs = dict ( domain_name = domain_name ) <EOL> elif ( action == "<STR_LIT>" ) : <EOL> kwargs = dict ( domain_name = domain_name , description = description ) <EOL> elif ( action == "<STR_LIT>" or action == "<STR_LIT>" ) : <EOL> kwargs = dict ( domain_name = user_domain_name , user_name = user_name ) <EOL> elif ( action == "<STR_LIT>" ) : <EOL> kwargs = dict ( domain_name = user_domain_name , description = description , user_name = user_name , <EOL> email = email , user_password = user_password ) <EOL> elif ( action == "<STR_LIT>" or action == "<STR_LIT>" ) : <EOL> kwargs = dict ( <EOL> domain_name = project_domain_name , project_name = project_name ) <EOL> elif ( action == "<STR_LIT>" ) : <EOL> kwargs = dict ( domain_name = project_domain_name , description = description , <EOL> project_name = project_name ) <EOL> elif ( action == "<STR_LIT>" or action == "<STR_LIT>" ) : <EOL> kwargs = dict ( role_name = role_name ) <EOL> elif ( action == "<STR_LIT>" ) : <EOL> kwargs = dict ( role_name = role_name , description = description ) <EOL> elif ( action == "<STR_LIT>" or action == "<STR_LIT>" ) : <EOL> kwargs = dict ( role_name = role_name , user_name = user_name , <EOL> user_domain_name = user_domain_name , project_name = project_name , <EOL> project_domain_name = project_domain_name ) <EOL> elif ( action == "<STR_LIT>" or action == "<STR_LIT>" ) : <EOL> kwargs = dict ( role_name = role_name , user_name = user_name , <EOL> user_domain_name = user_domain_name , <EOL> domain_name = domain_name ) <EOL> elif ( action == "<STR_LIT>" ) : <EOL> kwargs = dict ( service_name = service_name , service_type = service_type , <EOL> description = description ) <EOL> elif ( action == "<STR_LIT>" ) : <EOL> kwargs = dict ( service_name = service_name , region = region , <EOL> admin_url = admin_url , internal_url = internal_url , <EOL> public_url = public_url ) <EOL> elif ( action == "<STR_LIT>" ) : <EOL> kwargs = dict ( domain_name = user_domain_name , user_name = user_name , <EOL> user_password = user_password ) <EOL> return kwargs <EOL> dispatch_map = { <EOL> "<STR_LIT>" : find_domain , <EOL> "<STR_LIT>" : delete_domain , <EOL> "<STR_LIT>" : create_domain , <EOL> "<STR_LIT>" : find_user , <EOL> "<STR_LIT>" : delete_user , <EOL> "<STR_LIT>" : create_user , <EOL> "<STR_LIT>" : find_project , <EOL> "<STR_LIT>" : delete_project , <EOL> "<STR_LIT>" : create_project , <EOL> "<STR_LIT>" : find_role , <EOL> "<STR_LIT>" : delete_role , <EOL> "<STR_LIT>" : create_role , <EOL> "<STR_LIT>" : grant_project_role , <EOL> "<STR_LIT>" : revoke_project_role , <EOL> "<STR_LIT>" : grant_domain_role , <EOL> "<STR_LIT>" : revoke_domain_role , <EOL> "<STR_LIT>" : create_service , <EOL> "<STR_LIT>" : create_endpoint , <EOL> "<STR_LIT>" : get_login_token , <EOL> "<STR_LIT>" : update_user <EOL> } <EOL> def get_client ( module ) : <EOL> login_username = module . params . get ( "<STR_LIT>" ) <EOL> login_project_name = module . params . get ( "<STR_LIT>" ) <EOL> login_user_domain_name = module . params . get ( "<STR_LIT>" ) <EOL> login_project_domain_name = module . params . get ( "<STR_LIT>" ) <EOL> login_password = module . params . get ( "<STR_LIT>" ) <EOL> login_domain_name = module . params . get ( "<STR_LIT>" ) <EOL> auth_url = module . params . get ( "<STR_LIT>" ) <EOL> token = module . params . get ( "<STR_LIT>" ) <EOL> insecure = module . params . get ( "<STR_LIT>" ) <EOL> cacert = module . params . get ( "<STR_LIT>" ) <EOL> ks_client = _get_client ( login_username = login_username , <EOL> login_project_name = login_project_name , <EOL> login_user_domain_name = login_user_domain_name , <EOL> login_project_domain_name = login_project_domain_name , <EOL> login_password = login_password , <EOL> login_domain_name = login_domain_name , <EOL> auth_url = auth_url , token = token , <EOL> insecure = insecure , ca_cert = cacert ) <EOL> return ks_client <EOL> def process_module_action ( module ) : <EOL> ks_client = get_client ( module ) <EOL> action = module . params [ "<STR_LIT:action>" ] <EOL> kwargs = process_params ( module ) <EOL> try : <EOL> result = dispatch_map [ action ] ( ks_client , ** kwargs ) <EOL> except Exception as e : <EOL> module . fail_json ( msg = "<STR_LIT>" % e ) <EOL> else : <EOL> status , resource_data = result <EOL> if hasattr ( resource_data , '<STR_LIT>' ) : <EOL> resource_data = resource_data . _info <EOL> data = dict ( changed = status , result = resource_data ) <EOL> module . exit_json ( ** data ) <EOL> def main ( ) : <EOL> supported_actions = dispatch_map . keys ( ) <EOL> argument_spec = dict ( <EOL> login_username = dict ( default = None ) , <EOL> login_password = dict ( default = None , no_log = True ) , <EOL> login_project_name = dict ( default = None ) , <EOL> login_project_domain_name = dict ( default = None ) , <EOL> login_user_domain_name = dict ( default = None ) , <EOL> login_domain_name = dict ( default = None ) , <EOL> login_token = dict ( default = None , no_log = True ) , <EOL> insecure = dict ( default = None ) , <EOL> cacert = dict ( default = None ) , <EOL> endpoint = dict ( default = None ) , <EOL> description = dict ( default = "<STR_LIT>" ) , <EOL> email = dict ( default = None ) , <EOL> user_name = dict ( default = None ) , <EOL> user_password = dict ( default = None , no_log = True ) , <EOL> user_domain_name = dict ( default = None ) , <EOL> project_name = dict ( default = None ) , <EOL> project_domain_name = dict ( default = None ) , <EOL> domain_name = dict ( default = None ) , <EOL> role_name = dict ( default = None ) , <EOL> service_name = dict ( default = None ) , <EOL> service_type = dict ( default = None ) , <EOL> region = dict ( default = None ) , <EOL> admin_url = dict ( default = None ) , <EOL> public_url = dict ( default = None ) , <EOL> internal_url = dict ( default = None ) , <EOL> action = dict ( default = None , required = True , choices = supported_actions ) <EOL> ) <EOL> module = AnsibleModule ( <EOL> argument_spec = argument_spec , supports_check_mode = False ) <EOL> process_module_action ( module ) <EOL> from ansible . module_utils . basic import * <EOL> from ansible . module_utils . database import * <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
