new-openstackcheck/ng_openstack/openstackRequest.py

38 lines
1.3 KiB
Python
Raw Normal View History

2022-10-10 05:25:49 +00:00
import requests
import json
import ng_openstack.auth
import settings,os
def openstackRequest(getPost, url, data, apiEndpoint, scopedProjectID=""):
# 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("SNGD_OPENSTACK_SCOPEPROJECTID")
else:
scopedProjectID=scopedProjectID
str(url).replace(" ","%20")
data_json = json.dumps(data)
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}
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")
if 200<= response.status_code <= 300 :
return response
else:
raise ValueError ("Error in response return code" + str(response.status_code) + str(response.content))