Conditions | 9 |
Total Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
62 | def validate_trigger_parameters(trigger_type_ref, parameters): |
||
63 | """ |
||
64 | This function validates parameters for system and user-defined triggers. |
||
65 | |||
66 | :param trigger_type_ref: Reference of a trigger type. |
||
67 | :type trigger_type_ref: ``str`` |
||
68 | |||
69 | :param parameters: Trigger parameters. |
||
70 | :type parameters: ``dict`` |
||
71 | |||
72 | :return: Cleaned parameters on success, None if validation is not performed. |
||
73 | """ |
||
74 | if not trigger_type_ref: |
||
75 | return None |
||
76 | |||
77 | is_system_trigger = trigger_type_ref in SYSTEM_TRIGGER_TYPES |
||
78 | if is_system_trigger: |
||
79 | # System trigger |
||
80 | parameters_schema = SYSTEM_TRIGGER_TYPES[trigger_type_ref]['parameters_schema'] |
||
81 | else: |
||
82 | trigger_type_db = triggers.get_trigger_type_db(trigger_type_ref) |
||
83 | if not trigger_type_db: |
||
84 | # Trigger doesn't exist in the database |
||
85 | return None |
||
86 | |||
87 | parameters_schema = getattr(trigger_type_db, 'parameters_schema', {}) |
||
88 | if not parameters_schema: |
||
89 | # Parameters schema not defined for the this trigger |
||
90 | return None |
||
91 | |||
92 | # We only validate non-system triggers if config option is set (enabled) |
||
93 | if not is_system_trigger and not cfg.CONF.system.validate_trigger_parameters: |
||
94 | LOG.debug('Got non-system trigger "%s", but trigger parameter validation for non-system' |
||
|
|||
95 | 'triggers is disabled, skipping validation.' % (trigger_type_ref)) |
||
96 | return None |
||
97 | |||
98 | cleaned = util_schema.validate(instance=parameters, schema=parameters_schema, |
||
99 | cls=util_schema.CustomValidator, use_default=True, |
||
100 | allow_default_none=True) |
||
101 | |||
102 | # Additional validation for CronTimer trigger |
||
103 | # TODO: If we need to add more checks like this we should consider abstracting this out. |
||
104 | if trigger_type_ref == CRON_TIMER_TRIGGER_REF: |
||
105 | # Validate that the user provided parameters are valid. This is required since JSON schema |
||
106 | # allows arbitrary strings, but not any arbitrary string is a valid CronTrigger argument |
||
107 | # Note: Constructor throws ValueError on invalid parameters |
||
108 | try: |
||
109 | CronTrigger(**parameters) |
||
110 | except Exception as e: |
||
111 | msg = ('Cron trigger parameters do not match expected format.') |
||
112 | msg += " Validation error: %s" % e.message |
||
113 | LOG.exception(msg) |
||
114 | raise TriggerParametersValidationException(msg) |
||
115 | |||
116 | return cleaned |
||
117 | |||
160 |