Skip to content

Commit

Permalink
move callback to fake requests
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeni committed Apr 14, 2023
1 parent cf8ca2b commit 1041da3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
14 changes: 4 additions & 10 deletions plugins/callback/foreman.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
- This callback will report facts and task events to Foreman
requirements:
- whitelisting in configuration
- requests (python library)
options:
report_type:
description:
Expand Down Expand Up @@ -112,10 +111,9 @@
import time

try:
import requests
HAS_REQUESTS = True
from ansible_collections.theforeman.foreman.plugins.module_utils.ansible_requests import RequestSession
except ImportError:
HAS_REQUESTS = False
from plugins.module_utils.ansible_requests import RequestSession

from ansible.module_utils._text import to_text
from ansible.module_utils.parsing.convert_bool import boolean as to_bool
Expand Down Expand Up @@ -198,10 +196,7 @@ def set_options(self, task_keys=None, var_options=None, direct=None):
ssl_key = self.get_option('client_key')
self.dir_store = self.get_option('dir_store')

if not HAS_REQUESTS:
self._disable_plugin(u'The `requests` python module is not installed')

self.session = requests.Session()
self.session = RequestSession()
if self.foreman_url.startswith('https://'):
if not os.path.exists(ssl_cert):
self._disable_plugin(u'FOREMAN_SSL_CERT %s not found.' % ssl_cert)
Expand All @@ -226,7 +221,6 @@ def _ssl_verify(self, option):
verify = option

if verify is False: # is only set to bool if try block succeeds
requests.packages.urllib3.disable_warnings()
self._display.warning(
u"SSL verification of %s disabled" % self.foreman_url,
)
Expand All @@ -252,7 +246,7 @@ def _send_data(self, data_type, report_type, host, data):
try:
response = self.session.post(url=url, json=data)
response.raise_for_status()
except requests.exceptions.RequestException as err:
except Exception as err:
self._display.warning(u'Sending data to Foreman at {url} failed for {host}: {err}'.format(
host=to_text(host), err=to_text(err), url=to_text(self.foreman_url)))

Expand Down
12 changes: 12 additions & 0 deletions plugins/module_utils/ansible_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ def verify(self):
def verify(self, value):
self.validate_certs = value

@property
def cert(self):
return self._ssl_cert, self._ssl_key

@cert.setter
def cert(self, value):
self._ssl_cert, self._ssl_key = value
raise NotImplementedError

def request(self, method, url, **kwargs):
validate_certs = kwargs.pop('verify', None)
params = kwargs.pop('params', None)
Expand All @@ -105,3 +114,6 @@ def request(self, method, url, **kwargs):
headers['Content-Type'] = 'application/json'
result = self.open(method, url, validate_certs=validate_certs, data=data, headers=headers, **kwargs)
return RequestResponse(result)

def post(self, url, data=None, json=None, **kwargs):
return self.request('POST', url, data=data, json=json, **kwargs)

0 comments on commit 1041da3

Please sign in to comment.