<s> from ansible . module_utils . basic import * <EOL> from inspect import getmembers , ismethod , getargspec <EOL> try : <EOL> from girder_client import GirderClient , AuthenticationError , HttpError <EOL> HAS_GIRDER_CLIENT = True <EOL> except ImportError : <EOL> HAS_GIRDER_CLIENT = False <EOL> DOCUMENTATION = '''<STR_LIT>''' <EOL> EXAMPLES = '''<STR_LIT>''' <EOL> def class_spec ( cls , include = None ) : <EOL> include = include if include is not None else [ ] <EOL> for fn , method in getmembers ( cls , predicate = ismethod ) : <EOL> if fn in include : <EOL> spec = getargspec ( method ) <EOL> params = spec . args [ <NUM_LIT:1> : ] <EOL> d = len ( spec . defaults ) if spec . defaults is not None else <NUM_LIT:0> <EOL> r = len ( params ) - d <EOL> yield ( fn , { "<STR_LIT>" : params [ : r ] , <EOL> "<STR_LIT>" : params [ r : ] } ) <EOL> class GirderClientModule ( GirderClient ) : <EOL> _include_methods = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:user>' , '<STR_LIT>' ] <EOL> _debug = True <EOL> def exit ( self ) : <EOL> if not self . _debug : <EOL> del self . message [ '<STR_LIT>' ] <EOL> self . module . exit_json ( changed = self . changed , ** self . message ) <EOL> def fail ( self , msg ) : <EOL> self . module . fail_json ( msg = msg ) <EOL> def __init__ ( self ) : <EOL> self . changed = False <EOL> self . message = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : { } } <EOL> self . spec = dict ( class_spec ( self . __class__ , <EOL> GirderClientModule . _include_methods ) ) <EOL> self . required_one_of = self . spec . keys ( ) <EOL> def __call__ ( self , module ) : <EOL> self . module = module <EOL> super ( GirderClientModule , self ) . __init__ ( <EOL> ** { p : self . module . params [ p ] for p in <EOL> [ '<STR_LIT:host>' , '<STR_LIT:port>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if module . params [ p ] is not None } ) <EOL> if self . module . params [ '<STR_LIT:username>' ] is not None : <EOL> try : <EOL> self . authenticate ( <EOL> username = self . module . params [ '<STR_LIT:username>' ] , <EOL> password = self . module . params [ '<STR_LIT:password>' ] ) <EOL> except AuthenticationError : <EOL> self . fail ( "<STR_LIT>" ) <EOL> elif self . module . params [ '<STR_LIT>' ] is not None : <EOL> self . token = self . module . params [ '<STR_LIT>' ] <EOL> elif self . module . params [ '<STR_LIT:user>' ] is None : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . message [ '<STR_LIT>' ] = self . token <EOL> for method in self . required_one_of : <EOL> if self . module . params [ method ] is not None : <EOL> self . __process ( method ) <EOL> self . exit ( ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> def __process ( self , method ) : <EOL> params = self . module . params [ method ] <EOL> args = [ ] <EOL> kwargs = { } <EOL> if type ( params ) is dict : <EOL> for arg_name in self . spec [ method ] [ '<STR_LIT>' ] : <EOL> if arg_name not in params . keys ( ) : <EOL> self . fail ( "<STR_LIT>" . format ( arg_name , method ) ) <EOL> args . append ( params [ arg_name ] ) <EOL> for kwarg_name in self . spec [ method ] [ '<STR_LIT>' ] : <EOL> if kwarg_name in params . keys ( ) : <EOL> kwargs [ kwarg_name ] = params [ kwarg_name ] <EOL> elif type ( params ) is list : <EOL> args = params <EOL> else : <EOL> args = [ params ] <EOL> ret = getattr ( self , method ) ( * args , ** kwargs ) <EOL> self . message [ '<STR_LIT>' ] [ '<STR_LIT>' ] = method <EOL> self . message [ '<STR_LIT>' ] [ '<STR_LIT:args>' ] = args <EOL> self . message [ '<STR_LIT>' ] [ '<STR_LIT>' ] = kwargs <EOL> self . message [ '<STR_LIT>' ] [ '<STR_LIT>' ] = params <EOL> self . message [ '<STR_LIT>' ] = ret <EOL> def plugins ( self , * plugins ) : <EOL> import json <EOL> ret = [ ] <EOL> available_plugins = self . get ( "<STR_LIT>" ) <EOL> self . message [ '<STR_LIT>' ] [ '<STR_LIT>' ] = available_plugins <EOL> plugins = set ( plugins ) <EOL> enabled_plugins = set ( available_plugins [ '<STR_LIT>' ] ) <EOL> if "<STR_LIT:*>" in plugins : <EOL> plugins = set ( available_plugins [ '<STR_LIT:all>' ] . keys ( ) ) <EOL> if not plugins <= set ( available_plugins [ "<STR_LIT:all>" ] . keys ( ) ) : <EOL> self . fail ( "<STR_LIT>" . format ( <EOL> "<STR_LIT:U+002C>" . join ( list ( plugins - set ( available_plugins [ '<STR_LIT:all>' ] . keys ( ) ) ) ) <EOL> ) ) <EOL> if self . module . params [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> if not plugins <= enabled_plugins : <EOL> ret = self . put ( "<STR_LIT>" , <EOL> { "<STR_LIT>" : <EOL> json . dumps ( list ( plugins | enabled_plugins ) ) } ) <EOL> self . changed = True <EOL> elif self . module . params [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> if len ( enabled_plugins & plugins ) : <EOL> ret = self . put ( "<STR_LIT>" , <EOL> { "<STR_LIT>" : <EOL> json . dumps ( list ( enabled_plugins - plugins ) ) } ) <EOL> self . changed = True <EOL> return ret <EOL> def user ( self , login , password , firstName = None , <EOL> lastName = None , email = None , admin = False ) : <EOL> if self . module . params [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> for var_name , var in [ ( '<STR_LIT>' , firstName ) , <EOL> ( '<STR_LIT>' , lastName ) , ( '<STR_LIT:email>' , email ) ] : <EOL> if var is None : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( var_name ) ) <EOL> try : <EOL> ret = self . authenticate ( username = login , <EOL> password = password ) <EOL> me = self . get ( "<STR_LIT>" ) <EOL> updateable = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:email>' , '<STR_LIT>' ] <EOL> passed_in = [ firstName , lastName , email , admin ] <EOL> if set ( [ ( k , v ) for k , v in me . items ( ) if k in updateable ] ) ^ set ( zip ( updateable , passed_in ) ) : <EOL> self . put ( "<STR_LIT>" . format ( me [ '<STR_LIT>' ] ) , <EOL> parameters = { <EOL> "<STR_LIT>" : login , <EOL> "<STR_LIT>" : firstName , <EOL> "<STR_LIT>" : lastName , <EOL> "<STR_LIT:password>" : password , <EOL> "<STR_LIT:email>" : email , <EOL> "<STR_LIT>" : "<STR_LIT:true>" if admin else "<STR_LIT:false>" } ) <EOL> self . changed = True <EOL> except AuthenticationError : <EOL> ret = self . post ( "<STR_LIT:user>" , parameters = { <EOL> "<STR_LIT>" : login , <EOL> "<STR_LIT>" : firstName , <EOL> "<STR_LIT>" : lastName , <EOL> "<STR_LIT:password>" : password , <EOL> "<STR_LIT:email>" : email , <EOL> "<STR_LIT>" : "<STR_LIT:true>" if admin else "<STR_LIT:false>" <EOL> } ) <EOL> self . changed = True <EOL> elif self . module . params [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> ret = [ ] <EOL> try : <EOL> ret = self . authenticate ( username = login , <EOL> password = password ) <EOL> me = self . get ( "<STR_LIT>" ) <EOL> self . delete ( '<STR_LIT>' . format ( me [ '<STR_LIT>' ] ) ) <EOL> self . changed = True <EOL> except AuthenticationError : <EOL> ret = [ ] <EOL> return ret <EOL> assetstore_types = { <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:2> , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> def __validate_hdfs_assetstore ( self , * args , ** kwargs ) : <EOL> pass <EOL> def assetstore ( self , name , type , root = None , db = None , mongohost = None , <EOL> replicaset = '<STR_LIT>' , bucket = None , prefix = '<STR_LIT>' , accessKeyId = None , <EOL> secret = None , service = '<STR_LIT>' , host = None , <EOL> port = None , path = None , user = None , webHdfsPort = None , <EOL> readOnly = False , current = False ) : <EOL> if type not in self . assetstore_types . keys ( ) : <EOL> self . fail ( "<STR_LIT>" . format ( type ) ) <EOL> argument_hash = { <EOL> "<STR_LIT>" : { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:type>' : self . assetstore_types [ type ] , <EOL> '<STR_LIT:root>' : root } , <EOL> "<STR_LIT>" : { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:type>' : self . assetstore_types [ type ] , <EOL> '<STR_LIT>' : db , <EOL> '<STR_LIT>' : mongohost , <EOL> '<STR_LIT>' : replicaset } , <EOL> "<STR_LIT>" : { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:type>' : self . assetstore_types [ type ] , <EOL> '<STR_LIT>' : bucket , <EOL> '<STR_LIT>' : prefix , <EOL> '<STR_LIT>' : accessKeyId , <EOL> '<STR_LIT>' : secret , <EOL> '<STR_LIT>' : service } , <EOL> '<STR_LIT>' : { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:type>' : self . assetstore_types [ type ] , <EOL> '<STR_LIT:host>' : host , <EOL> '<STR_LIT:port>' : port , <EOL> '<STR_LIT:path>' : path , <EOL> '<STR_LIT:user>' : user , <EOL> '<STR_LIT>' : webHdfsPort } <EOL> } <EOL> for k , v in argument_hash [ type ] . items ( ) : <EOL> if v is None : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( type , k ) ) <EOL> argument_hash [ type ] [ '<STR_LIT>' ] = readOnly <EOL> argument_hash [ type ] [ '<STR_LIT>' ] = current <EOL> ret = [ ] <EOL> assetstores = { a [ '<STR_LIT:name>' ] : a for a in self . get ( "<STR_LIT>" ) } <EOL> self . message [ '<STR_LIT>' ] [ '<STR_LIT>' ] = assetstores <EOL> if self . module . params [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> if name in assetstores . keys ( ) : <EOL> id = assetstores [ name ] [ '<STR_LIT>' ] <EOL> updateable = [ "<STR_LIT:root>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT:host>" , "<STR_LIT:port>" , "<STR_LIT:path>" , "<STR_LIT:user>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> assetstore_items = set ( ( k , assetstores [ name ] [ k ] ) <EOL> for k in updateable <EOL> if k in assetstores [ name ] . keys ( ) ) <EOL> arg_hash_items = set ( ( k , argument_hash [ type ] [ k ] ) <EOL> for k in updateable <EOL> if k in argument_hash [ type ] . keys ( ) ) <EOL> if not arg_hash_items <= assetstore_items : <EOL> ret = self . put ( "<STR_LIT>" . format ( id ) , <EOL> parameters = argument_hash [ type ] ) <EOL> self . changed = True <EOL> else : <EOL> try : <EOL> getattr ( self , "<STR_LIT>" <EOL> . format ( type ) ) ( ** argument_hash ) <EOL> except AttributeError : <EOL> pass <EOL> ret = self . post ( "<STR_LIT>" , <EOL> parameters = argument_hash [ type ] ) <EOL> self . changed = True <EOL> elif self . module . params [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> if name in assetstores . keys ( ) : <EOL> id = assetstores [ name ] [ '<STR_LIT>' ] <EOL> ret = self . delete ( "<STR_LIT>" . format ( id ) , <EOL> parameters = argument_hash [ type ] ) <EOL> return ret <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> argument_spec = { <EOL> '<STR_LIT:host>' : dict ( ) , <EOL> '<STR_LIT:port>' : dict ( ) , <EOL> '<STR_LIT>' : dict ( ) , <EOL> '<STR_LIT>' : dict ( ) , <EOL> '<STR_LIT>' : dict ( ) , <EOL> '<STR_LIT>' : dict ( ) , <EOL> '<STR_LIT:username>' : dict ( ) , <EOL> '<STR_LIT:password>' : dict ( ) , <EOL> '<STR_LIT>' : dict ( ) , <EOL> '<STR_LIT:state>' : dict ( default = "<STR_LIT>" , choices = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> } <EOL> gcm = GirderClientModule ( ) <EOL> for method in gcm . required_one_of : <EOL> argument_spec [ method ] = dict ( ) <EOL> module = AnsibleModule ( <EOL> argument_spec = argument_spec , <EOL> required_one_of = [ gcm . required_one_of , <EOL> [ "<STR_LIT>" , "<STR_LIT:username>" , "<STR_LIT:user>" ] ] , <EOL> required_together = [ [ "<STR_LIT:username>" , "<STR_LIT:password>" ] ] , <EOL> mutually_exclusive = gcm . required_one_of , <EOL> supports_check_mode = False ) <EOL> if not HAS_GIRDER_CLIENT : <EOL> module . fail_json ( msg = "<STR_LIT>" ) <EOL> try : <EOL> gcm ( module ) <EOL> except HttpError as e : <EOL> import traceback <EOL> module . fail_json ( msg = "<STR_LIT>" . format ( e . __class__ , str ( e ) , <EOL> e . responseText , <EOL> traceback . format_exc ( ) ) ) <EOL> except Exception as e : <EOL> import traceback <EOL> module . fail_json ( msg = "<STR_LIT>" . format ( e . __class__ , <EOL> str ( e ) , <EOL> traceback . format_exc ( ) ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
