[openstack-dev] [quantum] "as_dic" method to be supported by QuantumBase (_make_<db_object>_dict)
Avishay Balderman
AvishayB at Radware.com
Mon Jan 7 12:54:05 UTC 2013
Hi
A common pattern that I found when looking at the DB layer is that after a we fetch an object from DB we call helper method that convert this object into a dict.
I have seen few "sub patterns"
1) 1:1 mapping : (Example 0) every attribute of the db object is copied "as is" into the dict
2) 1:1 mapping with "lazy" collections (Example 1) : every attribute of the db object is copied "as is" into the dict. The objects in the "joins" are not copied. Only their id's is copied into the dict
3) 1:1 mapping with "lazy" collections and some naming modifications (Example 2) : almost every attribute of the db object is copied "as is" into the dict. The objects in the "joins" are not copied. Only their id's is copied into the dict. Some of the attributes names are changed during the conversion.
Since it is a common pattern I think that the utility method "_make_XYZ_dict" should be a method in the super class QuantumBase.
It can come with a default implementation that can cover examples 1 and 2.
Sub classes that needs "special" implementation will be able to override it.
The method in the base class will be able to cover the _fields (see below) method as well.
Thanks
Avishay
Example 0:
-----------
def _make_member_dict(self, member, fields=None):
res = {'id': member['id'],
'tenant_id': member['tenant_id'],
'pool_id': member['pool_id'],
'address': member['address'],
'port': member['port'],
'weight': member['weight'],
'admin_state_up': member['admin_state_up'],
'status': member['status']}
return self._fields(res, fields)
Example 1:
-----------
def _make_network_dict(self, network, fields=None):
res = {'id': network['id'],
'name': network['name'],
'tenant_id': network['tenant_id'],
'admin_state_up': network['admin_state_up'],
'status': network['status'],
'shared': network['shared'],
'subnets': [subnet['id']
for subnet in network['subnets']]}
Example 2:
----------
def _make_subnet_dict(self, subnet, fields=None):
res = {'id': subnet['id'],
'name': subnet['name'],
'tenant_id': subnet['tenant_id'],
'network_id': subnet['network_id'],
'ip_version': subnet['ip_version'],
'cidr': subnet['cidr'],
'allocation_pools': [{'start': pool['first_ip'],
'end': pool['last_ip']}
for pool in subnet['allocation_pools']],
'gateway_ip': subnet['gateway_ip'],
'enable_dhcp': subnet['enable_dhcp'],
'dns_nameservers': [dns['address']
for dns in subnet['dns_nameservers']],
'host_routes': [{'destination': route['destination'],
'nexthop': route['nexthop']}
for route in subnet['routes']],
'shared': subnet['shared']
}
if subnet['gateway_ip']:
res['gateway_ip'] = subnet['gateway_ip']
return self._fields(res, fields)
_fields:
---------
def _fields(self, resource, fields):
if fields:
return dict(((key, item) for key, item in resource.iteritems()
if key in fields))
return resource
More information about the OpenStack-dev
mailing list