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
This commit is contained in:
Daniel P. Berrange 2016-01-18 17:28:53 +00:00
parent 11af752a5a
commit d8b14f110e
6 changed files with 93 additions and 27 deletions

View File

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

View File

@ -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())

View File

@ -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'),
}

View File

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

37
os_vif/objects/route.py Normal file
View File

@ -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'),
}

View File

@ -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):