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

@@ 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,