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.utils
from salt.utils.mac_utils import execute_return_result, \
execute_return_success, parse_return, validate_enabled
import salt.utils.mac_utils
from salt.exceptions import SaltInvocationError
from salt.ext.six.moves import range
@ -110,7 +109,10 @@ def set_sleep(minutes):
'''
value = _validate_sleep(minutes)
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():
@ -126,8 +128,9 @@ def get_computer_sleep():
salt '*' power.get_computer_sleep
'''
ret = execute_return_result('systemsetup -getcomputersleep')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getcomputersleep')
return salt.utils.mac_utils.parse_return(ret)
def set_computer_sleep(minutes):
@ -150,7 +153,8 @@ def set_computer_sleep(minutes):
'''
value = _validate_sleep(minutes)
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():
@ -166,8 +170,9 @@ def get_display_sleep():
salt '*' power.get_display_sleep
'''
ret = execute_return_result('systemsetup -getdisplaysleep')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getdisplaysleep')
return salt.utils.mac_utils.parse_return(ret)
def set_display_sleep(minutes):
@ -190,7 +195,8 @@ def set_display_sleep(minutes):
'''
value = _validate_sleep(minutes)
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():
@ -206,8 +212,9 @@ def get_harddisk_sleep():
salt '*' power.get_harddisk_sleep
'''
ret = execute_return_result('systemsetup -getharddisksleep')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getharddisksleep')
return salt.utils.mac_utils.parse_return(ret)
def set_harddisk_sleep(minutes):
@ -230,7 +237,8 @@ def set_harddisk_sleep(minutes):
'''
value = _validate_sleep(minutes)
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():
@ -246,8 +254,9 @@ def get_wake_on_modem():
salt '*' power.get_wake_on_modem
'''
ret = execute_return_result('systemsetup -getwakeonmodem')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getwakeonmodem')
return salt.utils.mac_utils.parse_return(ret)
def set_wake_on_modem(enabled):
@ -268,9 +277,9 @@ def set_wake_on_modem(enabled):
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)
return execute_return_success(cmd)
return salt.utils.mac_utils.execute_return_success(cmd)
def get_wake_on_network():
@ -286,8 +295,8 @@ def get_wake_on_network():
salt '*' power.get_wake_on_network
'''
ret = execute_return_result('systemsetup -getwakeonnetworkaccess')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result('systemsetup -getwakeonnetworkaccess')
return salt.utils.mac_utils.parse_return(ret)
def set_wake_on_network(enabled):
@ -308,9 +317,10 @@ def set_wake_on_network(enabled):
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)
return execute_return_success(cmd)
salt.utils.mac_utils.execute_return_success(cmd)
return get_wake_on_network() == state
def get_restart_power_failure():
@ -326,8 +336,9 @@ def get_restart_power_failure():
salt '*' power.get_restart_power_failure
'''
ret = execute_return_result('systemsetup -getrestartpowerfailure')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getrestartpowerfailure')
return salt.utils.mac_utils.parse_return(ret)
def set_restart_power_failure(enabled):
@ -348,9 +359,10 @@ def set_restart_power_failure(enabled):
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)
return execute_return_success(cmd)
salt.utils.mac_utils.execute_return_success(cmd)
return get_restart_power_failure() == state
def get_restart_freeze():
@ -366,8 +378,9 @@ def get_restart_freeze():
salt '*' power.get_restart_freeze
'''
ret = execute_return_result('systemsetup -getrestartfreeze')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getrestartfreeze')
return salt.utils.mac_utils.parse_return(ret)
def set_restart_freeze(enabled):
@ -390,9 +403,10 @@ def set_restart_freeze(enabled):
salt '*' power.set_restart_freeze True
'''
state = validate_enabled(enabled)
state = salt.utils.mac_utils.validate_enabled(enabled)
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():
@ -410,8 +424,9 @@ def get_sleep_on_power_button():
salt '*' power.get_sleep_on_power_button
'''
ret = execute_return_result('systemsetup -getallowpowerbuttontosleepcomputer')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getallowpowerbuttontosleepcomputer')
return salt.utils.mac_utils.parse_return(ret)
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
'''
state = validate_enabled(enabled)
state = salt.utils.mac_utils.validate_enabled(enabled)
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