Add sorting for releases collection
Implement sorting for ReleaseCollection in /api/releases Sorting criteria: 1) Fuel version 2) OpenStack release 3) Operating system. Ubuntu is preferred Change-Id: I6677f449b4435aa57c7e4f1c89a292284014d61c Closes-Bug: 1377920
This commit is contained in:
parent
acaad557b7
commit
a24ffc80d5
|
@ -19,6 +19,7 @@ Handlers dealing with releases
|
|||
"""
|
||||
|
||||
from nailgun.api.v1.handlers.base import CollectionHandler
|
||||
from nailgun.api.v1.handlers.base import content_json
|
||||
from nailgun.api.v1.handlers.base import SingleHandler
|
||||
|
||||
from nailgun.api.v1.validators.release import ReleaseValidator
|
||||
|
@ -41,3 +42,11 @@ class ReleaseCollectionHandler(CollectionHandler):
|
|||
|
||||
validator = ReleaseValidator
|
||||
collection = ReleaseCollection
|
||||
|
||||
@content_json
|
||||
def GET(self):
|
||||
""":returns: Sorted releases' collection in JSON format
|
||||
:http: * 200 (OK)
|
||||
"""
|
||||
q = sorted(self.collection.all(), reverse=True)
|
||||
return self.collection.to_json(q)
|
||||
|
|
|
@ -110,3 +110,53 @@ class Release(Base):
|
|||
)
|
||||
db().add(new_role)
|
||||
added_roles.append(role)
|
||||
|
||||
@property
|
||||
def openstack_version(self):
|
||||
return self.version.split('-')[0]
|
||||
|
||||
@property
|
||||
def fuel_version(self):
|
||||
try:
|
||||
version = self.version.split('-')[1]
|
||||
except IndexError:
|
||||
version = ''
|
||||
|
||||
return version
|
||||
|
||||
@property
|
||||
def os_weight(self):
|
||||
try:
|
||||
weight = consts.RELEASE_OS[::-1].index(self.operating_system)
|
||||
except ValueError:
|
||||
weight = -1
|
||||
|
||||
return weight
|
||||
|
||||
def __cmp__(self, other):
|
||||
"""Allows to compare two releases
|
||||
|
||||
:other: an instance of nailgun.db.sqlalchemy.models.release.Release
|
||||
"""
|
||||
if self.fuel_version < other.fuel_version:
|
||||
return -1
|
||||
if self.fuel_version > other.fuel_version:
|
||||
return 1
|
||||
|
||||
if self.openstack_version < other.openstack_version:
|
||||
return -1
|
||||
if self.openstack_version > other.openstack_version:
|
||||
return 1
|
||||
|
||||
if self.os_weight == other.os_weight == -1:
|
||||
if self.operating_system > other.operating_system:
|
||||
return -1
|
||||
if self.operating_system < other.operating_system:
|
||||
return 1
|
||||
else:
|
||||
if self.os_weight < other.os_weight:
|
||||
return -1
|
||||
if self.os_weight > other.os_weight:
|
||||
return 1
|
||||
|
||||
return 0
|
||||
|
|
|
@ -18,6 +18,7 @@ from nailgun.db.sqlalchemy.models import Release
|
|||
from nailgun.openstack.common import jsonutils
|
||||
from nailgun.test.base import BaseIntegrationTest
|
||||
from nailgun.test.base import reverse
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
class TestHandlers(BaseIntegrationTest):
|
||||
|
@ -257,3 +258,127 @@ class TestHandlers(BaseIntegrationTest):
|
|||
self.assertEqual(200, resp.status_code)
|
||||
self.assertEqual(1, len(resp.json_body))
|
||||
self.assertEqual(orch_data, resp.json_body[0]["orchestrator_data"])
|
||||
|
||||
|
||||
class ReleaseCollectionSortBaseTest(BaseIntegrationTest):
|
||||
releases = []
|
||||
expected = []
|
||||
|
||||
def setUp(self):
|
||||
super(ReleaseCollectionSortBaseTest, self).setUp()
|
||||
for r in self.releases:
|
||||
self.env.create_release(**{
|
||||
'version': r[0],
|
||||
'operating_system': r[1],
|
||||
'name': 'release_name_{0}'.format(uuid4())
|
||||
})
|
||||
|
||||
def test_release_collection_order(self):
|
||||
resp = self.app.get(
|
||||
reverse('ReleaseCollectionHandler'),
|
||||
headers=self.default_headers
|
||||
).json_body
|
||||
|
||||
actual = [(r['version'], r['operating_system']) for r in resp]
|
||||
|
||||
self.assertEqual(actual, self.expected)
|
||||
|
||||
|
||||
class TestReleaseCollectionSorting(BaseIntegrationTest):
|
||||
releases = [
|
||||
("2014.3-7.0", "CentOS"),
|
||||
("2014.2-6.1", "CentOS"),
|
||||
("2014.2.2-6.0", "CentOS"),
|
||||
("2014.2-6.0", "Ubuntu"),
|
||||
("2014.2-6.0", "CentOS"),
|
||||
("2014.1-6.0", "Ubuntu"),
|
||||
("2014.2-5.1.1", "Ubuntu"),
|
||||
("2014.3", "Ubuntu"),
|
||||
("2014.3", "CentOS"),
|
||||
("2014.2-5.1.1", "CentOS"),
|
||||
("2014.1.3-5.1.1", "CentOS"),
|
||||
("2014.1-5.1.1", "Ubuntu"),
|
||||
("2014.2-6.1", "CentOS"),
|
||||
("2014.2.2-6.0", "CentOS"),
|
||||
("2013.2.1-5.1", "CentOS"),
|
||||
("2013.2-5.0", "CentOS"),
|
||||
("2014.3", "Ubuntu"),
|
||||
("2014.3", "CentOS"),
|
||||
("2013.2-4.0", "Ubuntu"),
|
||||
("2013.2", "CentOS"),
|
||||
("2013.2", "Ubuntu"),
|
||||
("2013.2.1-5.1", "Ubuntu"),
|
||||
]
|
||||
|
||||
expected = [
|
||||
("2014.3-7.0", "CentOS"),
|
||||
("2014.2-6.1", "CentOS"),
|
||||
("2014.2.2-6.0", "CentOS"),
|
||||
("2014.2-6.0", "Ubuntu"),
|
||||
("2014.2-6.0", "CentOS"),
|
||||
("2014.1-6.0", "Ubuntu"),
|
||||
("2014.2-5.1.1", "Ubuntu"),
|
||||
("2014.2-5.1.1", "CentOS"),
|
||||
("2014.1.3-5.1.1", "CentOS"),
|
||||
("2014.1-5.1.1", "Ubuntu"),
|
||||
("2013.2.1-5.1", "Ubuntu"),
|
||||
("2013.2.1-5.1", "CentOS"),
|
||||
("2013.2-5.0", "CentOS"),
|
||||
("2013.2-4.0", "Ubuntu"),
|
||||
("2014.3", "Ubuntu"),
|
||||
("2014.3", "CentOS"),
|
||||
("2013.2", "Ubuntu"),
|
||||
("2013.2", "CentOS"),
|
||||
]
|
||||
|
||||
|
||||
class TestReleaseCollectionSortByFuelVersion(ReleaseCollectionSortBaseTest):
|
||||
releases = [
|
||||
("-7.1", "WhateverLinux"),
|
||||
("-7.0", "WhateverLinux"),
|
||||
("-6.0", "WhateverLinux"),
|
||||
("-6", "WhateverLinux"),
|
||||
]
|
||||
|
||||
expected = [
|
||||
("-7.1", "WhateverLinux"),
|
||||
("-7.0", "WhateverLinux"),
|
||||
("-6.0", "WhateverLinux"),
|
||||
("-6", "WhateverLinux"),
|
||||
]
|
||||
|
||||
|
||||
class TestReleaseCollectionSortByOpenstack(ReleaseCollectionSortBaseTest):
|
||||
releases = [
|
||||
("2011.1-", "WhateverLinux"),
|
||||
("2014.3-", "WhateverLinux"),
|
||||
("2012.3-", "WhateverLinux"),
|
||||
("2013.4-", "WhateverLinux"),
|
||||
]
|
||||
|
||||
expected = [
|
||||
("2014.3-", "WhateverLinux"),
|
||||
("2013.4-", "WhateverLinux"),
|
||||
("2012.3-", "WhateverLinux"),
|
||||
("2011.1-", "WhateverLinux"),
|
||||
]
|
||||
|
||||
|
||||
class TestReleaseCollectionSortByOS(ReleaseCollectionSortBaseTest):
|
||||
releases = [
|
||||
("X", "Debian"),
|
||||
("X", "Fedora"),
|
||||
("X", "ArchLinux"),
|
||||
("X", "Ubuntu"),
|
||||
("X", "WhateverLinux"),
|
||||
("X", "CentOS"),
|
||||
]
|
||||
|
||||
expected = [
|
||||
("X", "Ubuntu"),
|
||||
("X", "CentOS"),
|
||||
("X", "ArchLinux"),
|
||||
("X", "Debian"),
|
||||
("X", "Fedora"),
|
||||
("X", "WhateverLinux"),
|
||||
]
|
||||
|
|
|
@ -41,15 +41,7 @@ define(['utils', 'deepModel'], function(utils) {
|
|||
models.Releases = Backbone.Collection.extend({
|
||||
constructorName: 'Releases',
|
||||
model: models.Release,
|
||||
url: '/api/releases',
|
||||
comparator: function(release1, release2) {
|
||||
var version1 = release1.get('version'), version2 = release2.get('version');
|
||||
if (version1 == version2) {
|
||||
var name1 = release1.get('name').toLowerCase(), name2 = release2.get('name').toLowerCase();
|
||||
return name1 > name2 ? 1 : name1 < name2 ? -1 : 0;
|
||||
}
|
||||
return version1 < version2 ? 1 : -1;
|
||||
}
|
||||
url: '/api/releases'
|
||||
});
|
||||
|
||||
models.Cluster = Backbone.Model.extend({
|
||||
|
|
Loading…
Reference in New Issue