Code Duplication    Length = 111-141 lines in 2 locations

gvm/protocols/gmpv9/gmpv9.py 1 location

@@ 985-1095 (lines=111) @@
982
983
        return self._send_xml_command(cmd)
984
985
    def __create_task(
986
        self,
987
        name: str,
988
        config_id: str,
989
        target_id: str,
990
        scanner_id: str,
991
        usage_type: UsageType,
992
        function: str,
993
        *,
994
        alterable: Optional[bool] = None,
995
        hosts_ordering: Optional[HostsOrdering] = None,
996
        schedule_id: Optional[str] = None,
997
        alert_ids: Optional[List[str]] = None,
998
        comment: Optional[str] = None,
999
        schedule_periods: Optional[int] = None,
1000
        observers: Optional[List[str]] = None,
1001
        preferences: Optional[dict] = None
1002
    ) -> Any:
1003
        if not name:
1004
            raise RequiredArgument(function=function, argument='name')
1005
1006
        if not config_id:
1007
            raise RequiredArgument(function=function, argument='config_id')
1008
1009
        if not target_id:
1010
            raise RequiredArgument(function=function, argument='target_id')
1011
1012
        if not scanner_id:
1013
            raise RequiredArgument(function=function, argument='scanner_id')
1014
1015
        # don't allow to create a container task with create_task
1016
        if target_id == '0':
1017
            raise InvalidArgument(function=function, argument='target_id')
1018
1019
        cmd = XmlCommand("create_task")
1020
        cmd.add_element("name", name)
1021
        cmd.add_element("usage_type", usage_type.value)
1022
        cmd.add_element("config", attrs={"id": config_id})
1023
        cmd.add_element("target", attrs={"id": target_id})
1024
        cmd.add_element("scanner", attrs={"id": scanner_id})
1025
1026
        if comment:
1027
            cmd.add_element("comment", comment)
1028
1029
        if alterable is not None:
1030
            cmd.add_element("alterable", _to_bool(alterable))
1031
1032
        if hosts_ordering:
1033
            if not isinstance(hosts_ordering, self.types.HostsOrdering):
1034
                raise InvalidArgumentType(
1035
                    function=function,
1036
                    argument='hosts_ordering',
1037
                    arg_type=HostsOrdering.__name__,
1038
                )
1039
            cmd.add_element("hosts_ordering", hosts_ordering.value)
1040
1041
        if alert_ids:
1042
            if isinstance(alert_ids, str):
1043
                deprecation(
1044
                    "Please pass a list as alert_ids parameter to {}. "
1045
                    "Passing a string is deprecated and will be removed in "
1046
                    "future.".format(function)
1047
                )
1048
1049
                # if a single id is given as a string wrap it into a list
1050
                alert_ids = [alert_ids]
1051
            if _is_list_like(alert_ids):
1052
                # parse all given alert id's
1053
                for alert in alert_ids:
1054
                    cmd.add_element("alert", attrs={"id": str(alert)})
1055
1056
        if schedule_id:
1057
            cmd.add_element("schedule", attrs={"id": schedule_id})
1058
1059
            if schedule_periods is not None:
1060
                if (
1061
                    not isinstance(schedule_periods, numbers.Integral)
1062
                    or schedule_periods < 0
1063
                ):
1064
                    raise InvalidArgument(
1065
                        "schedule_periods must be an integer greater or equal "
1066
                        "than 0"
1067
                    )
1068
                cmd.add_element("schedule_periods", str(schedule_periods))
1069
1070
        if observers is not None:
1071
            if not _is_list_like(observers):
1072
                raise InvalidArgumentType(
1073
                    function=function, argument='observers', arg_type='list'
1074
                )
1075
1076
            # gvmd splits by comma and space
1077
            # gvmd tries to lookup each value as user name and afterwards as
1078
            # user id. So both user name and user id are possible
1079
            cmd.add_element("observers", _to_comma_list(observers))
1080
1081
        if preferences is not None:
1082
            if not isinstance(preferences, collections.abc.Mapping):
1083
                raise InvalidArgumentType(
1084
                    function=function,
1085
                    argument='preferences',
1086
                    arg_type=collections.abc.Mapping.__name__,
1087
                )
1088
1089
            _xmlprefs = cmd.add_element("preferences")
1090
            for pref_name, pref_value in preferences.items():
1091
                _xmlpref = _xmlprefs.add_element("preference")
1092
                _xmlpref.add_element("scanner_name", pref_name)
1093
                _xmlpref.add_element("value", str(pref_value))
1094
1095
        return self._send_xml_command(cmd)
