upd
This commit is contained in:
parent
8d19d38a53
commit
7e540c54e9
1
main.py
1
main.py
|
@ -1,4 +1,3 @@
|
|||
import ng_openstack.auth
|
||||
import ng_openstack.keystone
|
||||
import os
|
||||
import json
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
import imp
|
||||
from ng_openstack.auth import Openstack_Auth
|
||||
from ng_openstack.openstackRequest import Openstack_Request
|
||||
from ng_openstack.keystone import Openstack_Keystone
|
||||
|
||||
class OpenstackConnection():
|
||||
auth=Openstack_Auth
|
||||
requestor=Openstack_Request()
|
||||
keystone=Openstack_Keystone(requestor)
|
||||
|
||||
|
|
|
@ -1,99 +0,0 @@
|
|||
class Openstack_Auth:
|
||||
|
||||
import os
|
||||
import exceptions
|
||||
import requests
|
||||
import json
|
||||
import redis
|
||||
import ng_openstack.settings as settings
|
||||
|
||||
|
||||
appDebug = True
|
||||
|
||||
def getToken(username, password, authDomain, scopeDomain, scopeProject):
|
||||
|
||||
try:
|
||||
return lookupTokenFromRedis(username, authDomain, scopeDomain, scopeProject)
|
||||
except exceptions.ItemNotFoundError:
|
||||
|
||||
url = os.getenv("OS_AUTH_URL") + '/auth/tokens'
|
||||
xdata = {
|
||||
"auth": {
|
||||
"scope": {
|
||||
"project": {
|
||||
"domain": {
|
||||
"name": scopeDomain
|
||||
},
|
||||
"id": scopeProject
|
||||
}
|
||||
},
|
||||
"identity": {
|
||||
"password": {
|
||||
"user": {
|
||||
"domain": {
|
||||
"name": authDomain
|
||||
},
|
||||
"password": password,
|
||||
"name": username
|
||||
}
|
||||
},
|
||||
"methods": [
|
||||
"password"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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:
|
||||
saveTokenToRedis(username, authDomain, scopeDomain, scopeProject, response.headers['X-Subject-Token'])
|
||||
return response.headers['X-Subject-Token']
|
||||
else:
|
||||
raise ValueError("Error in token response to token request:"+response.text)
|
||||
|
||||
def lookupTokenFromRedis(username, authDomain, scopeDomain, scopeProject):
|
||||
|
||||
REDIS=redis.Redis()
|
||||
|
||||
REDIS = redis.Redis(
|
||||
host=os.getenv("REDIS_HOST"),
|
||||
port=6379,
|
||||
password="")
|
||||
RedisKey= "OpenstackToken_" + username + "_" + authDomain + "_" + scopeDomain + "_" + scopeProject
|
||||
try:
|
||||
if REDIS.exists(RedisKey):
|
||||
#print("Got token from redis")
|
||||
token=REDIS.get(RedisKey).decode("utf-8")
|
||||
return token
|
||||
else:
|
||||
raise exceptions.ItemNotFoundError("OpenstackToken not found in redis")
|
||||
except:
|
||||
raise exceptions.ItemNotFoundError("OpenstackToken not found in redis")
|
||||
|
||||
def saveTokenToRedis(username, authDomain, scopeDomain, scopeProject,token):
|
||||
|
||||
REDIS=redis.Redis()
|
||||
|
||||
REDIS = redis.Redis(
|
||||
host=os.getenv("REDIS_HOST"),
|
||||
port=6379,
|
||||
password="")
|
||||
RedisKey = "OpenstackToken_" + username + "_" + authDomain + "_" + scopeDomain + "_" + scopeProject
|
||||
Token_CacheTimeout=150
|
||||
# print("Saving Token to redis with a {} second timeout".format(Token_CacheTimeout))
|
||||
try:
|
||||
REDIS.set(RedisKey,token)
|
||||
REDIS.expire(RedisKey, Token_CacheTimeout)
|
||||
return
|
||||
except:
|
||||
raise exceptions.ItemNotFoundError("OpenstackToken not saved in redis")
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
import ng_openstack.settings
|
||||
import pymysql.cursors
|
||||
import logging
|
||||
import exceptions
|
||||
import ng_openstack.exceptions as exceptions
|
||||
import os,settings
|
||||
|
||||
SQL_HOST = os.getenv("SQL_HOST")
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import settings,os
|
||||
import ng_openstack.openstackRequest
|
||||
import exceptions
|
||||
import ng_openstack.exceptions as exceptions
|
||||
import json
|
||||
import pybill_redis
|
||||
import requests
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import requests
|
||||
import redis
|
||||
import json
|
||||
import ng_openstack.auth
|
||||
import os
|
||||
from ng_openstack import exceptions
|
||||
|
||||
class Openstack_Request:
|
||||
def make_request(_self, getPost, url, data, apiEndpoint, scopedProjectID=""):
|
||||
|
||||
|
@ -15,7 +17,7 @@ class Openstack_Request:
|
|||
|
||||
data_json = json.dumps(data)
|
||||
|
||||
token = ng_openstack.auth.getToken(os.getenv("OS_USERNAME"), os.getenv("OS_PASSWORD"),
|
||||
token = _self.getToken(os.getenv("OS_USERNAME"), os.getenv("OS_PASSWORD"),
|
||||
os.getenv("OS_USER_DOMAIN_NAME"), os.getenv("OS_USER_DOMAIN_NAME"),
|
||||
scopedProjectID)
|
||||
# print (token)
|
||||
|
@ -35,3 +37,94 @@ class Openstack_Request:
|
|||
return response
|
||||
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:
|
||||
return _self.lookupTokenFromRedis(username, authDomain, scopeDomain, scopeProject)
|
||||
except exceptions.ItemNotFoundError:
|
||||
|
||||
url = os.getenv("OS_AUTH_URL") + '/auth/tokens'
|
||||
xdata = {
|
||||
"auth": {
|
||||
"scope": {
|
||||
"project": {
|
||||
"domain": {
|
||||
"name": scopeDomain
|
||||
},
|
||||
"id": scopeProject
|
||||
}
|
||||
},
|
||||
"identity": {
|
||||
"password": {
|
||||
"user": {
|
||||
"domain": {
|
||||
"name": authDomain
|
||||
},
|
||||
"password": password,
|
||||
"name": username
|
||||
}
|
||||
},
|
||||
"methods": [
|
||||
"password"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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:
|
||||
_self.saveTokenToRedis(username, authDomain, scopeDomain, scopeProject, response.headers['X-Subject-Token'])
|
||||
return response.headers['X-Subject-Token']
|
||||
else:
|
||||
raise ValueError("Error in token response to token request:"+response.text)
|
||||
|
||||
def lookupTokenFromRedis(_self,username, authDomain, scopeDomain, scopeProject):
|
||||
|
||||
REDIS=redis.Redis()
|
||||
|
||||
REDIS = redis.Redis(
|
||||
host=os.getenv("REDIS_HOST"),
|
||||
port=6379,
|
||||
password="")
|
||||
RedisKey= "OpenstackToken_" + username + "_" + authDomain + "_" + scopeDomain + "_" + scopeProject
|
||||
try:
|
||||
if REDIS.exists(RedisKey):
|
||||
#print("Got token from redis")
|
||||
token=REDIS.get(RedisKey).decode("utf-8")
|
||||
return token
|
||||
else:
|
||||
raise exceptions.ItemNotFoundError("OpenstackToken not found in redis")
|
||||
except:
|
||||
raise exceptions.ItemNotFoundError("OpenstackToken not found in redis")
|
||||
|
||||
def saveTokenToRedis(username, authDomain, scopeDomain, scopeProject,token):
|
||||
|
||||
REDIS=redis.Redis()
|
||||
|
||||
REDIS = redis.Redis(
|
||||
host=os.getenv("REDIS_HOST"),
|
||||
port=6379,
|
||||
password="")
|
||||
RedisKey = "OpenstackToken_" + username + "_" + authDomain + "_" + scopeDomain + "_" + scopeProject
|
||||
Token_CacheTimeout=150
|
||||
# print("Saving Token to redis with a {} second timeout".format(Token_CacheTimeout))
|
||||
try:
|
||||
REDIS.set(RedisKey,token)
|
||||
REDIS.expire(RedisKey, Token_CacheTimeout)
|
||||
return
|
||||
except:
|
||||
raise exceptions.ItemNotFoundError("OpenstackToken not saved in redis")
|
||||
|
||||
|
|
Loading…
Reference in New Issue