Exemple #1
0
def get_action_class(action_name):
    """
    Gets the class that implements the specified action
    :param action_name: Name of the action
    :return: Class that implements the specified action. Raises an error if the class cant be found in the actions module of if
    the module contains an unexpected class name based on its filename
    """

    if action_name not in __actions:
        class_name = ACTION_CLASS.format(action_name)
        module_name = ACTION_MODULE_NAME.format(
            pascal_to_snake_case(class_name))
        try:
            mod = get_action_module(module_name)
        except Exception as ex:
            raise ImportError(
                ERR_NO_MODULE_FOR_ACTION.format(module_name, action_name, ex,
                                                ", ".join(all_actions())))

        cls = _get_action_class_from_module(mod)
        if cls is None or cls[0][0:-len(ACTION)] != action_name:
            raise ImportError(
                ERR_UNEXPECTED_ACTION_CLASS_IN_MODULE.format(
                    action_name, module_name, cls[0] if cls else "None"))
        __actions[action_name] = cls
    return __actions[action_name][1]
def get_module_for_handler(handler_name):
    """
    Gets the module for a handler using naming convention. First the name of the handler is capitalized and appended by the
    string "Handler". Then it is converted from camel to snake case to get the name of the module that will be loaded. Raises an
    ImportError exception if no module is found for the constructed module name
    :param handler_name:
    :return:
    """

    module_name = HANDLERS_MODULE_NAME.format(pascal_to_snake_case(handler_name))
    # noinspection PyPep8
    try:
        return _get_module(module_name)
    except:
        raise ImportError(ERR_NO_MODULE_FOR_HANDLER.format(module_name, handler_name, ", ".join(all_handlers())))
Exemple #3
0
def get_module_for_service(service_name):
    """
    Gets the module for a service using naming convention. First the name of the service is capitalized and appended by the
    string "Service". Then it is converted from camel to snake case to get the name of the module that will be loaded. Raises an
    ImportError exception if no module is found for the constructed module name
    :param service_name:
    :return:
    """

    name = service_name.capitalize()
    class_name = SERVICE_CLASS.format(name)
    module_name = SERVICE_MODULE_NAME.format(pascal_to_snake_case(class_name))
    try:
        return _get_module(module_name)
    except Exception as ex:
        raise ImportError(ERR_NO_MODULE_FOR_SERVICE.format(module_name, name, ", ".join(all_services())), ex)
 def get(self, name, default=None):
     return getattr(self, "_{}_".format(pascal_to_snake_case(name)),
                    default)
    def __init__(self, arguments, action_parameters):
        self._assumed_role_ = None
        self._context_ = None
        self._debug_ = None
        self._dryrun_ = None
        self._event_ = None
        self._events_ = None
        self._logger_ = None
        self._resources_ = None
        self._session_ = None
        self._stack_ = None
        self._stack_id_ = None
        self._stack_resources_ = None
        self._start_result_ = None
        self._started_at_ = None
        self._tagfilter_ = None
        self._task_ = None
        self._task_id_ = None
        self._task_timezone_ = None
        self._timeout_ = None
        self._timeout_event_ = None

        self._datetime_ = date_time_provider()

        for a in arguments:
            setattr(self, "_{}_".format(a), arguments[a])

        if not services.get_service_class(
                self._event_.get(actions.ACTION_SERVICE)).is_regional():
            self._region_ = self._session_.region_name
        else:
            action_properties = actions.get_action_properties(
                self._event_[actions.ACTION])
            aggregation_level = action_properties.get(
                actions.ACTION_AGGREGATION,
                actions.ACTION_AGGREGATION_RESOURCE)
            if aggregation_level is not None and isinstance(
                    aggregation_level, types.FunctionType):
                aggregation_level = aggregation_level(action_parameters)

            if aggregation_level in [
                    actions.ACTION_AGGREGATION_REGION,
                    actions.ACTION_AGGREGATION_RESOURCE
            ]:
                if isinstance(self._resources_, list):
                    if len(self._resources_) > 0:
                        self._region_ = self._resources_[0]["Region"]
                else:
                    if self._resources_ is not None:
                        self._region_ = self._resources_["Region"]
                if self._region_ is None:
                    self._region_ = self._session_.region_name

            else:
                self._region_ = self._session_.region_name

        self._account_ = self.get_account_for_task()

        if self._debug_ is None:
            self._debug_ = False

        if self._dryrun_ is None:
            self._dryrun_ = False

        for ap in action_parameters:
            setattr(self, "_{}_".format(pascal_to_snake_case(ap)),
                    action_parameters[ap])