Code Duplication    Length = 118-139 lines in 2 locations

gvm/protocols/gmpv208/entities/audits.py 1 location

@@ 49-187 (lines=139) @@
46
        cmd.add_element("copy", audit_id)
47
        return self._send_xml_command(cmd)
48
49
    def create_audit(
50
        self,
51
        name: str,
52
        policy_id: str,
53
        target_id: str,
54
        scanner_id: str,
55
        *,
56
        alterable: Optional[bool] = None,
57
        hosts_ordering: Optional[HostsOrdering] = None,
58
        schedule_id: Optional[str] = None,
59
        alert_ids: Optional[List[str]] = None,
60
        comment: Optional[str] = None,
61
        schedule_periods: Optional[int] = None,
62
        observers: Optional[List[str]] = None,
63
        preferences: Optional[dict] = None,
64
    ) -> Any:
65
        """Create a new audit task
66
67
        Arguments:
68
            name: Name of the new audit
69
            policy_id: UUID of policy to use by the audit
70
            target_id: UUID of target to be scanned
71
            scanner_id: UUID of scanner to use for scanning the target
72
            comment: Comment for the audit
73
            alterable: Whether the task should be alterable
74
            alert_ids: List of UUIDs for alerts to be applied to the audit
75
            hosts_ordering: The order hosts are scanned in
76
            schedule_id: UUID of a schedule when the audit should be run.
77
            schedule_periods: A limit to the number of times the audit will be
78
                scheduled, or 0 for no limit
79
            observers: List of names or ids of users which should be allowed to
80
                observe this audit
81
            preferences: Name/Value pairs of scanner preferences.
82
83
        Returns:
84
            The response. See :py:meth:`send_command` for details.
85
        """
86
87
        if not name:
88
            raise RequiredArgument(
89
                function=self.create_audit.__name__, argument='name'
90
            )
91
        if not policy_id:
92
            raise RequiredArgument(
93
                function=self.create_audit.__name__, argument='policy_id'
94
            )
95
96
        if not target_id:
97
            raise RequiredArgument(
98
                function=self.create_audit.__name__, argument='target_id'
99
            )
100
101
        if not scanner_id:
102
            raise RequiredArgument(
103
                function=self.create_audit.__name__, argument='scanner_id'
104
            )
105
106
        # don't allow to create a container task with create_task
107
        if target_id == '0':
108
            raise InvalidArgument(
109
                function=self.create_audit.__name__, argument='target_id'
110
            )
111
112
        cmd = XmlCommand("create_task")
113
        cmd.add_element("name", name)
114
        cmd.add_element("usage_type", "audit")
115
        cmd.add_element("config", attrs={"id": policy_id})
116
        cmd.add_element("target", attrs={"id": target_id})
117
        cmd.add_element("scanner", attrs={"id": scanner_id})
118
119
        if comment:
120
            cmd.add_element("comment", comment)
121
122
        if alterable is not None:
123
            cmd.add_element("alterable", to_bool(alterable))
124
125
        if hosts_ordering:
126
            if not isinstance(hosts_ordering, HostsOrdering):
127
                raise InvalidArgumentType(
128
                    function=self.create_audit.__name__,
129
                    argument='hosts_ordering',
130
                    arg_type=HostsOrdering.__name__,
131
                )
132
            cmd.add_element("hosts_ordering", hosts_ordering.value)
133
134
        if alert_ids is not None:
135
            if not is_list_like(alert_ids):
136
                raise InvalidArgumentType(
137
                    function=self.modify_task.__name__,
138
                    argument='alert_ids',
139
                    arg_type='list',
140
                )
141
142
            if not len(alert_ids) == 0:
143
                for alert in alert_ids:
144
                    cmd.add_element("alert", attrs={"id": str(alert)})
145
146
        if schedule_id:
147
            cmd.add_element("schedule", attrs={"id": schedule_id})
148
149
            if schedule_periods is not None:
150
                if (
151
                    not isinstance(schedule_periods, Integral)
152
                    or schedule_periods < 0
153
                ):
154
                    raise InvalidArgument(
155
                        "schedule_periods must be an integer greater or equal "
156
                        "than 0"
157
                    )
158
                cmd.add_element("schedule_periods", str(schedule_periods))
159
160
        if observers is not None:
161
            if not is_list_like(observers):
162
                raise InvalidArgumentType(
163
                    function=self.create_audit.__name__,
164
                    argument='observers',
165
                    arg_type='list',
166
                )
167
168
            # gvmd splits by comma and space
169
            # gvmd tries to lookup each value as user name and afterwards as
170
            # user id. So both user name and user id are possible
171
            cmd.add_element("observers", to_comma_list(observers))
172
173
        if preferences is not None:
174
            if not isinstance(preferences, Mapping):
175
                raise InvalidArgumentType(
176
                    function=self.create_audit.__name__,
177
                    argument='preferences',
178
                    arg_type=Mapping.__name__,
179
                )
