-
Notifications
You must be signed in to change notification settings - Fork 13
/
api_test.py
75 lines (59 loc) · 2.23 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
#!/usr/bin/env python3
import json
import sys
try:
import requests
except ImportError:
print("Please install the python-requests module.")
sys.exit(-1)
SAT = "sat6.parmstrong.ca"
# URL for the API to your deployed Satellite 6 server
SAT_API = f"https://{SAT}/api/"
KATELLO_API = f"https://{SAT}/katello/api/v2/"
POST_HEADERS = {'content-type': 'application/json'}
# Default credentials to login to Satellite 6
USERNAME = "admin"
PASSWORD = "#Legend2013"
# Ignore SSL for now
# SSL_VERIFY = False
SSL_VERIFY = "/home/parmstro/Downloads/sat6.parmstrong.ca-katello-server-ca.crt" # Put the path to your CA certificate here to allow SSL_VERIFY
def get_json(url):
# Performs a GET using the passed URL location
r = requests.get(url, auth=(USERNAME, PASSWORD), verify=SSL_VERIFY)
return r.json()
def get_results(url):
jsn = get_json(url)
if jsn.get('error'):
print("Error: " + jsn['error']['message'])
else:
if jsn.get('results'):
return jsn['results']
elif 'results' not in jsn:
return jsn
else:
print("No results found")
return None
def display_all_results(url):
results = get_results(url)
if results:
print(json.dumps(results, indent=4, sort_keys=True))
def display_info_for_hosts(url):
hosts = get_results(url)
if hosts:
print(f"{'ID':10}{'Name':40}{'IP':30}{'Operating System':30}")
for host in hosts:
print(f"{str(host['id']):10}{host['name']:40}{str(host['ip']):30}{str(host['operatingsystem_name']):30}")
def main():
host = SAT
print(f"Displaying all info for host {host} ...")
display_all_results(SAT_API + 'hosts/' + host)
print(f"Displaying all facts for host {host} ...")
display_all_results(SAT_API + f'hosts/{host}/facts')
host_pattern = 'parmstrong.ca'
print(f"Displaying basic info for hosts matching pattern '{host_pattern}'...")
display_info_for_hosts(SAT_API + 'hosts?per_page=1&search=name~' + host_pattern)
environment = 'SOE_Development'
print(f"Displaying basic info for hosts in lifecycle environment {environment}...")
display_info_for_hosts(SAT_API + 'hosts?search=lifecycle_environment=' + environment)
if __name__ == "__main__":
main()