Skip to content

Commit

Permalink
fixes saltstack#63545 file.keyvalue should allow creating a file if i…
Browse files Browse the repository at this point in the history
…t doesn't exist
  • Loading branch information
nicholasmhughes committed Apr 5, 2023
1 parent eaeccf7 commit 9955845
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/63545.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ability for file.keyvalue to create a file if it doesn't exist
19 changes: 16 additions & 3 deletions salt/states/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5418,6 +5418,7 @@ def keyvalue(
uncomment=None,
key_ignore_case=False,
value_ignore_case=False,
create_if_missing=False,
):
"""
Key/Value based editing of a file.
Expand Down Expand Up @@ -5491,6 +5492,11 @@ def keyvalue(
the current value is 'yes', will not result in changes when
``value_ignore_case`` is set to True.
create_if_missing
Create the file if the destination file is not found.
.. versionadded:: 3007.0
An example of using ``file.keyvalue`` to ensure sshd does not allow
for root to login with a password and at the same time setting the
login-gracetime to 1 minute and disabling all forwarding:
Expand Down Expand Up @@ -5564,7 +5570,16 @@ def keyvalue(
try:
with salt.utils.files.fopen(name, "r") as fd:
file_contents = fd.readlines()
except OSError:
fd.close()
except FileNotFoundError as exc:
if create_if_missing:
append_if_not_found = True
file_contents = []
else:
ret["comment"] = "unable to open {n}".format(n=name)
ret["result"] = True if ignore_if_missing else False
return ret
except OSError as exc:
ret["comment"] = "unable to open {n}".format(n=name)
ret["result"] = True if ignore_if_missing else False
return ret
Expand Down Expand Up @@ -5692,8 +5707,6 @@ def keyvalue(
# keys needed searching), the line can be added to the content to be
# written once the last checks have been performed
content.append(line)
# finally, close the file
fd.close()

# if append_if_not_found was requested, then append any key/value pairs
# still having a count left on them
Expand Down
27 changes: 27 additions & 0 deletions tests/pytests/unit/states/file/test_keyvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,30 @@ def test_file_keyvalue_not_dict(tmp_path):
f_contents = tempfile.read_text()
assert "PermitRootLogin yes" not in f_contents
assert "#StrictMode yes" in f_contents


def test_file_keyvalue_create_if_missing(tmp_path):
tempfile = tmp_path / "tempfile"
assert not tempfile.exists()

ret = filestate.keyvalue(
name=str(tempfile),
key="myKey",
value="likesIt",
create_if_missing=False,
)
assert ret["result"] is False
assert not tempfile.exists()

ret = filestate.keyvalue(
name=str(tempfile),
key="myKey",
value="likesIt",
create_if_missing=True,
)
assert ret["result"] is True
assert tempfile.exists()
f_contents = tempfile.read_text()
assert "myKey=likesIt" in f_contents

tempfile.unlink()

0 comments on commit 9955845

Please sign in to comment.