1096
1097
    def __create_config(
1098
        self,

gvm/protocols/gmpv7/gmpv7.py 1 location

@@ 1979-2119 (lines=141) @@
1976
        cmd.add_element("copy", target_id)
1977
        return self._send_xml_command(cmd)
1978
1979
    def create_task(
1980
        self,
1981
        name: str,
1982
        config_id: str,
1983
        target_id: str,
1984
        scanner_id: str,
1985
        *,
1986
        alterable: Optional[bool] = None,
1987
        hosts_ordering: Optional[HostsOrdering] = None,
1988
        schedule_id: Optional[str] = None,
1989
        alert_ids: Optional[List[str]] = None,
1990
        comment: Optional[str] = None,
1991
        schedule_periods: Optional[int] = None,
1992
        observers: Optional[List[str]] = None,
1993
        preferences: Optional[dict] = None
1994
    ) -> Any:
1995
        """Create a new task
1996
1997
        Arguments:
1998
            name: Name of the task
1999
            config_id: UUID of scan config to use by the task
2000
            target_id: UUID of target to be scanned
2001
            scanner_id: UUID of scanner to use for scanning the target
2002
            comment: Comment for the task
2003
            alterable: Whether the task should be alterable
2004
            alert_ids: List of UUIDs for alerts to be applied to the task
2005
            hosts_ordering: The order hosts are scanned in
2006
            schedule_id: UUID of a schedule when the task should be run.
2007
            schedule_periods: A limit to the number of times the task will be
2008
                scheduled, or 0 for no limit
2009
            observers: List of names or ids of users which should be allowed to
2010
                observe this task
2011
            preferences: Name/Value pairs of scanner preferences.
2012
2013
        Returns:
2014
            The response. See :py:meth:`send_command` for details.
2015
        """
2016
        if not name:
2017
            raise RequiredArgument(
2018
                function=self.create_task.__name__, argument='name'
2019
            )
2020
2021
        if not config_id:
2022
            raise RequiredArgument(
2023
                function=self.create_task.__name__, argument='config_id'
2024
            )
2025
2026
        if not target_id:
2027
            raise RequiredArgument(
2028
                function=self.create_task.__name__, argument='target_id'
2029
            )
2030
2031
        if not scanner_id:
2032
            raise RequiredArgument(
2033
                function=self.create_task.__name__, argument='scanner_id'
2034
            )
2035
2036
        # don't allow to create a container task with create_task
2037
        if target_id == '0':
2038
            raise InvalidArgument(
2039
                'Invalid argument {} for target_id'.format(target_id)
2040
            )
2041
2042
        cmd = XmlCommand("create_task")
2043
        cmd.add_element("name", name)
2044
        cmd.add_element("config", attrs={"id": config_id})
2045
        cmd.add_element("target", attrs={"id": target_id})
2046
        cmd.add_element("scanner", attrs={"id": scanner_id})
2047
2048
        if comment:
2049
            cmd.add_element("comment", comment)
2050
2051
        if alterable is not None:
2052
            cmd.add_element("alterable", _to_bool(alterable))
2053
2054
        if hosts_ordering:
2055
            if not isinstance(hosts_ordering, HostsOrdering):
2056
                raise InvalidArgumentType(
2057
                    function=self.create_task.__name__,
2058
                    argument='hosts_ordering',
2059
                    arg_type=HostsOrdering.__name__,
2060
                )
2061
            cmd.add_element("hosts_ordering", hosts_ordering.value)
2062
2063
        if alert_ids:
2064
            if isinstance(alert_ids, str):
2065
                deprecation(
2066
                    "Please pass a list as alert_ids parameter to create_task. "
2067
                    "Passing a string is deprecated and will be removed in "
2068
                    "future."
2069
                )
2070
2071
                # if a single id is given as a string wrap it into a list
2072
                alert_ids = [alert_ids]
2073
            if _is_list_like(alert_ids):
2074
                # parse all given alert id's
2075
                for alert in alert_ids:
2076
                    cmd.add_element("alert", attrs={"id": str(alert)})
2077
2078
        if schedule_id:
2079
            cmd.add_element("schedule", attrs={"id": schedule_id})
2080
2081
            if schedule_periods is not None:
2082
                if (
2083
                    not isinstance(schedule_periods, numbers.Integral)
2084
                    or schedule_periods < 0
2085
                ):
2086
                    raise InvalidArgument(
2087
                        "schedule_periods must be an integer greater or equal "
2088
                        "than 0"
2089
                    )
2090
                cmd.add_element("schedule_periods", str(schedule_periods))
2091
2092
        if observers is not None:
2093
            if not _is_list_like(observers):
2094
                raise InvalidArgumentType(
2095
                    function=self.create_task.__name__,
2096
                    argument='observers',
2097
                    arg_type='list',
2098
                )
2099
2100
            # gvmd splits by comma and space
2101
            # gvmd tries to lookup each value as user name and afterwards as
2102
            # user id. So both user name and user id are possible
2103
            cmd.add_element("observers", _to_comma_list(observers))
2104
2105
        if preferences is not None:
2106
            if not isinstance(preferences, collections.abc.Mapping):
2107
                raise InvalidArgumentType(
2108
                    function=self.create_task.__name__,
2109
                    argument='preferences',
2110
                    arg_type=collections.abc.Mapping.__name__,
2111
                )
2112
2113
            _xmlprefs = cmd.add_element("preferences")
2114
            for pref_name, pref_value in preferences.items():
2115
                _xmlpref = _xmlprefs.add_element("preference")
2116
                _xmlpref.add_element("scanner_name", pref_name)
2117
                _xmlpref.add_element("value", str(pref_value))
2118
2119
        return self._send_xml_command(cmd)
2120
2121
    def create_container_task(
2122
        self, name: str, *, comment: Optional[str] = None