Fix db purge type validation

Moved validation for age_in_days and max_rows from
glance.db.sqlalchemy.api to glance.cmd.manage since
code from purge_deleted_rows() method of
glance.db.sqlalchemy.api is unreachable as it is
throwing ValueError exception for invalid input from
glance.cmd.manage itself.

Related-Bug: #1543937
Change-Id: I4e976fde2a78fd901c233966adc350a0ded41841
This commit is contained in:
dineshbhor 2016-03-15 04:12:26 +00:00
parent e0ba0c4fd0
commit 371043656b
3 changed files with 27 additions and 18 deletions

View File

@ -157,8 +157,18 @@ class DbCommands(object):
help='Limit number of records to delete')
def purge(self, age_in_days=30, max_rows=100):
"""Purge deleted rows older than a given age from glance tables."""
age_in_days = int(age_in_days)
max_rows = int(max_rows)
try:
age_in_days = int(age_in_days)
except ValueError:
sys.exit(_("Invalid int value for age_in_days: "
"%(age_in_days)s") % {'age_in_days': age_in_days})
try:
max_rows = int(max_rows)
except ValueError:
sys.exit(_("Invalid int value for max_rows: "
"%(max_rows)s") % {'max_rows': max_rows})
if age_in_days <= 0:
sys.exit(_("Must supply a positive, non-zero value for age."))
if age_in_days >= (int(time.time()) / 86400):

View File

@ -54,7 +54,7 @@ from glance.db.sqlalchemy.metadef_api import property as metadef_property_api
from glance.db.sqlalchemy.metadef_api import tag as metadef_tag_api
from glance.db.sqlalchemy import models
from glance import glare as ga
from glance.i18n import _, _LW, _LE, _LI
from glance.i18n import _, _LW, _LI
BASE = models.BASE
sa_logger = None
@ -1273,21 +1273,6 @@ def purge_deleted_rows(context, age_in_days, max_rows, session=None):
Deletes rows of table images, table tasks and all dependent tables
according to given age for relevant models.
"""
try:
age_in_days = int(age_in_days)
except ValueError:
LOG.exception(_LE('Invalid value for age, %(age)d'),
{'age': age_in_days})
raise exception.InvalidParameterValue(value=age_in_days,
param='age_in_days')
try:
max_rows = int(max_rows)
except ValueError:
LOG.exception(_LE('Invalid value for max_rows, %(max_rows)d'),
{'max_rows': max_rows})
raise exception.InvalidParameterValue(value=max_rows,
param='max_rows')
session = session or get_session()
metadata = MetaData(get_engine())
deleted_age = timeutils.utcnow() - datetime.timedelta(days=age_in_days)

View File

@ -42,3 +42,17 @@ class DBCommandsTestCase(test_utils.BaseTestCase):
def test_purge_command_negative_rows(self):
exit = self.assertRaises(SystemExit, self.commands.purge, 1, -1)
self.assertEqual("Minimal rows limit is 1.", exit.code)
def test_purge_invalid_age_in_days(self):
age_in_days = 'abcd'
ex = self.assertRaises(SystemExit, self.commands.purge, age_in_days)
expected = ("Invalid int value for age_in_days: "
"%(age_in_days)s") % {'age_in_days': age_in_days}
self.assertEqual(expected, ex.code)
def test_purge_invalid_max_rows(self):
max_rows = 'abcd'
ex = self.assertRaises(SystemExit, self.commands.purge, 1, max_rows)
expected = ("Invalid int value for max_rows: "
"%(max_rows)s") % {'max_rows': max_rows}
self.assertEqual(expected, ex.code)