This commit is contained in:
Cory Hawkless 2022-11-02 16:00:36 +10:30
parent 097e315193
commit ec8ec4d40a
5 changed files with 25 additions and 12 deletions

View File

@ -3,8 +3,10 @@ import os
import json
import ng_openstack
# import ng_openstack
from ng_openstack.logger import log
log.info("Starting")
x=ng_openstack.OpenstackConnection("Fido",10)
# x.keystone.getAllProjects()
x.keystone.getCatalog()

View File

@ -1,6 +1,9 @@
import imp
from ng_openstack.openstackRequest import Openstack_Request
from ng_openstack.keystone import Openstack_Keystone
from ng_openstack.logger import log
class OpenstackConnection():
requestor=Openstack_Request()

View File

@ -2,6 +2,9 @@ class ItemNotFoundError(ValueError):
'''Used when looking up REDIS or SQL or any other database to indicate to the calling routine that the object
cant be found and to move along and try bother some other function
'''
class ItemNotSavedError(ValueError):
'''Item cant be saved to Redis
'''
class DeadIPFound(ValueError):
''''An ip has been saved to redis as being 'DEAD"'''

6
ng_openstack/logger.py Normal file
View File

@ -0,0 +1,6 @@
import os,logging
##LOGGING OPTIONS
LOG_LEVEL = os.getenv("LOG_LEVEL") or "DEBUG"
FORMAT = '%(asctime)s [%(levelname)s] %(message)s'
log = logging
log.basicConfig(format=FORMAT,level=LOG_LEVEL)

View File

@ -2,11 +2,14 @@ import requests
import redis
import json
import os
from ng_openstack.logger import log
from ng_openstack import exceptions
class Openstack_Request:
def make_request(_self, getPost, url, data, apiEndpoint, scopedProjectID=""):
print("Making a request {} {} ".format(apiEndpoint,url))
log.debug("Making a request {} {} ".format(apiEndpoint,url))
# 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")
@ -24,8 +27,7 @@ class Openstack_Request:
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":
@ -38,9 +40,6 @@ class Openstack_Request:
else:
raise ValueError ("Error in response return code" + str(response.status_code) + str(response.content))
appDebug = True
def getToken(_self,username, password, authDomain, scopeDomain, scopeProject):
try:
@ -76,17 +75,17 @@ class Openstack_Request:
}
response = requests.post(url)
#print(xdata)
headers = {'Content-type': 'application/json'}
#if appDebug: print(url)
response = requests.post(url, headers=headers,
data=json.dumps(xdata),
verify=False,
timeout=4)
if response.status_code == 201:
try:
_self.saveTokenToRedis( username, authDomain, scopeDomain, scopeProject, response.headers['X-Subject-Token'])
except exceptions.ItemNotSavedError:
log.warn("Error saving token to redis..meh")
return response.headers['X-Subject-Token']
else:
raise ValueError("Error in token response to token request:"+response.text)
@ -126,5 +125,5 @@ class Openstack_Request:
REDIS.expire(RedisKey, Token_CacheTimeout)
return
except:
raise exceptions.ItemNotFoundError("OpenstackToken not saved in redis")
raise exceptions.ItemNotSavedError("OpenstackToken not saved in redis")