Merge "Add zone type specific validators"

This commit is contained in:
Jenkins 2015-09-22 19:33:00 +00:00 committed by Gerrit Code Review
commit 3be131bc6c
4 changed files with 116 additions and 23 deletions

View File

@ -89,18 +89,12 @@ class ZonesController(rest.RestController):
if 'type' not in zone:
zone['type'] = 'PRIMARY'
if zone['type'] == 'SECONDARY':
mgmt_email = CONF['service:central'].managed_resource_email
zone['email'] = mgmt_email
zone = DesignateAdapter.parse('API_v2', zone, objects.Domain())
zone.validate()
# # TODO(ekarlso): Fix this once setter or so works.
# masters = values.pop('masters', [])
# zone = objects.Domain.from_dict(values)
# zone.set_masters(masters)
if zone.type == 'SECONDARY':
mgmt_email = CONF['service:central'].managed_resource_email
zone['email'] = mgmt_email
# Create the zone
zone = self.central_api.create_domain(context, zone)

View File

@ -52,7 +52,7 @@ class Domain(base.DictObjectMixin, base.SoftDeleteObjectMixin,
'format': 'email',
'maxLength': 255
},
'required': True
'required': False
},
'ttl': {
'schema': {
@ -177,19 +177,52 @@ class Domain(base.DictObjectMixin, base.SoftDeleteObjectMixin,
return srv
return False
def _raise(self, errors):
if len(errors) != 0:
raise exceptions.InvalidObject(
"Provided object does not match "
"schema", errors=errors, object=self)
def validate(self):
try:
if self.type == 'SECONDARY' and self.masters is None:
errors = ValidationErrorList()
errors = ValidationErrorList()
if self.type == 'PRIMARY':
if self.obj_attr_is_set('masters') and len(self.masters) != 0:
e = ValidationError()
e.path = ['type']
e.validator = 'maxItems'
e.validator_value = ['masters']
e.message = "'masters' has more items than allowed"
errors.append(e)
if self.email is None:
e = ValidationError()
e.path = ['type']
e.validator = 'required'
e.validator_value = ['masters']
e.message = "'masters' is a required property"
e.validator_value = 'email'
e.message = "'email' is a required property"
errors.append(e)
raise exceptions.InvalidObject(
"Provided object does not match "
"schema", errors=errors, object=self)
self._raise(errors)
try:
if self.type == 'SECONDARY':
if self.masters is None or len(self.masters) == 0:
e = ValidationError()
e.path = ['type']
e.validator = 'required'
e.validator_value = ['masters']
e.message = "'masters' is a required property"
errors.append(e)
for i in ['email', 'ttl']:
if i in self.obj_what_changed():
e = ValidationError()
e.path = ['type']
e.validator = 'not_allowed'
e.validator_value = i
e.message = "'%s' can't be specified when type is " \
"SECONDARY" % i
errors.append(e)
self._raise(errors)
super(Domain, self).validate()
except exceptions.RelationNotLoaded as ex:
@ -200,9 +233,7 @@ class Domain(base.DictObjectMixin, base.SoftDeleteObjectMixin,
e.validator_value = [ex.relation]
e.message = "'%s' is a required property" % ex.relation
errors.append(e)
raise exceptions.InvalidObject(
"Provided object does not match "
"schema", errors=errors, object=self)
self._raise(errors)
class DomainList(base.ListObjectMixin, base.DesignateObject,

View File

@ -20,6 +20,7 @@ import oslo_messaging as messaging
from oslo_log import log as logging
from designate import exceptions
from designate import objects
from designate.central import service as central_service
from designate.mdns import rpcapi as mdns_api
from designate.tests.test_api.test_v2 import ApiV2TestCase
@ -519,10 +520,12 @@ class ApiV2ZonesTest(ApiV2TestCase):
def test_update_secondary(self):
# Create a zone
fixture = self.get_domain_fixture('SECONDARY', 0)
fixture['email'] = cfg.CONF['service:central'].managed_resource_email
domain = objects.Domain(**fixture)
domain.email = cfg.CONF['service:central'].managed_resource_email
# Create a zone
zone = self.create_domain(**fixture)
zone = self.central_service.create_domain(self.admin_context, domain)
masters = ['10.0.0.1', '10.0.0.2']

View File

@ -89,3 +89,68 @@ class DomainTest(oslotest.base.BaseTestCase):
)
with testtools.ExpectedException(exceptions.InvalidObject):
domain.validate()
def test_validate_primary_with_masters(self):
masters = objects.DomainMasterList()
masters.append(objects.DomainMaster.from_data("10.0.0.1:53"))
domain = objects.Domain(
name='example.com.',
type='PRIMARY',
email="foo@example.com",
masters=masters
)
with testtools.ExpectedException(exceptions.InvalidObject):
domain.validate()
def test_validate_primary_no_email(self):
masters = objects.DomainMasterList()
masters.append(objects.DomainMaster.from_data("10.0.0.1:53"))
domain = objects.Domain(
name='example.com.',
type='PRIMARY',
)
with testtools.ExpectedException(exceptions.InvalidObject):
domain.validate()
def test_validate_secondary_with_email(self):
masters = objects.DomainMasterList()
masters.append(objects.DomainMaster.from_data("10.0.0.1:53"))
domain = objects.Domain(
name='example.com.',
type='SECONDARY',
email="foo@example.com",
masters=masters
)
with testtools.ExpectedException(exceptions.InvalidObject):
domain.validate()
def test_validate_secondary_with_ttl(self):
masters = objects.DomainMasterList()
masters.append(objects.DomainMaster.from_data("10.0.0.1:53"))
domain = objects.Domain(
name='example.com.',
type='SECONDARY',
ttl=600,
masters=masters
)
with testtools.ExpectedException(exceptions.InvalidObject):
domain.validate()
def test_validate_secondary_with_masters_empty_list(self):
masters = objects.DomainMasterList()
domain = objects.Domain(
name='example.com.',
type='SECONDARY',
masters=masters
)
with testtools.ExpectedException(exceptions.InvalidObject):
domain.validate()
def test_validate_secondary_with_masters_none(self):
domain = objects.Domain(
name='example.com.',
type='SECONDARY',
masters=None
)
with testtools.ExpectedException(exceptions.InvalidObject):
domain.validate()