mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add encoding and no_spaces options to state
This commit is contained in:
parent
fb031ac418
commit
d0d820673f
2 changed files with 214 additions and 41 deletions
|
@ -67,10 +67,10 @@ def set_option(file_name, sections=None, separator="=", encoding=None, no_spaces
|
|||
.. versionadded:: 3006.6
|
||||
|
||||
no_spaces (bool):
|
||||
A bool value that specifies if the separator will be wrapped with
|
||||
spaces. This parameter was added to have the ability to not wrap the
|
||||
separator with spaces. Default is ``False``, which maintains
|
||||
backwards compatibility.
|
||||
A bool value that specifies that the key/value separator will be
|
||||
wrapped with spaces. This parameter was added to have the ability to
|
||||
not wrap the separator with spaces. Default is ``False``, which
|
||||
maintains backwards compatibility.
|
||||
|
||||
.. warning::
|
||||
This will affect all key/value pairs in the ini file, not just
|
||||
|
|
|
@ -21,8 +21,61 @@ def __virtual__():
|
|||
return __virtualname__ if "ini.set_option" in __salt__ else False
|
||||
|
||||
|
||||
def options_present(name, sections=None, separator="=", strict=False):
|
||||
def options_present(
|
||||
name, sections=None, separator="=", strict=False, encoding=None, no_spaces=False
|
||||
):
|
||||
"""
|
||||
Set or create a key/value pair in an ``ini`` file. Options present in the
|
||||
ini file and not specified in the sections dict will be untouched, unless
|
||||
the ``strict: True`` flag is used.
|
||||
|
||||
Sections that do not exist will be created.
|
||||
|
||||
Args:
|
||||
|
||||
name (str):
|
||||
The path to the ini file
|
||||
|
||||
sections (dict):
|
||||
A dictionary of sections and key/value pairs that will be used to
|
||||
update the ini file. Other sections and key/value pairs in the ini
|
||||
file will be untouched unless ``strict: True`` is passed.
|
||||
|
||||
separator (str):
|
||||
The character used to separate keys and values. Standard ini files
|
||||
use the "=" character. The default is ``=``.
|
||||
|
||||
strict (bool):
|
||||
A boolean value that specifies that the ``sections`` dictionary
|
||||
contains all settings in the ini file. ``True`` will create an ini
|
||||
file with only the values specified in ``sections``. ``False`` will
|
||||
append or update values in an existing ini file and leave the rest
|
||||
untouched.
|
||||
|
||||
encoding (str):
|
||||
A string value representing encoding of the target ini file. If
|
||||
``None`` is passed, it uses the system default which is likely
|
||||
``utf-8``. Default is ``None``
|
||||
|
||||
.. versionadded:: 3006.10
|
||||
|
||||
no_spaces (bool):
|
||||
A bool value that specifies that the key/value separator will be
|
||||
wrapped with spaces. This parameter was added to have the ability to
|
||||
not wrap the separator with spaces. Default is ``False``, which
|
||||
maintains backwards compatibility.
|
||||
|
||||
.. warning::
|
||||
This will affect all key/value pairs in the ini file, not just
|
||||
the specific value being set.
|
||||
|
||||
.. versionadded:: 3006.10
|
||||
|
||||
Returns:
|
||||
dict: A dictionary containing list of changes made
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
/home/saltminion/api-paste.ini:
|
||||
|
@ -35,12 +88,6 @@ def options_present(name, sections=None, separator="=", strict=False):
|
|||
secondoption: 'secondvalue'
|
||||
test1:
|
||||
testkey1: 'testval121'
|
||||
|
||||
options present in file and not specified in sections
|
||||
dict will be untouched, unless `strict: True` flag is
|
||||
used
|
||||
|
||||
changes dict will contain the list of changes made
|
||||
"""
|
||||
ret = {
|
||||
"name": name,
|
||||
|
@ -58,7 +105,9 @@ def options_present(name, sections=None, separator="=", strict=False):
|
|||
for sname, sbody in sections.items():
|
||||
if not isinstance(sbody, (dict, OrderedDict)):
|
||||
options.update({sname: sbody})
|
||||
cur_ini = __salt__["ini.get_ini"](name, separator)
|
||||
cur_ini = __salt__["ini.get_ini"](
|
||||
file_name=name, separator=separator, encoding=encoding
|
||||
)
|
||||
original_top_level_opts = {}
|
||||
original_sections = {}
|
||||
for key, val in cur_ini.items():
|
||||
|
@ -78,7 +127,13 @@ def options_present(name, sections=None, separator="=", strict=False):
|
|||
ret["comment"] += f"Changed key {option}.\n"
|
||||
ret["result"] = None
|
||||
else:
|
||||
options_updated = __salt__["ini.set_option"](name, options, separator)
|
||||
options_updated = __salt__["ini.set_option"](
|
||||
file_name=name,
|
||||
sections=options,
|
||||
separator=separator,
|
||||
encoding=encoding,
|
||||
no_spaces=no_spaces,
|
||||
)
|
||||
changes.update(options_updated)
|
||||
if strict:
|
||||
for opt_to_remove in set(original_top_level_opts).difference(options):
|
||||
|
@ -87,7 +142,11 @@ def options_present(name, sections=None, separator="=", strict=False):
|
|||
ret["result"] = None
|
||||
else:
|
||||
__salt__["ini.remove_option"](
|
||||
name, None, opt_to_remove, separator
|
||||
file_name=name,
|
||||
section=None,
|
||||
option=opt_to_remove,
|
||||
separator=separator,
|
||||
encoding=encoding,
|
||||
)
|
||||
changes.update(
|
||||
{
|
||||
|
@ -119,7 +178,11 @@ def options_present(name, sections=None, separator="=", strict=False):
|
|||
ret["result"] = None
|
||||
else:
|
||||
__salt__["ini.remove_option"](
|
||||
name, section_name, key_to_remove, separator
|
||||
file_name=name,
|
||||
section=section_name,
|
||||
option=key_to_remove,
|
||||
separator=separator,
|
||||
encoding=encoding,
|
||||
)
|
||||
changes[section_name].update({key_to_remove: ""})
|
||||
changes[section_name].update(
|
||||
|
@ -140,7 +203,11 @@ def options_present(name, sections=None, separator="=", strict=False):
|
|||
ret["result"] = None
|
||||
else:
|
||||
options_updated = __salt__["ini.set_option"](
|
||||
name, {section_name: section_body}, separator
|
||||
file_name=name,
|
||||
sections={section_name: section_body},
|
||||
separator=separator,
|
||||
encoding=encoding,
|
||||
no_spaces=no_spaces,
|
||||
)
|
||||
if options_updated:
|
||||
changes[section_name].update(options_updated[section_name])
|
||||
|
@ -148,7 +215,13 @@ def options_present(name, sections=None, separator="=", strict=False):
|
|||
del changes[section_name]
|
||||
else:
|
||||
if not __opts__["test"]:
|
||||
changes = __salt__["ini.set_option"](name, sections, separator)
|
||||
changes = __salt__["ini.set_option"](
|
||||
file_name=name,
|
||||
sections=sections,
|
||||
separator=separator,
|
||||
encoding=encoding,
|
||||
no_spaces=no_spaces,
|
||||
)
|
||||
except (OSError, KeyError) as err:
|
||||
ret["comment"] = f"{err}"
|
||||
ret["result"] = False
|
||||
|
@ -165,8 +238,37 @@ def options_present(name, sections=None, separator="=", strict=False):
|
|||
return ret
|
||||
|
||||
|
||||
def options_absent(name, sections=None, separator="="):
|
||||
def options_absent(name, sections=None, separator="=", encoding=None):
|
||||
"""
|
||||
Remove a key/value pair from an ini file. Key/value pairs present in the ini
|
||||
file and not specified in sections dict will be untouched.
|
||||
|
||||
Args:
|
||||
|
||||
name (str):
|
||||
The path to the ini file
|
||||
|
||||
sections (dict):
|
||||
A dictionary of sections and key/value pairs that will be removed
|
||||
from the ini file. Other key/value pairs in the ini file will be
|
||||
untouched.
|
||||
|
||||
separator (str):
|
||||
The character used to separate keys and values. Standard ini files
|
||||
use the "=" character. The default is ``=``.
|
||||
|
||||
encoding (str):
|
||||
A string value representing encoding of the target ini file. If
|
||||
``None`` is passed, it uses the system default which is likely
|
||||
``utf-8``. Default is ``None``
|
||||
|
||||
.. versionadded:: 3006.10
|
||||
|
||||
Returns:
|
||||
dict: A dictionary containing list of changes made
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
/home/saltminion/api-paste.ini:
|
||||
|
@ -178,11 +280,6 @@ def options_absent(name, sections=None, separator="="):
|
|||
- secondoption
|
||||
test1:
|
||||
- testkey1
|
||||
|
||||
options present in file and not specified in sections
|
||||
dict will be untouched
|
||||
|
||||
changes dict will contain the list of changes made
|
||||
"""
|
||||
ret = {
|
||||
"name": name,
|
||||
|
@ -196,7 +293,12 @@ def options_absent(name, sections=None, separator="="):
|
|||
for section in sections or {}:
|
||||
section_name = " in section " + section if section else ""
|
||||
try:
|
||||
cur_section = __salt__["ini.get_section"](name, section, separator)
|
||||
cur_section = __salt__["ini.get_section"](
|
||||
file_name=name,
|
||||
section=section,
|
||||
separator=separator,
|
||||
encoding=encoding,
|
||||
)
|
||||
except OSError as err:
|
||||
ret["comment"] = f"{err}"
|
||||
ret["result"] = False
|
||||
|
@ -215,7 +317,13 @@ def options_absent(name, sections=None, separator="="):
|
|||
ret["result"] = None
|
||||
else:
|
||||
option = section
|
||||
if not __salt__["ini.get_option"](name, None, option, separator):
|
||||
if not __salt__["ini.get_option"](
|
||||
file_name=name,
|
||||
section=None,
|
||||
option=option,
|
||||
separator=separator,
|
||||
encoding=encoding,
|
||||
):
|
||||
ret["comment"] += f"Key {option} does not exist.\n"
|
||||
continue
|
||||
ret["comment"] += f"Deleted key {option}.\n"
|
||||
|
@ -229,7 +337,11 @@ def options_absent(name, sections=None, separator="="):
|
|||
for key in keys:
|
||||
try:
|
||||
current_value = __salt__["ini.remove_option"](
|
||||
name, section, key, separator
|
||||
file_name=name,
|
||||
section=section,
|
||||
option=key,
|
||||
separator=separator,
|
||||
encoding=encoding,
|
||||
)
|
||||
except OSError as err:
|
||||
ret["comment"] = f"{err}"
|
||||
|
@ -247,8 +359,38 @@ def options_absent(name, sections=None, separator="="):
|
|||
return ret
|
||||
|
||||
|
||||
def sections_present(name, sections=None, separator="="):
|
||||
def sections_present(name, sections=None, separator="=", encoding=None):
|
||||
"""
|
||||
Add sections to an ini file. This will only create empty sections. To also
|
||||
create key/value pairs, use options_present state.
|
||||
|
||||
Args:
|
||||
|
||||
name (str):
|
||||
The path to the ini file
|
||||
|
||||
sections (dict):
|
||||
A dictionary of sections and key/value pairs that will be used to
|
||||
update the ini file. Only the sections portion is used, key/value
|
||||
pairs are ignored. To also set key/value pairs, use the
|
||||
options_present state.
|
||||
|
||||
separator (str):
|
||||
The character used to separate keys and values. Standard ini files
|
||||
use the "=" character. The default is ``=``.
|
||||
|
||||
encoding (str):
|
||||
A string value representing encoding of the target ini file. If
|
||||
``None`` is passed, it uses the system default which is likely
|
||||
``utf-8``. Default is ``None``
|
||||
|
||||
.. versionadded:: 3006.10
|
||||
|
||||
Returns:
|
||||
dict: A dictionary containing list of changes made
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
/home/saltminion/api-paste.ini:
|
||||
|
@ -257,12 +399,6 @@ def sections_present(name, sections=None, separator="="):
|
|||
- sections:
|
||||
- section_one
|
||||
- section_two
|
||||
|
||||
This will only create empty sections. To also create options, use
|
||||
options_present state
|
||||
|
||||
options present in file and not specified in sections will be deleted
|
||||
changes dict will contain the sections that changed
|
||||
"""
|
||||
ret = {
|
||||
"name": name,
|
||||
|
@ -274,7 +410,9 @@ def sections_present(name, sections=None, separator="="):
|
|||
ret["result"] = True
|
||||
ret["comment"] = ""
|
||||
try:
|
||||
cur_ini = __salt__["ini.get_ini"](name, separator)
|
||||
cur_ini = __salt__["ini.get_ini"](
|
||||
file_name=name, separator=separator, encoding=encoding
|
||||
)
|
||||
except OSError as err:
|
||||
ret["result"] = False
|
||||
ret["comment"] = f"{err}"
|
||||
|
@ -293,7 +431,12 @@ def sections_present(name, sections=None, separator="="):
|
|||
for section_name in sections or []:
|
||||
section_to_update.update({section_name: {}})
|
||||
try:
|
||||
changes = __salt__["ini.set_option"](name, section_to_update, separator)
|
||||
changes = __salt__["ini.set_option"](
|
||||
file_name=name,
|
||||
section=section_to_update,
|
||||
separator=separator,
|
||||
encoding=encoding,
|
||||
)
|
||||
except OSError as err:
|
||||
ret["result"] = False
|
||||
ret["comment"] = f"{err}"
|
||||
|
@ -307,8 +450,37 @@ def sections_present(name, sections=None, separator="="):
|
|||
return ret
|
||||
|
||||
|
||||
def sections_absent(name, sections=None, separator="="):
|
||||
def sections_absent(name, sections=None, separator="=", encoding=None):
|
||||
"""
|
||||
Remove sections from the ini file. All key/value pairs in the section will
|
||||
also be removed.
|
||||
|
||||
Args:
|
||||
|
||||
name (str):
|
||||
The path to the ini file
|
||||
|
||||
sections (dict):
|
||||
A dictionary of sections and key/value pairs that will be used to
|
||||
update the ini file. Other sections and key/value pairs in the ini
|
||||
file will be untouched unless ``strict: True`` is passed.
|
||||
|
||||
separator (str):
|
||||
The character used to separate keys and values. Standard ini files
|
||||
use the "=" character. The default is ``=``.
|
||||
|
||||
encoding (str):
|
||||
A string value representing encoding of the target ini file. If
|
||||
``None`` is passed, it uses the system default which is likely
|
||||
``utf-8``. Default is ``None``
|
||||
|
||||
.. versionadded:: 3006.6
|
||||
|
||||
Returns:
|
||||
dict: A dictionary containing list of changes made
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
/home/saltminion/api-paste.ini:
|
||||
|
@ -317,9 +489,6 @@ def sections_absent(name, sections=None, separator="="):
|
|||
- sections:
|
||||
- test
|
||||
- test1
|
||||
|
||||
options present in file and not specified in sections will be deleted
|
||||
changes dict will contain the sections that changed
|
||||
"""
|
||||
ret = {
|
||||
"name": name,
|
||||
|
@ -331,7 +500,9 @@ def sections_absent(name, sections=None, separator="="):
|
|||
ret["result"] = True
|
||||
ret["comment"] = ""
|
||||
try:
|
||||
cur_ini = __salt__["ini.get_ini"](name, separator)
|
||||
cur_ini = __salt__["ini.get_ini"](
|
||||
file_name=name, separator=separator, encoding=encoding
|
||||
)
|
||||
except OSError as err:
|
||||
ret["result"] = False
|
||||
ret["comment"] = f"{err}"
|
||||
|
@ -347,7 +518,9 @@ def sections_absent(name, sections=None, separator="="):
|
|||
return ret
|
||||
for section in sections or []:
|
||||
try:
|
||||
cur_section = __salt__["ini.remove_section"](name, section, separator)
|
||||
cur_section = __salt__["ini.remove_section"](
|
||||
file_name=name, section=section, separator=separator, encoding=encoding
|
||||
)
|
||||
except OSError as err:
|
||||
ret["result"] = False
|
||||
ret["comment"] = f"{err}"
|
||||
|
|
Loading…
Add table
Reference in a new issue