[openstack-dev] [quantum] "as_dic" method to be supported by QuantumBase (_make_<db_object>_dict)

Zhongyue Luo zhongyue.nah at intel.com
Wed Jan 9 04:57:05 UTC 2013


+1

This was bothering me for a long time.

But I guess there must be some reason it had to be this way?


On Wed, Jan 9, 2013 at 12:15 PM, Maru Newby <mnewby at internap.com> wrote:

> Hi,
>
> I consider quantum's use of dicts instead of first-class model instances
> to be problematic, and the repeated compositional patterns are a symptom of
> that.  What do you think of updating the codebase to use objects instead of
> dicts, and relying on model-specific marshalling to and from wire formats
> like json or xml?
>
> Thanks,
>
>
> Maru
>
>
> On 2013-01-07, at 4:54 AM, Avishay Balderman <AvishayB at Radware.com> wrote:
>
> > 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
> >
> > _______________________________________________
> > OpenStack-dev mailing list
> > OpenStack-dev at lists.openstack.org
> > http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
>
>
> _______________________________________________
> OpenStack-dev mailing list
> OpenStack-dev at lists.openstack.org
> http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
>



-- 
*Intel SSG/SSD/SOTC/PRC/CITT*
880 Zixing Road, Zizhu Science Park, Minhang District, Shanghai, 200241,
China
+862161166500
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstack.org/pipermail/openstack-dev/attachments/20130109/62179982/attachment.html>


More information about the OpenStack-dev mailing list