only get specific docs when checking the existed ones in elasticsearch
[releng.git] / utils / test / dashboard / dashboard / common / elastic_access.py
1 import json
2
3 import urllib3
4
5 http = urllib3.PoolManager()
6
7
8 def _request(method, url, creds=None, body=None):
9     headers = urllib3.make_headers(basic_auth=creds)
10     return http.request(method, url, headers=headers, body=body)
11
12
13 def _post(url, creds=None, body=None):
14     return _request('POST', url, creds=creds, body=body)
15
16
17 def _get(url, creds=None, body=None):
18     return json.loads(_request('GET', url, creds=creds, body=body).data)
19
20
21 def delete_docs(url, creds=None, body=None):
22     return _request('DELETE', url, creds=creds, body=body)
23
24
25 def publish_docs(docs, creds, to):
26     json_docs = json.dumps(docs)
27     if to == 'stdout':
28         print json_docs
29         return 200, None
30     else:
31         result = _post(to, creds=creds, body=json_docs)
32         return result.status, result.data
33
34
35 def _get_docs_nr(url, creds=None, body=None):
36     res_data = _get('{}/_search?size=0'.format(url), creds=creds, body=body)
37     print type(res_data), res_data
38     return res_data['hits']['total']
39
40
41 def get_docs(url, creds=None, body=None, field='_source'):
42
43     docs_nr = _get_docs_nr(url, creds=creds, body=body)
44     res_data = _get('{}/_search?size={}'.format(url, docs_nr),
45                     creds=creds, body=body)
46
47     docs = []
48     for hit in res_data['hits']['hits']:
49         docs.append(hit[field])
50     return docs