new-openstackcheck/ng_openstack/openstackRequest.py

38 lines
1.4 KiB
Python
Raw Normal View History

2022-10-10 05:25:49 +00:00
import requests
import json
import ng_openstack.auth
2022-10-10 06:33:10 +00:00
import os
class Openstack_Request:
def make_request(_self, getPost, url, data, apiEndpoint, scopedProjectID=""):
2022-10-10 05:25:49 +00:00
2022-10-10 06:33:10 +00:00
# Default the scope project to the value set in .env.example, allow the user to override if required(EG creating backups)
if scopedProjectID== "":
scopedProjectID=os.getenv("OS_PROJECT_ID")
else:
scopedProjectID=scopedProjectID
2022-10-10 05:25:49 +00:00
2022-10-10 06:33:10 +00:00
str(url).replace(" ","%20")
2022-10-10 05:25:49 +00:00
2022-10-10 06:33:10 +00:00
data_json = json.dumps(data)
2022-10-10 05:25:49 +00:00
2022-10-10 06:33:10 +00:00
token = ng_openstack.auth.getToken(os.getenv("OS_USERNAME"), os.getenv("OS_PASSWORD"),
os.getenv("OS_USER_DOMAIN_NAME"), os.getenv("OS_USER_DOMAIN_NAME"),
scopedProjectID)
# print (token)
headers = {'Content-type': 'application/json', 'X-Auth-Token': token}
2022-10-10 05:25:49 +00:00
2022-10-10 06:33:10 +00:00
url = apiEndpoint + "/" + url
print (url)
# print(data_json)
if getPost=="GET":
response = requests.get(url, headers=headers)
elif getPost=="POST":
response = requests.post(url, data=data_json, headers=headers)
else:
raise ValueError("Unknown request type")
2022-10-10 05:25:49 +00:00
2022-10-10 06:33:10 +00:00
if 200<= response.status_code <= 300 :
return response
else:
raise ValueError ("Error in response return code" + str(response.status_code) + str(response.content))