Code Duplication    Length = 112-133 lines in 2 locations

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

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

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

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