From d8b14f110e0652913885c5f03aacc01d278dfe79 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Mon, 18 Jan 2016 17:28:53 +0000 Subject: [PATCH] Fix definition of subnet object to not be untyped strings The subnet object fields are currently all just untyped strings. These should all be formally defined objects or fields with type checking Change-Id: Id6ee8b1e89bf3e292406b2c7a4b6f8686e8e0da7 --- os_vif/objects/__init__.py | 2 ++ os_vif/objects/fields.py | 4 ++++ os_vif/objects/fixed_ip.py | 38 ++++++++++++++++++++++++++++++++++++++ os_vif/objects/network.py | 6 ++++-- os_vif/objects/route.py | 37 +++++++++++++++++++++++++++++++++++++ os_vif/objects/subnet.py | 33 ++++++++------------------------- 6 files changed, 93 insertions(+), 27 deletions(-) create mode 100644 os_vif/objects/fixed_ip.py create mode 100644 os_vif/objects/route.py diff --git a/os_vif/objects/__init__.py b/os_vif/objects/__init__.py index aaae7c92..70eb4c3c 100644 --- a/os_vif/objects/__init__.py +++ b/os_vif/objects/__init__.py @@ -12,7 +12,9 @@ def register_all(): + __import__('os_vif.objects.fixed_ip') __import__('os_vif.objects.instance_info') __import__('os_vif.objects.network') + __import__('os_vif.objects.route') __import__('os_vif.objects.subnet') __import__('os_vif.objects.vif') diff --git a/os_vif/objects/fields.py b/os_vif/objects/fields.py index ed048342..6c4a09f3 100644 --- a/os_vif/objects/fields.py +++ b/os_vif/objects/fields.py @@ -62,3 +62,7 @@ class VIFVHostUserMode(fields.Enum): class VIFVHostUserModeField(fields.BaseEnumField): AUTO_TYPE = VIFVHostUserMode() + + +class ListOfIPAddressField(fields.AutoTypedField): + AUTO_TYPE = fields.List(fields.IPAddress()) diff --git a/os_vif/objects/fixed_ip.py b/os_vif/objects/fixed_ip.py new file mode 100644 index 00000000..f64e0fc9 --- /dev/null +++ b/os_vif/objects/fixed_ip.py @@ -0,0 +1,38 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from oslo_versionedobjects import base +from oslo_versionedobjects import fields + +from os_vif.objects import fields as osv_fields + + +@base.VersionedObjectRegistry.register +class FixedIP(base.VersionedObject): + """Represents a fixed IP.""" + # Version 1.0: Initial version + VERSION = '1.0' + + fields = { + 'address': fields.IPAddressField(), + 'floating_ips': osv_fields.ListOfIPAddressField(), + } + + +@base.VersionedObjectRegistry.register +class FixedIPList(base.VersionedObject, base.ObjectListBase): + # Version 1.0: Initial version + VERSION = '1.0' + + fields = { + 'objects': fields.ListOfObjectsField('FixedIP'), + } diff --git a/os_vif/objects/network.py b/os_vif/objects/network.py index 9308a490..3eeca2f6 100644 --- a/os_vif/objects/network.py +++ b/os_vif/objects/network.py @@ -13,6 +13,8 @@ from oslo_versionedobjects import base from oslo_versionedobjects import fields +from os_vif import objects + @base.VersionedObjectRegistry.register class Network(base.VersionedObject): @@ -24,7 +26,7 @@ class Network(base.VersionedObject): 'id': fields.UUIDField(), 'bridge': fields.StringField(), 'label': fields.StringField(), - 'subnets': fields.ObjectField('SubnetList', nullable=True), + 'subnets': fields.ObjectField('SubnetList'), 'multi_host': fields.BooleanField(), 'should_provide_bridge': fields.BooleanField(), 'should_provide_vlan': fields.BooleanField(), @@ -39,7 +41,7 @@ class Network(base.VersionedObject): } def __init__(self, **kwargs): - kwargs.setdefault('subnets', []) + kwargs.setdefault('subnets', objects.subnet.SubnetList(objects=[])) kwargs.setdefault('multi_host', False) kwargs.setdefault('should_provide_bridge', False) kwargs.setdefault('should_provide_vlan', False) diff --git a/os_vif/objects/route.py b/os_vif/objects/route.py new file mode 100644 index 00000000..dface418 --- /dev/null +++ b/os_vif/objects/route.py @@ -0,0 +1,37 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from oslo_versionedobjects import base +from oslo_versionedobjects import fields + + +@base.VersionedObjectRegistry.register +class Route(base.VersionedObject): + """Represents a route.""" + # Version 1.0: Initial version + VERSION = '1.0' + + fields = { + 'cidr': fields.IPNetworkField(), + 'gateway': fields.IPAddressField(), + 'interface': fields.StringField(), + } + + +@base.VersionedObjectRegistry.register +class RouteList(base.VersionedObject, base.ObjectListBase): + # Version 1.0: Initial version + VERSION = '1.0' + + fields = { + 'objects': fields.ListOfObjectsField('Route'), + } diff --git a/os_vif/objects/subnet.py b/os_vif/objects/subnet.py index 11e4f303..d293e36a 100644 --- a/os_vif/objects/subnet.py +++ b/os_vif/objects/subnet.py @@ -10,11 +10,11 @@ # License for the specific language governing permissions and limitations # under the License. -import netaddr - from oslo_versionedobjects import base from oslo_versionedobjects import fields +from os_vif.objects import fields as osv_fields + @base.VersionedObjectRegistry.register class Subnet(base.VersionedObject): @@ -23,31 +23,14 @@ class Subnet(base.VersionedObject): VERSION = '1.0' fields = { - 'cidr': fields.StringField(nullable=True), - 'dns': fields.ListOfStringsField(), - 'gateway': fields.StringField(), - 'ips': fields.ListOfStringsField(), - 'routes': fields.ListOfStringsField(), - 'version': fields.IntegerField(nullable=True), + 'cidr': fields.IPNetworkField(), + 'dns': osv_fields.ListOfIPAddressField(), + 'gateway': fields.IPAddressField(), + 'ips': fields.ObjectField("FixedIPList"), + 'routes': fields.ObjectField("RouteList"), + 'dhcp_server': fields.IPAddressField(), } - def __init__(self, cidr=None, dns=None, gateway=None, ips=None, - routes=None, **kwargs): - - dns = dns or set() - ips = ips or set() - routes = routes or set() - version = kwargs.pop('version', None) - - if cidr and not version: - version = netaddr.IPNetwork(cidr).version - super(Subnet, self).__init__(cidr=cidr, dns=dns, gateway=gateway, - ips=ips, routes=routes, version=version) - - def as_netaddr(self): - """Convenience function to get cidr as a netaddr object.""" - return netaddr.IPNetwork(self.cidr) - @base.VersionedObjectRegistry.register class SubnetList(base.VersionedObject, base.ObjectListBase):