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')