Skip to content

Commit

Permalink
migrate inventory to fake requests
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeni committed Apr 14, 2023
1 parent 1041da3 commit 2d7e0cd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
18 changes: 5 additions & 13 deletions plugins/inventory/foreman.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,14 @@
from ansible_collections.theforeman.foreman.plugins.module_utils._version import LooseVersion
from time import sleep
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_bytes, to_native, to_text
from ansible.module_utils._text import to_native, to_text
from ansible.module_utils.common._collections_compat import MutableMapping
from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable, to_safe_group_name, Constructable

# 3rd party imports
try:
import requests
if LooseVersion(requests.__version__) < LooseVersion('1.1.0'):
raise ImportError
from requests.auth import HTTPBasicAuth
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


class InventoryModule(BaseInventoryPlugin, Cacheable, Constructable):
Expand All @@ -204,9 +199,6 @@ def __init__(self):
self.cache_key = None
self.use_cache = None

if not HAS_REQUESTS:
raise AnsibleError('This script requires python-requests 1.1 as a minimum version')

def verify_file(self, path):

valid = False
Expand All @@ -219,8 +211,8 @@ def verify_file(self, path):

def _get_session(self):
if not self.session:
self.session = requests.session()
self.session.auth = HTTPBasicAuth(self.get_option('user'), to_bytes(self.get_option('password')))
self.session = RequestSession()
self.session.auth = (self.get_option('user'), self.get_option('password'))
self.session.verify = self.get_option('validate_certs')
return self.session

Expand Down
3 changes: 3 additions & 0 deletions plugins/module_utils/ansible_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,8 @@ def request(self, method, url, **kwargs):
result = self.open(method, url, validate_certs=validate_certs, data=data, headers=headers, **kwargs)
return RequestResponse(result)

def get(self, url, **kwargs):
return self.request('GET', url, **kwargs)

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

0 comments on commit 2d7e0cd

Please sign in to comment.