Start building validation toolset

This commit is contained in:
Joseph Hall 2013-10-14 09:04:56 -06:00
parent 007e131a55
commit 01eb5f504c
4 changed files with 24 additions and 19 deletions

View File

@ -14,7 +14,7 @@ The following packages are required packages for this module:
import logging
# Import salt libs
import salt.utils
import salt.utils.validate.net
from salt.exceptions import CommandExecutionError
log = logging.getLogger(__name__)
@ -193,7 +193,7 @@ def block(bdaddr):
salt '*' bluetooth.block DE:AD:BE:EF:CA:FE
'''
if not salt.utils.valid_mac(bdaddr):
if not salt.utils.validate.net.mac(bdaddr):
raise CommandExecutionError(
'Invalid BD address passed to bluetooth.block'
)
@ -212,7 +212,7 @@ def unblock(bdaddr):
salt '*' bluetooth.unblock DE:AD:BE:EF:CA:FE
'''
if not salt.utils.valid_mac(bdaddr):
if not salt.utils.validate.net.mac(bdaddr):
raise CommandExecutionError(
'Invalid BD address passed to bluetooth.unblock'
)
@ -237,7 +237,7 @@ def pair(address, key):
TODO: This function is currently broken, as the bluez-simple-agent program
no longer ships with BlueZ >= 5.0. It needs to be refactored.
'''
if not salt.utils.valid_mac(address):
if not salt.utils.validate.net.mac(address):
raise CommandExecutionError(
'Invalid BD address passed to bluetooth.pair'
)
@ -272,7 +272,7 @@ def unpair(address):
TODO: This function is currently broken, as the bluez-simple-agent program
no longer ships with BlueZ >= 5.0. It needs to be refactored.
'''
if not salt.utils.valid_mac(address):
if not salt.utils.validate.net.mac(address):
raise CommandExecutionError(
'Invalid BD address passed to bluetooth.unpair'
)

View File

@ -1753,17 +1753,3 @@ def is_bin_str(data):
if len(text) / len(data) > 0.30:
return True
return False
def valid_mac(mac):
'''
Validates a mac address
'''
valid = re.compile(r'''
(^([0-9A-F]{2}[-]){5}([0-9A-F]{2})$
|^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$)
''',
re.VERBOSE|re.IGNORECASE)
if valid.match(mac) is None:
return False
return True

View File

View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
'''
Various network validation utilities
'''
import re
def mac(mac):
'''
Validates a mac address
'''
valid = re.compile(r'''
(^([0-9A-F]{2}[-]){5}([0-9A-F]{2})$
|^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$)
''',
re.VERBOSE|re.IGNORECASE)
if valid.match(mac) is None:
return False
return True