New base class for HttpApi plugins (#41915)

This commit is contained in:
Nathaniel Case 2018-06-25 15:48:12 -04:00 committed by GitHub
parent a8d4bf8642
commit 97ffb4c4d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 50 deletions

View file

@ -0,0 +1,34 @@
# (c) 2018 Red Hat Inc.
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from abc import abstractmethod
from ansible.plugins import AnsiblePlugin
class HttpApiBase(AnsiblePlugin):
def __init__(self, connection):
self.connection = connection
self._become = False
self._become_pass = ''
def set_become(self, become_context):
self._become = become_context.become
self._become_pass = getattr(become_context, 'become_pass') or ''
def login(self, username, password):
"""Call a defined login endpoint to receive an authentication token.
This should only be implemented if the API has a single endpoint which
can turn HTTP basic auth into a token which can be reused for the rest
of the calls for the session.
"""
pass
@abstractmethod
def send_request(self, data, **message_kwargs):
"""Prepares and sends request(s) to device."""
pass

View file

@ -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:

View file

@ -9,6 +9,7 @@ import json
from ansible.module_utils._text import to_text
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
@ -17,14 +18,12 @@ except ImportError:
display = Display()
class HttpApi:
def __init__(self, connection):
self.connection = connection
class HttpApi(HttpApiBase):
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'}
@ -74,10 +73,6 @@ 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):
resp = list()