def bulk_create_hosts(params): """Bulk create hosts""" body = params['body'] entries = body['entries'] failed_hosts = [] folder: watolib.CREFolder for folder, grouped_hosts in itertools.groupby( body['entries'], operator.itemgetter('folder')): validated_entries = [] folder.prepare_create_hosts() for host in grouped_hosts: host_name = host["host_name"] attributes = host["attributes"] try: folder.verify_host_details(host_name, host["attributes"]) except (MKUserError, MKAuthException): failed_hosts.append(host_name) validated_entries.append((host_name, attributes, None)) folder.create_validated_hosts(validated_entries, bake_hosts=False) try_bake_agents_for_hosts([host["host_name"] for host in body["entries"]]) if failed_hosts: return problem( status=400, title="Provided details for some hosts are faulty", detail= f"Validated hosts were saved. The configurations for following hosts are faulty and " f"were skipped: {' ,'.join(failed_hosts)}.") hosts = [watolib.Host.host(entry['host_name']) for entry in entries] return host_collection(hosts)
def bulk_create_hosts(params): """Bulk create hosts""" # TODO: addition of etag mechanism body = params['body'] entries = body['entries'] for folder, grouped_hosts in itertools.groupby(body['entries'], operator.itemgetter('folder')): folder.create_hosts( [(host['host_name'], host['attributes'], None) for host in grouped_hosts], bake_hosts=False) try_bake_agents_for_hosts([host["host_name"] for host in body["entries"]]) hosts = [watolib.Host.host(entry['host_name']) for entry in entries] return _host_collection(hosts)
def _bulk_action(self, request, action_name): result: Dict[str, Any] = { "succeeded_hosts": [], "failed_hosts": {}, } for host_request in request["hosts"]: try: if action_name == "add": self._add(host_request, bake_hosts=False) elif action_name == "edit": self._edit(host_request) else: raise NotImplementedError() result["succeeded_hosts"].append(host_request["hostname"]) except Exception as e: result["failed_hosts"][host_request["hostname"]] = "%s" % e try_bake_agents_for_hosts(result["succeeded_hosts"]) return result