Code Duplication    Length = 111-141 lines in 2 locations

gvm/protocols/gmpv7/gmpv7.py 1 location

@@ 1932-2072 (lines=141) @@
1929
        cmd.add_element("copy", target_id)
1930
        return self._send_xml_command(cmd)
1931
1932
    def create_task(
1933
        self,
1934
        name: str,
1935
        config_id: str,
1936
        target_id: str,
1937
        scanner_id: str,
1938
        *,
1939
        alterable: Optional[bool] = None,
1940
        hosts_ordering: Optional[HostsOrdering] = None,
1941
        schedule_id: Optional[str] = None,
1942
        alert_ids: Optional[List[str]] = None,
1943
        comment: Optional[str] = None,
1944
        schedule_periods: Optional[int] = None,
1945
        observers: Optional[List[str]] = None,
1946
        preferences: Optional[dict] = None
1947
    ) -> Any:
1948
        """Create a new task
1949
1950
        Arguments:
1951
            name: Name of the task
1952
            config_id: UUID of scan config to use by the task
1953
            target_id: UUID of target to be scanned
1954
            scanner_id: UUID of scanner to use for scanning the target
1955
            comment: Comment for the task
1956
            alterable: Whether the task should be alterable
1957
            alert_ids: List of UUIDs for alerts to be applied to the task
1958
            hosts_ordering: The order hosts are scanned in
1959
            schedule_id: UUID of a schedule when the task should be run.
1960
            schedule_periods: A limit to the number of times the task will be
1961
                scheduled, or 0 for no limit
1962
            observers: List of names or ids of users which should be allowed to
1963
                observe this task
1964
            preferences: Name/Value pairs of scanner preferences.
1965
1966
        Returns:
1967
            The response. See :py:meth:`send_command` for details.
1968
        """
1969
        if not name:
1970
            raise RequiredArgument(
1971
                function=self.create_task.__name__, argument='name'
1972
            )
1973
1974
        if not config_id:
1975
            raise RequiredArgument(
1976
                function=self.create_task.__name__, argument='config_id'
1977
            )
1978
1979
        if not target_id:
1980
            raise RequiredArgument(
1981
                function=self.create_task.__name__, argument='target_id'
1982
            )
1983
1984
        if not scanner_id:
1985
            raise RequiredArgument(
1986
                function=self.create_task.__name__, argument='scanner_id'
1987
            )
1988
1989
        # don't allow to create a container task with create_task
1990
        if target_id == '0':
1991
            raise InvalidArgument(
1992
                'Invalid argument {} for target_id'.format(target_id)
1993
            )
1994
1995
        cmd = XmlCommand("create_task")
1996
        cmd.add_element("name", name)
1997
        cmd.add_element("config", attrs={"id": config_id})
1998
        cmd.add_element("target", attrs={"id": target_id})
1999
        cmd.add_element("scanner", attrs={"id": scanner_id})
2000
2001
        if comment:
2002
            cmd.add_element("comment", comment)
2003
2004
        if alterable is not None:
2005
            cmd.add_element("alterable", _to_bool(alterable))
2006
2007
        if hosts_ordering:
2008
            if not isinstance(hosts_ordering, HostsOrdering):
2009
                raise InvalidArgumentType(
2010
                    function=self.create_task.__name__,
2011
                    argument='hosts_ordering',
2012
                    arg_type=HostsOrdering.__name__,
2013
                )
2014
            cmd.add_element("hosts_ordering", hosts_ordering.value)
2015
2016
        if alert_ids:
2017
            if isinstance(alert_ids, str):
2018
                deprecation(
2019
                    "Please pass a list as alert_ids parameter to create_task. "
2020
                    "Passing a string is deprecated and will be removed in "
2021
                    "future."
2022
                )
2023
2024
                # if a single id is given as a string wrap it into a list
2025
                alert_ids = [alert_ids]
2026
            if _is_list_like(alert_ids):
2027
                # parse all given alert id's
2028
                for alert in alert_ids:
2029
                    cmd.add_element("alert", attrs={"id": str(alert)})
2030
2031
        if schedule_id:
2032
            cmd.add_element("schedule", attrs={"id": schedule_id})
2033
2034
            if schedule_periods is not None:
2035
                if (
2036
                    not isinstance(schedule_periods, numbers.Integral)
2037
                    or schedule_periods < 0
2038
                ):
2039
                    raise InvalidArgument(
2040
                        "schedule_periods must be an integer greater or equal "
2041
                        "than 0"
2042
                    )
2043
                cmd.add_element("schedule_periods", str(schedule_periods))
2044
2045
        if observers is not None:
2046
            if not _is_list_like(observers):
2047
                raise InvalidArgumentType(
2048
                    function=self.create_task.__name__,
2049
                    argument='observers',
2050
                    arg_type='list',
2051
                )
2052
2053
            # gvmd splits by comma and space
2054
            # gvmd tries to lookup each value as user name and afterwards as
2055
            # user id. So both user name and user id are possible
2056
            cmd.add_element("observers", _to_comma_list(observers))
2057
2058
        if preferences is not None:
2059
            if not isinstance(preferences, collections.abc.Mapping):
2060
                raise InvalidArgumentType(
2061
                    function=self.create_task.__name__,
2062
                    argument='preferences',
2063
                    arg_type=collections.abc.Mapping.__name__,
2064
                )
2065
2066
            _xmlprefs = cmd.add_element("preferences")
2067
            for pref_name, pref_value in preferences.items():
2068
                _xmlpref = _xmlprefs.add_element("preference")
2069
                _xmlpref.add_element("scanner_name", pref_name)
2070
                _xmlpref.add_element("value", str(pref_value))
2071
2072
        return self._send_xml_command(cmd)
2073
2074
    def create_container_task(
2075
        self, name: str, *, comment: Optional[str] = None

gvm/protocols/gmpv9/gmpv9.py 1 location

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