Second Script test
To achieve what you're asking, we will modify your first script to store the request_number ("REQ0011458") in a variable, then save it to a file that the second script can read and use.
Step 1: Modify the First Script to Store the Request Number
You can store the request_number in a variable and write it to a file (for example, a text file or JSON file) that the second script can read.
Here's the modified first script (generate_req.py):
pythonimport requests
import json
import os
# Replace environment variables with actual values or use them securely
user_name = os.environ.get('USER_NAME', 'your_user_name_here')
pwd = os.environ.get('PASSWORD', 'your_password_here')
api_root = os.environ.get('API_ROOT', 'https://atlas-api.ubsdev.net/api/')
def generate_req():
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
cat_item_sys_id = '4516dddd87cf06d00dc141d30cbb358b'
api_endpoint = f'sn_sc/servicecatalog/items/{cat_item_sys_id}/order_now'
data = {
'sysparm_requested_for': '3792a9cf4799b1d050d346f8736d4334',
'sysparm_quantity': 1,
'variables': {'hostname': 'ZU322K10'}
}
response = requests.post(
f'{api_root}{api_endpoint}',
auth=(user_name, pwd),
data=json.dumps(data),
headers=headers,
verify=False
)
if response.status_code == 200:
response_data = response.json()
request_number = response_data.get('result', {}).get('request_number')
print(f"REQ generated successfully: {request_number}")
# Write the request_number to a file
with open("request_number.txt", "w") as f:
f.write(request_number)
return request_number
else:
print(f"Failed to generate REQ. Status Code: {response.status_code}")
print(response.text)
return None
if __name__ == "__main__":
generate_req()
Step 2: Read the Request Number in the Second Script
In your second script (extract_req.py), you can now read the request_number from the file that was written by the first script and use it for extracting details.
Here's the second script modified to read the request_number from a file:
pythonimport requests
import json
import os
# Replace with actual ServiceNow auth token
x_snow_auth = os.environ.get('X_SNOW_AUTH', 'your_auth_token_here')
def extract_req_details():
# Read the request_number from the file
with open("request_number.txt", "r") as f:
request_number = f.read().strip()
headers = {
'x-snow-auth': x_snow_auth,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
payload = [
{ "$match": { "request.display_value": request_number } },
{ "$lookup": {
"from": "sc_task",
"localField": "_id",
"foreignField": "parent.sys_id",
"as": "sc_task"
}},
{ "$unwind": "$sc_task" },
{ "$project": {
"RITM Number": "$number",
"REQ Number": "$request.display_value",
"Catalog Item": "$cat_item.display_value",
"Task Number": "$sc_task.number",
"Task Description": "$sc_task.short_description",
"Task Assignment Group": "$sc_task.assignment_group.display_value",
"Task State": "$sc_task.state.display_value",
"Task Assigned To": "$sc_task.assigned_to.display_value"
}}
]
response = requests.post(
f'https://atlas-api.ubsdev.net/api/now/table/<table_name>', # Replace with correct API endpoint
headers=headers,
data=json.dumps(payload)
)
if response.status_code == 200:
req_details = response.json()
print(f"REQ details: {json.dumps(req_details, indent=4)}")
else:
print(f"Failed to retrieve REQ details. Status Code: {response.status_code}")
print(response.text)
if __name__ == "__main__":
extract_req_details()
Step 3: Running the Scripts in Order
Run the first script (
generate_req.py) to generate the REQ and store therequest_number:- This will create a file named
request_number.txtcontaining the generated REQ number.
bashpython generate_req.py- This will create a file named
Run the second script (
extract_req.py) to read therequest_numberfrom the file and extract the details:bashpython extract_req.py
Explanation:
The first script:
- Generates the REQ.
- Extracts the
request_numberfrom the response. - Writes the
request_numberto a file (request_number.txt).
The second script:
- Reads the
request_numberfrom the file. - Uses the
request_numberto fetch the details using the ServiceNow API.
- Reads the
This approach decouples the scripts and allows you to pass the request_number between them using a file. Let me know if you need more details!
No comments