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