upd
This commit is contained in:
parent
e8436e6540
commit
8d19d38a53
26
main.py
26
main.py
|
@ -1,9 +1,25 @@
|
||||||
import ng_openstack.auth
|
import ng_openstack.auth
|
||||||
import ng_openstack.keystone
|
import ng_openstack.keystone
|
||||||
import os
|
import os
|
||||||
|
import json
|
||||||
|
|
||||||
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"),
|
import ng_openstack
|
||||||
os.getenv("OS_PROJECT_ID"))
|
|
||||||
print(token)
|
x=ng_openstack.OpenstackConnection("Fido",10)
|
||||||
print (ng_openstack.keystone.getCatalog())
|
print (x.keystone.getAllProjects())
|
||||||
|
|
||||||
|
|
||||||
|
# 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"),
|
||||||
|
# os.getenv("OS_PROJECT_ID"))
|
||||||
|
# print(token)
|
||||||
|
|
||||||
|
# # print(json.dumps(ng_openstack.keystone.getAllProjects(), indent=4))
|
||||||
|
# cat=ng_openstack.keystone.getCatalog()
|
||||||
|
|
||||||
|
|
||||||
|
# print (ng_openstack.keystone.getServicebyID("11f21a87fdaa4f5d9bd93c7396bdd93c"))
|
||||||
|
|
||||||
|
# # openstack=newOpenstack.connect()
|
||||||
|
# # projects=openstack.projects.getAll()
|
|
@ -0,0 +1,59 @@
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
def __init__(self, name, age):
|
||||||
|
self.name = name
|
||||||
|
self.age = age
|
||||||
|
self.speed = 0
|
||||||
|
self.moving = False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def human_age(self):
|
||||||
|
return self.age * 7
|
||||||
|
|
||||||
|
@human_age.setter
|
||||||
|
def human_age(self, value):
|
||||||
|
self.age = value / 7
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return '{} is {} years old'.format(
|
||||||
|
self.name, self.age
|
||||||
|
)
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.name == other.name and self.age == other.age
|
||||||
|
|
||||||
|
def make_sound(self):
|
||||||
|
print(self.SOUND)
|
||||||
|
|
||||||
|
def bark(self):
|
||||||
|
self.make_sound()
|
||||||
|
|
||||||
|
def walk(self, speed, distance):
|
||||||
|
self._set_speed(speed)
|
||||||
|
self._move(distance)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def can_woof(cls, animal):
|
||||||
|
return animal.SOUND == cls.SOUND
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def is_dog(animal):
|
||||||
|
return animal.can_woof()
|
||||||
|
|
||||||
|
def _set_speed(self, speed):
|
||||||
|
self.speed = speed
|
||||||
|
|
||||||
|
def _move(self, distance):
|
||||||
|
self.moving = True
|
||||||
|
for _ in range(distance):
|
||||||
|
print('.', end='')
|
||||||
|
print('')
|
|
@ -1,98 +1,99 @@
|
||||||
import os
|
class Openstack_Auth:
|
||||||
import exceptions
|
|
||||||
import requests
|
import os
|
||||||
import json
|
import exceptions
|
||||||
import redis
|
import requests
|
||||||
import ng_openstack.settings as settings
|
import json
|
||||||
|
import redis
|
||||||
|
import ng_openstack.settings as settings
|
||||||
|
|
||||||
|
|
||||||
appDebug = True
|
appDebug = True
|
||||||
|
|
||||||
def getToken(username, password, authDomain, scopeDomain, scopeProject):
|
def getToken(username, password, authDomain, scopeDomain, scopeProject):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return lookupTokenFromRedis(username, authDomain, scopeDomain, scopeProject)
|
return lookupTokenFromRedis(username, authDomain, scopeDomain, scopeProject)
|
||||||
except exceptions.ItemNotFoundError:
|
except exceptions.ItemNotFoundError:
|
||||||
|
|
||||||
url = os.getenv("OS_AUTH_URL") + '/auth/tokens'
|
url = os.getenv("OS_AUTH_URL") + '/auth/tokens'
|
||||||
xdata = {
|
xdata = {
|
||||||
"auth": {
|
"auth": {
|
||||||
"scope": {
|
"scope": {
|
||||||
"project": {
|
"project": {
|
||||||
"domain": {
|
|
||||||
"name": scopeDomain
|
|
||||||
},
|
|
||||||
"id": scopeProject
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"identity": {
|
|
||||||
"password": {
|
|
||||||
"user": {
|
|
||||||
"domain": {
|
"domain": {
|
||||||
"name": authDomain
|
"name": scopeDomain
|
||||||
},
|
},
|
||||||
"password": password,
|
"id": scopeProject
|
||||||
"name": username
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"methods": [
|
"identity": {
|
||||||
"password"
|
"password": {
|
||||||
]
|
"user": {
|
||||||
|
"domain": {
|
||||||
|
"name": authDomain
|
||||||
|
},
|
||||||
|
"password": password,
|
||||||
|
"name": username
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"methods": [
|
||||||
|
"password"
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
response = requests.post(url)
|
response = requests.post(url)
|
||||||
#print(xdata)
|
#print(xdata)
|
||||||
headers = {'Content-type': 'application/json'}
|
headers = {'Content-type': 'application/json'}
|
||||||
#if appDebug: print(url)
|
#if appDebug: print(url)
|
||||||
|
|
||||||
response = requests.post(url, headers=headers,
|
response = requests.post(url, headers=headers,
|
||||||
data=json.dumps(xdata),
|
data=json.dumps(xdata),
|
||||||
verify=False,
|
verify=False,
|
||||||
timeout=4)
|
timeout=4)
|
||||||
|
|
||||||
if response.status_code == 201:
|
if response.status_code == 201:
|
||||||
saveTokenToRedis(username, authDomain, scopeDomain, scopeProject, response.headers['X-Subject-Token'])
|
saveTokenToRedis(username, authDomain, scopeDomain, scopeProject, response.headers['X-Subject-Token'])
|
||||||
return response.headers['X-Subject-Token']
|
return response.headers['X-Subject-Token']
|
||||||
else:
|
else:
|
||||||
raise ValueError("Error in token response to token request:"+response.text)
|
raise ValueError("Error in token response to token request:"+response.text)
|
||||||
|
|
||||||
|
def lookupTokenFromRedis(username, authDomain, scopeDomain, scopeProject):
|
||||||
|
|
||||||
def lookupTokenFromRedis(username, authDomain, scopeDomain, scopeProject):
|
REDIS=redis.Redis()
|
||||||
|
|
||||||
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")
|
||||||
|
|
||||||
REDIS = redis.Redis(
|
def saveTokenToRedis(username, authDomain, scopeDomain, scopeProject,token):
|
||||||
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()
|
REDIS = redis.Redis(
|
||||||
|
host=os.getenv("REDIS_HOST"),
|
||||||
REDIS = redis.Redis(
|
port=6379,
|
||||||
host=os.getenv("REDIS_HOST"),
|
password="")
|
||||||
port=6379,
|
RedisKey = "OpenstackToken_" + username + "_" + authDomain + "_" + scopeDomain + "_" + scopeProject
|
||||||
password="")
|
Token_CacheTimeout=150
|
||||||
RedisKey = "OpenstackToken_" + username + "_" + authDomain + "_" + scopeDomain + "_" + scopeProject
|
# print("Saving Token to redis with a {} second timeout".format(Token_CacheTimeout))
|
||||||
Token_CacheTimeout=150
|
try:
|
||||||
# print("Saving Token to redis with a {} second timeout".format(Token_CacheTimeout))
|
REDIS.set(RedisKey,token)
|
||||||
try:
|
REDIS.expire(RedisKey, Token_CacheTimeout)
|
||||||
REDIS.set(RedisKey,token)
|
return
|
||||||
REDIS.expire(RedisKey, Token_CacheTimeout)
|
except:
|
||||||
return
|
raise exceptions.ItemNotFoundError("OpenstackToken not saved in redis")
|
||||||
except:
|
|
||||||
raise exceptions.ItemNotFoundError("OpenstackToken not saved in redis")
|
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,38 @@
|
||||||
|
import imp
|
||||||
import json
|
import json
|
||||||
import ng_openstack.openstackRequest
|
import ng_openstack.openstackRequest
|
||||||
|
import ng_openstack.settings
|
||||||
|
import os
|
||||||
|
|
||||||
def getAllProjects():
|
class Openstack_Keystone():
|
||||||
projectData=ng_openstack.openstackRequest.openstackRequest("GET", "v3/projects", "",
|
def __init__(self, requestor):
|
||||||
"http://172.25.110.138:5000").json()
|
self.serviceData={}
|
||||||
return projectData
|
self.catalogData={}
|
||||||
|
self.projectData={}
|
||||||
|
self.requestor=requestor
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def getCatalog():
|
def getAllProjects(_self):
|
||||||
catalogData=ng_openstack.openstackRequest.openstackRequest("GET", "v3/endpoints", "",
|
_self.projectData=_self.requestor.make_request("GET", "projects", "",
|
||||||
os.getenv("OS_AUTH_URL")).json()
|
os.getenv("OS_AUTH_URL")).json()
|
||||||
return catalogData
|
return _self.projectData
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def getCatalog():
|
||||||
|
catalogData=json.dumps(ng_openstack.openstackRequest.openstackRequest("GET", "endpoints", "",
|
||||||
|
os.getenv("OS_AUTH_URL")).json())
|
||||||
|
|
||||||
|
serviceData=json.dumps(ng_openstack.openstackRequest.openstackRequest("GET", "services", "",
|
||||||
|
os.getenv("OS_AUTH_URL")).json())
|
||||||
|
print(serviceData)
|
||||||
|
|
||||||
|
return serviceData
|
||||||
|
|
||||||
|
|
||||||
|
def getServicebyID(id):
|
||||||
|
print(serviceData)
|
||||||
|
# for _service in service_Data['services']:
|
||||||
|
# if _service['id']==id:
|
||||||
|
# return(_service)
|
|
@ -1,37 +1,37 @@
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
import ng_openstack.auth
|
import ng_openstack.auth
|
||||||
import settings,os
|
import os
|
||||||
|
class Openstack_Request:
|
||||||
|
def make_request(_self, getPost, url, data, apiEndpoint, scopedProjectID=""):
|
||||||
|
|
||||||
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("OS_PROJECT_ID")
|
||||||
|
else:
|
||||||
|
scopedProjectID=scopedProjectID
|
||||||
|
|
||||||
# Default the scope project to the value set in .env.example, allow the user to override if required(EG creating backups)
|
str(url).replace(" ","%20")
|
||||||
if scopedProjectID== "":
|
|
||||||
scopedProjectID=os.getenv("SNGD_OPENSTACK_SCOPEPROJECTID")
|
|
||||||
else:
|
|
||||||
scopedProjectID=scopedProjectID
|
|
||||||
|
|
||||||
str(url).replace(" ","%20")
|
data_json = json.dumps(data)
|
||||||
|
|
||||||
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}
|
||||||
|
|
||||||
token = ng_openstack.auth.getToken(os.getenv("OS_USERNAME"), os.getenv("OS_PASSWORD"),
|
url = apiEndpoint + "/" + url
|
||||||
os.getenv("OS_USER_DOMAIN_NAME"), os.getenv("OS_USER_DOMAIN_NAME"),
|
print (url)
|
||||||
scopedProjectID)
|
# print(data_json)
|
||||||
# print (token)
|
if getPost=="GET":
|
||||||
headers = {'Content-type': 'application/json', 'X-Auth-Token': token}
|
response = requests.get(url, headers=headers)
|
||||||
|
elif getPost=="POST":
|
||||||
|
response = requests.post(url, data=data_json, headers=headers)
|
||||||
|
else:
|
||||||
|
raise ValueError("Unknown request type")
|
||||||
|
|
||||||
url = apiEndpoint + "/" + url
|
if 200<= response.status_code <= 300 :
|
||||||
# print (url)
|
return response
|
||||||
# print(data_json)
|
else:
|
||||||
if getPost=="GET":
|
raise ValueError ("Error in response return code" + str(response.status_code) + str(response.content))
|
||||||
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))
|
|
||||||
|
|
Loading…
Reference in New Issue