Sort security group rules more meaningfully

At now, security gorup rules are sorted based on ip_protocol and
from_port by default, but there are more higher level classification
like direction and ethertype (like IPv4, IPv6).
This commit improves the default sort order in more meaningful order.

Also improves the handling of IP protocol which can be a string,
an integer or None. Sorting in python 3 assumes all corresponding
values have a same type, so we need to ensure IP protocols in all rules
have a same type, so IP protocol fields are converted into a string
before sorting.

Change-Id: Ia45830e9953e92141a3ce91a78dd338960b5ac11
Closes-Bug: #1264738
This commit is contained in:
Akihiro Motoki 2017-10-10 11:32:46 +00:00
parent cc2c5f9f14
commit 1afba5f082
1 changed files with 14 additions and 2 deletions

View File

@ -58,8 +58,20 @@ class DetailView(tables.DataTableView):
data = self._get_data()
if data is None:
return []
return sorted(data.rules, key=lambda rule: (rule.ip_protocol or '',
rule.from_port or 0))
def _sort_key(rule):
return (
rule.direction or '',
rule.ethertype or '',
# IP protocol can be a string, an integer or None,
# so we need to normalize into string
# to make sorting work with py3
str(rule.ip_protocol) if rule.ip_protocol is not None else '',
rule.from_port or 0,
rule.to_port or 0,
)
return sorted(data.rules, key=_sort_key)
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)