Code Duplication    Length = 111-141 lines in 3 locations

gvm/protocols/gmpv7/gmpv7.py 1 location

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

gvm/protocols/gmpv208/gmpv208.py 1 location

@@ 1797-1907 (lines=111) @@
1794
1795
        return self._send_xml_command(cmd)
1796
1797
    def __create_task(
1798
        self,
1799
        name: str,
1800
        config_id: str,
1801
        target_id: str,
1802
        scanner_id: str,
1803
        usage_type: UsageType,
1804
        function: str,
1805
        *,
1806
        alterable: Optional[bool] = None,
1807
        hosts_ordering: Optional[HostsOrdering] = None,
1808
        schedule_id: Optional[str] = None,
1809
        alert_ids: Optional[List[str]] = None,
1810
        comment: Optional[str] = None,
1811
        schedule_periods: Optional[int] = None,
1812
        observers: Optional[List[str]] = None,
1813
        preferences: Optional[dict] = None,
1814
    ) -> Any:
1815
        if not name:
1816
            raise RequiredArgument(function=function, argument='name')
1817
1818
        if not config_id:
1819
            raise RequiredArgument(function=function, argument='config_id')
1820
1821
        if not target_id:
1822
            raise RequiredArgument(function=function, argument='target_id')
1823
1824
        if not scanner_id:
1825
            raise RequiredArgument(function=function, argument='scanner_id')
1826
1827
        # don't allow to create a container task with create_task
1828
        if target_id == '0':
1829
            raise InvalidArgument(function=function, argument='target_id')
1830
1831
        cmd = XmlCommand("create_task")
1832
        cmd.add_element("name", name)
1833
        cmd.add_element("usage_type", usage_type.value)
1834
        cmd.add_element("config", attrs={"id": config_id})
1835
        cmd.add_element("target", attrs={"id": target_id})
1836
        cmd.add_element("scanner", attrs={"id": scanner_id})
1837
1838
        if comment:
1839
            cmd.add_element("comment", comment)
1840
1841
        if alterable is not None:
1842
            cmd.add_element("alterable", _to_bool(alterable))
1843
1844
        if hosts_ordering:
1845
            if not isinstance(hosts_ordering, self.types.HostsOrdering):
1846
                raise InvalidArgumentType(
1847
                    function=function,
1848
                    argument='hosts_ordering',
1849
                    arg_type=HostsOrdering.__name__,
1850
                )
1851
            cmd.add_element("hosts_ordering", hosts_ordering.value)
1852
1853
        if alert_ids:
1854
            if isinstance(alert_ids, str):
1855
                deprecation(
1856
                    "Please pass a list as alert_ids parameter to {}. "
1857
                    "Passing a string is deprecated and will be removed in "
1858
                    "future.".format(function)
1859
                )
1860
1861
                # if a single id is given as a string wrap it into a list
1862
                alert_ids = [alert_ids]
1863
            if _is_list_like(alert_ids):
1864
                # parse all given alert id's
1865
                for alert in alert_ids:
1866
                    cmd.add_element("alert", attrs={"id": str(alert)})
1867
1868
        if schedule_id:
1869
            cmd.add_element("schedule", attrs={"id": schedule_id})
1870
1871
            if schedule_periods is not None:
1872
                if (
1873
                    not isinstance(schedule_periods, numbers.Integral)
1874
                    or schedule_periods < 0
1875
                ):
1876
                    raise InvalidArgument(
1877
                        "schedule_periods must be an integer greater or equal "
1878
                        "than 0"
1879
                    )
1880
                cmd.add_element("schedule_periods", str(schedule_periods))
1881
1882
        if observers is not None:
1883
            if not _is_list_like(observers):
1884
                raise InvalidArgumentType(
1885
                    function=function, argument='observers', arg_type='list'
1886
                )
1887
1888
            # gvmd splits by comma and space
1889
            # gvmd tries to lookup each value as user name and afterwards as
1890
            # user id. So both user name and user id are possible
1891
            cmd.add_element("observers", _to_comma_list(observers))
1892
1893
        if preferences is not None:
1894
            if not isinstance(preferences, collections.abc.Mapping):
1895
                raise InvalidArgumentType(
1896
                    function=function,
1897
                    argument='preferences',
1898
                    arg_type=collections.abc.Mapping.__name__,
1899
                )
1900
1901
            _xmlprefs = cmd.add_element("preferences")
1902
            for pref_name, pref_value in preferences.items():
1903
                _xmlpref = _xmlprefs.add_element("preference")
1904
                _xmlpref.add_element("scanner_name", pref_name)
1905
                _xmlpref.add_element("value", str(pref_value))
1906
1907
        return self._send_xml_command(cmd)
