I'm trying to create an endpoint to stop my openstasck server instance using pythonSDK, but I'm getting an error when I try to access / stop_server, could you help me?
from flask import Flask, request
from openstack import connection
from pprint import pprint
import json
import os
app = Flask(__name__)
# Source the openstack project rc file before running this app, to create the
# environment variables needed
conn = connection.Connection(auth_url=os.environ['OS_AUTH_URL'],
project_name=os.environ['OS_PROJECT_NAME'],
username=os.environ['OS_USERNAME'],
password=os.environ['OS_PASSWORD'],
user_domain_id="default",
project_domain_id=os.environ['OS_PROJECT_DOMAIN_ID'])
@app.route("/")
def hello():
return "Hello from Flask!"
@app.route("/list")
def list_openstack_resources():
# Check if connection is established
print("conn: ", conn)
# Print list of servers, images, flavors, endpoints, projects, users
server_list = list(conn.compute.servers())
image_list = list(conn.compute.images())
flavor_list = list(conn.compute.flavors())
project_list = list(conn.identity.projects())
user_list = list(conn.identity.users())
pprint(server_list)
pprint(image_list)
pprint(flavor_list)
pprint(project_list)
pprint(user_list)
return "List printed to stdout"
@app.route("/create_server")
def create_server():
# Check if connection is established
print("conn: ", conn)
# Create the volume first
volume_size = request.args.get('volume_size')
print("Starting to create volume with size (in GiB): ", volume_size)
volume = conn.create_volume(size=volume_size,
image="cirros-0.5.1-x86_64-disk",
wait=True,
bootable=True,
)
print("Create volume: ", json.dumps(volume))
# Create the server using the server_name parameter in the GET request
server_name = request.args.get('server_name')
print("Starting to create the server with name: ", server_name)
server = conn.create_server(name=server_name,
flavor="m1.micro",
terminate_volume=True,
timeout=180,
boot_volume=volume.id,
key_name="test-app",
network='shared'
)
print("Created server: ", json.dumps(server))
return "Server create request sent!"
@app.route("/stop_server")
def stop_server():
for server in conn.compute.servers():
print(json.dumps(server))
@app.route("/delete_server")
def delete_server():
print("delete: ", conn)
server_id = request.args.get('server_id')
delete_server = conn.delete_server(name=server_id, ignore_missing=False, force=False)
print('removed', json.dumps(delete_server))
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=8080)
I receive this error:
TypeError
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
In technology
client-server
whenclient
sendsrequest
thenserver
should send backresponse
.But in function
stop_server()
anddelete_server()
you don't usereturn text
norreturn render_template(...)
norreturn jsonify()
norreturn response(...)
, etc. - so Python uses standardreturn None
(as default) andflask
doesn't know what to send to client - and this generates your error.You should send at least empty string
return ""
or better send information about success.