Compare commits
No commits in common. "master" and "e8436e65407154816ad5a9f07717408cfe350d08" have entirely different histories.
master
...
e8436e6540
|
@ -2,9 +2,6 @@ class ItemNotFoundError(ValueError):
|
||||||
'''Used when looking up REDIS or SQL or any other database to indicate to the calling routine that the object
|
'''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
|
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):
|
class DeadIPFound(ValueError):
|
||||||
''''An ip has been saved to redis as being 'DEAD"'''
|
''''An ip has been saved to redis as being 'DEAD"'''
|
34
main.py
34
main.py
|
@ -1,31 +1,9 @@
|
||||||
|
import ng_openstack.auth
|
||||||
import ng_openstack.keystone
|
import ng_openstack.keystone
|
||||||
import os
|
import os
|
||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
# import ng_openstack
|
|
||||||
from ng_openstack.logger import log
|
|
||||||
|
|
||||||
log.info("Starting")
|
|
||||||
myOpenstack=ng_openstack.OpenstackConnection()
|
|
||||||
myOpenstack.keystone.getCatalog()
|
|
||||||
|
|
||||||
|
|
||||||
allProjects=myOpenstack.keystone.getAllProjects()
|
|
||||||
testProjectName="SmokeTest"
|
|
||||||
testProjectExists=False
|
|
||||||
|
|
||||||
#Does my test project ID already exist?
|
|
||||||
for _project in allProjects['projects']:
|
|
||||||
if _project['name']==testProjectName:
|
|
||||||
testProjectExists=True
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if testProjectExists:
|
|
||||||
log.error("Project already exists")
|
|
||||||
else:
|
|
||||||
# Create test project
|
|
||||||
log.info("{} project does not exist, creating".format(testProjectName))
|
|
||||||
myOpenstack.keystone.createProject(testProjectName,"Description goes here")
|
|
||||||
|
|
||||||
|
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())
|
|
@ -1,35 +0,0 @@
|
||||||
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()
|
|
||||||
keystone=Openstack_Keystone(requestor)
|
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, someVar="", someotherVar=""):
|
|
||||||
self.someotherVar = someotherVar
|
|
||||||
self.someVar = someVar
|
|
||||||
|
|
||||||
|
|
||||||
@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)
|
|
|
@ -0,0 +1,98 @@
|
||||||
|
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 ng_openstack.settings
|
||||||
import pymysql.cursors
|
import pymysql.cursors
|
||||||
import logging
|
import logging
|
||||||
import ng_openstack.exceptions as exceptions
|
import exceptions
|
||||||
import os,settings
|
import os,settings
|
||||||
|
|
||||||
SQL_HOST = os.getenv("SQL_HOST")
|
SQL_HOST = os.getenv("SQL_HOST")
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import settings,os
|
import settings,os
|
||||||
import ng_openstack.openstackRequest
|
import ng_openstack.openstackRequest
|
||||||
import ng_openstack.exceptions as exceptions
|
import exceptions
|
||||||
import json
|
import json
|
||||||
import pybill_redis
|
import pybill_redis
|
||||||
import requests
|
import requests
|
||||||
|
|
|
@ -1,77 +1,15 @@
|
||||||
import imp
|
|
||||||
import json
|
import json
|
||||||
import ng_openstack.openstackRequest
|
import ng_openstack.openstackRequest
|
||||||
import ng_openstack.settings
|
|
||||||
import os
|
|
||||||
from ng_openstack.logger import log
|
|
||||||
|
|
||||||
class Openstack_Keystone():
|
def getAllProjects():
|
||||||
def __init__(self, requestor):
|
projectData=ng_openstack.openstackRequest.openstackRequest("GET", "v3/projects", "",
|
||||||
self.serviceData={}
|
"http://172.25.110.138:5000").json()
|
||||||
self.catalogData={}
|
return projectData
|
||||||
self.projectData={}
|
|
||||||
self.requestor=requestor
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def getAllProjects(_self):
|
|
||||||
_self.projectData=_self.requestor.make_request("GET", "projects", "",
|
|
||||||
os.getenv("OS_AUTH_URL")).json()
|
|
||||||
return _self.projectData
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def getCatalog(_self):
|
def getCatalog():
|
||||||
catalogData=_self.requestor.make_request("GET", "endpoints", "",
|
catalogData=ng_openstack.openstackRequest.openstackRequest("GET", "v3/endpoints", "",
|
||||||
os.getenv("OS_AUTH_URL")).json()
|
os.getenv("OS_AUTH_URL")).json()
|
||||||
|
return catalogData
|
||||||
|
|
||||||
serviceData=_self.requestor.make_request("GET", "services", "",
|
|
||||||
os.getenv("OS_AUTH_URL")).json()
|
|
||||||
# print(serviceData)
|
|
||||||
_self.serviceData=serviceData
|
|
||||||
_self.catalogData=catalogData
|
|
||||||
return catalogData
|
|
||||||
|
|
||||||
|
|
||||||
def getServicebyID(_self,id):
|
|
||||||
# print(_self.serviceData)
|
|
||||||
for _service in _self.serviceData['services']:
|
|
||||||
if str(_service['id']).lower()==str(id).lower():
|
|
||||||
# print(_service)
|
|
||||||
return(_service)
|
|
||||||
|
|
||||||
def getServicebyName(_self,name):
|
|
||||||
for _service in _self.serviceData['services']:
|
|
||||||
if str(_service['name']).lower()==str(name).lower():
|
|
||||||
# print(_service)
|
|
||||||
return(_service)
|
|
||||||
|
|
||||||
|
|
||||||
def getEndpointByServiceIDAndInterface(_self,id,interface):
|
|
||||||
for _endpoint in _self.catalogData['endpoints']:
|
|
||||||
if str(_endpoint['service_id']).lower()==str(id).lower():
|
|
||||||
if str(_endpoint['interface']).lower()==str(interface).lower():
|
|
||||||
# print(_endpoint)
|
|
||||||
return(_endpoint)
|
|
||||||
|
|
||||||
def getEndpointByNameIDAndInterface(_self,name,interface):
|
|
||||||
id=_self.getServicebyName(name)['id']
|
|
||||||
for _endpoint in _self.catalogData['endpoints']:
|
|
||||||
if str(_endpoint['service_id']).lower()==str(id).lower():
|
|
||||||
if str(_endpoint['interface']).lower()==str(interface).lower():
|
|
||||||
# print(_endpoint)
|
|
||||||
return(_endpoint)
|
|
||||||
|
|
||||||
def createProject(_self,name,description):
|
|
||||||
log.info("Creating a project")
|
|
||||||
data={
|
|
||||||
"project": {
|
|
||||||
"description": description,
|
|
||||||
"enabled": True,
|
|
||||||
"is_domain": False,
|
|
||||||
"name": name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_self.newProjectData=_self.requestor.make_request("POST", "projects", data,
|
|
||||||
os.getenv("OS_AUTH_URL")).json()
|
|
||||||
return _self.newProjectData
|
|
|
@ -1,6 +0,0 @@
|
||||||
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)
|
|
|
@ -9,15 +9,3 @@ def listAllFloatingIPsByProject(projectID):
|
||||||
def ipInSubnet(network,netmask,ip):
|
def ipInSubnet(network,netmask,ip):
|
||||||
#Network, netmask and ip must be supplied in integer form
|
#Network, netmask and ip must be supplied in integer form
|
||||||
return (ip & netmask) == network
|
return (ip & netmask) == network
|
||||||
|
|
||||||
def createNetwork(_self,name,project_id,description=""):
|
|
||||||
data={
|
|
||||||
"network": {
|
|
||||||
"name": name,
|
|
||||||
"admin_state_up": true,
|
|
||||||
"tenant_id": project_id,
|
|
||||||
"description": description
|
|
||||||
}
|
|
||||||
}
|
|
||||||
newNetwork=_self.requestor.make_request("POST", "projects", data, )
|
|
||||||
return newNetwork
|
|
|
@ -1,129 +1,37 @@
|
||||||
import requests
|
import requests
|
||||||
import redis
|
|
||||||
import json
|
import json
|
||||||
import os
|
import ng_openstack.auth
|
||||||
from ng_openstack.logger import log
|
import settings,os
|
||||||
|
|
||||||
|
def openstackRequest(getPost, url, data, apiEndpoint, scopedProjectID=""):
|
||||||
|
|
||||||
from ng_openstack import exceptions
|
# 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
|
||||||
|
|
||||||
class Openstack_Request:
|
str(url).replace(" ","%20")
|
||||||
def make_request(_self, getPost, url, data, apiEndpoint, scopedProjectID=""):
|
|
||||||
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")
|
|
||||||
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 = _self.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":
|
||||||
url = apiEndpoint + "/" + url
|
response = requests.post(url, data=data_json, headers=headers)
|
||||||
|
else:
|
||||||
if getPost=="GET":
|
raise ValueError("Unknown request type")
|
||||||
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))
|
|
||||||
|
|
||||||
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)
|
|
||||||
headers = {'Content-type': 'application/json'}
|
|
||||||
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)
|
|
||||||
|
|
||||||
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(_self, 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.ItemNotSavedError("OpenstackToken not saved in redis")
|
|
||||||
|
|
||||||
|
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