示例#1
0
    def get_status(self):

        version_str = ""
        try:
            fp = request.urlopen("http://www.elastichq.org/currversion.json",
                                 timeout=10)
            mbyte = fp.read()
            version_str = mbyte.decode("utf-8")
            fp.close()
        except Exception as ex:
            LOG.error("error retrieving version information", ex)

        stable_version = (json.loads(version_str)).get("version", None)

        clusters = ClusterService().get_clusters(create_if_missing=False)
        schema = ClusterDTO(many=True)
        result = schema.dump(clusters)

        status = {
            "name": "ElasticHQ",
            "installed_version": current_app.config.get('API_VERSION'),
            "current_stable_version": stable_version,
            "tagline": "You know, for Elasticsearch",
            "clusters": result.data
        }
        return status
示例#2
0
    def get(self):
        """Returns a collection of clusters.


        **Example request**:

        .. sourcecode:: http

          GET /api/clusters/ HTTP/1.1
          Accept: application/json

        **Example response**:

        .. sourcecode:: http

          HTTP/1.1 200 OK
          Vary: Accept
          Content-Type: application/json

        .. code-block:: json

            {
                "status_code": 200,
                "response_time": 1648,
                "message": null,
                "data": [
                    {
                        "cluster_name": "",
                        "cluster_ip": "",
                        "cluster_port": "9200",
                        "cluster_scheme": "http",
                        "cluster_connected": true,
                        "cluster_host": "http://10.0.0.0:9200",
                        "cluster_version": "2.3.5",
                        "cluster_health": {  }
                    }
                ]
            }

        **Response Structure**

          - *(dict) --*

            - **cluster_name** *(string) --* cluster name
            - **cluster_ip** *(string) --* IP or host
            - **cluster_port** *(string) --*
            - **cluster_scheme** *(string) --*
            - **cluster_connected** *(boolean) --* Whether there was a successful connection.
            - **cluster_host** *(string) --* The complete connection url
            - **cluster_version** *(string) --* Elasticsearch version

        :resheader Content-Type: application/json
        :status 200: OK
        :status 500: server error
        """
        response = ClusterService().get_clusters()

        schema = ClusterDTO(many=True)
        result = schema.dump(response)
        return APIResponse(result.data, HTTP_Status.OK, None)
示例#3
0
    def post(self):
        """

        Creates a connection to a given host/port. Accepts a JSON POST BODY. This will add the connection, if it doesn't already
        exist, to the pool of connections and save the details in the database.

        .. :quickref: ClusterConnection; Creates a connection to the cluster.

        **Example request**:

        .. sourcecode:: http

          POST /api/clusters/_connect/ HTTP/1.1
          Accept: application/json

        .. code-block:: json

            {
                "ip": "127.0.0.1",
                "port": "9200",
                "use_ssl": false
            }

        **Request Structure**

          - *(dict) --*

            - **ip** *(string) --* IP address or host name
            - **port** *(string) --* ES REST API port
            - **use_ssl** *(boolean) --* Whether to use HTTPS or not.

        **Example response**:

        .. sourcecode:: http

          HTTP/1.1 201
          Content-Type: application/json

        .. code-block:: json

            {
              "data": [
                {
                  "cluster_name": "",
                  "cluster_ip": "",
                  "cluster_port": "9200",
                  "cluster_scheme": "http",
                  "cluster_connected": true,
                  "cluster_host": "http://10.0.0.0:9200",
                  "cluster_version": "2.3.5"
                }
              ],
              "status_code": 200,
              "message": null,
              "response_time": 92
            }


        **Response Structure**

          - *(dict) --*

            - **cluster_name** *(string) --* cluster name
            - **cluster_ip** *(string) --* IP or host
            - **cluster_port** *(string) --*
            - **cluster_scheme** *(string) --*
            - **cluster_connected** *(boolean) --* Whether there was a successful connection.
            - **cluster_host** *(string) --* The complete connection url
            - **cluster_version** *(string) --* Elasticsearch version


        :reqheader Accept: application/json
        :resheader Content-Type: application/json
        :status 201: connection created
        :status 400: bad request
        :status 500: server error
        """
        json_data = request.get_json(force=True)
        params = request.values.to_dict()
        params.update(json_data)

        if params.get('ip', None) is None:
            raise BadRequest(message='Missing required parameters.')

        scheme = 'http'
        if params.get('use_ssl', False) is True:
            scheme = 'https'

        try:
            enable_ssl = current_app.config.get('ENABLE_SSL', False)
            ca_certs = current_app.config.get('CA_CERTS', None)
            verify_certs = current_app.config.get('VERIFY_CERTS', None)
            client_key = current_app.config.get('CLIENT_KEY', None)
            client_cert = current_app.config.get('CLIENT_CERT', None)

            print(client_key)
            print(client_cert)
            response = ConnectionService().create_connection(ip=params['ip'], port=params.get('port', "9200"),
                                                             scheme=scheme, username=params.get('username', None),
                                                             password=params.get('password', None),
                                                             fail_on_exception=True,
                                                             enable_ssl=enable_ssl, ca_certs=ca_certs,
                                                             verify_certs=verify_certs,
                                                             client_key=client_key,
                                                             client_cert=client_cert)

            schema = ClusterDTO(many=False)
            result = schema.dump(response)
            return APIResponse(result.data, HTTP_Status.CREATED, None)
        except ConnectionNotAuthorized as cna:
            return APIResponse([], HTTP_Status.UNAUTHORIZED, None)
        except ConnectionError as ce:
            return APIResponse([], HTTP_Status.NOT_FOUND, None)