Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python 3 upgrades #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 99 additions & 94 deletions api_test.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
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
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
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
{
Expand All @@ -44,83 +47,85 @@ def _req(url, payload, auth, headers, verify=True):

'''


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
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
selectors = ""
if len(fields) > 1:
for f in fields:
if isinstance(f, str):
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
# put selectors and nodename into simple query
query = " SELECT %s FROM view_device_v1 WHERE name = '%s' " % (selectors, nodename)
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'] is not 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()
Loading