Fixes to httpapi for EAPI (#40675)
* Replace errant uses of str * Hook up become to eapi * Hook become up to nxapi, toopull/4420/head
parent
38c86b7eef
commit
0ad4b7b785
|
@ -100,7 +100,7 @@ def map_obj_to_commands(updates, module):
|
|||
state = module.params['state']
|
||||
|
||||
if state == 'absent' and have.get('text'):
|
||||
if isinstance(have['text'], str):
|
||||
if isinstance(have['text'], string_types):
|
||||
commands.append('no banner %s' % module.params['banner'])
|
||||
elif have['text'].get('loginBanner') or have['text'].get('motd'):
|
||||
commands.append({'cmd': 'no banner %s' % module.params['banner']})
|
||||
|
@ -147,7 +147,7 @@ def map_config_to_obj(module):
|
|||
def map_params_to_obj(module):
|
||||
text = module.params['text']
|
||||
if text:
|
||||
text = str(text).strip()
|
||||
text = to_text(text).strip()
|
||||
|
||||
return {
|
||||
'banner': module.params['banner'],
|
||||
|
|
|
@ -188,6 +188,8 @@ class Connection(ConnectionBase):
|
|||
|
||||
self._httpapi = httpapi_loader.get(network_os, self)
|
||||
if self._httpapi:
|
||||
if hasattr(self._httpapi, 'set_become'):
|
||||
self._httpapi.set_become(play_context)
|
||||
display.vvvv('loaded API plugin for network_os %s' % network_os, host=self._play_context.remote_addr)
|
||||
else:
|
||||
raise AnsibleConnectionFailure('unable to load API plugin for network_os %s' % network_os)
|
||||
|
@ -229,13 +231,8 @@ class Connection(ConnectionBase):
|
|||
play_context.deserialize(pc_data)
|
||||
|
||||
messages = ['updating play_context for connection']
|
||||
if self._play_context.become is False and play_context.become is True:
|
||||
self._enable = True
|
||||
messages.append('authorizing connection')
|
||||
|
||||
elif self._play_context.become is True and not play_context.become:
|
||||
self._enable = False
|
||||
messages.append('deauthorizing connection')
|
||||
if self._play_context.become ^ play_context.become:
|
||||
self._httpapi.set_become(play_context)
|
||||
|
||||
self._play_context = play_context
|
||||
return messages
|
||||
|
|
|
@ -21,11 +21,13 @@ except ImportError:
|
|||
class HttpApi:
|
||||
def __init__(self, connection):
|
||||
self.connection = connection
|
||||
self._become = False
|
||||
|
||||
def send_request(self, data, **message_kwargs):
|
||||
if 'become' in message_kwargs:
|
||||
data = to_list(data)
|
||||
if self._become:
|
||||
display.vvvv('firing event: on_become')
|
||||
# TODO ??? self._terminal.on_become(passwd=auth_pass)
|
||||
data.insert(0, {"cmd": "enable", "input": self._become_pass})
|
||||
|
||||
output = message_kwargs.get('output', 'text')
|
||||
request = request_builder(data, output)
|
||||
|
@ -33,12 +35,23 @@ class HttpApi:
|
|||
|
||||
response = self.connection.send('/command-api', request, headers=headers, method='POST')
|
||||
response = json.loads(to_text(response.read()))
|
||||
return handle_response(response)
|
||||
results = handle_response(response)
|
||||
|
||||
if self._become:
|
||||
results = results[1:]
|
||||
if len(results) == 1:
|
||||
results = results[0]
|
||||
|
||||
return results
|
||||
|
||||
def get_prompt(self):
|
||||
# Hack to keep @enable_mode working
|
||||
return '#'
|
||||
|
||||
def set_become(self, play_context):
|
||||
self._become = play_context.become
|
||||
self._become_pass = getattr(play_context, 'become_pass') or ''
|
||||
|
||||
# Imported from module_utils
|
||||
def edit_config(self, config, commit=False, replace=False):
|
||||
"""Loads the configuration onto the remote devices
|
||||
|
@ -148,11 +161,9 @@ def handle_response(response):
|
|||
else:
|
||||
results.append(json.dumps(result))
|
||||
|
||||
if len(results) == 1:
|
||||
return results[0]
|
||||
return results
|
||||
|
||||
|
||||
def request_builder(commands, output, reqid=None):
|
||||
params = dict(version=1, cmds=to_list(commands), format=output)
|
||||
params = dict(version=1, cmds=commands, format=output)
|
||||
return json.dumps(dict(jsonrpc='2.0', id=reqid, method='runCmds', params=params))
|
||||
|
|
|
@ -22,12 +22,19 @@ class HttpApi:
|
|||
self.connection = connection
|
||||
|
||||
def _run_queue(self, queue, output):
|
||||
if self._become:
|
||||
display.vvvv('firing event: on_become')
|
||||
queue.insert(0, 'enable')
|
||||
request = request_builder(queue, output)
|
||||
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
|
||||
response = self.connection.send('/ins', request, headers=headers, method='POST')
|
||||
response = json.loads(to_text(response.read()))
|
||||
return handle_response(response)
|
||||
results = handle_response(response)
|
||||
|
||||
if self._become:
|
||||
results = results[1:]
|
||||
return results
|
||||
|
||||
def send_request(self, data, **message_kwargs):
|
||||
output = None
|
||||
|
@ -62,6 +69,10 @@ class HttpApi:
|
|||
return responses[0]
|
||||
return responses
|
||||
|
||||
def set_become(self, play_context):
|
||||
self._become = play_context.become
|
||||
self._become_pass = getattr(play_context, 'become_pass') or ''
|
||||
|
||||
# Migrated from module_utils
|
||||
def edit_config(self, command):
|
||||
responses = self.send_request(command, output='config')
|
||||
|
|
Loading…
Reference in New Issue