mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-29 12:29:10 -07:00
Fixes to httpapi for EAPI (#40675)
* Replace errant uses of str * Hook up become to eapi * Hook become up to nxapi, too
This commit is contained in:
parent
38c86b7eef
commit
0ad4b7b785
4 changed files with 36 additions and 17 deletions
|
@ -100,7 +100,7 @@ def map_obj_to_commands(updates, module):
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
|
|
||||||
if state == 'absent' and have.get('text'):
|
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'])
|
commands.append('no banner %s' % module.params['banner'])
|
||||||
elif have['text'].get('loginBanner') or have['text'].get('motd'):
|
elif have['text'].get('loginBanner') or have['text'].get('motd'):
|
||||||
commands.append({'cmd': 'no banner %s' % module.params['banner']})
|
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):
|
def map_params_to_obj(module):
|
||||||
text = module.params['text']
|
text = module.params['text']
|
||||||
if text:
|
if text:
|
||||||
text = str(text).strip()
|
text = to_text(text).strip()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'banner': module.params['banner'],
|
'banner': module.params['banner'],
|
||||||
|
|
|
@ -188,6 +188,8 @@ class Connection(ConnectionBase):
|
||||||
|
|
||||||
self._httpapi = httpapi_loader.get(network_os, self)
|
self._httpapi = httpapi_loader.get(network_os, self)
|
||||||
if self._httpapi:
|
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)
|
display.vvvv('loaded API plugin for network_os %s' % network_os, host=self._play_context.remote_addr)
|
||||||
else:
|
else:
|
||||||
raise AnsibleConnectionFailure('unable to load API plugin for network_os %s' % network_os)
|
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)
|
play_context.deserialize(pc_data)
|
||||||
|
|
||||||
messages = ['updating play_context for connection']
|
messages = ['updating play_context for connection']
|
||||||
if self._play_context.become is False and play_context.become is True:
|
if self._play_context.become ^ play_context.become:
|
||||||
self._enable = True
|
self._httpapi.set_become(play_context)
|
||||||
messages.append('authorizing connection')
|
|
||||||
|
|
||||||
elif self._play_context.become is True and not play_context.become:
|
|
||||||
self._enable = False
|
|
||||||
messages.append('deauthorizing connection')
|
|
||||||
|
|
||||||
self._play_context = play_context
|
self._play_context = play_context
|
||||||
return messages
|
return messages
|
||||||
|
|
|
@ -21,11 +21,13 @@ except ImportError:
|
||||||
class HttpApi:
|
class HttpApi:
|
||||||
def __init__(self, connection):
|
def __init__(self, connection):
|
||||||
self.connection = connection
|
self.connection = connection
|
||||||
|
self._become = False
|
||||||
|
|
||||||
def send_request(self, data, **message_kwargs):
|
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')
|
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')
|
output = message_kwargs.get('output', 'text')
|
||||||
request = request_builder(data, output)
|
request = request_builder(data, output)
|
||||||
|
@ -33,12 +35,23 @@ class HttpApi:
|
||||||
|
|
||||||
response = self.connection.send('/command-api', request, headers=headers, method='POST')
|
response = self.connection.send('/command-api', request, headers=headers, method='POST')
|
||||||
response = json.loads(to_text(response.read()))
|
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):
|
def get_prompt(self):
|
||||||
# Hack to keep @enable_mode working
|
# Hack to keep @enable_mode working
|
||||||
return '#'
|
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
|
# Imported from module_utils
|
||||||
def edit_config(self, config, commit=False, replace=False):
|
def edit_config(self, config, commit=False, replace=False):
|
||||||
"""Loads the configuration onto the remote devices
|
"""Loads the configuration onto the remote devices
|
||||||
|
@ -148,11 +161,9 @@ def handle_response(response):
|
||||||
else:
|
else:
|
||||||
results.append(json.dumps(result))
|
results.append(json.dumps(result))
|
||||||
|
|
||||||
if len(results) == 1:
|
|
||||||
return results[0]
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
def request_builder(commands, output, reqid=None):
|
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))
|
return json.dumps(dict(jsonrpc='2.0', id=reqid, method='runCmds', params=params))
|
||||||
|
|
|
@ -22,12 +22,19 @@ class HttpApi:
|
||||||
self.connection = connection
|
self.connection = connection
|
||||||
|
|
||||||
def _run_queue(self, queue, output):
|
def _run_queue(self, queue, output):
|
||||||
|
if self._become:
|
||||||
|
display.vvvv('firing event: on_become')
|
||||||
|
queue.insert(0, 'enable')
|
||||||
request = request_builder(queue, output)
|
request = request_builder(queue, output)
|
||||||
|
|
||||||
headers = {'Content-Type': 'application/json'}
|
headers = {'Content-Type': 'application/json'}
|
||||||
|
|
||||||
response = self.connection.send('/ins', request, headers=headers, method='POST')
|
response = self.connection.send('/ins', request, headers=headers, method='POST')
|
||||||
response = json.loads(to_text(response.read()))
|
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):
|
def send_request(self, data, **message_kwargs):
|
||||||
output = None
|
output = None
|
||||||
|
@ -62,6 +69,10 @@ class HttpApi:
|
||||||
return responses[0]
|
return responses[0]
|
||||||
return responses
|
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
|
# Migrated from module_utils
|
||||||
def edit_config(self, command):
|
def edit_config(self, command):
|
||||||
responses = self.send_request(command, output='config')
|
responses = self.send_request(command, output='config')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue