Revert "Add time format check in API"

This reverts commit c59f1aff8d.

Change-Id: Ie993b3ac521973984c04a7e5bb5d34999e6d811e
This commit is contained in:
Jiao Pengju 2018-01-02 07:55:33 +00:00 committed by jiaopengju
parent 2e369e76cc
commit f740efac08
9 changed files with 48 additions and 78 deletions

View File

@ -13,8 +13,6 @@
"""The triggers api."""
from datetime import datetime
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import uuidutils
from webob import exc
@ -30,7 +28,6 @@ from karbor.policies import triggers as trigger_policy
from karbor.services.operationengine import api as operationengine_api
from karbor import utils
CONF = cfg.CONF
LOG = logging.getLogger(__name__)
@ -96,16 +93,6 @@ class TriggersController(wsgi.Controller):
trigger_type = trigger_info.get("type", None)
trigger_property = trigger_info.get("properties", None)
trigger_format = trigger_property.get('format', None)
if trigger_format != CONF.time_format:
msg = _("Trigger format(%s) is invalid.") % trigger_format
raise exc.HTTPBadRequest(explanation=msg)
trigger_pattern = trigger_property.get('pattern', None)
if CONF.time_format == 'calendar':
utils.validate_calendar_time_format(trigger_pattern)
if CONF.time_format == 'crontab':
utils.validate_crontab_time_format(trigger_pattern)
trigger_property.setdefault(
'start_time', datetime.utcnow().replace(microsecond=0))
trigger_definition = {

View File

@ -73,14 +73,6 @@ global_opts = [
CONF.register_opts(global_opts)
global_trigger_opts = [
cfg.StrOpt('time_format',
default='calendar',
choices=['crontab', 'calendar'],
help='The type of time format which is used to compute time')
]
CONF.register_opts(global_trigger_opts)
service_client_opts = [
cfg.StrOpt('service_name',

View File

@ -65,7 +65,6 @@ _opts = [
karbor.common.config.core_opts,
karbor.common.config.debug_opts,
karbor.common.config.global_opts,
karbor.common.config.global_trigger_opts,
karbor.api.common.api_common_opts,
karbor.api.v1.protectables.query_instance_filters_opts,
karbor.api.v1.providers.query_provider_filters_opts,

View File

@ -26,6 +26,11 @@ time_trigger_opts = [
default=1800,
help='The maximum window time'),
cfg.StrOpt('time_format',
default='calendar',
choices=['crontab', 'calendar'],
help='The type of time format which is used to compute time'),
cfg.IntOpt('trigger_poll_interval',
default=15,
help='Interval, in seconds, in which Karbor will poll for '

View File

@ -14,11 +14,13 @@ import os
from datetime import timedelta
from dateutil import rrule
from icalendar import Calendar
from oslo_serialization import jsonutils
from oslo_utils import timeutils
from karbor import exception
from karbor.i18n import _
from karbor.services.operationengine.engine.triggers.timetrigger import \
timeformats
from karbor import utils
RATE = 2
@ -44,12 +46,21 @@ class ICal(timeformats.TimeFormat):
def __init__(self, start_time, pattern):
super(ICal, self).__init__(start_time, pattern)
cal = Calendar.from_ical(utils.decode_calendar_pattern(pattern))
cal = Calendar.from_ical(self._decode_calendar_pattern(pattern))
vevent = cal.walk('VEVENT')[0]
self.dtstart = start_time
self.min_freq = self._get_min_freq(vevent)
self.rrule_obj = self._get_rrule_obj(vevent, start_time)
@staticmethod
def _decode_calendar_pattern(pattern):
try:
pattern.index('\\')
pattern_dict = jsonutils.loads('{"pattern": "%s"}' % pattern)
return pattern_dict["pattern"]
except Exception:
return pattern
@staticmethod
def _get_rrule_obj(vevent, dtstart):
rrules = vevent.get('RRULE')
@ -74,7 +85,25 @@ class ICal(timeformats.TimeFormat):
:param pattern: The pattern of the icalendar time
"""
utils.validate_calendar_time_format(pattern)
try:
cal_obj = Calendar.from_ical(cls._decode_calendar_pattern(pattern))
except Exception:
msg = (_("The trigger pattern(%s) is invalid") % pattern)
raise exception.InvalidInput(msg)
try:
vevent = cal_obj.walk('VEVENT')[0]
except Exception:
msg = (_("The trigger pattern(%s) must include less than one "
"VEVENT component") % pattern)
raise exception.InvalidInput(msg)
try:
vevent.decoded('RRULE')
except Exception:
msg = (_("The first VEVENT component of trigger pattern(%s) must "
"include less than one RRULE property") % pattern)
raise exception.InvalidInput(msg)
def compute_next_time(self, current_time):
"""Compute next time

View File

@ -14,9 +14,10 @@ from croniter import croniter
from datetime import datetime
from oslo_utils import timeutils
from karbor import exception
from karbor.i18n import _
from karbor.services.operationengine.engine.triggers.timetrigger import \
timeformats
from karbor import utils
class Crontab(timeformats.TimeFormat):
@ -28,7 +29,15 @@ class Crontab(timeformats.TimeFormat):
@classmethod
def check_time_format(cls, pattern):
utils.validate_crontab_time_format(pattern)
if not pattern:
msg = (_("The trigger pattern is None"))
raise exception.InvalidInput(msg)
try:
croniter(pattern)
except Exception:
msg = (_("The trigger pattern(%s) is invalid") % pattern)
raise exception.InvalidInput(msg)
def compute_next_time(self, current_time):
time = current_time if current_time >= self._start_time else (

View File

@ -11,7 +11,6 @@
# under the License.
import mock
from oslo_config import cfg
from oslo_utils import uuidutils
from webob import exc
@ -52,7 +51,6 @@ class ScheduledOperationApiTest(base.TestCase):
self.ctxt = context.RequestContext('demo', 'fakeproject', True)
self.req = fakes.HTTPRequest.blank('/v1/scheduled_operations')
cfg.CONF.set_default('time_format', 'crontab')
trigger = self._create_trigger()
self._plan = self._create_plan(uuidutils.generate_uuid())
self.default_create_operation_param = {

View File

@ -10,8 +10,6 @@
# License for the specific language governing permissions and limitations
# under the License.
from datetime import datetime
from oslo_config import cfg
from webob import exc
from karbor.api.v1 import triggers as trigger_api
@ -47,7 +45,6 @@ class TriggerApiTest(base.TestCase):
self.controller.operationengine_api = FakeRemoteOperationApi()
self.ctxt = context.RequestContext('demo', 'fakeproject',
True)
cfg.CONF.set_default('time_format', 'crontab')
self.req = fakes.HTTPRequest.blank('/v1/triggers')
self.default_create_trigger_param = {
"name": "123",

View File

@ -19,12 +19,9 @@ import six
import tempfile
import webob.exc
from croniter import croniter
from icalendar import Calendar
from keystoneclient import discover as ks_discover
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import importutils
from oslo_utils import strutils
from oslo_utils import timeutils
@ -37,49 +34,6 @@ CONF = cfg.CONF
LOG = logging.getLogger(__name__)
def decode_calendar_pattern(pattern):
try:
pattern.index('\\')
pattern_dict = jsonutils.loads('{"pattern": "%s"}' % pattern)
return pattern_dict["pattern"]
except Exception:
return pattern
def validate_calendar_time_format(pattern):
try:
cal_obj = Calendar.from_ical(decode_calendar_pattern(pattern))
except Exception:
msg = (_("The trigger pattern(%s) is invalid") % pattern)
raise exception.InvalidInput(msg)
try:
vevent = cal_obj.walk('VEVENT')[0]
except Exception:
msg = (_("The trigger pattern(%s) must include less than one "
"VEVENT component") % pattern)
raise exception.InvalidInput(msg)
try:
vevent.decoded('RRULE')
except Exception:
msg = (_("The first VEVENT component of trigger pattern(%s) must "
"include less than one RRULE property") % pattern)
raise exception.InvalidInput(msg)
def validate_crontab_time_format(pattern):
if not pattern:
msg = (_("The trigger pattern is None"))
raise exception.InvalidInput(msg)
try:
croniter(pattern)
except Exception:
msg = (_("The trigger pattern(%s) is invalid") % pattern)
raise exception.InvalidInput(msg)
def find_config(config_path):
"""Find a configuration file using the given hint.