<s> DOCUMENTATION = """<STR_LIT>""" <EOL> EXAMPLES = """<STR_LIT>""" <EOL> import syslog <EOL> import collections <EOL> from ansible . module_utils . basic import * <EOL> try : <EOL> import pyeapi <EOL> PYEAPI_AVAILABLE = True <EOL> except ImportError : <EOL> PYEAPI_AVAILABLE = False <EOL> DEFAULT_SYSLOG_PRIORITY = syslog . LOG_NOTICE <EOL> DEFAULT_CONNECTION = '<STR_LIT:localhost>' <EOL> TRANSPORTS = [ '<STR_LIT>' , '<STR_LIT:http>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class EosConnection ( object ) : <EOL> __attributes__ = [ '<STR_LIT:username>' , '<STR_LIT:password>' , '<STR_LIT:host>' , '<STR_LIT>' , '<STR_LIT:port>' ] <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . connection = kwargs [ '<STR_LIT>' ] <EOL> self . transport = kwargs . get ( '<STR_LIT>' ) <EOL> self . username = kwargs . get ( '<STR_LIT:username>' ) <EOL> self . password = kwargs . get ( '<STR_LIT:password>' ) <EOL> self . host = kwargs . get ( '<STR_LIT:host>' ) <EOL> self . port = kwargs . get ( '<STR_LIT:port>' ) <EOL> self . config = kwargs . get ( '<STR_LIT>' ) <EOL> def connect ( self ) : <EOL> if self . config is not None : <EOL> pyeapi . load_config ( self . config ) <EOL> config = dict ( ) <EOL> if self . connection is not None : <EOL> config = pyeapi . config_for ( self . connection ) <EOL> if not config : <EOL> msg = '<STR_LIT>' . format ( self . connection ) <EOL> for key in self . __attributes__ : <EOL> if getattr ( self , key ) is not None : <EOL> config [ key ] = getattr ( self , key ) <EOL> if '<STR_LIT>' not in config : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> connection = pyeapi . client . make_connection ( ** config ) <EOL> node = pyeapi . client . Node ( connection , ** config ) <EOL> try : <EOL> node . enable ( '<STR_LIT>' ) <EOL> except ( pyeapi . eapilib . ConnectionError , pyeapi . eapilib . CommandError ) : <EOL> raise ValueError ( '<STR_LIT>' . format ( node ) ) <EOL> return node <EOL> class EosAnsibleModule ( AnsibleModule ) : <EOL> meta_args = { <EOL> '<STR_LIT>' : dict ( ) , <EOL> '<STR_LIT:username>' : dict ( ) , <EOL> '<STR_LIT:password>' : dict ( ) , <EOL> '<STR_LIT:host>' : dict ( ) , <EOL> '<STR_LIT>' : dict ( default = DEFAULT_CONNECTION ) , <EOL> '<STR_LIT>' : dict ( choices = TRANSPORTS ) , <EOL> '<STR_LIT:port>' : dict ( ) , <EOL> '<STR_LIT>' : dict ( type = '<STR_LIT:bool>' , default = '<STR_LIT:false>' ) , <EOL> '<STR_LIT>' : dict ( type = '<STR_LIT:bool>' , default = '<STR_LIT:true>' ) <EOL> } <EOL> stateful_args = { <EOL> '<STR_LIT:state>' : dict ( default = '<STR_LIT>' , choices = [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> } <EOL> def __init__ ( self , stateful = True , autorefresh = False , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] . update ( self . meta_args ) <EOL> self . _stateful = stateful <EOL> if stateful : <EOL> kwargs [ '<STR_LIT>' ] . update ( self . stateful_args ) <EOL> self . _logging = kwargs . get ( '<STR_LIT>' ) <EOL> super ( EosAnsibleModule , self ) . __init__ ( * args , ** kwargs ) <EOL> self . result = dict ( changed = False , changes = dict ( ) ) <EOL> self . _debug = kwargs . get ( '<STR_LIT>' ) or self . boolean ( self . params [ '<STR_LIT>' ] ) <EOL> self . _logging = kwargs . get ( '<STR_LIT>' ) or self . params [ '<STR_LIT>' ] <EOL> self . log ( '<STR_LIT>' % self . _debug ) <EOL> self . debug ( '<STR_LIT>' , self . check_pyeapi ( ) ) <EOL> self . debug ( '<STR_LIT>' , self . _stateful ) <EOL> self . debug ( '<STR_LIT>' , self . params ) <EOL> self . _attributes = self . map_argument_spec ( ) <EOL> self . validate ( ) <EOL> self . _autorefresh = autorefresh <EOL> self . _node = EosConnection ( ** self . params ) <EOL> self . _node . connect ( ) <EOL> self . _node = self . connect ( ) <EOL> self . _instance = None <EOL> self . desired_state = self . params [ '<STR_LIT:state>' ] if self . _stateful else None <EOL> self . exit_after_flush = kwargs . get ( '<STR_LIT>' ) <EOL> @ property <EOL> def instance ( self ) : <EOL> if self . _instance : <EOL> return self . _instance <EOL> func = self . func ( '<STR_LIT>' ) <EOL> if not func : <EOL> self . fail ( '<STR_LIT>' ) <EOL> try : <EOL> self . _instance = func ( self ) <EOL> except Exception as exc : <EOL> self . fail ( '<STR_LIT>' % exc . message ) <EOL> self . log ( "<STR_LIT>" % self . _instance ) <EOL> return self . _instance <EOL> @ property <EOL> def attributes ( self ) : <EOL> return self . _attributes <EOL> @ property <EOL> def node ( self ) : <EOL> return self . _node <EOL> def check_pyeapi ( self ) : <EOL> if not PYEAPI_AVAILABLE : <EOL> self . fail ( '<STR_LIT>' ) <EOL> return pyeapi . __version__ <EOL> def map_argument_spec ( self ) : <EOL> """<STR_LIT>""" <EOL> keys = set ( self . params ) . difference ( self . meta_args ) <EOL> attrs = dict ( ) <EOL> attrs = dict ( [ ( k , self . params [ k ] ) for k in self . params if k in keys ] ) <EOL> if '<STR_LIT>' in attrs : <EOL> del attrs [ '<STR_LIT>' ] <EOL> return attrs <EOL> def validate ( self ) : <EOL> for key , value in self . attributes . iteritems ( ) : <EOL> func = self . func ( '<STR_LIT>' % key ) <EOL> if func : <EOL> self . attributes [ key ] = func ( value ) <EOL> def create ( self ) : <EOL> if not self . check_mode : <EOL> func = self . func ( '<STR_LIT>' ) <EOL> if not func : <EOL> self . fail ( '<STR_LIT>' ) <EOL> return self . invoke ( func , self ) <EOL> def remove ( self ) : <EOL> if not self . check_mode : <EOL> func = self . func ( '<STR_LIT>' ) <EOL> if not func : <EOL> self . fail ( '<STR_LIT>' ) <EOL> return self . invoke ( func , self ) <EOL> def flush ( self , exit_after_flush = False ) : <EOL> self . exit_after_flush = exit_after_flush <EOL> if self . desired_state == '<STR_LIT>' or not self . _stateful : <EOL> if self . instance . get ( '<STR_LIT:state>' ) == '<STR_LIT>' : <EOL> changed = self . create ( ) <EOL> self . result [ '<STR_LIT>' ] = changed or True <EOL> self . refresh ( ) <EOL> self . _node . _running_config = None <EOL> changeset = self . attributes . viewitems ( ) - self . instance . viewitems ( ) <EOL> if self . _debug : <EOL> self . debug ( '<STR_LIT>' , self . attributes ) <EOL> self . debug ( '<STR_LIT>' , self . instance ) <EOL> changes = self . update ( changeset ) <EOL> if changes : <EOL> self . result [ '<STR_LIT>' ] = changes <EOL> self . result [ '<STR_LIT>' ] = True <EOL> self . _attributes . update ( changes ) <EOL> flush = self . func ( '<STR_LIT>' ) <EOL> if flush : <EOL> self . invoke ( flush , self ) <EOL> elif self . desired_state == '<STR_LIT>' and self . _stateful : <EOL> if self . instance . get ( '<STR_LIT:state>' ) == '<STR_LIT>' : <EOL> changed = self . remove ( ) <EOL> self . result [ '<STR_LIT>' ] = changed or True <EOL> elif self . _stateful : <EOL> if self . desired_state != self . instance . get ( '<STR_LIT:state>' ) : <EOL> func = self . func ( self . desired_state ) <EOL> changed = self . invoke ( func , self ) <EOL> self . result [ '<STR_LIT>' ] = changed or True <EOL> self . refresh ( ) <EOL> if self . _debug : <EOL> self . result [ '<STR_LIT>' ] = self . instance <EOL> if self . exit_after_flush : <EOL> self . exit ( ) <EOL> def update ( self , changeset ) : <EOL> changes = dict ( ) <EOL> for key , value in changeset : <EOL> if value is not None : <EOL> changes [ key ] = value <EOL> func = self . func ( '<STR_LIT>' % key ) <EOL> if func and not self . check_mode : <EOL> try : <EOL> self . invoke ( func , self ) <EOL> except Exception as exc : <EOL> self . fail ( exc . message ) <EOL> return changes <EOL> def connect ( self ) : <EOL> if self . params [ '<STR_LIT>' ] : <EOL> pyeapi . load_config ( self . params [ '<STR_LIT>' ] ) <EOL> config = dict ( ) <EOL> if self . params [ '<STR_LIT>' ] : <EOL> config = pyeapi . config_for ( self . params [ '<STR_LIT>' ] ) <EOL> if not config : <EOL> msg = '<STR_LIT>' % self . params [ '<STR_LIT>' ] <EOL> self . fail ( msg ) <EOL> if self . params [ '<STR_LIT:username>' ] : <EOL> config [ '<STR_LIT:username>' ] = self . params [ '<STR_LIT:username>' ] <EOL> if self . params [ '<STR_LIT:password>' ] : <EOL> config [ '<STR_LIT:password>' ] = self . params [ '<STR_LIT:password>' ] <EOL> if self . params [ '<STR_LIT>' ] : <EOL> config [ '<STR_LIT>' ] = self . params [ '<STR_LIT>' ] <EOL> if self . params [ '<STR_LIT:port>' ] : <EOL> config [ '<STR_LIT:port>' ] = self . params [ '<STR_LIT:port>' ] <EOL> if self . params [ '<STR_LIT:host>' ] : <EOL> config [ '<STR_LIT:host>' ] = self . params [ '<STR_LIT:host>' ] <EOL> if '<STR_LIT>' not in config : <EOL> self . fail ( '<STR_LIT>' ) <EOL> connection = pyeapi . client . make_connection ( ** config ) <EOL> self . log ( '<STR_LIT>' % self . _autorefresh ) <EOL> node = pyeapi . client . Node ( connection , autorefresh = self . _autorefresh , <EOL> ** config ) <EOL> try : <EOL> resp = node . enable ( '<STR_LIT>' ) <EOL> self . debug ( '<STR_LIT>' , resp [ <NUM_LIT:0> ] [ '<STR_LIT:result>' ] [ '<STR_LIT:version>' ] ) <EOL> self . debug ( '<STR_LIT>' , resp [ <NUM_LIT:0> ] [ '<STR_LIT:result>' ] [ '<STR_LIT>' ] ) <EOL> except ( pyeapi . eapilib . ConnectionError , pyeapi . eapilib . CommandError ) : <EOL> self . fail ( '<STR_LIT>' % node ) <EOL> else : <EOL> self . log ( '<STR_LIT>' % node ) <EOL> self . debug ( '<STR_LIT>' , str ( node ) ) <EOL> return node <EOL> def config ( self , commands ) : <EOL> self . result [ '<STR_LIT>' ] = True <EOL> if not self . check_mode : <EOL> self . node . config ( commands ) <EOL> def api ( self , module ) : <EOL> return self . node . api ( module ) <EOL> def func ( self , name ) : <EOL> return globals ( ) . get ( name ) <EOL> def invoke ( self , func , * args , ** kwargs ) : <EOL> try : <EOL> return func ( * args , ** kwargs ) <EOL> except Exception as exc : <EOL> self . fail ( exc . message ) <EOL> def invoke_function ( self , name , * args , ** kwargs ) : <EOL> func = self . func ( name ) <EOL> if func : <EOL> return self . invoke ( func , * args , ** kwargs ) <EOL> def fail ( self , msg ) : <EOL> self . invoke_function ( '<STR_LIT>' , self ) <EOL> self . log ( '<STR_LIT>' % msg , syslog . LOG_ERR ) <EOL> self . fail_json ( msg = msg ) <EOL> def exit ( self ) : <EOL> self . invoke_function ( '<STR_LIT>' , self ) <EOL> self . log ( '<STR_LIT>' ) <EOL> self . exit_json ( ** self . result ) <EOL> def refresh ( self ) : <EOL> self . _instance = None <EOL> def debug ( self , key , value ) : <EOL> if self . _debug : <EOL> if '<STR_LIT>' not in self . result : <EOL> self . result [ '<STR_LIT>' ] = dict ( ) <EOL> self . result [ '<STR_LIT>' ] [ key ] = value <EOL> def log ( self , message , log_args = None , priority = None ) : <EOL> if self . _logging : <EOL> syslog . openlog ( '<STR_LIT>' ) <EOL> priority = priority or DEFAULT_SYSLOG_PRIORITY <EOL> syslog . syslog ( priority , str ( message ) ) <EOL> @ classmethod <EOL> def add_state ( cls , name ) : <EOL> cls . stateful_args [ '<STR_LIT:state>' ] [ '<STR_LIT>' ] . append ( name ) <EOL> def instance ( module ) : <EOL> """<STR_LIT>""" <EOL> name = module . attributes [ '<STR_LIT:name>' ] <EOL> result = module . node . api ( '<STR_LIT>' ) . get ( name ) <EOL> _instance = dict ( name = name , state = '<STR_LIT>' ) <EOL> if result : <EOL> _instance [ '<STR_LIT:state>' ] = '<STR_LIT>' <EOL> _instance . update ( result ) <EOL> desc = '<STR_LIT>' if not result [ '<STR_LIT:description>' ] else result [ '<STR_LIT:description>' ] <EOL> _instance [ '<STR_LIT:description>' ] = desc <EOL> _instance [ '<STR_LIT>' ] = not result [ '<STR_LIT>' ] <EOL> _instance [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( result [ '<STR_LIT>' ] ) <EOL> lacp_mode = result [ '<STR_LIT>' ] <EOL> _instance [ '<STR_LIT>' ] = '<STR_LIT>' if lacp_mode == '<STR_LIT>' else lacp_mode <EOL> return _instance <EOL> def create ( module ) : <EOL> """<STR_LIT>""" <EOL> name = module . attributes [ '<STR_LIT:name>' ] <EOL> module . log ( '<STR_LIT>' % name ) <EOL> module . node . api ( '<STR_LIT>' ) . create ( name ) <EOL> def remove ( module ) : <EOL> """<STR_LIT>""" <EOL> name = module . attributes [ '<STR_LIT:name>' ] <EOL> module . log ( '<STR_LIT>' % name ) <EOL> module . node . api ( '<STR_LIT>' ) . delete ( name ) <EOL> def set_description ( module ) : <EOL> """<STR_LIT>""" <EOL> value = module . attributes [ '<STR_LIT:description>' ] <EOL> name = module . attributes [ '<STR_LIT:name>' ] <EOL> value = None if value == '<STR_LIT>' else value <EOL> module . log ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( name , value ) ) <EOL> module . node . api ( '<STR_LIT>' ) . set_description ( name , value ) <EOL> def set_enable ( module ) : <EOL> """<STR_LIT>""" <EOL> value = module . attributes [ '<STR_LIT>' ] <EOL> name = module . attributes [ '<STR_LIT:name>' ] <EOL> module . log ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( name , value ) ) <EOL> module . node . api ( '<STR_LIT>' ) . set_shutdown ( name , disable = value ) <EOL> def set_members ( module ) : <EOL> """<STR_LIT>""" <EOL> value = module . attributes [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> mode = module . attributes [ '<STR_LIT>' ] <EOL> name = module . attributes [ '<STR_LIT:name>' ] <EOL> module . log ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( name , value , mode ) ) <EOL> module . node . api ( '<STR_LIT>' ) . set_members ( name , value , mode = mode ) <EOL> def set_minimum_links ( module ) : <EOL> """<STR_LIT>""" <EOL> value = module . attributes [ '<STR_LIT>' ] <EOL> name = module . attributes [ '<STR_LIT:name>' ] <EOL> module . log ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( name , value ) ) <EOL> module . node . api ( '<STR_LIT>' ) . set_minimum_links ( name , value ) <EOL> def set_lacp_mode ( module ) : <EOL> """<STR_LIT>""" <EOL> value = module . attributes [ '<STR_LIT>' ] <EOL> value = '<STR_LIT>' if value == '<STR_LIT>' else value <EOL> name = module . attributes [ '<STR_LIT:name>' ] <EOL> module . log ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( name , value ) ) <EOL> module . node . api ( '<STR_LIT>' ) . set_lacp_mode ( name , value ) <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> argument_spec = dict ( <EOL> name = dict ( required = True ) , <EOL> enable = dict ( type = '<STR_LIT:bool>' , default = True ) , <EOL> description = dict ( ) , <EOL> members = dict ( ) , <EOL> minimum_links = dict ( type = '<STR_LIT:int>' ) , <EOL> lacp_mode = dict ( choices = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> ) <EOL> module = EosAnsibleModule ( argument_spec = argument_spec , <EOL> supports_check_mode = True , <EOL> autorefresh = True ) <EOL> module . flush ( True ) <EOL> main ( ) </s>
