From 3921224b5e13d9393425b864d53ef942a7fc8959 Mon Sep 17 00:00:00 2001 From: Julian Sy Date: Fri, 28 Oct 2016 19:58:52 +0000 Subject: [PATCH] Create schema generation for AddressBase This creates a get_schema() method for AddressBase FieldType. implements bp json-schema-for-versioned-object Change-Id: Ifa8ec843aa933bdfcf12c4b2f553c6f7d4473cae --- nova/objects/fields.py | 3 +++ nova/tests/unit/objects/test_fields.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/nova/objects/fields.py b/nova/objects/fields.py index 9229a54f04e2..7631fda8c980 100644 --- a/nova/objects/fields.py +++ b/nova/objects/fields.py @@ -738,6 +738,9 @@ class AddressBase(FieldType): else: raise ValueError(_('Value must match %s') % obj.PATTERN) + def get_schema(self): + return {'type': ['string'], 'pattern': self.PATTERN} + class PCIAddress(AddressBase): PATTERN = '[a-f0-9]{4}:[a-f0-9]{2}:[a-f0-9]{2}.[a-f0-9]' diff --git a/nova/tests/unit/objects/test_fields.py b/nova/tests/unit/objects/test_fields.py index fbfb99b51637..9fc1fc8f0a4c 100644 --- a/nova/tests/unit/objects/test_fields.py +++ b/nova/tests/unit/objects/test_fields.py @@ -60,6 +60,14 @@ class FakeEnumAlt(fields.Enum): **kwargs) +class FakeAddress(fields.AddressBase): + PATTERN = '[a-z]+[0-9]+' + + +class FakeAddressField(fields.AutoTypedField): + AUTO_TYPE = FakeAddress() + + class FakeEnumField(fields.BaseEnumField): AUTO_TYPE = FakeEnum() @@ -621,3 +629,11 @@ class TestSecureBoot(TestField): def test_stringify_invalid(self): self.assertRaises(ValueError, self.field.stringify, 'enabled') + + +class TestSchemaGeneration(test.NoDBTestCase): + def test_address_base_get_schema(self): + field = FakeAddressField() + expected = {'type': ['string'], 'pattern': '[a-z]+[0-9]+', + 'readonly': False} + self.assertEqual(expected, field.get_schema())