This repository has been archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
python_jams_rest.py
122 lines (91 loc) · 3.02 KB
/
python_jams_rest.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
import requests
import json
# This sets all of our variables and end points
uri_base = 'http://host_or_ip/JAMS'
auth_uri = uri_base + '/api/authentication/login'
jobs_uri = uri_base + '/api/job'
setups_uri = uri_base + '/api/setup'
# This one looks different, because we're passing an existing Agent name in
existing_agent_uri = uri_base + '/api/agent/NewServer'
agent_uri = uri_base + '/api/agent'
queue_uri = uri_base + '/api/batchqueue'
cfg_uri = uri_base + '/api/cfg'
spcfc_cfg_uri = uri_base + '/api/cfg/SMTPServer'
username = ''
password = ''
# Create and populate the payload dictionary
payload = {
"username": username,
"password": password
}
# Populate the headers
headers = {'content-type': 'application/json'}
# Login to the API
response = requests.post(auth_uri, data=json.dumps(payload), headers=headers)
# Raise an exception if there is an error
response.raise_for_status()
# Save the authentication token
resp = response.json()
token = resp['access_token']
expires = resp['expires_in']
print('{}'.format(token))
# Recreate the headers with the token now
headers = {
'Authorization': 'Bearer ' + token,
'content-type': 'application/json'
}
# Get a list of Jobs
# Make the GET request
response = requests.get(jobs_uri, headers=headers)
# Raise an exception if there is an error
response.raise_for_status()
jobs = response.json()
# Now we want to only get the name of the Jobs
list_of_jobs = [j['jobName'] for j in jobs]
for job in list_of_jobs:
print job
# Now we can do the same exact process, but with setups
# Make the GET request
response = requests.get(setups_uri, headers=headers)
# Raise an exception if there is an error
response.raise_for_status()
setups = response.json()
# Now we want to only get the name of the Setups
list_of_setups = [s['setupName'] for s in sets]
for setup in list_of_setups:
print setup
# Create a new Agent from an existing one as a template
agent = requests.get(existing_agent_uri, headers=headers)
agent.raise_for_status()
agent = agent.json()
agent['agentName'] = 'NewAgentFromPython'
agent['description'] = 'This was created in Python'
response = requests.post(agent_uri, headers=headers, data=json.dumps(agent))
# Create a batch queue
batch_queue = {
"queueName": "TestCreate",
"description": "Testing from Python",
"jobCount": 4,
"jobLimit": 5,
"started": True,
"startedOn": [
{
"nodeName": "testnode1"
},
{
"nodeName": "testnode2"
}
]
}
response = requests.post(queue_uri, headers=headers, data=json.dumps(batch_queue))
response.raise_for_status()
# Check config information
response = requests.get(cfg_uri, headers=headers)
# Check to make sure our SMTP server is set
response = requests.get(spcfc_cfg_uri, headers=headers)
smtp_config = response.json()
smtp_server = smtp_config['value']
if smtp_server:
print('SMTP Server defined as {}'.format(smtp_server))
else:
print('SMTP Server not defined! Please set.')