Commit 1cbbd60e authored by Christos Stavrakakis's avatar Christos Stavrakakis

pep8 fixes

parent 5292a594
......@@ -29,7 +29,7 @@ def decrypt(s):
def encrypt_db_charfield(plaintext):
if plaintext == None:
if not plaintext:
return plaintext
salt = "".join([choice(letters + digits) for i in xrange(SALT_LEN)])
......@@ -42,7 +42,7 @@ def encrypt_db_charfield(plaintext):
def decrypt_db_charfield(ciphertext):
if ciphertext == None:
if not ciphertext:
return ciphertext
has_prefix = ciphertext.startswith(DB_ENCRYPTED_FIELD_PREFIX + ':')
if not has_prefix: # Non-encoded value
......
......@@ -71,10 +71,10 @@ class ForUpdateManager(Manager):
if num == 1:
return query[0]
if not num:
raise self.model.DoesNotExist(
"%s matching query does not exist. "
"Lookup parameters were %s" %
(self.model._meta.object_name, kwargs))
raise self.model.DoesNotExist("%s matching query does not exist. "
"Lookup parameters were %s" %
(self.model._meta.object_name,
kwargs))
raise self.model.MultipleObjectsReturned(
"get() returned more than one %s -- it returned %s! "
"Lookup parameters were %s" %
......
......@@ -55,7 +55,7 @@ class Flavor(models.Model):
ram = models.IntegerField('RAM size in MiB', default=0)
disk = models.IntegerField('Disk size in GiB', default=0)
disk_template = models.CharField('Disk template', max_length=32,
default=settings.DEFAULT_GANETI_DISK_TEMPLATE)
default=settings.DEFAULT_GANETI_DISK_TEMPLATE)
deleted = models.BooleanField('Deleted', default=False)
class Meta:
......@@ -78,7 +78,7 @@ class Backend(models.Model):
username = models.CharField('Username', max_length=64, blank=True,
null=True)
password_hash = models.CharField('Password', max_length=128, blank=True,
null=True)
null=True)
# Sha1 is up to 40 characters long
hash = models.CharField('Hash', max_length=40, editable=False, null=False)
# Unique index of the Backend, used for the mac-prefixes of the
......@@ -128,9 +128,9 @@ class Backend(models.Model):
def create_hash(self):
"""Create a hash for this backend. """
return sha1('%s%s%s%s' % \
(self.clustername, self.port, self.username, self.password)) \
.hexdigest()
sha = sha1('%s%s%s%s' %
(self.clustername, self.port, self.username, self.password))
return sha.hexdigest()
@property
def password(self):
......@@ -147,7 +147,8 @@ class Backend(models.Model):
super(Backend, self).save(*args, **kwargs)
if self.hash != old_hash:
# Populate the new hash to the new instances
self.virtual_machines.filter(deleted=False).update(backend_hash=self.hash)
self.virtual_machines.filter(deleted=False)\
.update(backend_hash=self.hash)
def delete(self, *args, **kwargs):
# Integrity Error if non-deleted VMs are associated with Backend
......@@ -192,7 +193,8 @@ BACKEND_STATUSES = (
class QuotaHolderSerial(models.Model):
serial = models.BigIntegerField(null=False, primary_key=True, db_index=True)
serial = models.BigIntegerField(null=False, primary_key=True,
db_index=True)
pending = models.BooleanField(default=True, db_index=True)
accepted = models.BooleanField(default=False)
rejected = models.BooleanField(default=False)
......@@ -209,12 +211,12 @@ class QuotaHolderSerial(models.Model):
class VirtualMachine(models.Model):
# The list of possible actions for a VM
ACTIONS = (
('CREATE', 'Create VM'),
('START', 'Start VM'),
('STOP', 'Shutdown VM'),
('SUSPEND', 'Admin Suspend VM'),
('REBOOT', 'Reboot VM'),
('DESTROY', 'Destroy VM')
('CREATE', 'Create VM'),
('START', 'Start VM'),
('STOP', 'Shutdown VM'),
('SUSPEND', 'Admin Suspend VM'),
('REBOOT', 'Reboot VM'),
('DESTROY', 'Destroy VM')
)
# The internal operating state of a VM
......@@ -296,7 +298,7 @@ class VirtualMachine(models.Model):
suspended = models.BooleanField('Administratively Suspended',
default=False)
serial = models.ForeignKey(QuotaHolderSerial,
related_name='virtual_machine', null=True)
related_name='virtual_machine', null=True)
# VM State
# The following fields are volatile data, in the sense
......@@ -422,8 +424,8 @@ class Network(models.Model):
)
ACTIONS = (
('CREATE', 'Create Network'),
('DESTROY', 'Destroy Network'),
('CREATE', 'Create Network'),
('DESTROY', 'Destroy Network'),
)
RSAPI_STATE_FROM_OPER_STATE = {
......@@ -435,36 +437,36 @@ class Network(models.Model):
FLAVORS = {
'CUSTOM': {
'mode': 'bridged',
'link': settings.DEFAULT_BRIDGE,
'mac_prefix': settings.DEFAULT_MAC_PREFIX,
'tags': None,
'desc': "Basic flavor used for a bridged network",
'mode': 'bridged',
'link': settings.DEFAULT_BRIDGE,
'mac_prefix': settings.DEFAULT_MAC_PREFIX,
'tags': None,
'desc': "Basic flavor used for a bridged network",
},
'IP_LESS_ROUTED': {
'mode': 'routed',
'link': settings.DEFAULT_ROUTING_TABLE,
'mac_prefix': settings.DEFAULT_MAC_PREFIX,
'tags': 'ip-less-routed',
'desc': "Flavor used for an IP-less routed network using"
" Proxy ARP",
'mode': 'routed',
'link': settings.DEFAULT_ROUTING_TABLE,
'mac_prefix': settings.DEFAULT_MAC_PREFIX,
'tags': 'ip-less-routed',
'desc': "Flavor used for an IP-less routed network using"
" Proxy ARP",
},
'MAC_FILTERED': {
'mode': 'bridged',
'link': settings.DEFAULT_MAC_FILTERED_BRIDGE,
'mac_prefix': 'pool',
'tags': 'private-filtered',
'desc': "Flavor used for bridged networks that offer isolation"
" via filtering packets based on their src "
" MAC (ebtables)",
'mode': 'bridged',
'link': settings.DEFAULT_MAC_FILTERED_BRIDGE,
'mac_prefix': 'pool',
'tags': 'private-filtered',
'desc': "Flavor used for bridged networks that offer isolation"
" via filtering packets based on their src "
" MAC (ebtables)",
},
'PHYSICAL_VLAN': {
'mode': 'bridged',
'link': 'pool',
'mac_prefix': settings.DEFAULT_MAC_PREFIX,
'tags': 'physical-vlan',
'desc': "Flavor used for bridged network that offer isolation"
" via dedicated physical vlan",
'mode': 'bridged',
'link': 'pool',
'mac_prefix': settings.DEFAULT_MAC_PREFIX,
'tags': 'physical-vlan',
'desc': "Flavor used for bridged network that offer isolation"
" via dedicated physical vlan",
},
}
......@@ -528,8 +530,10 @@ class Network(models.Model):
backends = [backend] if backend\
else Backend.objects.filter(offline=False)
for backend in backends:
if not BackendNetwork.objects.filter(backend=backend, network=self)\
.exists():
backend_exists =\
BackendNetwork.objects.filter(backend=backend, network=self)\
.exists()
if not backend_exists:
BackendNetwork.objects.create(backend=backend, network=self)
def get_pool(self):
......@@ -564,8 +568,8 @@ class Network(models.Model):
self.status = status
def __str__(self):
return repr('<opcode: %s, status: %s>' % (self.opcode,
self.status))
return repr('<opcode: %s, status: %s>'
% (self.opcode, self.status))
class InvalidActionError(Exception):
def __init__(self, action):
......@@ -647,8 +651,8 @@ class BackendNetwork(models.Model):
try:
utils.validate_mac(mac_prefix + ":00:00:00")
except utils.InvalidMacAddress:
raise utils.InvalidMacAddress("Invalid MAC prefix '%s'" % \
mac_prefix)
raise utils.InvalidMacAddress("Invalid MAC prefix '%s'" %
mac_prefix)
self.mac_prefix = mac_prefix
......@@ -754,7 +758,7 @@ class VirtualMachineDiagnosticManager(models.Manager):
def since(self, vm, created_since, **kwargs):
return self.get_query_set().filter(vm=vm, created__gt=created_since,
**kwargs)
**kwargs)
class VirtualMachineDiagnostic(models.Model):
......
......@@ -173,8 +173,8 @@ class NetworkInterfaceFactory(factory.Factory):
network = factory.SubFactory(NetworkFactory)
index = factory.Sequence(lambda x: x, type=int)
mac = factory.Sequence(lambda n:
'aa:{0}{0}:{0}{0}:aa:{0}{0}:{0}{0}'.format(hex(int(n) % 15)[2:3]))
ipv4 = factory.LazyAttributeSequence(lambda a, n: a.network.subnet[:-4] + \
'aa:{0}{0}:{0}{0}:aa:{0}{0}:{0}{0}'.format(hex(int(n) % 15)[2:3]))
ipv4 = factory.LazyAttributeSequence(lambda a, n: a.network.subnet[:-4] +
'{0}'.format(int(n) + 2))
firewall_profile =\
factory.Sequence(round_seq_first(FACTORY_FOR.FIREWALL_PROFILES))
......
......@@ -33,6 +33,7 @@ from synnefo.db.models import VirtualMachine, Network
from django.conf import settings
from copy import deepcopy
def id_from_instance_name(name):
"""Returns VirtualMachine's Django id, given a ganeti machine name.
......@@ -82,14 +83,15 @@ def get_rsapi_state(vm):
(vm.operstate) through the RSAPI_STATE_FROM_OPER_STATE dictionary.
The last state reported by Ganeti is set whenever Ganeti reports
successful completion of an operation. If Ganeti says an OP_INSTANCE_STARTUP
operation succeeded, vm.operstate is set to "STARTED".
* To support any transitional states defined by the API (only REBOOT for the time
being) this mapping is amended with information reported by Ganeti regarding
any outstanding operation. If an OP_INSTANCE_STARTUP had succeeded previously
and an OP_INSTANCE_REBOOT has been reported as in progress, the API state is
"REBOOT".
successful completion of an operation. If Ganeti says an
OP_INSTANCE_STARTUP operation succeeded, vm.operstate is set to
"STARTED".
* To support any transitional states defined by the API (only REBOOT for
the time being) this mapping is amended with information reported by Ganeti
regarding any outstanding operation. If an OP_INSTANCE_STARTUP had
succeeded previously and an OP_INSTANCE_REBOOT has been reported as in
progress, the API state is "REBOOT".
"""
try:
......
......@@ -77,8 +77,8 @@ class DummyQuotaholderClient(object):
if resource == "cyclades.network.private":
user_networks = Network.objects.filter(userid=userid,
deleted=False).count()
user_network_limit = NETWORKS_USER_QUOTA.get(userid,
MAX_NETWORKS_PER_USER)
user_network_limit =\
NETWORKS_USER_QUOTA.get(userid, MAX_NETWORKS_PER_USER)
if user_networks + size >= user_network_limit:
raise NoQuantityError()
......@@ -244,14 +244,14 @@ def create_commission(user, resources, delete=False):
resources = invert_resources(resources)
provisions = [('cyclades', 'cyclades.' + r, s)
for r, s in resources.items()]
return {"context": {},
"target": user,
"key": "1",
"clientkey": "cyclades",
#"owner": "",
#"ownerkey": "1",
"name": "",
"provisions": provisions}
return {"context": {},
"target": user,
"key": "1",
"clientkey": "cyclades",
#"owner": "",
#"ownerkey": "1",
"name": "",
"provisions": provisions}
##
## Reconcile pending commissions
......
......@@ -69,10 +69,12 @@ class Command(BaseCommand):
continue
reset_holding = []
for res, val in resources.items():
reset_holding.append((user, "cyclades." + res, "1", val, 0, 0, 0))
reset_holding.append((user, "cyclades." + res, "1", val, 0,
0, 0))
if not options['dry_run']:
try:
qh.reset_holding(context={}, reset_holding=reset_holding)
qh.reset_holding(context={},
reset_holding=reset_holding)
except Exception as e:
self.stderr.write("Can not set up holding:%s" % e)
else:
......
......@@ -86,11 +86,11 @@ class Command(BaseCommand):
db_extra = db_res - qh_res
if db_extra:
for res in db_extra:
write("Resource %s exists in DB for %s but not in QH\n"\
write("Resource %s exists in DB for %s but not in QH\n"
% (res, user))
qh_extra = qh_res - db_res
if qh_extra:
for res in qh_extra:
write("Resource %s exists in QH for %s but not in DB\n"\
write("Resource %s exists in QH for %s but not in DB\n"
% (res, user))
return False
......@@ -54,11 +54,11 @@ class Command(BaseCommand):
accepted, rejected = quotas.resolve_pending_commissions()
if accepted:
self.stdout.write("Pending accepted commissions:\n %s\n" \
self.stdout.write("Pending accepted commissions:\n %s\n"
% list_to_string(accepted))
if rejected:
self.stdout.write("Pending rejected commissions:\n %s\n" \
self.stdout.write("Pending rejected commissions:\n %s\n"
% list_to_string(rejected))
if fix and (accepted or rejected):
......
......@@ -65,7 +65,7 @@ def get_db_holdings(users=None):
# Get resources related with networks
net_resources = networks.values("userid")\
.annotate(num=Count("id"))
.annotate(num=Count("id"))
for net_res in net_resources:
user = net_res['userid']
if user not in holdings:
......@@ -99,7 +99,6 @@ def get_quotaholder_holdings(users=[]):
def decode_holding(holding):
entity, resource, imported, exported, returned, released = \
holding
entity, resource, imported, exported, returned, released = holding
res = resource.replace("cyclades.", "")
return (res, imported - exported + returned - released)
......@@ -38,9 +38,11 @@ from django.core import signals
from synnefo.vmapi.settings import CACHE_KEY_PREFIX, CACHE_BACKEND
def get_uuid():
return str(uuid4())
def get_key(*args):
args = map(str, filter(bool, list(args)))
args.insert(0, CACHE_KEY_PREFIX)
......@@ -54,4 +56,3 @@ backend = get_cache(CACHE_BACKEND)
# here.
if hasattr(backend, 'close'):
signals.request_finished.connect(backend.close)
......@@ -41,6 +41,7 @@ from synnefo.vmapi import backend, get_key, get_uuid, settings
log = getLogger('synnefo.vmapi')
def create_server_params(sender, created_vm_params, **kwargs):
json_value = json.dumps(created_vm_params)
uuid = get_uuid()
......@@ -55,4 +56,3 @@ def create_server_params(sender, created_vm_params, **kwargs):
return uuid
server_created.connect(create_server_params)
......@@ -34,9 +34,9 @@
from django.conf import settings
CACHE_BACKEND = getattr(settings, 'VMAPI_CACHE_BACKEND',
settings.CACHE_BACKEND)
settings.CACHE_BACKEND)
CACHE_KEY_PREFIX = getattr(settings, 'VMAPI_CACHE_KEY_PREFIX',
'vmapi')
'vmapi')
RESET_PARAMS = getattr(settings, 'VMAPI_RESET_PARAMS', True)
BASE_URL = getattr(settings, 'VMAPI_BASE_URL',
'https://cyclades.okeanos.grnet.gr/')
'https://cyclades.okeanos.grnet.gr/')
......@@ -37,6 +37,7 @@ from django.utils import simplejson as json
from synnefo.vmapi import settings
class TestServerParams(TestCase):
def test_cache_backend(self):
......@@ -55,7 +56,6 @@ class TestServerParams(TestCase):
from synnefo.vmapi.models import create_server_params
from synnefo.vmapi import backend
try:
from synnefo.api.servers import server_created
from synnefo.db.models import VirtualMachine
except ImportError:
print "Skipping test_params_create"
......@@ -66,7 +66,8 @@ class TestServerParams(TestCase):
params = {'password': 'X^942Jjfdsa', 'personality': {}}
uuid = create_server_params(sender=vm, created_vm_params=params)
self.assertEqual(vm.config_url, settings.BASE_URL + '/vmapi/server-params/%s' % uuid)
self.assertEqual(vm.config_url, settings.BASE_URL +
'/vmapi/server-params/%s' % uuid)
key = "vmapi_%s" % uuid
self.assertEqual(type(backend.get(key)), str)
data = json.loads(backend.get(key))
......@@ -78,8 +79,3 @@ class TestServerParams(TestCase):
self.assertEqual(response.status_code, 200)
response = self.client.get('/vmapi/server-params/%s' % uuid)
self.assertEqual(response.status_code, 404)
def test_params_view(self):
pass
......@@ -31,8 +31,8 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from django.conf.urls.defaults import include, patterns, url
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('synnefo.vmapi.views',
url(r'^server-params/(?P<uuid>.*)$', 'server_params', name="vmapi_server_params"),
)
url(r'^server-params/(?P<uuid>.*)$', 'server_params',
name="vmapi_server_params"),)
......@@ -40,6 +40,7 @@ from synnefo.vmapi import settings
log = getLogger('synnefo.vmapi')
def server_params(request, uuid):
if not uuid:
raise Http404
......@@ -54,4 +55,3 @@ def server_params(request, uuid):
backend.set(cache_key, None)
return HttpResponse(params, content_type="application/json")
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