Code Duplication    Length = 111-141 lines in 2 locations

gvm/protocols/gmpv7/__init__.py 1 location

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

gvm/protocols/gmpv9/__init__.py 1 location

@@ 622-732 (lines=111) @@
619
620
        return self._send_xml_command(cmd)
621
622
    def __create_task(
623
        self,
624
        name: str,
625
        config_id: str,
626
        target_id: str,
627
        scanner_id: str,
628
        usage_type: UsageType,
629
        function: str,
630
        *,
631
        alterable: Optional[bool] = None,
632
        hosts_ordering: Optional[HostsOrdering] = None,
633
        schedule_id: Optional[str] = None,
634
        alert_ids: Optional[List[str]] = None,
635
        comment: Optional[str] = None,
636
        schedule_periods: Optional[int] = None,
637
        observers: Optional[List[str]] = None,
638
        preferences: Optional[dict] = None
639
    ) -> Any:
640
        if not name:
641
            raise RequiredArgument(function=function, argument='name')
642
643
        if not config_id:
644
            raise RequiredArgument(function=function, argument='config_id')
645
646
        if not target_id:
647
            raise RequiredArgument(function=function, argument='target_id')
648
649
        if not scanner_id:
650
            raise RequiredArgument(function=function, argument='scanner_id')
651
652
        # don't allow to create a container task with create_task
653
        if target_id == '0':
654
            raise InvalidArgument(function=function, argument='target_id')
655
656
        cmd = XmlCommand("create_task")
657
        cmd.add_element("name", name)
658
        cmd.add_element("usage_type", usage_type.value)
659
        cmd.add_element("config", attrs={"id": config_id})
660
        cmd.add_element("target", attrs={"id": target_id})
661
        cmd.add_element("scanner", attrs={"id": scanner_id})
662
663
        if comment:
664
            cmd.add_element("comment", comment)
665
666
        if alterable is not None:
667
            cmd.add_element("alterable", _to_bool(alterable))
668
669
        if hosts_ordering:
670
            if not isinstance(hosts_ordering, HostsOrdering):
671
                raise InvalidArgumentType(
672
                    function=function,
673
                    argument='hosts_ordering',
674
                    arg_type=HostsOrdering.__name__,
675
                )
676
            cmd.add_element("hosts_ordering", hosts_ordering.value)
677
678
        if alert_ids:
679
            if isinstance(alert_ids, str):
680
                deprecation(
681
                    "Please pass a list as alert_ids parameter to {}. "
682
                    "Passing a string is deprecated and will be removed in "
683
                    "future.".format(function)
684
                )
685
686
                # if a single id is given as a string wrap it into a list
687
                alert_ids = [alert_ids]
688
            if _is_list_like(alert_ids):
689
                # parse all given alert id's
690
                for alert in alert_ids:
691
                    cmd.add_element("alert", attrs={"id": str(alert)})
692
693
        if schedule_id:
694
            cmd.add_element("schedule", attrs={"id": schedule_id})
695
696
            if schedule_periods is not None:
697
                if (
698
                    not isinstance(schedule_periods, numbers.Integral)
699
                    or schedule_periods < 0
700
                ):
701
                    raise InvalidArgument(
702
                        "schedule_periods must be an integer greater or equal "
703
                        "than 0"
704
                    )
705
                cmd.add_element("schedule_periods", str(schedule_periods))
706
707
        if observers is not None:
708
            if not _is_list_like(observers):
709
                raise InvalidArgumentType(
710
                    function=function, argument='observers', arg_type='list'
711
                )
712
713
            # gvmd splits by comma and space
714
            # gvmd tries to lookup each value as user name and afterwards as
715
            # user id. So both user name and user id are possible
716
            cmd.add_element("observers", _to_comma_list(observers))
717
718
        if preferences is not None:
719
            if not isinstance(preferences, collections.abc.Mapping):
720
                raise InvalidArgumentType(
721
                    function=function,
722
                    argument='preferences',
723
                    arg_type=collections.abc.Mapping.__name__,
724
                )
725
726
            _xmlprefs = cmd.add_element("preferences")
727
            for pref_name, pref_value in preferences.items():
728
                _xmlpref = _xmlprefs.add_element("preference")
729
                _xmlpref.add_element("scanner_name", pref_name)
730
                _xmlpref.add_element("value", str(pref_value))
731
732
        return self._send_xml_command(cmd)
733
734
    def __create_config(
735
        self, config_id: str, name: str, usage_type: UsageType, function: str