Add encoding option to API modules (#95)

* Set encoding to UTF-8.

* Add encoding parameter to API modules.

* librouteros is now patched.
This commit is contained in:
Felix Fontein 2022-05-24 22:19:26 +02:00 committed by GitHub
parent 7452195071
commit 023f11f7e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 33 deletions

View file

@ -43,10 +43,11 @@ def api_argument_spec():
validate_certs=dict(type='bool', default=True),
validate_cert_hostname=dict(type='bool', default=False),
ca_path=dict(type='path'),
encoding=dict(type='str', default='ASCII')
)
def _ros_api_connect(module, username, password, host, port, use_tls, validate_certs, validate_cert_hostname, ca_path):
def _ros_api_connect(module, username, password, host, port, use_tls, validate_certs, validate_cert_hostname, ca_path, encoding):
'''Connect to RouterOS API.'''
if not port:
if use_tls:
@ -54,6 +55,13 @@ def _ros_api_connect(module, username, password, host, port, use_tls, validate_c
else:
port = 8728
try:
params = dict(
username=username,
password=password,
host=host,
port=port,
encoding=encoding,
)
if use_tls:
ctx = ssl.create_default_context(cafile=ca_path)
wrap_context = ctx.wrap_socket
@ -68,20 +76,8 @@ def _ros_api_connect(module, username, password, host, port, use_tls, validate_c
def wrap_context(*args, **kwargs):
kwargs.pop('server_hostname', None)
return ctx.wrap_socket(*args, server_hostname=host, **kwargs)
api = connect(
username=username,
password=password,
host=host,
ssl_wrapper=wrap_context,
port=port,
)
else:
api = connect(
username=username,
password=password,
host=host,
port=port,
)
params['ssl_wrapper'] = wrap_context
api = connect(**params)
except Exception as e:
connection = {
'username': username,
@ -105,4 +101,5 @@ def create_api(module):
module.params['validate_certs'],
module.params['validate_cert_hostname'],
module.params['ca_path'],
module.params['encoding'],
)