aa8a65d79305c1d9c99bc537ce50dceaae4c44b6
[releng.git] / utils / test / scripts / shared_utils.py
1 import json
2
3 import urllib3
4
5 http = urllib3.PoolManager()
6
7
8 def delete_request(url, creds, body=None):
9     headers = urllib3.make_headers(basic_auth=creds)
10     http.request('DELETE', url, headers=headers, body=body)
11
12
13 def publish_json(json_ojb, creds, to):
14     json_dump = json.dumps(json_ojb)
15     if to == 'stdout':
16         print json_dump
17         return 200, None
18     else:
19         headers = urllib3.make_headers(basic_auth=creds)
20         result = http.request('POST', to, headers=headers, body=json_dump)
21         return result.status, result.data
22
23
24 def _get_nr_of_hits(elastic_json):
25     return elastic_json['hits']['total']
26
27
28 def get_elastic_docs(elastic_url, creds, body=None, field = '_source'):
29
30     # 1. get the number of results
31     headers = urllib3.make_headers(basic_auth=creds)
32     elastic_json = json.loads(http.request('GET', elastic_url + '/_search?size=0', headers=headers, body=body).data)
33     print elastic_json
34     nr_of_hits = _get_nr_of_hits(elastic_json)
35
36     # 2. get all results
37     elastic_json = json.loads(http.request('GET', elastic_url + '/_search?size={}'.format(nr_of_hits), headers=headers, body=body).data)
38
39     elastic_docs = []
40     for hit in elastic_json['hits']['hits']:
41         elastic_docs.append(hit[field])
42     return elastic_docs
43
44 def get_elastic_docs_by_days(elastic_url, creds, days):
45     if days == 0:
46         body = '''{
47             "query": {
48                 "match_all": {}
49             }
50         }'''
51     elif days > 0:
52         body = '''{{
53             "query" : {{
54                 "range" : {{
55                     "start_date" : {{
56                         "gte" : "now-{}d"
57                     }}
58                 }}
59             }}
60         }}'''.format(days)
61     else:
62         raise Exception('Update days must be non-negative')
63     return get_elastic_docs(elastic_url, creds, body)