| Conditions | 12 |
| Total Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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:
Complex classes like create_request() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
| 47 | def create_request(liveaction): |
||
| 48 | """ |
||
| 49 | Create an action execution. |
||
| 50 | |||
| 51 | :return: (liveaction, execution) |
||
| 52 | :rtype: tuple |
||
| 53 | """ |
||
| 54 | # Use the user context from the parent action execution. Subtasks in a workflow |
||
| 55 | # action can be invoked by a system user and so we want to use the user context |
||
| 56 | # from the original workflow action. |
||
| 57 | parent_context = executions.get_parent_context(liveaction) |
||
| 58 | if parent_context: |
||
| 59 | parent_user = parent_context.get('user', None) |
||
| 60 | if parent_user: |
||
| 61 | liveaction.context['user'] = parent_user |
||
| 62 | |||
| 63 | # Validate action. |
||
| 64 | action_db = action_utils.get_action_by_ref(liveaction.action) |
||
| 65 | if not action_db: |
||
| 66 | raise ValueError('Action "%s" cannot be found.' % liveaction.action) |
||
| 67 | if not action_db.enabled: |
||
| 68 | raise ValueError('Unable to execute. Action "%s" is disabled.' % liveaction.action) |
||
| 69 | |||
| 70 | runnertype_db = action_utils.get_runnertype_by_name(action_db.runner_type['name']) |
||
| 71 | |||
| 72 | if not hasattr(liveaction, 'parameters'): |
||
| 73 | liveaction.parameters = dict() |
||
| 74 | |||
| 75 | # Validate action parameters. |
||
| 76 | schema = util_schema.get_schema_for_action_parameters(action_db) |
||
| 77 | validator = util_schema.get_validator() |
||
| 78 | util_schema.validate(liveaction.parameters, schema, validator, use_default=True, |
||
| 79 | allow_default_none=True) |
||
| 80 | |||
| 81 | # validate that no immutable params are being overriden. Although possible to |
||
| 82 | # ignore the override it is safer to inform the user to avoid surprises. |
||
| 83 | immutables = _get_immutable_params(action_db.parameters) |
||
| 84 | immutables.extend(_get_immutable_params(runnertype_db.runner_parameters)) |
||
| 85 | overridden_immutables = [p for p in six.iterkeys(liveaction.parameters) if p in immutables] |
||
| 86 | if len(overridden_immutables) > 0: |
||
| 87 | raise ValueError('Override of immutable parameter(s) %s is unsupported.' |
||
| 88 | % str(overridden_immutables)) |
||
| 89 | |||
| 90 | # Set notification settings for action. |
||
| 91 | # XXX: There are cases when we don't want notifications to be sent for a particular |
||
| 92 | # execution. So we should look at liveaction.parameters['notify'] |
||
| 93 | # and not set liveaction.notify. |
||
| 94 | if not _is_notify_empty(action_db.notify): |
||
| 95 | liveaction.notify = action_db.notify |
||
| 96 | |||
| 97 | # Write to database and send to message queue. |
||
| 98 | liveaction.status = action_constants.LIVEACTION_STATUS_REQUESTED |
||
| 99 | liveaction.start_timestamp = date_utils.get_datetime_utc_now() |
||
| 100 | |||
| 101 | # Publish creation after both liveaction and actionexecution are created. |
||
| 102 | liveaction = LiveAction.add_or_update(liveaction, publish=False) |
||
| 103 | |||
| 104 | # Get trace_db if it exists. This could throw. If it throws, we have to cleanup |
||
| 105 | # liveaction object so we don't see things in requested mode. |
||
| 106 | trace_db = None |
||
| 107 | try: |
||
| 108 | _, trace_db = trace_service.get_trace_db_by_live_action(liveaction) |
||
| 109 | except StackStormDBObjectNotFoundError as e: |
||
| 110 | _cleanup_liveaction(liveaction) |
||
| 111 | raise TraceNotFoundException(str(e)) |
||
| 112 | |||
| 113 | execution = executions.create_execution_object(liveaction, publish=False) |
||
| 114 | |||
| 115 | if trace_db: |
||
| 116 | trace_service.add_or_update_given_trace_db( |
||
| 117 | trace_db=trace_db, |
||
| 118 | action_executions=[ |
||
| 119 | trace_service.get_trace_component_for_action_execution(execution, liveaction) |
||
| 120 | ]) |
||
| 121 | |||
| 122 | return liveaction, execution |
||
| 123 | |||
| 232 |