Code Duplication    Length = 111-141 lines in 2 locations

gvm/protocols/gmpv7/__init__.py 1 location

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

gvm/protocols/gmpv9/__init__.py 1 location

@@ 363-473 (lines=111) @@
360
361
        return self._send_xml_command(cmd)
362
363
    def __create_task(
364
        self,
365
        name: str,
366
        config_id: str,
367
        target_id: str,
368
        scanner_id: str,
369
        usage_type: UsageType,
370
        function: str,
371
        *,
372
        alterable: Optional[bool] = None,
373
        hosts_ordering: Optional[HostsOrdering] = None,
374
        schedule_id: Optional[str] = None,
375
        alert_ids: Optional[List[str]] = None,
376
        comment: Optional[str] = None,
377
        schedule_periods: Optional[int] = None,
378
        observers: Optional[List[str]] = None,
379
        preferences: Optional[dict] = None
380
    ) -> Any:
381
        if not name:
382
            raise RequiredArgument(function=function, argument='name')
383
384
        if not config_id:
385
            raise RequiredArgument(function=function, argument='config_id')
386
387
        if not target_id:
388
            raise RequiredArgument(function=function, argument='target_id')
389
390
        if not scanner_id:
391
            raise RequiredArgument(function=function, argument='scanner_id')
392
393
        # don't allow to create a container task with create_task
394
        if target_id == '0':
395
            raise InvalidArgument(function=function, argument='target_id')
396
397
        cmd = XmlCommand("create_task")
398
        cmd.add_element("name", name)
399
        cmd.add_element("usage_type", usage_type.value)
400
        cmd.add_element("config", attrs={"id": config_id})
401
        cmd.add_element("target", attrs={"id": target_id})
402
        cmd.add_element("scanner", attrs={"id": scanner_id})
403
404
        if comment:
405
            cmd.add_element("comment", comment)
406
407
        if not alterable is None:
408
            cmd.add_element("alterable", _to_bool(alterable))
409
410
        if hosts_ordering:
411
            if not isinstance(hosts_ordering, HostsOrdering):
412
                raise InvalidArgumentType(
413
                    function=function,
414
                    argument='hosts_ordering',
415
                    arg_type=HostsOrdering.__name__,
416
                )
417
            cmd.add_element("hosts_ordering", hosts_ordering.value)
418
419
        if alert_ids:
420
            if isinstance(alert_ids, str):
421
                deprecation(
422
                    "Please pass a list as alert_ids parameter to {}. "
423
                    "Passing a string is deprecated and will be removed in "
424
                    "future.".format(function)
425
                )
426
427
                # if a single id is given as a string wrap it into a list
428
                alert_ids = [alert_ids]
429
            if _is_list_like(alert_ids):
430
                # parse all given alert id's
431
                for alert in alert_ids:
432
                    cmd.add_element("alert", attrs={"id": str(alert)})
433
434
        if schedule_id:
435
            cmd.add_element("schedule", attrs={"id": schedule_id})
436
437
            if schedule_periods is not None:
438
                if (
439
                    not isinstance(schedule_periods, numbers.Integral)
440
                    or schedule_periods < 0
441
                ):
442
                    raise InvalidArgument(
443
                        "schedule_periods must be an integer greater or equal "
444
                        "than 0"
445
                    )
446
                cmd.add_element("schedule_periods", str(schedule_periods))
447
448
        if observers is not None:
449
            if not _is_list_like(observers):
450
                raise InvalidArgumentType(
451
                    function=function, argument='obeservers', arg_type='list'
452
                )
453
454
            # gvmd splits by comma and space
455
            # gvmd tries to lookup each value as user name and afterwards as
456
            # user id. So both user name and user id are possible
457
            cmd.add_element("observers", _to_comma_list(observers))
458
459
        if preferences is not None:
460
            if not isinstance(preferences, collections.abc.Mapping):
461
                raise InvalidArgumentType(
462
                    function=function,
463
                    argument='preferences',
464
                    arg_type=collections.abc.Mapping.__name__,
465
                )
466
467
            _xmlprefs = cmd.add_element("preferences")
468
            for pref_name, pref_value in preferences.items():
469
                _xmlpref = _xmlprefs.add_element("preference")
470
                _xmlpref.add_element("scanner_name", pref_name)
471
                _xmlpref.add_element("value", str(pref_value))
472
473
        return self._send_xml_command(cmd)
474
475
    def __create_config(
476
        self, config_id: str, name: str, usage_type: UsageType, function: str