1908
1909
    def __create_config(
1910
        self,

gvm/protocols/gmpv9/gmpv9.py 1 location

@@ 1670-1780 (lines=111) @@
1667
1668
        return self._send_xml_command(cmd)
1669
1670
    def __create_task(
1671
        self,
1672
        name: str,
1673
        config_id: str,
1674
        target_id: str,
1675
        scanner_id: str,
1676
        usage_type: UsageType,
1677
        function: str,
1678
        *,
1679
        alterable: Optional[bool] = None,
1680
        hosts_ordering: Optional[HostsOrdering] = None,
1681
        schedule_id: Optional[str] = None,
1682
        alert_ids: Optional[List[str]] = None,
1683
        comment: Optional[str] = None,
1684
        schedule_periods: Optional[int] = None,
1685
        observers: Optional[List[str]] = None,
1686
        preferences: Optional[dict] = None,
1687
    ) -> Any:
1688
        if not name:
1689
            raise RequiredArgument(function=function, argument='name')
1690
1691
        if not config_id:
1692
            raise RequiredArgument(function=function, argument='config_id')
1693
1694
        if not target_id:
1695
            raise RequiredArgument(function=function, argument='target_id')
1696
1697
        if not scanner_id:
1698
            raise RequiredArgument(function=function, argument='scanner_id')
1699
1700
        # don't allow to create a container task with create_task
1701
        if target_id == '0':
1702
            raise InvalidArgument(function=function, argument='target_id')
1703
1704
        cmd = XmlCommand("create_task")
1705
        cmd.add_element("name", name)
1706
        cmd.add_element("usage_type", usage_type.value)
1707
        cmd.add_element("config", attrs={"id": config_id})
1708
        cmd.add_element("target", attrs={"id": target_id})
1709
        cmd.add_element("scanner", attrs={"id": scanner_id})
1710
1711
        if comment:
1712
            cmd.add_element("comment", comment)
1713
1714
        if alterable is not None:
1715
            cmd.add_element("alterable", _to_bool(alterable))
1716
1717
        if hosts_ordering:
1718
            if not isinstance(hosts_ordering, self.types.HostsOrdering):
1719
                raise InvalidArgumentType(
1720
                    function=function,
1721
                    argument='hosts_ordering',
1722
                    arg_type=HostsOrdering.__name__,
1723
                )
1724
            cmd.add_element("hosts_ordering", hosts_ordering.value)
1725
1726
        if alert_ids:
1727
            if isinstance(alert_ids, str):
1728
                deprecation(
1729
                    "Please pass a list as alert_ids parameter to {}. "
1730
                    "Passing a string is deprecated and will be removed in "
1731
                    "future.".format(function)
1732
                )
1733
1734
                # if a single id is given as a string wrap it into a list
1735
                alert_ids = [alert_ids]
1736
            if _is_list_like(alert_ids):
1737
                # parse all given alert id's
1738
                for alert in alert_ids:
1739
                    cmd.add_element("alert", attrs={"id": str(alert)})
1740
1741
        if schedule_id:
1742
            cmd.add_element("schedule", attrs={"id": schedule_id})
1743
1744
            if schedule_periods is not None:
1745
                if (
1746
                    not isinstance(schedule_periods, numbers.Integral)
1747
                    or schedule_periods < 0
1748
                ):
1749
                    raise InvalidArgument(
1750
                        "schedule_periods must be an integer greater or equal "
1751
                        "than 0"
1752
                    )
1753
                cmd.add_element("schedule_periods", str(schedule_periods))
1754
1755
        if observers is not None:
1756
            if not _is_list_like(observers):
1757
                raise InvalidArgumentType(
1758
                    function=function, argument='observers', arg_type='list'
1759
                )
1760
1761
            # gvmd splits by comma and space
1762
            # gvmd tries to lookup each value as user name and afterwards as
1763
            # user id. So both user name and user id are possible
1764
            cmd.add_element("observers", _to_comma_list(observers))
1765
1766
        if preferences is not None:
1767
            if not isinstance(preferences, collections.abc.Mapping):
1768
                raise InvalidArgumentType(
1769
                    function=function,
1770
                    argument='preferences',
1771
                    arg_type=collections.abc.Mapping.__name__,
1772
                )
1773
1774
            _xmlprefs = cmd.add_element("preferences")
1775
            for pref_name, pref_value in preferences.items():
1776
                _xmlpref = _xmlprefs.add_element("preference")
1777
                _xmlpref.add_element("scanner_name", pref_name)
1778
                _xmlpref.add_element("value", str(pref_value))
1779
1780
        return self._send_xml_command(cmd)
1781
1782
    def __create_config(
1783
        self,