Beispiel #1
0
    def get(self, jwt, host_id, page_id, host_name=None):
        data = []

        # If page_id is None do it the slow way.
        if page_id is None:
            # If no host_id is passed in get all hosts.
            if (host_id is None) and (host_name is None):
                # This is slow so lets just return an error
                # hosts = Host.get_all()
                return jsonify(
                    error=True,
                    msg=
                    "Getting all hosts is slow. Please use the pagination endpoint at ./manage_host/page/"
                )
            elif host_id:
                if Host.get_by_id(host_id):
                    hosts = [Host.get_by_id(host_id)]
                else:
                    hosts = []
            elif host_name:
                hosts = [Host.get_by_hostname(host_name)]

            # Loop over results and get json form of host to return.
            if len(hosts) > 0:
                for host in hosts:
                    temp_data = host.serialize()
                    temp_data_services = Service.get_all_by_host_name(
                        host.host_name)
                    temp_data["all_services"] = []
                    for tds in temp_data_services:
                        temp_data["all_services"].append(tds.serialize())
                    data.append(temp_data)
                    pass
                return jsonify(data=data)
            else:
                return jsonify(error=True, msg="Host does not exist.")
        else:
            per_page = 10
            totalhosts = Host.get_count()
            total_pages = floor(totalhosts / per_page)
            hosts = Host.get_by_page((page_id * per_page), per_page)

            if hosts is not None:
                for host in hosts:
                    temp_data = host.serialize()
                    temp_data_services = Service.get_all_by_host_name(
                        host.host_name)
                    temp_data["all_services"] = []
                    for tds in temp_data_services:
                        temp_data["all_services"].append(tds.serialize())
                    data.append(temp_data)

            return jsonify({
                "data": data,
                "totalhosts": totalhosts,
                "this_page": page_id,
                "more": (page_id < total_pages)
            })