Refactor to use mac_utils and verify settings

This commit is contained in:
twangboy 2016-03-18 15:39:13 -06:00
parent fe86a3d209
commit cff04598c3

View File

@ -8,8 +8,7 @@ from __future__ import absolute_import
# Import salt libs # Import salt libs
import salt.utils import salt.utils
from salt.utils.mac_utils import execute_return_result, \ import salt.utils.mac_utils
execute_return_success, parse_return, validate_enabled
from salt.exceptions import SaltInvocationError from salt.exceptions import SaltInvocationError
from salt.ext.six.moves import range from salt.ext.six.moves import range
@ -110,7 +109,10 @@ def set_sleep(minutes):
''' '''
value = _validate_sleep(minutes) value = _validate_sleep(minutes)
cmd = 'systemsetup -setsleep {0}'.format(value) cmd = 'systemsetup -setsleep {0}'.format(value)
return execute_return_success(cmd) salt.utils.mac_utils.execute_return_success(cmd)
return get_sleep() == {'Computer': value,
'Display': value,
'Hard Disk': value}
def get_computer_sleep(): def get_computer_sleep():
@ -126,8 +128,9 @@ def get_computer_sleep():
salt '*' power.get_computer_sleep salt '*' power.get_computer_sleep
''' '''
ret = execute_return_result('systemsetup -getcomputersleep') ret = salt.utils.mac_utils.execute_return_result(
return parse_return(ret) 'systemsetup -getcomputersleep')
return salt.utils.mac_utils.parse_return(ret)
def set_computer_sleep(minutes): def set_computer_sleep(minutes):
@ -150,7 +153,8 @@ def set_computer_sleep(minutes):
''' '''
value = _validate_sleep(minutes) value = _validate_sleep(minutes)
cmd = 'systemsetup -setcomputersleep {0}'.format(value) cmd = 'systemsetup -setcomputersleep {0}'.format(value)
return execute_return_success(cmd) salt.utils.mac_utils.execute_return_success(cmd)
return get_computer_sleep() == value
def get_display_sleep(): def get_display_sleep():
@ -166,8 +170,9 @@ def get_display_sleep():
salt '*' power.get_display_sleep salt '*' power.get_display_sleep
''' '''
ret = execute_return_result('systemsetup -getdisplaysleep') ret = salt.utils.mac_utils.execute_return_result(
return parse_return(ret) 'systemsetup -getdisplaysleep')
return salt.utils.mac_utils.parse_return(ret)
def set_display_sleep(minutes): def set_display_sleep(minutes):
@ -190,7 +195,8 @@ def set_display_sleep(minutes):
''' '''
value = _validate_sleep(minutes) value = _validate_sleep(minutes)
cmd = 'systemsetup -setdisplaysleep {0}'.format(value) cmd = 'systemsetup -setdisplaysleep {0}'.format(value)
return execute_return_success(cmd) salt.utils.mac_utils.execute_return_success(cmd)
return get_display_sleep() == value
def get_harddisk_sleep(): def get_harddisk_sleep():
@ -206,8 +212,9 @@ def get_harddisk_sleep():
salt '*' power.get_harddisk_sleep salt '*' power.get_harddisk_sleep
''' '''
ret = execute_return_result('systemsetup -getharddisksleep') ret = salt.utils.mac_utils.execute_return_result(
return parse_return(ret) 'systemsetup -getharddisksleep')
return salt.utils.mac_utils.parse_return(ret)
def set_harddisk_sleep(minutes): def set_harddisk_sleep(minutes):
@ -230,7 +237,8 @@ def set_harddisk_sleep(minutes):
''' '''
value = _validate_sleep(minutes) value = _validate_sleep(minutes)
cmd = 'systemsetup -setharddisksleep {0}'.format(value) cmd = 'systemsetup -setharddisksleep {0}'.format(value)
return execute_return_success(cmd) salt.utils.mac_utils.execute_return_success(cmd)
return get_harddisk_sleep() == value
def get_wake_on_modem(): def get_wake_on_modem():
@ -246,8 +254,9 @@ def get_wake_on_modem():
salt '*' power.get_wake_on_modem salt '*' power.get_wake_on_modem
''' '''
ret = execute_return_result('systemsetup -getwakeonmodem') ret = salt.utils.mac_utils.execute_return_result(
return parse_return(ret) 'systemsetup -getwakeonmodem')
return salt.utils.mac_utils.parse_return(ret)
def set_wake_on_modem(enabled): def set_wake_on_modem(enabled):
@ -268,9 +277,9 @@ def set_wake_on_modem(enabled):
salt '*' power.set_wake_on_modem True salt '*' power.set_wake_on_modem True
''' '''
state = validate_enabled(enabled) state = salt.utils.mac_utils.validate_enabled(enabled)
cmd = 'systemsetup -setwakeonmodem {0}'.format(state) cmd = 'systemsetup -setwakeonmodem {0}'.format(state)
return execute_return_success(cmd) return salt.utils.mac_utils.execute_return_success(cmd)
def get_wake_on_network(): def get_wake_on_network():
@ -286,8 +295,8 @@ def get_wake_on_network():
salt '*' power.get_wake_on_network salt '*' power.get_wake_on_network
''' '''
ret = execute_return_result('systemsetup -getwakeonnetworkaccess') ret = salt.utils.mac_utils.execute_return_result('systemsetup -getwakeonnetworkaccess')
return parse_return(ret) return salt.utils.mac_utils.parse_return(ret)
def set_wake_on_network(enabled): def set_wake_on_network(enabled):
@ -308,9 +317,10 @@ def set_wake_on_network(enabled):
salt '*' power.set_wake_on_network True salt '*' power.set_wake_on_network True
''' '''
state = validate_enabled(enabled) state = salt.utils.mac_utils.validate_enabled(enabled)
cmd = 'systemsetup -setwakeonnetworkaccess {0}'.format(state) cmd = 'systemsetup -setwakeonnetworkaccess {0}'.format(state)
return execute_return_success(cmd) salt.utils.mac_utils.execute_return_success(cmd)
return get_wake_on_network() == state
def get_restart_power_failure(): def get_restart_power_failure():
@ -326,8 +336,9 @@ def get_restart_power_failure():
salt '*' power.get_restart_power_failure salt '*' power.get_restart_power_failure
''' '''
ret = execute_return_result('systemsetup -getrestartpowerfailure') ret = salt.utils.mac_utils.execute_return_result(
return parse_return(ret) 'systemsetup -getrestartpowerfailure')
return salt.utils.mac_utils.parse_return(ret)
def set_restart_power_failure(enabled): def set_restart_power_failure(enabled):
@ -348,9 +359,10 @@ def set_restart_power_failure(enabled):
salt '*' power.set_restart_power_failure True salt '*' power.set_restart_power_failure True
''' '''
state = validate_enabled(enabled) state = salt.utils.mac_utils.validate_enabled(enabled)
cmd = 'systemsetup -setrestartpowerfailure {0}'.format(state) cmd = 'systemsetup -setrestartpowerfailure {0}'.format(state)
return execute_return_success(cmd) salt.utils.mac_utils.execute_return_success(cmd)
return get_restart_power_failure() == state
def get_restart_freeze(): def get_restart_freeze():
@ -366,8 +378,9 @@ def get_restart_freeze():
salt '*' power.get_restart_freeze salt '*' power.get_restart_freeze
''' '''
ret = execute_return_result('systemsetup -getrestartfreeze') ret = salt.utils.mac_utils.execute_return_result(
return parse_return(ret) 'systemsetup -getrestartfreeze')
return salt.utils.mac_utils.parse_return(ret)
def set_restart_freeze(enabled): def set_restart_freeze(enabled):
@ -390,9 +403,10 @@ def set_restart_freeze(enabled):
salt '*' power.set_restart_freeze True salt '*' power.set_restart_freeze True
''' '''
state = validate_enabled(enabled) state = salt.utils.mac_utils.validate_enabled(enabled)
cmd = 'systemsetup -setrestartfreeze {0}'.format(state) cmd = 'systemsetup -setrestartfreeze {0}'.format(state)
return execute_return_success(cmd) salt.utils.mac_utils.execute_return_success(cmd)
return get_restart_freeze() == state
def get_sleep_on_power_button(): def get_sleep_on_power_button():
@ -410,8 +424,9 @@ def get_sleep_on_power_button():
salt '*' power.get_sleep_on_power_button salt '*' power.get_sleep_on_power_button
''' '''
ret = execute_return_result('systemsetup -getallowpowerbuttontosleepcomputer') ret = salt.utils.mac_utils.execute_return_result(
return parse_return(ret) 'systemsetup -getallowpowerbuttontosleepcomputer')
return salt.utils.mac_utils.parse_return(ret)
def set_sleep_on_power_button(enabled): def set_sleep_on_power_button(enabled):
@ -431,6 +446,7 @@ def set_sleep_on_power_button(enabled):
salt '*' power.set_sleep_on_power_button True salt '*' power.set_sleep_on_power_button True
''' '''
state = validate_enabled(enabled) state = salt.utils.mac_utils.validate_enabled(enabled)
cmd = 'systemsetup -setallowpowerbuttontosleepcomputer {0}'.format(state) cmd = 'systemsetup -setallowpowerbuttontosleepcomputer {0}'.format(state)
return execute_return_success(cmd) salt.utils.mac_utils.execute_return_success(cmd)
return get_sleep_on_power_button() == state