-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_test.py
126 lines (106 loc) · 3.48 KB
/
api_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import requests, csv, json, yaml, os, sys
# api_test.py is to help you test doql queries without the full external pillar script
CUR_DIR = os.path.dirname(os.path.abspath(__file__))
def get_config(cfgpath):
if not os.path.exists(cfgpath):
if not os.path.exists(os.path.join(CUR_DIR, cfgpath)):
raise ValueError("Config file %s is not found!" % cfgpath)
cfgpath = os.path.join(CUR_DIR, cfgpath)
with open(cfgpath, 'r') as cfgf:
config = yaml.load(cfgf.read())
return config
def _req(url, payload, auth, headers, verify=True):
response = requests.request(
"POST",
url,
data=payload,
auth=auth,
headers=headers,
verify=verify
)
if response.status_code >= 400:
print 'D42 Response text: {0}'.format(response.text)
response.raise_for_status()
return response
'''
this is what is returned from the view_device_custom_fields_v1 table
{
"device_fk": "19",
"device_name": "ubuntu.saltmaster5",
"filterable": "f",
"is_multi": "f",
"key": "Salt Node ID",
"log_for_api": "t",
"mandatory": "f",
"related_model_name": "",
"type": "Text",
"type_id": "1",
"value": "ubuntu.saltmaster5"
}
'''
def request_minion_info(query, config):
payload = {
"query": query,
#"query": "SELECT %s FROM view_device_v1, view_device_custom_fields_v1 WHERE view_device_v1.name = '%s' AND view_device_v1.name=view_device_custom_fields_v1.device_name" % (fields_to_get, nodename),
# original "query": "SELECT %s FROM view_device_v1 WHERE name = '%s'" % (fields_to_get, nodename),
# works "query": "SELECT %s FROM view_device_custom_fields_v1 WHERE device_name='%s' " % (fields_to_get, nodename),
"header": "yes"
}
print 'generated payload: ' + json.dumps(payload, indent=4)
headers={'Accept': 'application/json'}
auth = (config['user'], config['pass'] )
url = "%s/services/data/v1.0/query/" % config['host']
sslverify = config['sslverify']
try:
response = _req(url, payload, auth, headers, sslverify)
except Exception as e:
print "D42 Error: {0}".format(str(e))
return None
return response
def generate_simple_query(fields, nodename):
selectors = ""
if len(fields) > 1:
for f in fields:
if isinstance(f, basestring):
print 'yes: ' + f
selectors += "%s," % f
else:
# throw exception for type error
print 'no: ' + f
selectors = selectors[:-1] # remove coma which will trail this due to loop above
else: # only 1 field in fields...
selectors += fields
# write the simple query
query = " SELECT %s FROM view_device_v1 WHERE name = '%s' " % (selectors, nodename) # put selectors and nodename into simple query
return query
def main():
config = get_config('settings_d42.yaml')
if len(sys.argv) < 2:
print 'Error: Missing hostname to test query'
print 'Usage: {0} <hostname>'.format(sys.argv[0])
return None
else:
nodename = sys.argv[1]
if config['query'] != None:
query = config['query'].format(minion_name=nodename)
else:
query = generate_simple_query(config['default_fields_to_get'], nodename)
print '\n\n query: %s \n\n ' % (query)
response = request_minion_info(query, config)
if response:
listrows = response.text.split('\n')
fields = listrows[0].split(',')
rows = csv.reader(listrows[1:])
out = []
for row in rows:
items = zip(fields, row)
item = {}
for (name, value) in items:
item[name] = value.strip()
out.append(item)
print "output: " + json.dumps(out[0], indent=4, sort_keys=True)
return out
else:
return None
if __name__ == "__main__":
main()