| @@ 628-738 (lines=111) @@ | ||
| 625 | ||
| 626 | return self._send_xml_command(cmd) |
|
| 627 | ||
| 628 | def __create_task( |
|
| 629 | self, |
|
| 630 | name: str, |
|
| 631 | config_id: str, |
|
| 632 | target_id: str, |
|
| 633 | scanner_id: str, |
|
| 634 | usage_type: UsageType, |
|
| 635 | function: str, |
|
| 636 | *, |
|
| 637 | alterable: Optional[bool] = None, |
|
| 638 | hosts_ordering: Optional[HostsOrdering] = None, |
|
| 639 | schedule_id: Optional[str] = None, |
|
| 640 | alert_ids: Optional[List[str]] = None, |
|
| 641 | comment: Optional[str] = None, |
|
| 642 | schedule_periods: Optional[int] = None, |
|
| 643 | observers: Optional[List[str]] = None, |
|
| 644 | preferences: Optional[dict] = None |
|
| 645 | ) -> Any: |
|
| 646 | if not name: |
|
| 647 | raise RequiredArgument(function=function, argument='name') |
|
| 648 | ||
| 649 | if not config_id: |
|
| 650 | raise RequiredArgument(function=function, argument='config_id') |
|
| 651 | ||
| 652 | if not target_id: |
|
| 653 | raise RequiredArgument(function=function, argument='target_id') |
|
| 654 | ||
| 655 | if not scanner_id: |
|
| 656 | raise RequiredArgument(function=function, argument='scanner_id') |
|
| 657 | ||
| 658 | # don't allow to create a container task with create_task |
|
| 659 | if target_id == '0': |
|
| 660 | raise InvalidArgument(function=function, argument='target_id') |
|
| 661 | ||
| 662 | cmd = XmlCommand("create_task") |
|
| 663 | cmd.add_element("name", name) |
|
| 664 | cmd.add_element("usage_type", usage_type.value) |
|
| 665 | cmd.add_element("config", attrs={"id": config_id}) |
|
| 666 | cmd.add_element("target", attrs={"id": target_id}) |
|
| 667 | cmd.add_element("scanner", attrs={"id": scanner_id}) |
|
| 668 | ||
| 669 | if comment: |
|
| 670 | cmd.add_element("comment", comment) |
|
| 671 | ||
| 672 | if alterable is not None: |
|
| 673 | cmd.add_element("alterable", _to_bool(alterable)) |
|
| 674 | ||
| 675 | if hosts_ordering: |
|
| 676 | if not isinstance(hosts_ordering, self.types.HostsOrdering): |
|
| 677 | raise InvalidArgumentType( |
|
| 678 | function=function, |
|
| 679 | argument='hosts_ordering', |
|
| 680 | arg_type=HostsOrdering.__name__, |
|
| 681 | ) |
|
| 682 | cmd.add_element("hosts_ordering", hosts_ordering.value) |
|
| 683 | ||
| 684 | if alert_ids: |
|
| 685 | if isinstance(alert_ids, str): |
|
| 686 | deprecation( |
|
| 687 | "Please pass a list as alert_ids parameter to {}. " |
|
| 688 | "Passing a string is deprecated and will be removed in " |
|
| 689 | "future.".format(function) |
|
| 690 | ) |
|
| 691 | ||
| 692 | # if a single id is given as a string wrap it into a list |
|
| 693 | alert_ids = [alert_ids] |
|
| 694 | if _is_list_like(alert_ids): |
|
| 695 | # parse all given alert id's |
|
| 696 | for alert in alert_ids: |
|
| 697 | cmd.add_element("alert", attrs={"id": str(alert)}) |
|
| 698 | ||
| 699 | if schedule_id: |
|
| 700 | cmd.add_element("schedule", attrs={"id": schedule_id}) |
|
| 701 | ||
| 702 | if schedule_periods is not None: |
|
| 703 | if ( |
|
| 704 | not isinstance(schedule_periods, numbers.Integral) |
|
| 705 | or schedule_periods < 0 |
|
| 706 | ): |
|
| 707 | raise InvalidArgument( |
|
| 708 | "schedule_periods must be an integer greater or equal " |
|
| 709 | "than 0" |
|
| 710 | ) |
|
| 711 | cmd.add_element("schedule_periods", str(schedule_periods)) |
|
| 712 | ||
| 713 | if observers is not None: |
|
| 714 | if not _is_list_like(observers): |
|
| 715 | raise InvalidArgumentType( |
|
| 716 | function=function, argument='observers', arg_type='list' |
|
| 717 | ) |
|
| 718 | ||
| 719 | # gvmd splits by comma and space |
|
| 720 | # gvmd tries to lookup each value as user name and afterwards as |
|
| 721 | # user id. So both user name and user id are possible |
|
| 722 | cmd.add_element("observers", _to_comma_list(observers)) |
|
| 723 | ||
| 724 | if preferences is not None: |
|
| 725 | if not isinstance(preferences, collections.abc.Mapping): |
|
| 726 | raise InvalidArgumentType( |
|
| 727 | function=function, |
|
| 728 | argument='preferences', |
|
| 729 | arg_type=collections.abc.Mapping.__name__, |
|
| 730 | ) |
|
| 731 | ||
| 732 | _xmlprefs = cmd.add_element("preferences") |
|
| 733 | for pref_name, pref_value in preferences.items(): |
|
| 734 | _xmlpref = _xmlprefs.add_element("preference") |
|
| 735 | _xmlpref.add_element("scanner_name", pref_name) |
|
| 736 | _xmlpref.add_element("value", str(pref_value)) |
|
| 737 | ||
| 738 | return self._send_xml_command(cmd) |
|
| 739 | ||
| 740 | def __create_config( |
|
| 741 | self, config_id: str, name: str, usage_type: UsageType, function: str |
|
| @@ 1927-2067 (lines=141) @@ | ||
| 1924 | cmd.add_element("copy", target_id) |
|
| 1925 | return self._send_xml_command(cmd) |
|
| 1926 | ||
| 1927 | def create_task( |
|
| 1928 | self, |
|
| 1929 | name: str, |
|
| 1930 | config_id: str, |
|
| 1931 | target_id: str, |
|
| 1932 | scanner_id: str, |
|
| 1933 | *, |
|
| 1934 | alterable: Optional[bool] = None, |
|
| 1935 | hosts_ordering: Optional[HostsOrdering] = None, |
|
| 1936 | schedule_id: Optional[str] = None, |
|
| 1937 | alert_ids: Optional[List[str]] = None, |
|
| 1938 | comment: Optional[str] = None, |
|
| 1939 | schedule_periods: Optional[int] = None, |
|
| 1940 | observers: Optional[List[str]] = None, |
|
| 1941 | preferences: Optional[dict] = None |
|
| 1942 | ) -> Any: |
|
| 1943 | """Create a new task |
|
| 1944 | ||
| 1945 | Arguments: |
|
| 1946 | name: Name of the task |
|
| 1947 | config_id: UUID of scan config to use by the task |
|
| 1948 | target_id: UUID of target to be scanned |
|
| 1949 | scanner_id: UUID of scanner to use for scanning the target |
|
| 1950 | comment: Comment for the task |
|
| 1951 | alterable: Whether the task should be alterable |
|
| 1952 | alert_ids: List of UUIDs for alerts to be applied to the task |
|
| 1953 | hosts_ordering: The order hosts are scanned in |
|
| 1954 | schedule_id: UUID of a schedule when the task should be run. |
|
| 1955 | schedule_periods: A limit to the number of times the task will be |
|
| 1956 | scheduled, or 0 for no limit |
|
| 1957 | observers: List of names or ids of users which should be allowed to |
|
| 1958 | observe this task |
|
| 1959 | preferences: Name/Value pairs of scanner preferences. |
|
| 1960 | ||
| 1961 | Returns: |
|
| 1962 | The response. See :py:meth:`send_command` for details. |
|
| 1963 | """ |
|
| 1964 | if not name: |
|
| 1965 | raise RequiredArgument( |
|
| 1966 | function=self.create_task.__name__, argument='name' |
|
| 1967 | ) |
|
| 1968 | ||
| 1969 | if not config_id: |
|
| 1970 | raise RequiredArgument( |
|
| 1971 | function=self.create_task.__name__, argument='config_id' |
|
| 1972 | ) |
|
| 1973 | ||
| 1974 | if not target_id: |
|
| 1975 | raise RequiredArgument( |
|
| 1976 | function=self.create_task.__name__, argument='target_id' |
|
| 1977 | ) |
|
| 1978 | ||
| 1979 | if not scanner_id: |
|
| 1980 | raise RequiredArgument( |
|
| 1981 | function=self.create_task.__name__, argument='scanner_id' |
|
| 1982 | ) |
|
| 1983 | ||
| 1984 | # don't allow to create a container task with create_task |
|
| 1985 | if target_id == '0': |
|
| 1986 | raise InvalidArgument( |
|
| 1987 | 'Invalid argument {} for target_id'.format(target_id) |
|
| 1988 | ) |
|
| 1989 | ||
| 1990 | cmd = XmlCommand("create_task") |
|
| 1991 | cmd.add_element("name", name) |
|
| 1992 | cmd.add_element("config", attrs={"id": config_id}) |
|
| 1993 | cmd.add_element("target", attrs={"id": target_id}) |
|
| 1994 | cmd.add_element("scanner", attrs={"id": scanner_id}) |
|
| 1995 | ||
| 1996 | if comment: |
|
| 1997 | cmd.add_element("comment", comment) |
|
| 1998 | ||
| 1999 | if alterable is not None: |
|
| 2000 | cmd.add_element("alterable", _to_bool(alterable)) |
|
| 2001 | ||
| 2002 | if hosts_ordering: |
|
| 2003 | if not isinstance(hosts_ordering, HostsOrdering): |
|
| 2004 | raise InvalidArgumentType( |
|
| 2005 | function=self.create_task.__name__, |
|
| 2006 | argument='hosts_ordering', |
|
| 2007 | arg_type=HostsOrdering.__name__, |
|
| 2008 | ) |
|
| 2009 | cmd.add_element("hosts_ordering", hosts_ordering.value) |
|
| 2010 | ||
| 2011 | if alert_ids: |
|
| 2012 | if isinstance(alert_ids, str): |
|
| 2013 | deprecation( |
|
| 2014 | "Please pass a list as alert_ids parameter to create_task. " |
|
| 2015 | "Passing a string is deprecated and will be removed in " |
|
| 2016 | "future." |
|
| 2017 | ) |
|
| 2018 | ||
| 2019 | # if a single id is given as a string wrap it into a list |
|
| 2020 | alert_ids = [alert_ids] |
|
| 2021 | if _is_list_like(alert_ids): |
|
| 2022 | # parse all given alert id's |
|
| 2023 | for alert in alert_ids: |
|
| 2024 | cmd.add_element("alert", attrs={"id": str(alert)}) |
|
| 2025 | ||
| 2026 | if schedule_id: |
|
| 2027 | cmd.add_element("schedule", attrs={"id": schedule_id}) |
|
| 2028 | ||
| 2029 | if schedule_periods is not None: |
|
| 2030 | if ( |
|
| 2031 | not isinstance(schedule_periods, numbers.Integral) |
|
| 2032 | or schedule_periods < 0 |
|
| 2033 | ): |
|
| 2034 | raise InvalidArgument( |
|
| 2035 | "schedule_periods must be an integer greater or equal " |
|
| 2036 | "than 0" |
|
| 2037 | ) |
|
| 2038 | cmd.add_element("schedule_periods", str(schedule_periods)) |
|
| 2039 | ||
| 2040 | if observers is not None: |
|
| 2041 | if not _is_list_like(observers): |
|
| 2042 | raise InvalidArgumentType( |
|
| 2043 | function=self.create_task.__name__, |
|
| 2044 | argument='observers', |
|
| 2045 | arg_type='list', |
|
| 2046 | ) |
|
| 2047 | ||
| 2048 | # gvmd splits by comma and space |
|
| 2049 | # gvmd tries to lookup each value as user name and afterwards as |
|
| 2050 | # user id. So both user name and user id are possible |
|
| 2051 | cmd.add_element("observers", _to_comma_list(observers)) |
|
| 2052 | ||
| 2053 | if preferences is not None: |
|
| 2054 | if not isinstance(preferences, collections.abc.Mapping): |
|
| 2055 | raise InvalidArgumentType( |
|
| 2056 | function=self.create_task.__name__, |
|
| 2057 | argument='preferences', |
|
| 2058 | arg_type=collections.abc.Mapping.__name__, |
|
| 2059 | ) |
|
| 2060 | ||
| 2061 | _xmlprefs = cmd.add_element("preferences") |
|
| 2062 | for pref_name, pref_value in preferences.items(): |
|
| 2063 | _xmlpref = _xmlprefs.add_element("preference") |
|
| 2064 | _xmlpref.add_element("scanner_name", pref_name) |
|
| 2065 | _xmlpref.add_element("value", str(pref_value)) |
|
| 2066 | ||
| 2067 | return self._send_xml_command(cmd) |
|
| 2068 | ||
| 2069 | def create_container_task( |
|
| 2070 | self, name: str, *, comment: Optional[str] = None |
|