mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-25 11:51:26 -07:00
New base class for HttpApi plugins (#41915)
This commit is contained in:
parent
a8d4bf8642
commit
97ffb4c4d2
4 changed files with 83 additions and 50 deletions
|
@ -8,8 +8,9 @@ import json
|
|||
import time
|
||||
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.network.common.utils import to_list
|
||||
from ansible.module_utils.connection import ConnectionError
|
||||
from ansible.module_utils.network.common.utils import to_list
|
||||
from ansible.plugins.httpapi import HttpApiBase
|
||||
|
||||
try:
|
||||
from __main__ import display
|
||||
|
@ -18,11 +19,7 @@ except ImportError:
|
|||
display = Display()
|
||||
|
||||
|
||||
class HttpApi:
|
||||
def __init__(self, connection):
|
||||
self.connection = connection
|
||||
self._become = False
|
||||
|
||||
class HttpApi(HttpApiBase):
|
||||
def send_request(self, data, **message_kwargs):
|
||||
data = to_list(data)
|
||||
if self._become:
|
||||
|
@ -39,6 +36,7 @@ class HttpApi:
|
|||
response = json.loads(response_text)
|
||||
except ValueError:
|
||||
raise ConnectionError('Response was not valid JSON, got {0}'.format(response_text))
|
||||
|
||||
results = handle_response(response)
|
||||
|
||||
if self._become:
|
||||
|
@ -55,10 +53,6 @@ class HttpApi:
|
|||
else:
|
||||
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
|
||||
|
@ -120,23 +114,24 @@ class HttpApi:
|
|||
return response
|
||||
|
||||
for item in to_list(commands):
|
||||
cmd_output = None
|
||||
cmd_output = 'text'
|
||||
if isinstance(item, dict):
|
||||
command = item['command']
|
||||
if command.endswith('| json'):
|
||||
command = command.replace('| json', '')
|
||||
cmd_output = 'json'
|
||||
elif 'output' in item:
|
||||
if 'output' in item:
|
||||
cmd_output = item['output']
|
||||
else:
|
||||
command = item
|
||||
|
||||
# Emulate '| json' from CLI
|
||||
if command.endswith('| json'):
|
||||
command = command.rsplit('|', 1)[0]
|
||||
cmd_output = 'json'
|
||||
|
||||
if output and output != cmd_output:
|
||||
responses.extend(run_queue(queue, output))
|
||||
queue = list()
|
||||
|
||||
output = cmd_output or 'json'
|
||||
output = cmd_output
|
||||
queue.append(command)
|
||||
|
||||
if queue:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue