Commit b0ab28c0 authored by Christos Stavrakakis's avatar Christos Stavrakakis

cyclades: Common function to get object by ID

Create common helper function, for command line tools, to get an object
from DB by it's ID, and raise the appropriate command error if the
object does not exist or the ID is invalid.
parent bfa01648
...@@ -384,7 +384,7 @@ class VirtualMachine(models.Model): ...@@ -384,7 +384,7 @@ class VirtualMachine(models.Model):
return u"<vm:%s@backend:%s>" % (self.id, self.backend_id) return u"<vm:%s@backend:%s>" % (self.id, self.backend_id)
# Error classes # Error classes
class InvalidBackendIdError(Exception): class InvalidBackendIdError(ValueError):
def __init__(self, value): def __init__(self, value):
self.value = value self.value = value
...@@ -587,7 +587,7 @@ class Network(models.Model): ...@@ -587,7 +587,7 @@ class Network(models.Model):
free += ip_pool.count_available() free += ip_pool.count_available()
return total, free return total, free
class InvalidBackendIdError(Exception): class InvalidBackendIdError(ValueError):
def __init__(self, value): def __init__(self, value):
self.value = value self.value = value
......
# Copyright 2012 GRNET S.A. All rights reserved. # Copyright 2012-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or # Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following # without modification, are permitted provided that the following
...@@ -35,7 +35,7 @@ from optparse import make_option ...@@ -35,7 +35,7 @@ from optparse import make_option
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from synnefo.db.models import Backend from synnefo.db.models import Backend
from snf_django.management.utils import parse_bool from snf_django.management.utils import parse_bool
from synnefo.management.common import (get_backend, check_backend_credentials) from synnefo.management import common
HYPERVISORS = [h[0] for h in Backend.HYPERVISORS] HYPERVISORS = [h[0] for h in Backend.HYPERVISORS]
...@@ -82,7 +82,7 @@ class Command(BaseCommand): ...@@ -82,7 +82,7 @@ class Command(BaseCommand):
if len(args) != 1: if len(args) != 1:
raise CommandError("Please provide a backend ID") raise CommandError("Please provide a backend ID")
backend = get_backend(args[0]) backend = common.get_resource("backend", args[0], for_update=True)
# Ensure fields correspondence with options and Backend model # Ensure fields correspondence with options and Backend model
credentials_changed = False credentials_changed = False
...@@ -95,8 +95,10 @@ class Command(BaseCommand): ...@@ -95,8 +95,10 @@ class Command(BaseCommand):
if credentials_changed: if credentials_changed:
# check credentials, if any of them changed! # check credentials, if any of them changed!
check_backend_credentials(backend.clustername, backend.port, common.check_backend_credentials(backend.clustername,
backend.username, backend.password) backend.port,
backend.username,
backend.password)
if options['drained']: if options['drained']:
backend.drained = parse_bool(options['drained'], strict=True) backend.drained = parse_bool(options['drained'], strict=True)
if options['offline']: if options['offline']:
......
# Copyright 2011-2012 GRNET S.A. All rights reserved. # Copyright 2011-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions # modification, are permitted provided that the following conditions
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
# #
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from synnefo.management.common import get_backend from synnefo.management import common
from synnefo.logic import backend as backend_mod from synnefo.logic import backend as backend_mod
from synnefo.db.models import Backend from synnefo.db.models import Backend
from django.db import transaction, models from django.db import transaction, models
...@@ -50,7 +50,7 @@ class Command(BaseCommand): ...@@ -50,7 +50,7 @@ class Command(BaseCommand):
if len(args) < 1: if len(args) < 1:
raise CommandError("Please provide a backend ID") raise CommandError("Please provide a backend ID")
backend = get_backend(args[0]) backend = common.get_resource("backend", args[0], for_update=True)
write("Trying to remove backend: %s\n" % backend.clustername) write("Trying to remove backend: %s\n" % backend.clustername)
......
# Copyright 2012 GRNET S.A. All rights reserved. # Copyright 2012-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or # Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following # without modification, are permitted provided that the following
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
from optparse import make_option from optparse import make_option
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from synnefo.management.common import get_flavor from synnefo.management.common import get_resource
from snf_django.management.utils import parse_bool from snf_django.management.utils import parse_bool
...@@ -67,7 +67,7 @@ class Command(BaseCommand): ...@@ -67,7 +67,7 @@ class Command(BaseCommand):
if len(args) != 1: if len(args) != 1:
raise CommandError("Please provide a flavor ID") raise CommandError("Please provide a flavor ID")
flavor = get_flavor(args[0], for_update=True) flavor = get_resource("flavor", args[0], for_update=True)
deleted = options['deleted'] deleted = options['deleted']
......
# Copyright 2012 GRNET S.A. All rights reserved. # Copyright 2012-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or # Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following # without modification, are permitted provided that the following
...@@ -61,9 +61,9 @@ class Command(BaseCommand): ...@@ -61,9 +61,9 @@ class Command(BaseCommand):
raise CommandError('Please give either a server or a router id') raise CommandError('Please give either a server or a router id')
#get the vm #get the vm
vm = common.get_vm(device, for_update=True) vm = common.get_resource("server", device, for_update=True)
floating_ip = common.get_floating_ip_by_id(floating_ip_id, floating_ip = common.get_resource("floating-ip", floating_ip_id,
for_update=True) for_update=True)
servers.create_port(vm.userid, floating_ip.network, servers.create_port(vm.userid, floating_ip.network,
use_ipaddress=floating_ip, machine=vm) use_ipaddress=floating_ip, machine=vm)
......
# Copyright 2012 GRNET S.A. All rights reserved. # Copyright 2012-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or # Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following # without modification, are permitted provided that the following
...@@ -71,8 +71,9 @@ class Command(BaseCommand): ...@@ -71,8 +71,9 @@ class Command(BaseCommand):
raise CommandError("'owner' is required for floating IP creation") raise CommandError("'owner' is required for floating IP creation")
if network_id is not None: if network_id is not None:
network = util.get_network(network_id, owner, for_update=True, network = util.get_resource("network", network_id, for_update=True)
non_deleted=True) if network.deleted:
raise CommandError("Network '%s' is deleted" % network.id)
if not network.floating_ip_pool: if not network.floating_ip_pool:
raise CommandError("Network '%s' is not a floating IP pool." raise CommandError("Network '%s' is not a floating IP pool."
% network) % network)
......
# Copyright 2012 GRNET S.A. All rights reserved. # Copyright 2012-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or # Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following # without modification, are permitted provided that the following
...@@ -49,8 +49,8 @@ class Command(BaseCommand): ...@@ -49,8 +49,8 @@ class Command(BaseCommand):
floating_ip_id = args[0] floating_ip_id = args[0]
#get the floating-ip #get the floating-ip
floating_ip = common.get_floating_ip_by_id(floating_ip_id, floating_ip = common.get_resource("floating-ip", floating_ip_id,
for_update=True) for_update=True)
if not floating_ip.nic: if not floating_ip.nic:
raise CommandError('This floating IP is not attached to a device') raise CommandError('This floating IP is not attached to a device')
......
# Copyright 2013 GRNET S.A. All rights reserved. # Copyright 2013-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or # Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following # without modification, are permitted provided that the following
...@@ -57,8 +57,9 @@ class Command(RemoveCommand): ...@@ -57,8 +57,9 @@ class Command(RemoveCommand):
for floating_ip_id in args: for floating_ip_id in args:
self.stdout.write("\n") self.stdout.write("\n")
try: try:
floating_ip = common.get_floating_ip_by_id(floating_ip_id, floating_ip = common.get_resource("floating-ip",
for_update=True) floating_ip_id,
for_update=True)
ips.delete_floating_ip(floating_ip) ips.delete_floating_ip(floating_ip)
self.stdout.write("Deleted floating IP '%s'.\n" % self.stdout.write("Deleted floating IP '%s'.\n" %
floating_ip_id) floating_ip_id)
......
# Copyright 2012-2013 GRNET S.A. All rights reserved. # Copyright 2012-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or # Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following # without modification, are permitted provided that the following
...@@ -54,7 +54,7 @@ class Command(BaseCommand): ...@@ -54,7 +54,7 @@ class Command(BaseCommand):
if len(args) != 1: if len(args) != 1:
raise CommandError("Please provide a network ID.") raise CommandError("Please provide a network ID.")
network = common.get_network(args[0]) network = common.get_resource("network", args[0])
displayname = options['displayname'] displayname = options['displayname']
pprint.pprint_network(network, display_mails=displayname, pprint.pprint_network(network, display_mails=displayname,
......
# Copyright 2012-2013 GRNET S.A. All rights reserved. # Copyright 2012-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or # Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following # without modification, are permitted provided that the following
...@@ -36,7 +36,7 @@ from optparse import make_option ...@@ -36,7 +36,7 @@ from optparse import make_option
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from synnefo.db.models import Backend from synnefo.db.models import Backend
from synnefo.management.common import (get_network, get_backend) from synnefo.management.common import get_resource
from snf_django.management.utils import parse_bool from snf_django.management.utils import parse_bool
from synnefo.logic import networks, backend as backend_mod from synnefo.logic import networks, backend as backend_mod
from django.db import transaction from django.db import transaction
...@@ -95,7 +95,7 @@ class Command(BaseCommand): ...@@ -95,7 +95,7 @@ class Command(BaseCommand):
if len(args) != 1: if len(args) != 1:
raise CommandError("Please provide a network ID") raise CommandError("Please provide a network ID")
network = get_network(args[0]) network = get_resource("network", args[0])
new_name = options.get("name") new_name = options.get("name")
if new_name is not None: if new_name is not None:
...@@ -159,7 +159,7 @@ class Command(BaseCommand): ...@@ -159,7 +159,7 @@ class Command(BaseCommand):
add_to_backend = options["add_to_backend"] add_to_backend = options["add_to_backend"]
if add_to_backend is not None: if add_to_backend is not None:
backend = get_backend(add_to_backend) backend = get_resource("backend", add_to_backend)
bnet, jobs = backend_mod.ensure_network_is_active(backend, bnet, jobs = backend_mod.ensure_network_is_active(backend,
network.id) network.id)
if jobs: if jobs:
...@@ -168,7 +168,7 @@ class Command(BaseCommand): ...@@ -168,7 +168,7 @@ class Command(BaseCommand):
remove_from_backend = options["remove_from_backend"] remove_from_backend = options["remove_from_backend"]
if remove_from_backend is not None: if remove_from_backend is not None:
backend = get_backend(remove_from_backend) backend = get_resource("backend", remove_from_backend)
if network.nics.filter(machine__backend=backend, if network.nics.filter(machine__backend=backend,
machine__deleted=False).exists(): machine__deleted=False).exists():
msg = "Cannot remove. There are still connected VMs to this"\ msg = "Cannot remove. There are still connected VMs to this"\
......
# Copyright 2011-2013 GRNET S.A. All rights reserved. # Copyright 2011-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions # modification, are permitted provided that the following conditions
...@@ -32,7 +32,7 @@ from django.core.management.base import CommandError ...@@ -32,7 +32,7 @@ from django.core.management.base import CommandError
from snf_django.management.commands import RemoveCommand from snf_django.management.commands import RemoveCommand
from snf_django.lib.api import faults from snf_django.lib.api import faults
from synnefo.logic import networks from synnefo.logic import networks
from synnefo.management.common import get_network, convert_api_faults from synnefo.management import common
class Command(RemoveCommand): class Command(RemoveCommand):
...@@ -40,7 +40,7 @@ class Command(RemoveCommand): ...@@ -40,7 +40,7 @@ class Command(RemoveCommand):
args = "<Network ID> [<Network ID> ...]" args = "<Network ID> [<Network ID> ...]"
help = "Remove a network from the Database, and Ganeti" help = "Remove a network from the Database, and Ganeti"
@convert_api_faults @common.convert_api_faults
def handle(self, *args, **options): def handle(self, *args, **options):
if not args: if not args:
raise CommandError("Please provide a network ID") raise CommandError("Please provide a network ID")
...@@ -52,7 +52,8 @@ class Command(RemoveCommand): ...@@ -52,7 +52,8 @@ class Command(RemoveCommand):
for network_id in args: for network_id in args:
self.stdout.write("\n") self.stdout.write("\n")
try: try:
network = get_network(network_id, for_update=True) network = common.get_resource("network", network_id,
for_update=True)
self.stdout.write('Removing network: %s\n' % self.stdout.write('Removing network: %s\n' %
network.backend_id) network.backend_id)
......
# Copyright 2013 GRNET S.A. All rights reserved. # Copyright 2013-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or # Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following # without modification, are permitted provided that the following
...@@ -127,12 +127,12 @@ class Command(BaseCommand): ...@@ -127,12 +127,12 @@ class Command(BaseCommand):
owner = None owner = None
if server_id: if server_id:
owner = "vm" owner = "vm"
vm = common.get_vm(server_id, for_update=True) vm = common.get_resource("server", server_id, for_update=True)
#if vm.router: #if vm.router:
# raise CommandError("Server '%s' does not exist." % server_id) # raise CommandError("Server '%s' does not exist." % server_id)
elif router_id: elif router_id:
owner = "router" owner = "router"
vm = common.get_vm(router_id, for_update=True) vm = common.get_resource("server", router_id, for_update=True)
if not vm.router: if not vm.router:
raise CommandError("Router '%s' does not exist." % router_id) raise CommandError("Router '%s' does not exist." % router_id)
...@@ -143,21 +143,20 @@ class Command(BaseCommand): ...@@ -143,21 +143,20 @@ class Command(BaseCommand):
raise CommandError("Please specify the owner of the port.") raise CommandError("Please specify the owner of the port.")
# get the network # get the network
network = common.get_network(network_id) network = common.get_resource("network", network_id)
# Get either floating IP or fixed ip address # Get either floating IP or fixed ip address
ipaddress = None ipaddress = None
floating_ip_id = options["floating_ip_id"] floating_ip_id = options["floating_ip_id"]
ipv4_address = options["ipv4_address"] ipv4_address = options["ipv4_address"]
if floating_ip_id: if floating_ip_id:
ipaddress = common.get_floating_ip_by_id(floating_ip_id, ipaddress = common.get_resource("floating-ip", floating_ip_id,
for_update=True) for_update=True)
if ipv4_address is not None and ipaddress.address != ipv4_address: if ipv4_address is not None and ipaddress.address != ipv4_address:
raise CommandError("Floating IP address '%s' is different from" raise CommandError("Floating IP address '%s' is different from"
" specified address '%s'" % " specified address '%s'" %
(ipaddress.address, ipv4_address)) (ipaddress.address, ipv4_address))
# validate security groups # validate security groups
sg_list = [] sg_list = []
if security_group_ids: if security_group_ids:
......
# Copyright 2013 GRNET S.A. All rights reserved. # Copyright 2013-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or # Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following # without modification, are permitted provided that the following
...@@ -62,7 +62,7 @@ class Command(BaseCommand): ...@@ -62,7 +62,7 @@ class Command(BaseCommand):
if len(args) != 1: if len(args) != 1:
raise CommandError("Please provide a port ID") raise CommandError("Please provide a port ID")
port = common.get_port(args[0]) port = common.get_resource("port", args[0])
pprint.pprint_port(port, stdout=self.stdout) pprint.pprint_port(port, stdout=self.stdout)
self.stdout.write('\n\n') self.stdout.write('\n\n')
......
# Copyright 2011-2013 GRNET S.A. All rights reserved. # Copyright 2011-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions # modification, are permitted provided that the following conditions
...@@ -62,7 +62,7 @@ class Command(RemoveCommand): ...@@ -62,7 +62,7 @@ class Command(RemoveCommand):
for port_id in args: for port_id in args:
self.stdout.write("\n") self.stdout.write("\n")
try: try:
port = common.get_port(port_id, for_update=True) port = common.get_resource("port", port_id, for_update=True)
servers.delete_port(port) servers.delete_port(port)
......
# Copyright 2011-2013 GRNET S.A. All rights reserved. # Copyright 2011-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions # modification, are permitted provided that the following conditions
...@@ -39,7 +39,7 @@ import logging ...@@ -39,7 +39,7 @@ import logging
import subprocess import subprocess
from optparse import make_option from optparse import make_option
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from synnefo.management.common import get_backend from synnefo.management.common import get_resource
from synnefo.logic import reconciliation from synnefo.logic import reconciliation
from snf_django.management.utils import parse_bool from snf_django.management.utils import parse_bool
...@@ -87,7 +87,7 @@ class Command(BaseCommand): ...@@ -87,7 +87,7 @@ class Command(BaseCommand):
def handle(self, **options): def handle(self, **options):
backend_id = options['backend-id'] backend_id = options['backend-id']
if backend_id: if backend_id:
backends = [get_backend(backend_id)] backends = [get_resource("backend", backend_id)]
else: else:
backends = reconciliation.get_online_backends() backends = reconciliation.get_online_backends()
......
# Copyright 2012 GRNET S.A. All rights reserved. # Copyright 2012-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or # Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following # without modification, are permitted provided that the following
...@@ -108,10 +108,10 @@ class Command(BaseCommand): ...@@ -108,10 +108,10 @@ class Command(BaseCommand):
if not image_id: if not image_id:
raise CommandError("image-id is mandatory") raise CommandError("image-id is mandatory")
flavor = common.get_flavor(flavor_id) flavor = common.get_resource("flavor", flavor_id)
image = common.get_image(image_id, user_id) image = common.get_image(image_id, user_id)
if backend_id: if backend_id:
backend = common.get_backend(backend_id) backend = common.get_resource("backend", backend_id)
else: else:
backend = None backend = None
...@@ -147,7 +147,8 @@ def parse_connections(con_list): ...@@ -147,7 +147,8 @@ def parse_connections(con_list):
val = {"port": port_id} val = {"port": port_id}
elif con_kind == "floatingip": elif con_kind == "floatingip":
fip_id = opt.split(":")[1] fip_id = opt.split(":")[1]
fip = common.get_floating_ip_by_id(fip_id, for_update=True) fip = common.get_resource("floating-ip", fip_id,
for_update=True)
val = {"uuid": fip.network_id, "fixed_ip": fip.address} val = {"uuid": fip.network_id, "fixed_ip": fip.address}
else: else:
raise CommandError("Unknown argument for option --port") raise CommandError("Unknown argument for option --port")
......
...@@ -129,8 +129,8 @@ class Command(BaseCommand): ...@@ -129,8 +129,8 @@ class Command(BaseCommand):
def import_server(instance_name, backend_id, flavor_id, image_id, user_id, def import_server(instance_name, backend_id, flavor_id, image_id, user_id,
new_public_nic, stream=sys.stdout): new_public_nic, stream=sys.stdout):
flavor = common.get_flavor(flavor_id) flavor = common.get_resource("flavor", flavor_id)
backend = common.get_backend(backend_id) backend = common.get_resource("backend", backend_id)
backend_client = backend.get_client() backend_client = backend.get_client()
......
# Copyright 2012-2013 GRNET S.A. All rights reserved. # Copyright 2012-2014 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or # Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following # without modification, are permitted provided that the following
...@@ -61,7 +61,7 @@ class Command(BaseCommand): ...@@ -61,7 +61,7 @@ class Command(BaseCommand):
if len(args) != 1: if len(args) != 1:
raise CommandError("Please provide a server ID")