mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-23 10:51:24 -07:00
* Adjusted case in class names - transparent to users
* Adjustments to module code:
- No need to try/except everything, in fact it complicated debugging
- Replaced second call to xfconf.get() with xfconf.previous_value
* the actual test
* removed extraneous empty lines
* added changelog fragment
* rolled back removing the try/except around the main execution
* Update changelogs/fragments/1305-added-xfconf-tests.yaml
Co-authored-by: Felix Fontein <felix@fontein.de>
* Update plugins/modules/system/xfconf.py
Co-authored-by: Felix Fontein <felix@fontein.de>
* Removed extraneous import
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit f4c63ede7f
)
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
parent
b529955c07
commit
ca97eb6f93
3 changed files with 212 additions and 16 deletions
|
@ -112,15 +112,17 @@ RETURN = '''
|
|||
sample: "96"
|
||||
'''
|
||||
|
||||
import traceback
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.six.moves import shlex_quote
|
||||
|
||||
|
||||
class XfConfException(Exception):
|
||||
class XFConfException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class XfConfProperty(object):
|
||||
class XFConfProperty(object):
|
||||
SET = "present"
|
||||
GET = "get"
|
||||
RESET = "absent"
|
||||
|
@ -162,12 +164,12 @@ class XfConfProperty(object):
|
|||
if err.rstrip() == self.does_not:
|
||||
return None
|
||||
if rc or len(err):
|
||||
raise XfConfException('xfconf-query failed with error (rc={0}): {1}'.format(rc, err))
|
||||
raise XFConfException('xfconf-query failed with error (rc={0}): {1}'.format(rc, err))
|
||||
|
||||
return out.rstrip()
|
||||
|
||||
except OSError as exception:
|
||||
XfConfException('xfconf-query failed with exception: {0}'.format(exception))
|
||||
XFConfException('xfconf-query failed with exception: {0}'.format(exception))
|
||||
|
||||
def get(self):
|
||||
previous_value = self._execute_xfconf_query()
|
||||
|
@ -216,7 +218,7 @@ class XfConfProperty(object):
|
|||
if self.value is None and self.value_type is None:
|
||||
return
|
||||
if (self.value is None) ^ (self.value_type is None):
|
||||
raise XfConfException('Must set both "value" and "value_type"')
|
||||
raise XFConfException('Must set both "value" and "value_type"')
|
||||
|
||||
# stringify all values - in the CLI they will all be happy strings anyway
|
||||
# and by doing this here the rest of the code can be agnostic to it
|
||||
|
@ -230,7 +232,7 @@ class XfConfProperty(object):
|
|||
self.value_type = self.value_type * values_len
|
||||
elif types_len != values_len:
|
||||
# or complain if lists' lengths are different
|
||||
raise XfConfException('Same number of "value" and "value_type" needed')
|
||||
raise XFConfException('Same number of "value" and "value_type" needed')
|
||||
|
||||
# fix boolean values
|
||||
self.value = [self._fix_bool(v[0]) if v[1] == 'bool' else v[0] for v in zip(self.value, self.value_type)]
|
||||
|
@ -247,18 +249,18 @@ def main():
|
|||
# Setup the Ansible module
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
state=dict(default=XfConfProperty.SET,
|
||||
choices=XfConfProperty.VALID_STATES,
|
||||
state=dict(default=XFConfProperty.SET,
|
||||
choices=XFConfProperty.VALID_STATES,
|
||||
type='str'),
|
||||
channel=dict(required=True, type='str'),
|
||||
property=dict(required=True, type='str'),
|
||||
value_type=dict(required=False, type='list',
|
||||
elements='str', choices=XfConfProperty.VALID_VALUE_TYPES),
|
||||
elements='str', choices=XFConfProperty.VALID_VALUE_TYPES),
|
||||
value=dict(required=False, type='list', elements='raw'),
|
||||
force_array=dict(default=False, type='bool', aliases=['array']),
|
||||
),
|
||||
required_if=[
|
||||
('state', XfConfProperty.SET, ['value', 'value_type'])
|
||||
('state', XFConfProperty.SET, ['value', 'value_type'])
|
||||
],
|
||||
supports_check_mode=True
|
||||
)
|
||||
|
@ -269,11 +271,10 @@ def main():
|
|||
state = module.params['state']
|
||||
|
||||
try:
|
||||
# Create a Xfconf preference
|
||||
xfconf = XfConfProperty(module)
|
||||
xfconf = XFConfProperty(module)
|
||||
xfconf.sanitize()
|
||||
|
||||
previous_value = xfconf.get()
|
||||
previous_value = xfconf.previous_value
|
||||
facts = {
|
||||
facts_name: dict(
|
||||
channel=xfconf.channel,
|
||||
|
@ -283,9 +284,9 @@ def main():
|
|||
)
|
||||
}
|
||||
|
||||
if state == XfConfProperty.GET \
|
||||
if state == XFConfProperty.GET \
|
||||
or (previous_value is not None
|
||||
and (state, set(previous_value)) == (XfConfProperty.SET, set(xfconf.value))):
|
||||
and (state, set(previous_value)) == (XFConfProperty.SET, set(xfconf.value))):
|
||||
module.exit_json(changed=False, ansible_facts=facts)
|
||||
return
|
||||
|
||||
|
@ -299,7 +300,7 @@ def main():
|
|||
module.exit_json(changed=True, ansible_facts=facts)
|
||||
|
||||
except Exception as e:
|
||||
module.fail_json(msg="Failed with exception: {0}".format(e))
|
||||
module.fail_json(msg="Failed with exception: {0}".format(e), exception=traceback.format_exc())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue