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.keystone
|
||||
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"),
|
||||
os.getenv("OS_PROJECT_ID"))
|
||||
print(token)
|
||||
print (ng_openstack.keystone.getCatalog())
|
||||
|
||||
import ng_openstack
|
||||
|
||||
x=ng_openstack.OpenstackConnection("Fido",10)
|
||||
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
|
||||
import exceptions
|
||||
import requests
|
||||
import json
|
||||
import redis
|
||||
import ng_openstack.settings as settings
|
||||
class Openstack_Auth:
|
||||
|
||||
import os
|
||||
import exceptions
|
||||
import requests
|
||||
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:
|
||||
return lookupTokenFromRedis(username, authDomain, scopeDomain, scopeProject)
|
||||
except exceptions.ItemNotFoundError:
|
||||
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": {
|
||||
url = os.getenv("OS_AUTH_URL") + '/auth/tokens'
|
||||
xdata = {
|
||||
"auth": {
|
||||
"scope": {
|
||||
"project": {
|
||||
"domain": {
|
||||
"name": authDomain
|
||||
"name": scopeDomain
|
||||
},
|
||||
"password": password,
|
||||
"name": username
|
||||
"id": scopeProject
|
||||
}
|
||||
},
|
||||
"methods": [
|
||||
"password"
|
||||
]
|
||||
"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)
|
||||
#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)
|
||||
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)
|
||||
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):
|
||||
|
||||
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(
|
||||
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):
|
||||
|
||||
def saveTokenToRedis(username, authDomain, scopeDomain, scopeProject,token):
|
||||
REDIS=redis.Redis()
|
||||
|
||||
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")
|
||||
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,15 +1,38 @@
|
|||
import imp
|
||||
import json
|
||||
import ng_openstack.openstackRequest
|
||||
import ng_openstack.settings
|
||||
import os
|
||||
|
||||
def getAllProjects():
|
||||
projectData=ng_openstack.openstackRequest.openstackRequest("GET", "v3/projects", "",
|
||||
"http://172.25.110.138:5000").json()
|
||||
return projectData
|
||||
class Openstack_Keystone():
|
||||
def __init__(self, requestor):
|
||||
self.serviceData={}
|
||||
self.catalogData={}
|
||||
self.projectData={}
|
||||
self.requestor=requestor
|
||||
|
||||
|
||||
|
||||
def getCatalog():
|
||||
catalogData=ng_openstack.openstackRequest.openstackRequest("GET", "v3/endpoints", "",
|
||||
os.getenv("OS_AUTH_URL")).json()
|
||||
return catalogData
|
||||
def getAllProjects(_self):
|
||||
_self.projectData=_self.requestor.make_request("GET", "projects", "",
|
||||
os.getenv("OS_AUTH_URL")).json()
|
||||
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 json
|
||||
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)
|
||||
if scopedProjectID== "":
|
||||
scopedProjectID=os.getenv("SNGD_OPENSTACK_SCOPEPROJECTID")
|
||||
else:
|
||||
scopedProjectID=scopedProjectID
|
||||
str(url).replace(" ","%20")
|
||||
|
||||
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"),
|
||||
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")
|
||||
|
||||
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))
|
||||
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