<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import , print_function <EOL> import yaml <EOL> import logging <EOL> import salt . utils . http <EOL> import salt . ext . six as six <EOL> from salt . _compat import ElementTree as ET <EOL> log = logging . getLogger ( __name__ ) <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if _apikey ( ) : <EOL> return True <EOL> return ( False , '<STR_LIT>' ) <EOL> def _apikey ( ) : <EOL> '''<STR_LIT>''' <EOL> return __opts__ . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , None ) <EOL> def list_employees ( order_by = '<STR_LIT:id>' ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> status , result = _query ( action = '<STR_LIT>' , command = '<STR_LIT>' ) <EOL> root = ET . fromstring ( result ) <EOL> directory = root . getchildren ( ) <EOL> for cat in directory : <EOL> if cat . tag != '<STR_LIT>' : <EOL> continue <EOL> for item in cat : <EOL> emp_id = item . items ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> emp_ret = { '<STR_LIT:id>' : emp_id } <EOL> for details in item . getchildren ( ) : <EOL> emp_ret [ details . items ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ] = details . text <EOL> ret [ emp_ret [ order_by ] ] = emp_ret <EOL> return ret <EOL> def show_employee ( emp_id , fields = None ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> if fields is None : <EOL> fields = '<STR_LIT:U+002C>' . join ( ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:id>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:location>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) ) <EOL> status , result = _query ( <EOL> action = '<STR_LIT>' , <EOL> command = emp_id , <EOL> args = { '<STR_LIT>' : fields } <EOL> ) <EOL> root = ET . fromstring ( result ) <EOL> items = root . getchildren ( ) <EOL> ret = { '<STR_LIT:id>' : emp_id } <EOL> for item in items : <EOL> ret [ item . items ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ] = item . text <EOL> return ret <EOL> def update_employee ( emp_id , key = None , value = None , items = None ) : <EOL> '''<STR_LIT>''' <EOL> if items is None : <EOL> if key is None or value is None : <EOL> return { '<STR_LIT>' : '<STR_LIT>' } <EOL> items = { key : value } <EOL> elif isinstance ( items , six . string_types ) : <EOL> items = yaml . safe_load ( items ) <EOL> xml_items = '<STR_LIT>' <EOL> for pair in items . keys ( ) : <EOL> xml_items += '<STR_LIT>' . format ( pair , items [ pair ] ) <EOL> xml_items = '<STR_LIT>' . format ( xml_items ) <EOL> status , result = _query ( <EOL> action = '<STR_LIT>' , <EOL> command = emp_id , <EOL> data = xml_items , <EOL> method = '<STR_LIT:POST>' , <EOL> ) <EOL> return show_employee ( emp_id , '<STR_LIT:U+002C>' . join ( items . keys ( ) ) ) <EOL> def list_users ( order_by = '<STR_LIT:id>' ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> status , result = _query ( action = '<STR_LIT>' , command = '<STR_LIT>' ) <EOL> root = ET . fromstring ( result ) <EOL> users = root . getchildren ( ) <EOL> for user in users : <EOL> user_id = None <EOL> user_ret = { } <EOL> for item in user . items ( ) : <EOL> user_ret [ item [ <NUM_LIT:0> ] ] = item [ <NUM_LIT:1> ] <EOL> if item [ <NUM_LIT:0> ] == '<STR_LIT:id>' : <EOL> user_id = item [ <NUM_LIT:1> ] <EOL> for item in user . getchildren ( ) : <EOL> user_ret [ item . tag ] = item . text <EOL> ret [ user_ret [ order_by ] ] = user_ret <EOL> return ret <EOL> def list_meta_fields ( ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> status , result = _query ( action = '<STR_LIT>' , command = '<STR_LIT>' ) <EOL> root = ET . fromstring ( result ) <EOL> fields = root . getchildren ( ) <EOL> for field in fields : <EOL> field_id = None <EOL> field_ret = { '<STR_LIT:name>' : field . text } <EOL> for item in field . items ( ) : <EOL> field_ret [ item [ <NUM_LIT:0> ] ] = item [ <NUM_LIT:1> ] <EOL> if item [ <NUM_LIT:0> ] == '<STR_LIT:id>' : <EOL> field_id = item [ <NUM_LIT:1> ] <EOL> ret [ field_id ] = field_ret <EOL> return ret <EOL> def _query ( action = None , <EOL> command = None , <EOL> args = None , <EOL> method = '<STR_LIT:GET>' , <EOL> data = None ) : <EOL> '''<STR_LIT>''' <EOL> subdomain = __opts__ . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , None ) <EOL> path = '<STR_LIT>' . format ( <EOL> subdomain <EOL> ) <EOL> if action : <EOL> path += action <EOL> if command : <EOL> path += '<STR_LIT>' . format ( command ) <EOL> log . debug ( '<STR_LIT>' . format ( path ) ) <EOL> if not isinstance ( args , dict ) : <EOL> args = { } <EOL> return_content = None <EOL> result = salt . utils . http . query ( <EOL> path , <EOL> method , <EOL> username = _apikey ( ) , <EOL> password = '<STR_LIT>' , <EOL> params = args , <EOL> data = data , <EOL> decode = False , <EOL> text = True , <EOL> status = True , <EOL> opts = __opts__ , <EOL> ) <EOL> log . debug ( <EOL> '<STR_LIT>' . format ( <EOL> result [ '<STR_LIT:status>' ] <EOL> ) <EOL> ) <EOL> return [ result [ '<STR_LIT:status>' ] , result [ '<STR_LIT:text>' ] ] </s>
