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