mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Handle non-ascii characters in foreman.py (#46779)
* Handle non-ascii characters in foreman.py * Add test to validate non-ascii results
This commit is contained in:
parent
96db9a55e2
commit
1ded3f9890
2 changed files with 49 additions and 4 deletions
|
@ -46,6 +46,8 @@ if LooseVersion(requests.__version__) < LooseVersion('1.1.0'):
|
||||||
|
|
||||||
from requests.auth import HTTPBasicAuth
|
from requests.auth import HTTPBasicAuth
|
||||||
|
|
||||||
|
from ansible.module_utils._text import to_text
|
||||||
|
|
||||||
|
|
||||||
def json_format_dict(data, pretty=False):
|
def json_format_dict(data, pretty=False):
|
||||||
"""Converts a dict to a JSON object and dumps it as a formatted string"""
|
"""Converts a dict to a JSON object and dumps it as a formatted string"""
|
||||||
|
@ -285,20 +287,32 @@ class ForemanInventory(object):
|
||||||
group = 'hostgroup'
|
group = 'hostgroup'
|
||||||
val = host.get('%s_title' % group) or host.get('%s_name' % group)
|
val = host.get('%s_title' % group) or host.get('%s_name' % group)
|
||||||
if val:
|
if val:
|
||||||
safe_key = self.to_safe('%s%s_%s' % (self.group_prefix, group, val.lower()))
|
safe_key = self.to_safe('%s%s_%s' % (
|
||||||
|
to_text(self.group_prefix),
|
||||||
|
group,
|
||||||
|
to_text(val).lower()
|
||||||
|
))
|
||||||
self.inventory[safe_key].append(dns_name)
|
self.inventory[safe_key].append(dns_name)
|
||||||
|
|
||||||
# Create ansible groups for environment, location and organization
|
# Create ansible groups for environment, location and organization
|
||||||
for group in ['environment', 'location', 'organization']:
|
for group in ['environment', 'location', 'organization']:
|
||||||
val = host.get('%s_name' % group)
|
val = host.get('%s_name' % group)
|
||||||
if val:
|
if val:
|
||||||
safe_key = self.to_safe('%s%s_%s' % (self.group_prefix, group, val.lower()))
|
safe_key = self.to_safe('%s%s_%s' % (
|
||||||
|
to_text(self.group_prefix),
|
||||||
|
group,
|
||||||
|
to_text(val).lower()
|
||||||
|
))
|
||||||
self.inventory[safe_key].append(dns_name)
|
self.inventory[safe_key].append(dns_name)
|
||||||
|
|
||||||
for group in ['lifecycle_environment', 'content_view']:
|
for group in ['lifecycle_environment', 'content_view']:
|
||||||
val = host.get('content_facet_attributes', {}).get('%s_name' % group)
|
val = host.get('content_facet_attributes', {}).get('%s_name' % group)
|
||||||
if val:
|
if val:
|
||||||
safe_key = self.to_safe('%s%s_%s' % (self.group_prefix, group, val.lower()))
|
safe_key = self.to_safe('%s%s_%s' % (
|
||||||
|
to_text(self.group_prefix),
|
||||||
|
group,
|
||||||
|
to_text(val).lower()
|
||||||
|
))
|
||||||
self.inventory[safe_key].append(dns_name)
|
self.inventory[safe_key].append(dns_name)
|
||||||
|
|
||||||
params = self._resolve_params(host_params)
|
params = self._resolve_params(host_params)
|
||||||
|
@ -307,7 +321,7 @@ class ForemanInventory(object):
|
||||||
# attributes.
|
# attributes.
|
||||||
groupby = dict()
|
groupby = dict()
|
||||||
for k, v in params.items():
|
for k, v in params.items():
|
||||||
groupby[k] = self.to_safe(str(v))
|
groupby[k] = self.to_safe(to_text(v))
|
||||||
|
|
||||||
# The name of the ansible groups is given by group_patterns:
|
# The name of the ansible groups is given by group_patterns:
|
||||||
for pattern in self.group_patterns:
|
for pattern in self.group_patterns:
|
||||||
|
|
|
@ -6,6 +6,11 @@ export FOREMAN_HOST="${FOREMAN_HOST:-localhost}"
|
||||||
export FOREMAN_PORT="${FOREMAN_PORT:-8080}"
|
export FOREMAN_PORT="${FOREMAN_PORT:-8080}"
|
||||||
export FOREMAN_INI_PATH="${OUTPUT_DIR}/foreman.ini"
|
export FOREMAN_INI_PATH="${OUTPUT_DIR}/foreman.ini"
|
||||||
|
|
||||||
|
|
||||||
|
############################################
|
||||||
|
# SMOKETEST WITH SIMPLE INI
|
||||||
|
############################################
|
||||||
|
|
||||||
cat > "$FOREMAN_INI_PATH" <<FOREMAN_INI
|
cat > "$FOREMAN_INI_PATH" <<FOREMAN_INI
|
||||||
[foreman]
|
[foreman]
|
||||||
url = http://${FOREMAN_HOST}:${FOREMAN_PORT}
|
url = http://${FOREMAN_HOST}:${FOREMAN_PORT}
|
||||||
|
@ -16,3 +21,29 @@ FOREMAN_INI
|
||||||
|
|
||||||
# use ansible to validate the return data
|
# use ansible to validate the return data
|
||||||
ansible-playbook -i foreman.sh test_foreman_inventory.yml --connection=local
|
ansible-playbook -i foreman.sh test_foreman_inventory.yml --connection=local
|
||||||
|
RC=$?
|
||||||
|
if [[ $RC != 0 ]]; then
|
||||||
|
echo "foreman inventory script smoketest failed"
|
||||||
|
exit $RC
|
||||||
|
fi
|
||||||
|
|
||||||
|
############################################
|
||||||
|
# SMOKETEST WITH NON-ASCII INI
|
||||||
|
############################################
|
||||||
|
|
||||||
|
cat > "$FOREMAN_INI_PATH" <<FOREMAN_INI
|
||||||
|
[foreman]
|
||||||
|
url = http://${FOREMAN_HOST}:${FOREMAN_PORT}
|
||||||
|
user = ansible-tester
|
||||||
|
password = secure
|
||||||
|
ssl_verify = False
|
||||||
|
group_prefix = Ľuboš_
|
||||||
|
FOREMAN_INI
|
||||||
|
|
||||||
|
# use ansible to validate the return data
|
||||||
|
ansible-playbook -i foreman.sh test_foreman_inventory.yml --connection=local
|
||||||
|
RC=$?
|
||||||
|
if [[ $RC != 0 ]]; then
|
||||||
|
echo "foreman inventory script non-ascii failed"
|
||||||
|
exit $RC
|
||||||
|
fi
|
||||||
|
|
Loading…
Reference in a new issue