mirror of
https://github.com/saltstack/salt.git
synced 2025-04-16 09:40:20 +00:00
Set registry settings at the same time
This commit is contained in:
parent
6050c91ff1
commit
0ae0b5bcdc
3 changed files with 127 additions and 5 deletions
|
@ -56,6 +56,7 @@ import logging
|
|||
|
||||
import salt.utils.platform
|
||||
import salt.utils.win_lgpo_reg
|
||||
import salt.utils.win_reg
|
||||
from salt.exceptions import SaltInvocationError
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
@ -323,8 +324,10 @@ def set_value(
|
|||
# Verify input
|
||||
if policy_class.lower() in ["computer", "machine"]:
|
||||
policy_class = "Machine"
|
||||
hive = "HKLM"
|
||||
elif policy_class.lower() in ["user"]:
|
||||
policy_class = "User"
|
||||
hive = "HKCU"
|
||||
else:
|
||||
raise SaltInvocationError("An invalid policy class was specified")
|
||||
|
||||
|
@ -378,7 +381,15 @@ def set_value(
|
|||
else:
|
||||
pol_data[key] = {v_name: {"data": v_data, "type": v_type}}
|
||||
|
||||
return write_reg_pol(pol_data)
|
||||
write_reg_pol(pol_data)
|
||||
|
||||
salt.utils.win_reg.set_value(
|
||||
hive=hive,
|
||||
key=key,
|
||||
vname=v_name,
|
||||
vdata=v_data,
|
||||
vtype=v_type,
|
||||
)
|
||||
|
||||
|
||||
def disable_value(key, v_name, policy_class="machine"):
|
||||
|
@ -419,8 +430,10 @@ def disable_value(key, v_name, policy_class="machine"):
|
|||
# Verify input
|
||||
if policy_class.lower() in ["computer", "machine"]:
|
||||
policy_class = "Machine"
|
||||
hive = "HKLM"
|
||||
elif policy_class.lower() in ["user"]:
|
||||
policy_class = "User"
|
||||
hive = "HKCU"
|
||||
else:
|
||||
raise SaltInvocationError("An invalid policy class was specified")
|
||||
|
||||
|
@ -451,7 +464,9 @@ def disable_value(key, v_name, policy_class="machine"):
|
|||
else:
|
||||
pol_data[key] = {"**del.{}".format(v_name): {"data": " ", "type": "REG_SZ"}}
|
||||
|
||||
return write_reg_pol(pol_data)
|
||||
write_reg_pol(pol_data)
|
||||
|
||||
salt.utils.win_reg.delete_value(hive=hive, key=key, vname=v_name)
|
||||
|
||||
|
||||
def delete_value(key, v_name, policy_class="Machine"):
|
||||
|
@ -493,8 +508,10 @@ def delete_value(key, v_name, policy_class="Machine"):
|
|||
# Verify input
|
||||
if policy_class.lower() in ["computer", "machine"]:
|
||||
policy_class = "Machine"
|
||||
hive = "HKLM"
|
||||
elif policy_class.lower() in ["user"]:
|
||||
policy_class = "User"
|
||||
hive = "HKCU"
|
||||
else:
|
||||
raise SaltInvocationError("An invalid policy class was specified")
|
||||
|
||||
|
@ -517,7 +534,9 @@ def delete_value(key, v_name, policy_class="Machine"):
|
|||
else:
|
||||
return None
|
||||
|
||||
return write_reg_pol(pol_data)
|
||||
write_reg_pol(pol_data)
|
||||
|
||||
salt.utils.win_reg.delete_value(hive=hive, key=key, vname=v_name)
|
||||
|
||||
|
||||
# This is for testing different settings and verifying that we are writing the
|
||||
|
|
|
@ -48,6 +48,28 @@ def value_present(name, key, v_data, v_type="REG_DWORD", policy_class="Machine")
|
|||
- User
|
||||
|
||||
Default is ``Machine``
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-clock:: yaml
|
||||
|
||||
# Using the name parameter in the definition
|
||||
set_reg_pol_value:
|
||||
lgpo_reg.present:
|
||||
- key: SOFTWARE\MyKey
|
||||
- name: MyValue
|
||||
- v_type: REG_SZ
|
||||
- v_data: "some string data"
|
||||
- policy_class: Machine
|
||||
|
||||
|
||||
# Using the name as the parameter and modifying the User policy
|
||||
MyValue:
|
||||
lgpo_reg.present:
|
||||
- key: SOFTWARE\MyKey
|
||||
- v_type: REG_SZ
|
||||
- v_data: "some string data"
|
||||
- policy_class: User
|
||||
"""
|
||||
ret = {"name": name, "changes": {}, "result": False, "comment": ""}
|
||||
|
||||
|
@ -103,6 +125,24 @@ def value_disabled(name, key, policy_class="Machine"):
|
|||
- User
|
||||
|
||||
Default is ``Machine``
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-clock:: yaml
|
||||
|
||||
# Using the name parameter in the definition
|
||||
set_reg_pol_value:
|
||||
lgpo_reg.disabled:
|
||||
- key: SOFTWARE\MyKey
|
||||
- name: MyValue
|
||||
- policy_class: Machine
|
||||
|
||||
|
||||
# Using the name as the parameter and modifying the User policy
|
||||
MyValue:
|
||||
lgpo_reg.disabled:
|
||||
- key: SOFTWARE\MyKey
|
||||
- policy_class: User
|
||||
"""
|
||||
ret = {"name": name, "changes": {}, "result": False, "comment": ""}
|
||||
|
||||
|
@ -152,6 +192,24 @@ def value_absent(name, key, policy_class="Machine"):
|
|||
- User
|
||||
|
||||
Default is ``Machine``
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-clock:: yaml
|
||||
|
||||
# Using the name parameter in the definition
|
||||
set_reg_pol_value:
|
||||
lgpo_reg.absent:
|
||||
- key: SOFTWARE\MyKey
|
||||
- name: MyValue
|
||||
- policy_class: Machine
|
||||
|
||||
|
||||
# Using the name as the parameter and modifying the User policy
|
||||
MyValue:
|
||||
lgpo_reg.absent:
|
||||
- key: SOFTWARE\MyKey
|
||||
- policy_class: User
|
||||
"""
|
||||
ret = {"name": name, "changes": {}, "result": False, "comment": ""}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ import pytest
|
|||
import salt.modules.win_lgpo_reg as lgpo_reg
|
||||
import salt.utils.files
|
||||
import salt.utils.win_lgpo_reg
|
||||
import salt.utils.win_reg
|
||||
from salt.exceptions import SaltInvocationError
|
||||
|
||||
pytestmark = [
|
||||
|
@ -18,7 +19,11 @@ def empty_reg_pol():
|
|||
reg_pol_file = class_info["Machine"]["policy_path"]
|
||||
with salt.utils.files.fopen(reg_pol_file, "wb") as f:
|
||||
f.write(salt.utils.win_lgpo_reg.REG_POL_HEADER.encode("utf-16-le"))
|
||||
salt.utils.win_reg.delete_key_recursive(hive="HKLM", key="SOFTWARE\\MyKey1")
|
||||
salt.utils.win_reg.delete_key_recursive(hive="HKLM", key="SOFTWARE\\MyKey2")
|
||||
yield
|
||||
salt.utils.win_reg.delete_key_recursive(hive="HKLM", key="SOFTWARE\\MyKey1")
|
||||
salt.utils.win_reg.delete_key_recursive(hive="HKLM", key="SOFTWARE\\MyKey2")
|
||||
with salt.utils.files.fopen(reg_pol_file, "wb") as f:
|
||||
f.write(salt.utils.win_lgpo_reg.REG_POL_HEADER.encode("utf-16-le"))
|
||||
|
||||
|
@ -26,7 +31,7 @@ def empty_reg_pol():
|
|||
@pytest.fixture
|
||||
def reg_pol():
|
||||
data_to_write = {
|
||||
r"SOFTWARE\MyKey1": {
|
||||
"SOFTWARE\\MyKey1": {
|
||||
"MyValue1": {
|
||||
"data": "squidward",
|
||||
"type": "REG_SZ",
|
||||
|
@ -36,7 +41,7 @@ def reg_pol():
|
|||
"type": "REG_SZ",
|
||||
},
|
||||
},
|
||||
r"SOFTWARE\MyKey2": {
|
||||
"SOFTWARE\\MyKey2": {
|
||||
"MyValue3": {
|
||||
"data": ["spongebob", "squarepants"],
|
||||
"type": "REG_MULTI_SZ",
|
||||
|
@ -44,7 +49,23 @@ def reg_pol():
|
|||
},
|
||||
}
|
||||
lgpo_reg.write_reg_pol(data_to_write)
|
||||
salt.utils.win_reg.set_value(
|
||||
hive="HKLM",
|
||||
key="SOFTWARE\\MyKey1",
|
||||
vname="MyValue1",
|
||||
vdata="squidward",
|
||||
vtype="REG_SZ",
|
||||
)
|
||||
salt.utils.win_reg.set_value(
|
||||
hive="HKLM",
|
||||
key="SOFTWARE\\MyKey2",
|
||||
vname="MyValue3",
|
||||
vdata=["spongebob", "squarepants"],
|
||||
vtype="REG_MULTI_SZ",
|
||||
)
|
||||
yield
|
||||
salt.utils.win_reg.delete_key_recursive(hive="HKLM", key="SOFTWARE\\MyKey1")
|
||||
salt.utils.win_reg.delete_key_recursive(hive="HKLM", key="SOFTWARE\\MyKey2")
|
||||
class_info = salt.utils.win_lgpo_reg.CLASS_INFO
|
||||
reg_pol_file = class_info["Machine"]["policy_path"]
|
||||
with salt.utils.files.fopen(reg_pol_file, "wb") as f:
|
||||
|
@ -119,6 +140,16 @@ def test_set_value(empty_reg_pol):
|
|||
lgpo_reg.set_value(key=key, v_name=v_name, v_data="1")
|
||||
result = lgpo_reg.get_value(key=key, v_name=v_name)
|
||||
assert result == expected
|
||||
expected = {
|
||||
"hive": "HKLM",
|
||||
"key": key,
|
||||
"vname": v_name,
|
||||
"vdata": 1,
|
||||
"vtype": "REG_DWORD",
|
||||
"success": True,
|
||||
}
|
||||
result = salt.utils.win_reg.read_value(hive="HKLM", key=key, vname=v_name)
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_set_value_existing_change(reg_pol):
|
||||
|
@ -128,6 +159,16 @@ def test_set_value_existing_change(reg_pol):
|
|||
lgpo_reg.set_value(key=key, v_name=v_name, v_data="1")
|
||||
result = lgpo_reg.get_value(key=key, v_name=v_name)
|
||||
assert result == expected
|
||||
expected = {
|
||||
"hive": "HKLM",
|
||||
"key": key,
|
||||
"vname": v_name,
|
||||
"vdata": 1,
|
||||
"vtype": "REG_DWORD",
|
||||
"success": True,
|
||||
}
|
||||
result = salt.utils.win_reg.read_value(hive="HKLM", key=key, vname=v_name)
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_set_value_existing_no_change(reg_pol):
|
||||
|
@ -198,6 +239,8 @@ def test_disable_value(reg_pol):
|
|||
lgpo_reg.disable_value(key=key, v_name="MyValue1")
|
||||
result = lgpo_reg.get_key(key=key)
|
||||
assert result == expected
|
||||
result = salt.utils.win_reg.value_exists(hive="HKLM", key=key, vname="MyValue1")
|
||||
assert result is False
|
||||
|
||||
|
||||
def test_disable_value_no_change(reg_pol):
|
||||
|
@ -232,6 +275,8 @@ def test_delete_value_existing(reg_pol):
|
|||
lgpo_reg.delete_value(key=key, v_name="MyValue1")
|
||||
result = lgpo_reg.get_key(key=key)
|
||||
assert result == expected
|
||||
result = salt.utils.win_reg.value_exists(hive="HKLM", key=key, vname="MyValue2")
|
||||
assert result is False
|
||||
|
||||
|
||||
def test_delete_value_no_change(empty_reg_pol):
|
||||
|
|
Loading…
Add table
Reference in a new issue