Commit 16059916 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

pep8 fixes

parent 54998522
# Copyright 2011 GRNET S.A. All rights reserved.
#
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......@@ -25,7 +25,7 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
......@@ -41,11 +41,10 @@ from django.template.loader import render_to_string
from django.utils import simplejson as json
from synnefo.api.faults import (BadRequest, ServiceUnavailable,
ItemNotFound, BuildInProgress,
OverLimit)
BuildInProgress, OverLimit)
from synnefo.api.util import (random_password, get_vm, get_nic_from_index,
get_network_free_address)
from synnefo.db.models import NetworkInterface, Network
from synnefo.db.models import NetworkInterface
from synnefo.db.pools import EmptyPool
from synnefo.logic import backend
from synnefo.logic.utils import get_rsapi_state
......
# Copyright 2011 GRNET S.A. All rights reserved.
#
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......@@ -25,7 +25,7 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
......@@ -39,6 +39,7 @@ from synnefo.api.util import api_method
def not_found(request):
raise BadRequest('Not found.')
@api_method()
def method_not_allowed(request):
raise BadRequest('Method not allowed')
# Copyright 2011-2012 GRNET S.A. All rights reserved.
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
......@@ -34,13 +34,7 @@
import logging
from urlparse import urlparse
import urllib
import urllib2
from django.http import (
HttpResponseNotFound, HttpResponseRedirect, HttpResponseBadRequest,
HttpResponse)
from django.utils.http import urlencode
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
......@@ -74,17 +68,18 @@ def proxy(request, url, headers={}, body=None):
finally:
conn.close()
@csrf_exempt
def delegate_to_feedback_service(request):
token = request.META.get('HTTP_X_AUTH_TOKEN')
headers = {'X-Auth-Token': token}
return proxy(
request, USER_FEEDBACK_URL, headers=headers, body=request.raw_post_data)
return proxy(request, USER_FEEDBACK_URL, headers=headers,
body=request.raw_post_data)
@csrf_exempt
def delegate_to_user_catalogs_service(request):
token = request.META.get('HTTP_X_AUTH_TOKEN')
headers = {'X-Auth-Token': token, 'content-type': 'application/json'}
return proxy(
request, USER_CATALOG_URL, headers=headers, body=request.raw_post_data)
return proxy(request, USER_CATALOG_URL, headers=headers,
body=request.raw_post_data)
# Copyright 2011 GRNET S.A. All rights reserved.
#
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......@@ -25,12 +25,13 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
def camelCase(s):
return s[0].lower() + s[1:]
......@@ -42,32 +43,42 @@ class Fault(Exception):
self.details = details
self.name = name or camelCase(self.__class__.__name__)
class BadRequest(Fault):
code = 400
class Unauthorized(Fault):
code = 401
class ResizeNotAllowed(Fault):
code = 403
class Forbidden(Fault):
code = 403
class ItemNotFound(Fault):
code = 404
class BuildInProgress(Fault):
code = 409
class OverLimit(Fault):
code = 413
class BadMediaType(Fault):
code = 415
class NetworkInUse(Fault):
code = 421
class ServiceUnavailable(Fault):
code = 503
# Copyright 2011 GRNET S.A. All rights reserved.
#
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......@@ -25,7 +25,7 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
......@@ -45,7 +45,8 @@ from synnefo.db.models import Flavor
log = getLogger('synnefo.api')
urlpatterns = patterns('synnefo.api.flavors',
urlpatterns = patterns(
'synnefo.api.flavors',
(r'^(?:/|.json|.xml)?$', 'list_flavors'),
(r'^/detail(?:.json|.xml)?$', 'list_flavors', {'detail': True}),
(r'^/(\d+)(?:.json|.xml)?$', 'get_flavor_details'),
......@@ -73,7 +74,7 @@ def list_flavors(request, detail=False):
log.debug('list_flavors detail=%s', detail)
active_flavors = Flavor.objects.exclude(deleted=True)
flavors = [flavor_to_dict(flavor, detail)\
flavors = [flavor_to_dict(flavor, detail)
for flavor in active_flavors.order_by('id')]
if request.serialization == 'xml':
......
......@@ -51,7 +51,8 @@ from synnefo.plankton.backend import ImageBackend
log = getLogger('synnefo.api')
urlpatterns = patterns('synnefo.api.images',
urlpatterns = patterns(
'synnefo.api.images',
(r'^(?:/|.json|.xml)?$', 'demux'),
(r'^/detail(?:.json|.xml)?$', 'list_images', {'detail': True}),
(r'^/([\w-]+)(?:.json|.xml)?$', 'image_demux'),
......@@ -59,6 +60,7 @@ urlpatterns = patterns('synnefo.api.images',
(r'^/([\w-]+)/meta/(.+?)(?:.json|.xml)?$', 'metadata_item_demux')
)
def demux(request):
if request.method == 'GET':
return list_images(request)
......@@ -67,6 +69,7 @@ def demux(request):
else:
return method_not_allowed(request)
def image_demux(request, image_id):
if request.method == 'GET':
return get_image_details(request, image_id)
......@@ -75,6 +78,7 @@ def image_demux(request, image_id):
else:
return method_not_allowed(request)
def metadata_demux(request, image_id):
if request.method == 'GET':
return list_metadata(request, image_id)
......@@ -83,6 +87,7 @@ def metadata_demux(request, image_id):
else:
return method_not_allowed(request)
def metadata_item_demux(request, image_id, key):
if request.method == 'GET':
return get_metadata_item(request, image_id, key)
......@@ -114,6 +119,7 @@ def image_backend(userid):
finally:
backend.close()
@api_method('GET')
def list_images(request, detail=False):
# Normal Response Codes: 200, 203
......
......@@ -54,7 +54,8 @@ from synnefo.logic import backend
log = getLogger('synnefo.api')
urlpatterns = patterns('synnefo.api.networks',
urlpatterns = patterns(
'synnefo.api.networks',
(r'^(?:/|.json|.xml)?$', 'demux'),
(r'^/detail(?:.json|.xml)?$', 'list_networks', {'detail': True}),
(r'^/(\w+)(?:.json|.xml)?$', 'network_demux'),
......@@ -97,7 +98,7 @@ def network_to_dict(network, user_id, detail=True):
d['public'] = network.public
attachments = [util.construct_nic_id(nic)
for nic in network.nics.filter(machine__userid=user_id)\
for nic in network.nics.filter(machine__userid=user_id)
.order_by('machine')]
d['attachments'] = {'values': attachments}
return d
......@@ -200,21 +201,21 @@ def create_network(serials, request):
try:
mode, link, mac_prefix, tags = util.values_from_flavor(flavor)
network = Network.objects.create(
name=name,
userid=user_id,
subnet=subnet,
subnet6=subnet6,
gateway=gateway,
gateway6=gateway6,
dhcp=dhcp,
flavor=flavor,
mode=mode,
link=link,
mac_prefix=mac_prefix,
tags=tags,
action='CREATE',
state='PENDING',
serial=serial)
name=name,
userid=user_id,
subnet=subnet,
subnet6=subnet6,
gateway=gateway,
gateway6=gateway6,
dhcp=dhcp,
flavor=flavor,
mode=mode,
link=link,
mac_prefix=mac_prefix,
tags=tags,
action='CREATE',
state='PENDING',
serial=serial)
except EmptyPool:
log.error("Failed to allocate resources for network of type: %s",
flavor)
......@@ -305,7 +306,6 @@ def delete_network(request, network_id):
if net.machines.all(): # Nics attached on network
raise NetworkInUse('Machines are connected to network.')
net.action = 'DESTROY'
net.save()
......
......@@ -57,7 +57,8 @@ server_created = dispatch.Signal(providing_args=["created_vm_params"])
from logging import getLogger
log = getLogger('synnefo.api')
urlpatterns = patterns('synnefo.api.servers',
urlpatterns = patterns(
'synnefo.api.servers',
(r'^(?:/|.json|.xml)?$', 'demux'),
(r'^/detail(?:.json|.xml)?$', 'list_servers', {'detail': True}),
(r'^/(\d+)(?:.json|.xml)?$', 'server_demux'),
......@@ -128,7 +129,7 @@ def vm_to_dict(vm, detail=False):
if detail:
d['status'] = get_rsapi_state(vm)
d['progress'] = 100 if get_rsapi_state(vm) == 'ACTIVE' \
else vm.buildpercentage
else vm.buildpercentage
d['hostId'] = vm.hostid
d['updated'] = util.isoformat(vm.updated)
d['created'] = util.isoformat(vm.created)
......@@ -229,7 +230,7 @@ def list_servers(request, detail=False):
else:
user_vms = user_vms.filter(deleted=False)
servers = [vm_to_dict(server, detail)\
servers = [vm_to_dict(server, detail)
for server in user_vms.order_by('id')]
if request.serialization == 'xml':
......@@ -455,12 +456,12 @@ def delete_server(request, server_id):
# additional server actions
ARBITRARY_ACTIONS = ['console', 'firewallProfile']
@util.api_method('POST')
def server_action(request, server_id):
req = util.get_request_dict(request)
log.debug('server_action %s %s', server_id, req)
if len(req) != 1:
raise faults.BadRequest("Malformed request")
......
......@@ -83,7 +83,7 @@ class BaseAPITest(TestCase):
content_type = 'application/json'
with astakos_user(user):
response = self.client.put(url, params, content_type=content_type,
*args, **kwargs)
*args, **kwargs)
return response
def assertSuccess(self, response):
......
# Copyright 2011 GRNET S.A. All rights reserved.
#
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......@@ -25,7 +25,7 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
......@@ -41,7 +41,8 @@ from synnefo.api.versions import versions_list, version_details
#
# The OpenStack Compute API v1.1
#
api11_patterns = patterns('',
api11_patterns = patterns(
'',
(r'^servers', include(servers)),
(r'^flavors', include(flavors)),
(r'^images', include(images)),
......@@ -49,9 +50,11 @@ api11_patterns = patterns('',
)
urlpatterns = patterns('',
urlpatterns = patterns(
'',
(r'^(?:.json|.xml|.atom)?$', versions_list),
(r'^v1.1/(?:.json|.xml|.atom)?$', version_details, {'api_version': 'v1.1'}),
(r'^v1.1/(?:.json|.xml|.atom)?$', version_details,
{'api_version': 'v1.1'}),
(r'^v1.1/', include(api11_patterns)),
(r'^.+', not_found),
)
......@@ -206,7 +206,7 @@ def get_image_dict(image_id, user_id):
properties = img.get('properties', {})
image['backend_id'] = img['location']
image['format'] = img['disk_format']
image['metadata'] = dict((key.upper(), val) \
image['metadata'] = dict((key.upper(), val)
for key, val in properties.items())
image['checksum'] = img['checksum']
......@@ -318,7 +318,7 @@ def get_nic_from_index(vm, nic_index):
matching_nics = vm.nics.filter(index=nic_index)
matching_nics_len = len(matching_nics)
if matching_nics_len < 1:
raise ItemNotFound('NIC not found on VM')
raise ItemNotFound('NIC not found on VM')
elif matching_nics_len > 1:
raise BadMediaType('NIC index conflict on VM')
nic = matching_nics[0]
......@@ -379,10 +379,9 @@ def render_fault(request, fault):
if request.serialization == 'xml':
data = render_to_string('fault.xml', {'fault': fault})
else:
d = {fault.name: {
'code': fault.code,
'message': fault.message,
'details': fault.details}}
d = {fault.name: {'code': fault.code,
'message': fault.message,
'details': fault.details}}
data = json.dumps(d)
resp = HttpResponse(data, status=fault.code)
......@@ -468,7 +467,7 @@ def verify_personality(personality):
"""Verify that a a list of personalities is well formed"""
if len(personality) > settings.MAX_PERSONALITY:
raise OverLimit("Maximum number of personalities"
" exceeded")
" exceeded")
for p in personality:
# Verify that personalities are well-formed
try:
......@@ -500,6 +499,7 @@ def get_flavor_provider(flavor):
disk_template, provider = disk_template.split("_", 1)
return disk_template, provider
def values_from_flavor(flavor):
"""Get Ganeti connectivity info from flavor type.
......@@ -560,12 +560,11 @@ def get_existing_users():
from synnefo.db.models import VirtualMachine, Network
keypairusernames = PublicKeyPair.objects.filter().values_list('user',
flat=True)
flat=True)
serverusernames = VirtualMachine.objects.filter().values_list('userid',
flat=True)
flat=True)
networkusernames = Network.objects.filter().values_list('userid',
flat=True)
return set(list(keypairusernames) + list(serverusernames) + \
list(networkusernames))
return set(list(keypairusernames) + list(serverusernames) +
list(networkusernames))
# Copyright 2011 GRNET S.A. All rights reserved.
#
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......@@ -25,7 +25,7 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
......@@ -100,7 +100,7 @@ def version_details(request, api_version):
# unauthorized (401),
# badRequest (400),
# overLimit(413)
log.debug('version_details %s', api_version)
# We hardcode to v1.1 since it is the only one we support
version = VERSION_1_1.copy()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment