mirror of
https://github.com/saltstack/salt.git
synced 2025-04-16 09:40:20 +00:00
[merge jam] Master port 48609 - file.serialize (#58055)
* implement file.serialize dict diffing * existing_data must always be set * update changes.diff instead of changes * Return the diff dict instead of the dictdiffer object * Updating integration tests to reflect changes when diffinng and merge_if_exists is enabled. * Adding changelog. Co-authored-by: Andreas Lutro <anlutro@gmail.com> Co-authored-by: Erik Johnson <palehose@gmail.com> Co-authored-by: Megan Wilhite <mwilhite@vmware.com>
This commit is contained in:
parent
764f182d7f
commit
b4617e8391
3 changed files with 47 additions and 20 deletions
1
changelog/48609.changed
Normal file
1
changelog/48609.changed
Normal file
|
@ -0,0 +1 @@
|
|||
More intelligent diffing in changes of file.serialize state.
|
|
@ -299,6 +299,7 @@ import salt.loader
|
|||
import salt.payload
|
||||
import salt.utils.data
|
||||
import salt.utils.dateutils
|
||||
import salt.utils.dictdiffer
|
||||
import salt.utils.dictupdate
|
||||
import salt.utils.files
|
||||
import salt.utils.hashutils
|
||||
|
@ -8010,6 +8011,7 @@ def serialize(
|
|||
salt.utils.data.repack_dictlist(deserializer_opts)
|
||||
)
|
||||
|
||||
existing_data = None
|
||||
if merge_if_exists:
|
||||
if os.path.isfile(name):
|
||||
if deserializer_name not in __serializers__:
|
||||
|
@ -8100,9 +8102,8 @@ def serialize(
|
|||
else:
|
||||
ret["result"] = True
|
||||
ret["comment"] = "The file {} is in the correct state".format(name)
|
||||
return ret
|
||||
|
||||
return __salt__["file.manage_file"](
|
||||
else:
|
||||
ret = __salt__["file.manage_file"](
|
||||
name=name,
|
||||
sfn="",
|
||||
ret=ret,
|
||||
|
@ -8122,6 +8123,13 @@ def serialize(
|
|||
contents=contents,
|
||||
)
|
||||
|
||||
if isinstance(existing_data, dict) and isinstance(merged_data, dict):
|
||||
ret["changes"]["diff"] = salt.utils.dictdiffer.recursive_diff(
|
||||
existing_data, merged_data
|
||||
).diffs
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def mknod(name, ntype, major=0, minor=0, user=None, group=None, mode="0600"):
|
||||
"""
|
||||
|
|
|
@ -2052,6 +2052,12 @@ class FileTest(ModuleCase, SaltReturnAssertsMixin):
|
|||
)
|
||||
ret = ret[next(iter(ret))]
|
||||
assert ret["result"], ret
|
||||
assert "changes" in ret
|
||||
assert "diff" in ret["changes"]
|
||||
assert "foo" in ret["changes"]["diff"]
|
||||
assert "abc" in ret["changes"]["diff"]["foo"]
|
||||
assert "new" in ret["changes"]["diff"]["foo"]["abc"]
|
||||
assert ret["changes"]["diff"]["foo"]["abc"]["new"], 123
|
||||
|
||||
with salt.utils.files.fopen(name) as fp_:
|
||||
serialized_data = salt.serializers.configparser.deserialize(fp_)
|
||||
|
@ -2098,6 +2104,12 @@ class FileTest(ModuleCase, SaltReturnAssertsMixin):
|
|||
)
|
||||
ret = ret[next(iter(ret))]
|
||||
assert ret["result"], ret
|
||||
assert "changes" in ret
|
||||
assert "diff" in ret["changes"]
|
||||
assert "foo" in ret["changes"]["diff"]
|
||||
assert "abc" in ret["changes"]["diff"]["foo"]
|
||||
assert "new" in ret["changes"]["diff"]["foo"]["abc"]
|
||||
assert ret["changes"]["diff"]["foo"]["abc"]["new"], 123
|
||||
|
||||
with salt.utils.files.fopen(name, "rb") as fp_:
|
||||
serialized_data = salt.serializers.plist.deserialize(fp_)
|
||||
|
@ -2134,6 +2146,12 @@ class FileTest(ModuleCase, SaltReturnAssertsMixin):
|
|||
)
|
||||
ret = ret[next(iter(ret))]
|
||||
assert ret["result"], ret
|
||||
assert "changes" in ret
|
||||
assert "diff" in ret["changes"]
|
||||
assert "foo" in ret["changes"]["diff"]
|
||||
assert "abc" in ret["changes"]["diff"]["foo"]
|
||||
assert "new" in ret["changes"]["diff"]["foo"]["abc"]
|
||||
assert ret["changes"]["diff"]["foo"]["abc"]["new"], 123
|
||||
|
||||
with salt.utils.files.fopen(name, "rb") as fp_:
|
||||
serialized_data = salt.serializers.plist.deserialize(fp_)
|
||||
|
|
Loading…
Add table
Reference in a new issue