diff options
author | Graham Hayes <graham.hayes@hp.com> | 2015-04-13 12:17:42 +0200 |
---|---|---|
committer | Graham Hayes <graham.hayes@hp.com> | 2015-04-13 12:47:24 +0200 |
commit | 29869a648bcf56f6ec95001e169ca1aa327df8f1 (patch) | |
tree | dc3f1ddd2a1567f74bf69def3542ef2f6a41dde3 | |
parent | 83f6d28fdbc4ddb1bd21a7f4ac2d75fe6e1505dd (diff) | |
download | designate-29869a648bcf56f6ec95001e169ca1aa327df8f1.tar.gz |
Add validation of RRSet Type
Change-Id: I91861dbbd9f74edd76cb2d2c8cf3ac9507278aba
Closes-Bug: #1443348
-rw-r--r-- | designate/objects/recordset.py | 20 | ||||
-rw-r--r-- | designate/tests/test_api/test_v2/test_recordsets.py | 23 |
2 files changed, 40 insertions, 3 deletions
diff --git a/designate/objects/recordset.py b/designate/objects/recordset.py index a18e7c8e..1b6bbc7d 100644 --- a/designate/objects/recordset.py +++ b/designate/objects/recordset.py @@ -129,9 +129,24 @@ class RecordSet(base.DictObjectMixin, base.PersistentObjectMixin, def validate(self): + errors = ValidationErrorList() + # Get the right classes (e.g. A for Recordsets with type: 'A') - record_list_cls = self.obj_cls_from_name('%sList' % self.type) - record_cls = self.obj_cls_from_name(self.type) + try: + record_list_cls = self.obj_cls_from_name('%sList' % self.type) + record_cls = self.obj_cls_from_name(self.type) + except KeyError as e: + e = ValidationError() + e.path = ['recordset', 'type'] + e.validator = 'value' + e.validator_value = [self.type] + e.message = ("'%(type)s' is not a supported Record type" + % {'type': self.type}) + # Add it to the list for later + errors.append(e) + raise exceptions.InvalidObject( + "Provided object does not match " + "schema", errors=errors, object=self) # Get any rules that the record type imposes on the record changes = record_cls.get_recordset_schema_changes() @@ -142,7 +157,6 @@ class RecordSet(base.DictObjectMixin, base.PersistentObjectMixin, old_fields = deepcopy(self.FIELDS) self.FIELDS = utils.deep_dict_merge(self.FIELDS, changes) - errors = ValidationErrorList() error_indexes = [] # Copy these for safekeeping old_records = deepcopy(self.records) diff --git a/designate/tests/test_api/test_v2/test_recordsets.py b/designate/tests/test_api/test_v2/test_recordsets.py index 561c76df..84f2476e 100644 --- a/designate/tests/test_api/test_v2/test_recordsets.py +++ b/designate/tests/test_api/test_v2/test_recordsets.py @@ -107,6 +107,29 @@ class ApiV2RecordSetsTest(ApiV2TestCase): self._assert_exception( 'invalid_object', 400, self.client.post_json, url, body) + def test_create_recordset_with_invalid_type(self): + # Prepare a RecordSet fixture + body = self.get_recordset_fixture( + self.domain['name'], + 'A', + fixture=0, + values={ + 'name': 'name.%s' % self.domain['name'], + 'records': [ + '192.0.2.1', + '192.0.2.2', + ] + } + ) + + del body['type'] + + url = '/zones/%s/recordsets' % self.domain['id'] + + # Ensure it fails with a 400 + self._assert_exception( + 'invalid_object', 400, self.client.post_json, url, body) + def test_create_recordset_invalid_id(self): self._assert_invalid_uuid(self.client.post, '/zones/%s/recordsets') |