Skip to content

Commit

Permalink
Bugfixes in REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Jan 27, 2016
1 parent 2d78218 commit f11fc40
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
42 changes: 34 additions & 8 deletions IM/REST.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import json
from config import Config
from radl.radl_json import parse_radl as parse_radl_json, dump_radl as dump_radl_json
from radl.radl_parse import parse_radl
from bottle import HTTPError

AUTH_LINE_SEPARATOR = '\\n'

Expand Down Expand Up @@ -97,6 +97,16 @@ def run(host, port):
def stop():
bottle_server.shutdown()

def get_media_type(header):
"""
Function to get only the header media type
"""
accept = bottle.request.headers.get(header)
pos = accept.find(";")
if pos != 1:
accept = accept[:pos]
return accept.strip()

@app.route('/infrastructures/:id', method='DELETE')
def RESTDestroyInfrastructure(id=None):
try:
Expand Down Expand Up @@ -169,6 +179,8 @@ def RESTGetInfrastructureProperty(id=None, prop=None):
else:
bottle.abort(403, "Incorrect infrastructure property")
return str(res)
except HTTPError, ex:
raise ex
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error Getting Inf. info: " + str(ex))
return False
Expand Down Expand Up @@ -212,7 +224,7 @@ def RESTCreateInfrastructure():
bottle.abort(401, "No authentication data provided")

try:
content_type = bottle.request.headers.get('Content-Type')
content_type = get_media_type('Content-Type')
radl_data = bottle.request.body.read()

if content_type:
Expand All @@ -226,6 +238,8 @@ def RESTCreateInfrastructure():

bottle.response.content_type = "text/uri-list"
return "http://" + bottle.request.environ['HTTP_HOST'] + "/infrastructures/" + str(inf_id)
except HTTPError, ex:
raise ex
except UnauthorizedUserException, ex:
bottle.abort(401, "Error Getting Inf. info: " + str(ex))
return False
Expand All @@ -242,7 +256,7 @@ def RESTGetVMInfo(infid=None, vmid=None):
bottle.abort(401, "No authentication data provided")

try:
accept = bottle.request.headers.get('Accept')
accept = get_media_type('Accept')

radl = InfrastructureManager.GetVMInfo(infid, vmid, auth)

Expand All @@ -261,6 +275,8 @@ def RESTGetVMInfo(infid=None, vmid=None):
bottle.response.content_type = "text/plain"

return info
except HTTPError, ex:
raise ex
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error Getting VM. info: " + str(ex))
return False
Expand Down Expand Up @@ -291,7 +307,7 @@ def RESTGetVMProperty(infid=None, vmid=None, prop=None):
else:
info = InfrastructureManager.GetVMProperty(infid, vmid, prop, auth)

accept = bottle.request.headers.get('Accept')
accept = get_media_type('Accept')
if accept:
if accept == "application/json":
bottle.response.content_type = accept
Expand All @@ -306,6 +322,8 @@ def RESTGetVMProperty(infid=None, vmid=None, prop=None):
bottle.response.content_type = "text/plain"

return str(info)
except HTTPError, ex:
raise ex
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error Getting VM. property: " + str(ex))
return False
Expand Down Expand Up @@ -341,7 +359,7 @@ def RESTAddResource(id=None):
else:
bottle.abort(400, "Incorrect value in context parameter")

content_type = bottle.request.headers.get('Content-Type')
content_type = get_media_type('Content-Type')
radl_data = bottle.request.body.read()

if content_type:
Expand All @@ -361,6 +379,8 @@ def RESTAddResource(id=None):

bottle.response.content_type = "text/uri-list"
return res
except HTTPError, ex:
raise ex
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error Adding resources: " + str(ex))
return False
Expand Down Expand Up @@ -393,6 +413,8 @@ def RESTRemoveResource(infid=None, vmid=None):
InfrastructureManager.RemoveResource(infid, vmid, auth, context)
bottle.response.content_type = "text/plain"
return ""
except HTTPError, ex:
raise ex
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error Removing resources: " + str(ex))
return False
Expand All @@ -418,8 +440,8 @@ def RESTAlterVM(infid=None, vmid=None):
bottle.abort(401, "No authentication data provided")

try:
content_type = bottle.request.headers.get('Content-Type')
accept = bottle.request.headers.get('Accept')
content_type = get_media_type('Content-Type')
accept = get_media_type('Accept')
radl_data = bottle.request.body.read()

if content_type:
Expand All @@ -445,6 +467,8 @@ def RESTAlterVM(infid=None, vmid=None):
bottle.response.content_type = "text/plain"

return res
except HTTPError, ex:
raise ex
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error modifying resources: " + str(ex))
return False
Expand Down Expand Up @@ -478,7 +502,7 @@ def RESTReconfigureInfrastructure(id=None):
except:
bottle.abort(400, "Incorrect vm_list format.")

content_type = bottle.request.headers.get('Content-Type')
content_type = get_media_type('Content-Type')
radl_data = bottle.request.body.read()

if radl_data:
Expand All @@ -491,6 +515,8 @@ def RESTReconfigureInfrastructure(id=None):
else:
radl_data = ""
return InfrastructureManager.Reconfigure(id, radl_data, auth, vm_list)
except HTTPError, ex:
raise ex
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error reconfiguring infrastructure: " + str(ex))
return False
Expand Down
2 changes: 1 addition & 1 deletion IM/ansible/ansible_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def launch_playbook_v2(self):

loader = DataLoader()
# Add this to avoid the Ansible bug: no host vars as host is not in inventory
# Hope to remove this if this problem is solved in Ansible
# In version 2.0.1 it must be fixed
ansible.inventory.HOSTS_PATTERNS_CACHE = {}

inventory = ansible.inventory.Inventory(loader=loader, variable_manager=variable_manager, host_list=options.inventory)
Expand Down
1 change: 1 addition & 0 deletions changelog
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,4 @@ IM 1.4.2
* Change in Auth Header in new version of FogBow and support for requirements
* Code improvements in OpenStack, OpenNebula and FogBow connectors
* Added workaround to problems in ansible_launcher with HOSTS_PATTERNS_CACHE
* Bugfixes in REST API

0 comments on commit f11fc40

Please sign in to comment.