neutron资源添加属性

理论上来说neutron client和neutron server是分离的,server端添加属性,client端无影响,只是show或不show而已,但有时候添加了属性,client用-c参数也show不出来。这时候一般是server添加属性的时候拉下地方了。

举例firewall来阐述一下添加属性步骤如下:

  • 数据库增加一列attr_new

这步比较简单,轻松搞定

  • 修改Firewall对象,增加一列,在firewall_db.py里
1
2
3
4
5
6
7
8
9
10
11
12
13
class Firewall(model_base.BASEV2, models_v2.HasId, models_v2.HasTenant):
"""Represents a Firewall resource."""
__tablename__ = 'firewalls'
name = sa.Column(sa.String(255))
description = sa.Column(sa.String(1024))
shared = sa.Column(sa.Boolean)
admin_state_up = sa.Column(sa.Boolean)
status = sa.Column(sa.String(16))
firewall_policy_id = sa.Column(sa.String(36),
sa.ForeignKey('firewall_policies.id'),
nullable=True)
creator = sa.Column(sa.String(255))
attr_new = sa.Column(sa.String(255)) # new attribute
  • 修改show的地方即,get_firewalls函数,其实主要是函数_make_firewall_dict
1
2
3
4
5
6
7
8
9
10
11
12
def _make_firewall_dict(self, fw, fields=None):
res = {'id': fw['id'],
'tenant_id': fw['tenant_id'],
'name': fw['name'],
'description': fw['description'],
'shared': fw['shared'],
'admin_state_up': fw['admin_state_up'],
'status': fw['status'],
'firewall_policy_id': fw['firewall_policy_id'],
'creator': fw['creator']
'attr_new': fw['attr_new']}
return self._fields(res, fields)
  • 别以为完事了,最重要的是修改plugin的RESOURCE_ATTRIBUTE_MAP,这个是每个plugin/service给api的接口,来介绍自己的属性列表
1
2
3
4
5
6
7
8
9
10
11
12
'firewalls': {
'id': {'allow_post': False, 'allow_put': False,
'validate': {'type:uuid': None},
'is_visible': True,
'primary_key': True},
'tenant_id': {'allow_post': True, 'allow_put': False,
'required_by_policy': True,
'is_visible': True},
'name': {'allow_post': True, 'allow_put': True,
'validate': {'type:string': None},
'attr_new': {'allow_post': True, 'allow_put': True,
'validate': {'type:string': None},

好了,大功告成

如果想用neutron client自动show firewall的新属性 则

1
2
3
4
5
6
7
8
9
10

25 class ListFirewall(neutronv20.ListCommand):
26 """List firewalls that belong to a given tenant."""
27
28 resource = 'firewall'
29 list_columns = ['id', 'name', 'firewall_policy_id', 'attr_new']
30 _formatters = {}
31 pagination_support = True
32 sorting_support = True