From c6c5332395412231c2dd0d7c3802a899feee9f33 Mon Sep 17 00:00:00 2001 From: ran Date: Thu, 18 Jul 2013 09:44:18 +0300 Subject: [PATCH] reg module/state support for all Value Types --- salt/modules/reg.py | 14 ++++++++++---- salt/states/reg.py | 5 +++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/salt/modules/reg.py b/salt/modules/reg.py index 9a4dfe1049..4a922992ed 100644 --- a/salt/modules/reg.py +++ b/salt/modules/reg.py @@ -79,27 +79,33 @@ def read_key(hkey, path, key): return False -def set_key(hkey, path, key, value): +def set_key(hkey, path, key, value, vtype='REG_DWORD'): ''' Set a registry key + vtype: http://docs.python.org/2/library/_winreg.html#value-types CLI Example:: - salt '*' reg.set_key HKEY_CURRENT_USER 'SOFTWARE\\Salt' 'version' '0.97' + salt '*' reg.set_key HKEY_CURRENT_USER 'SOFTWARE\\Salt' 'version' '0.97' REG_DWORD ''' registry = Registry() hkey2 = getattr(registry, hkey) # fullpath = '\\\\'.join([path, key]) + + try: + _type = getattr(_winreg, vtype) + except AttributeError: + return False try: # handle = _winreg.OpenKey(hkey2, fullpath, 0, _winreg.KEY_ALL_ACCESS) handle = _winreg.OpenKey(hkey2, path, 0, _winreg.KEY_ALL_ACCESS) - _winreg.SetValueEx(handle, key, 0, _winreg.REG_DWORD, value) + _winreg.SetValueEx(handle, key, 0, _type, value) _winreg.CloseKey(handle) return True except Exception: handle = _winreg.CreateKey(hkey2, path) - _winreg.SetValueEx(handle, key, 0, _winreg.REG_DWORD, value) + _winreg.SetValueEx(handle, key, 0, _type, value) _winreg.CloseKey(handle) return True diff --git a/salt/states/reg.py b/salt/states/reg.py index a44920354e..87fe2c7c9b 100644 --- a/salt/states/reg.py +++ b/salt/states/reg.py @@ -23,7 +23,7 @@ def _parse_key(key): return hive, path, key -def present(name, value): +def present(name, value, vtype='REG_DWORD'): ''' Set a registry entry @@ -32,6 +32,7 @@ def present(name, value): 'HKEY_CURRENT_USER\\SOFTWARE\\Salt\\version': reg.present: - value: 0.15.3 + - vtype: REG_SZ ''' ret = {'name': name, @@ -51,7 +52,7 @@ def present(name, value): return ret # configure the key - ret['result'] = __salt__['reg.set_key'](hive, path, key, int(value)) + ret['result'] = __salt__['reg.set_key'](hive, path, key, value, vtype) if not ret: ret['changes'] = {} ret['comment'] = 'could not configure the registry key'