180
181
            _xmlprefs = cmd.add_element("preferences")
182
            for pref_name, pref_value in preferences.items():
183
                _xmlpref = _xmlprefs.add_element("preference")
184
                _xmlpref.add_element("scanner_name", pref_name)
185
                _xmlpref.add_element("value", str(pref_value))
186
187
        return self._send_xml_command(cmd)
188
189
    def delete_audit(
190
        self, audit_id: str, *, ultimate: Optional[bool] = False

gvm/protocols/gmpv208/entities/tasks.py 1 location

@@ 78-195 (lines=118) @@
75
76
        return self._send_xml_command(cmd)
77
78
    def create_task(
79
        self,
80
        name: str,
81
        config_id: str,
82
        target_id: str,
83
        scanner_id: str,
84
        *,
85
        alterable: Optional[bool] = None,
86
        hosts_ordering: Optional[HostsOrdering] = None,
87
        schedule_id: Optional[str] = None,
88
        alert_ids: Optional[List[str]] = None,
89
        comment: Optional[str] = None,
90
        schedule_periods: Optional[int] = None,
91
        observers: Optional[List[str]] = None,
92
        preferences: Optional[dict] = None,
93
    ) -> Any:
94
        if not name:
95
            raise RequiredArgument(
96
                function=self.create_task.__name__, argument='name'
97
            )
98
99
        if not config_id:
100
            raise RequiredArgument(
101
                function=self.create_task.__name__, argument='config_id'
102
            )
103
104
        if not target_id:
105
            raise RequiredArgument(
106
                function=self.create_task.__name__, argument='target_id'
107
            )
108
109
        if not scanner_id:
110
            raise RequiredArgument(
111
                function=self.create_task.__name__, argument='scanner_id'
112
            )
113
114
        # don't allow to create a container task with create_task
115
        if target_id == '0':
116
            raise InvalidArgument(
117
                function=self.create_task.__name__, argument='target_id'
118
            )
119
120
        cmd = XmlCommand("create_task")
121
        cmd.add_element("name", name)
122
        cmd.add_element("usage_type", "scan")
123
        cmd.add_element("config", attrs={"id": config_id})
124
        cmd.add_element("target", attrs={"id": target_id})
125
        cmd.add_element("scanner", attrs={"id": scanner_id})
126
127
        if comment:
128
            cmd.add_element("comment", comment)
129
130
        if alterable is not None:
131
            cmd.add_element("alterable", to_bool(alterable))
132
133
        if hosts_ordering:
134
            if not isinstance(hosts_ordering, HostsOrdering):
135
                raise InvalidArgumentType(
136
                    function=self.create_task.__name__,
137
                    argument='hosts_ordering',
138
                    arg_type=HostsOrdering.__name__,
139
                )
140
            cmd.add_element("hosts_ordering", hosts_ordering.value)
141
142
        if alert_ids is not None:
143
            if not is_list_like(alert_ids):
144
                raise InvalidArgumentType(
145
                    function=self.modify_task.__name__,
146
                    argument='alert_ids',
147
                    arg_type='list',
148
                )
149
150
            if not len(alert_ids) == 0:
151
                for alert in alert_ids:
152
                    cmd.add_element("alert", attrs={"id": str(alert)})
153
154
        if schedule_id:
155
            cmd.add_element("schedule", attrs={"id": schedule_id})
156
157
            if schedule_periods is not None:
158
                if (
159
                    not isinstance(schedule_periods, Integral)
160
                    or schedule_periods < 0
161
                ):
162
                    raise InvalidArgument(
163
                        "schedule_periods must be an integer greater or equal "
164
                        "than 0"
165
                    )
166
                cmd.add_element("schedule_periods", str(schedule_periods))
167
168
        if observers is not None:
169
            if not is_list_like(observers):
170
                raise InvalidArgumentType(
171
                    function=self.create_task.__name__,
172
                    argument='observers',
173
                    arg_type='list',
174
                )
175
176
            # gvmd splits by comma and space
177
            # gvmd tries to lookup each value as user name and afterwards as
178
            # user id. So both user name and user id are possible
179
            cmd.add_element("observers", to_comma_list(observers))
180
181
        if preferences is not None:
182
            if not isinstance(preferences, Mapping):
183
                raise InvalidArgumentType(
184
                    function=self.create_task.__name__,
185
                    argument='preferences',
186
                    arg_type=Mapping.__name__,
187
                )
188
189
            _xmlprefs = cmd.add_element("preferences")
190
            for pref_name, pref_value in preferences.items():
191
                _xmlpref = _xmlprefs.add_element("preference")
192
                _xmlpref.add_element("scanner_name", pref_name)
193
                _xmlpref.add_element("value", str(pref_value))
194
195
        return self._send_xml_command(cmd)
196
197
    def delete_task(
198
        self, task_id: str, *, ultimate: Optional[bool] = False