Code Duplication    Length = 111-141 lines in 2 locations

gvm/protocols/gmpv9/gmpv9.py 1 location

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

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