1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add check_type option, to allow defaults type changes (#8173)

* Add check_type option, to allow defaults type changes

* Add changelog fragment

* Changelog fragments are yaml, not markdown

* Update changelogs/fragments/8173-osx_defaults-check_type.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/osx_defaults.py

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Kris Matthews 2024-04-08 16:06:55 -04:00 committed by GitHub
parent 610ecf9bf5
commit bc2ff24f74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 4 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- osx_defaults - add option ``check_types`` to enable changing the type of existing defaults on the fly (https://github.com/ansible-collections/community.general/pull/8173).

View file

@ -50,6 +50,13 @@ options:
type: str type: str
choices: [ array, bool, boolean, date, float, int, integer, string ] choices: [ array, bool, boolean, date, float, int, integer, string ]
default: string default: string
check_type:
description:
- Checks if the type of the provided O(value) matches the type of an existing default.
- If the types do not match, raises an error.
type: bool
default: true
version_added: 8.6.0
array_add: array_add:
description: description:
- Add new elements to the array for a key which has an array as its value. - Add new elements to the array for a key which has an array as its value.
@ -158,6 +165,7 @@ class OSXDefaults(object):
self.domain = module.params['domain'] self.domain = module.params['domain']
self.host = module.params['host'] self.host = module.params['host']
self.key = module.params['key'] self.key = module.params['key']
self.check_type = module.params['check_type']
self.type = module.params['type'] self.type = module.params['type']
self.array_add = module.params['array_add'] self.array_add = module.params['array_add']
self.value = module.params['value'] self.value = module.params['value']
@ -349,10 +357,11 @@ class OSXDefaults(object):
self.delete() self.delete()
return True return True
# There is a type mismatch! Given type does not match the type in defaults # Check if there is a type mismatch, e.g. given type does not match the type in defaults
value_type = type(self.value) if self.check_type:
if self.current_value is not None and not isinstance(self.current_value, value_type): value_type = type(self.value)
raise OSXDefaultsException("Type mismatch. Type in defaults: %s" % type(self.current_value).__name__) if self.current_value is not None and not isinstance(self.current_value, value_type):
raise OSXDefaultsException("Type mismatch. Type in defaults: %s" % type(self.current_value).__name__)
# Current value matches the given value. Nothing need to be done. Arrays need extra care # Current value matches the given value. Nothing need to be done. Arrays need extra care
if self.type == "array" and self.current_value is not None and not self.array_add and \ if self.type == "array" and self.current_value is not None and not self.array_add and \
@ -383,6 +392,7 @@ def main():
domain=dict(type='str', default='NSGlobalDomain'), domain=dict(type='str', default='NSGlobalDomain'),
host=dict(type='str'), host=dict(type='str'),
key=dict(type='str', no_log=False), key=dict(type='str', no_log=False),
check_type=dict(type='bool', default=True),
type=dict(type='str', default='string', choices=['array', 'bool', 'boolean', 'date', 'float', 'int', 'integer', 'string']), type=dict(type='str', default='string', choices=['array', 'bool', 'boolean', 'date', 'float', 'int', 'integer', 'string']),
array_add=dict(type='bool', default=False), array_add=dict(type='bool', default=False),
value=dict(type='raw'), value=dict(type='raw'),