Merge "Add mac address format validation"

This commit is contained in:
Jenkins 2015-05-14 20:50:51 +00:00 committed by Gerrit Code Review
commit 656cb46dab
4 changed files with 43 additions and 2 deletions

View File

@ -11,6 +11,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import netaddr
from neutronclient.common import exceptions
from neutronclient.neutron import v2_0 as neutronV20
from neutronclient.v2_0 import client as nc
@ -180,3 +182,10 @@ class IPConstraint(constraints.BaseCustomConstraint):
def validate(self, value, context):
self._error_message = 'Invalid IP address'
return netutils.is_valid_ip(value)
class MACConstraint(constraints.BaseCustomConstraint):
def validate(self, value, context):
self._error_message = 'Invalid MAC address.'
return netaddr.valid_mac(value)

View File

@ -143,7 +143,10 @@ class Port(neutron.NeutronResource):
),
MAC_ADDRESS: properties.Schema(
properties.Schema.STRING,
_('MAC address to give to this port.')
_('MAC address to give to this port.'),
constraints=[
constraints.CustomConstraint('mac_addr')
]
),
DEVICE_ID: properties.Schema(
properties.Schema.STRING,
@ -164,7 +167,10 @@ class Port(neutron.NeutronResource):
schema={
ALLOWED_ADDRESS_PAIR_MAC_ADDRESS: properties.Schema(
properties.Schema.STRING,
_('MAC address to allow through this port.')
_('MAC address to allow through this port.'),
constraints=[
constraints.CustomConstraint('mac_addr')
]
),
ALLOWED_ADDRESS_PAIR_IP_ADDRESS: properties.Schema(
properties.Schema.STRING,

View File

@ -229,3 +229,28 @@ class TestIPConstraint(common.HeatTestCase):
]
for ip in invalidate_format:
self.assertFalse(self.constraint.validate(ip, None))
class TestMACConstraint(common.HeatTestCase):
def setUp(self):
super(TestMACConstraint, self).setUp()
self.constraint = neutron.MACConstraint()
def test_valid_mac_format(self):
validate_format = [
'01:23:45:67:89:ab',
'01-23-45-67-89-ab',
'0123.4567.89ab'
]
for mac in validate_format:
self.assertTrue(self.constraint.validate(mac, None))
def test_invalid_mac_format(self):
invalidate_format = [
'8.8.8.8',
'0a-1b-3c-4d-5e-6f-1f',
'0a-1b-3c-4d-5e-xx'
]
for mac in invalidate_format:
self.assertFalse(self.constraint.validate(mac, None))

View File

@ -74,6 +74,7 @@ heat.constraints =
sahara.image = heat.engine.clients.os.sahara:ImageConstraint
trove.flavor = heat.engine.clients.os.trove:FlavorConstraint
ip_addr = heat.engine.clients.os.neutron:IPConstraint
mac_addr = heat.engine.clients.os.neutron:MACConstraint
heat.stack_lifecycle_plugins =