Somewhat working

This commit is contained in:
Cory Hawkvelt 2022-11-20 23:17:58 +10:30
parent e4d0e8c695
commit 85dadb1428
3 changed files with 110 additions and 62 deletions

142
main.py
View File

@ -1,4 +1,4 @@
import os, sys
import os, sys,time
sys.path.append(os.path.join(os.path.dirname(__file__), "lib"))
from myOpenstackApp.logger import log
@ -9,6 +9,7 @@ testProjectExists=False
testProject=""
testNetwork=""
testSubnet=""
timingText=""
cleanupResourcesOnCompletion=True
@ -17,65 +18,20 @@ y.initalise()
y.ks.getCatalog()
# print(y.something())
# print(y._keystone)
log.info("Starting")
tic = time.perf_counter()
allProjects=y.ks.getAllProjects()
toc = time.perf_counter()
timingText +=(f"Queried keystone in {toc - tic:0.4f} seconds\n")
def sendFailureNotice(e,timingText):
log.error("JOB FAILED\n\n" + str(e))
log.error(timingText)
exit()
#Does my test project ID already exist?
for _project in allProjects['projects']:
if _project['name']==testProjectName:
testProject=_project
testProjectExists=True
if testProjectExists:
log.error("Test project already exists with ID " + testProject['id'])
else:
# Create test project
log.info("{} project does not exist, creating".format(testProjectName))
testProject=y.ks.createProject(testProjectName,"Description goes here")['project']
log.info("Test project created with ID " + testProject['id'])
#Does my test network exist?
allNetworks=y.neutron.getAllNetworks(testProject['id'])
log.info(allNetworks)
#Does my test network ID already exist?
for _network in allNetworks['networks']:
if _network['name']==testProjectName+"_network":
testNetwork=_network
if testNetwork != "":
log.info("Test network already exists with ID " + testNetwork['id'])
else:
# Create test network
log.info("{} network does not exist, creating".format(testProjectName))
testNetwork=y.neutron.createNetwork(testProjectName+"_network",testProject['id'],"Test network description")['network']
log.info("Test network created with ID " + testNetwork['id'])
#Create subnet
log.info("Creating new subnet")
testSubnet=y.neutron.createSubnet(testProjectName + "_subnet",testNetwork['id'])
log.debug(testSubnet)
#Create a test router
log.info("Creating new router")
testRouter=y.neutron.createRouter(testProjectName + "_router",os.getenv("EXTERNAL_NETWORK_ID"))
log.debug(testRouter)
#Create
#Project
#Network
#Subnet
#Router
#bind router to network
#Volume from image
#Vm from volume
def cleanUp():
log.info("Cleaning up")
@ -87,6 +43,82 @@ def cleanUp():
#Delete project
pass
try:
#Does my test project ID already exist?
for _project in allProjects['projects']:
if _project['name']==testProjectName:
testProject=_project
testProjectExists=True
if testProjectExists:
log.error("Test project already exists with ID " + testProject['id'])
else:
# Create test project
log.info("{} project does not exist, creating".format(testProjectName))
testProject=y.ks.createProject(testProjectName,"Description goes here")['project']
log.info("Test project created with ID " + testProject['id'])
toc = time.perf_counter()
timingText +=(f"Queried projects in {toc - tic:0.4f} seconds\n")
except Exception as e:
log.error("Error managing project" + str(e))
cleanUp()
sendFailureNotice(e,timingText)
try:
#Does my test network exist?
allNetworks=y.neutron.getAllNetworks(testProject['id'])
log.info(allNetworks)
#Does my test network ID already exist?
for _network in allNetworks['networks']:
if _network['name']==testProjectName+"_network":
testNetwork=_network
if testNetwork != "":
log.info("Test network already exists with ID " + testNetwork['id'])
else:
# Create test network
log.info("{} network does not exist, creating".format(testProjectName))
testNetwork=y.neutron.createNetwork(testProjectName+"_network",testProject['id'],"Test network description")['network']
log.info("Test network created with ID " + testNetwork['id'])
#Create subnet
log.info("Creating new subnet")
testSubnet=y.neutron.createSubnet(testProjectName + "_subnet",testNetwork['id'])
log.debug(testSubnet)
#Create a test router
log.info("Creating new router")
testRouter=y.neutron.createRouter(testProjectName + "_router",os.getenv("EXTERNAL_NETWORK_ID"))
log.debug(testRouter)
toc = time.perf_counter()
timingText +=(f"Queried neutron in {toc - tic:0.4f} seconds")
except Exception as e:
log.error("Error managing network" + str(e))
toc = time.perf_counter()
timingText +=(f"Neutron failed after {toc - tic:0.4f} seconds\n")
cleanUp()
sendFailureNotice(e,timingText)
#Create
#Project
#Network
#Subnet
#Router
#bind router to network
#Volume from image
#Vm from volume
if cleanupResourcesOnCompletion:
cleanUp()
toc = time.perf_counter()
log.info(f"Total runtime {toc - tic:0.4f} seconds")

View File

@ -35,8 +35,8 @@ class OpenStackConnection_x:
self.ks=myopenstack_keystone(self)
self.nova=myopenstack_nova(self)
self.neutron=myopenstack_neutron(self)
self.cinder-myopenstack_cinder(self)
self.cinder=myopenstack_cinder(self)
self.interface=os.getenv("OS_INTERFACE")
@ -71,11 +71,11 @@ class OpenStackConnection_x:
url = apiEndpoint + "/" + url
if getPost=="GET":
response = requests.get(url, headers=headers)
response = requests.get(url, headers=headers,timeout=15)
elif getPost=="POST":
response = requests.post(url, data=data_json, headers=headers)
response = requests.post(url, data=data_json, headers=headers,timeout=15)
elif getPost=="DELETE":
response = requests.delete(url, headers=headers)
response = requests.delete(url, headers=headers,timeout=15)
else:
raise ValueError("Unknown request type")
@ -118,12 +118,12 @@ class OpenStackConnection_x:
}
}
response = requests.post(url)
# response = requests.post(url)
headers = {'Content-type': 'application/json'}
response = requests.post(url, headers=headers,
data=json.dumps(xdata),
verify=False,
timeout=4)
timeout=15)
if response.status_code == 201:
try:

16
test.py Normal file
View File

@ -0,0 +1,16 @@
import time
timingText=""
tic = time.perf_counter()
import requests
toc = time.perf_counter()
timingText +=(f"Queried projects in {toc - tic:0.4f} seconds\n")
x=requests.get("http://google.com")
toc = time.perf_counter()
time.sleep(1)
timingText +=(f"Queried projects in {toc - tic:0.4f} seconds\n")
toc = time.perf_counter()
timingText +=(f"Queried projects in {toc - tic:0.4f} seconds\n")
print(timingText)