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):
return u"<vm:%s@backend:%s>" % (self.id, self.backend_id)
# Error classes
class InvalidBackendIdError(Exception):
class InvalidBackendIdError(ValueError):
def __init__(self, value):
self.value = value
......@@ -587,7 +587,7 @@ class Network(models.Model):
free += ip_pool.count_available()
return total, free
class InvalidBackendIdError(Exception):
class InvalidBackendIdError(ValueError):
def __init__(self, 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
# without modification, are permitted provided that the following
......@@ -35,7 +35,7 @@ from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.db.models import Backend
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]
......@@ -82,7 +82,7 @@ class Command(BaseCommand):
if len(args) != 1:
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
credentials_changed = False
......@@ -95,8 +95,10 @@ class Command(BaseCommand):
if credentials_changed:
# check credentials, if any of them changed!
check_backend_credentials(backend.clustername, backend.port,
backend.username, backend.password)
common.check_backend_credentials(backend.clustername,
backend.port,
backend.username,
backend.password)
if options['drained']:
backend.drained = parse_bool(options['drained'], strict=True)
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
# modification, are permitted provided that the following conditions
......@@ -29,7 +29,7 @@
#
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.db.models import Backend
from django.db import transaction, models
......@@ -50,7 +50,7 @@ class Command(BaseCommand):
if len(args) < 1:
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)
......
# 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
# without modification, are permitted provided that the following
......@@ -34,7 +34,7 @@
from optparse import make_option
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
......@@ -67,7 +67,7 @@ class Command(BaseCommand):
if len(args) != 1:
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']
......
# 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
# without modification, are permitted provided that the following
......@@ -61,9 +61,9 @@ class Command(BaseCommand):
raise CommandError('Please give either a server or a router id')
#get the vm
vm = common.get_vm(device, for_update=True)
floating_ip = common.get_floating_ip_by_id(floating_ip_id,
for_update=True)
vm = common.get_resource("server", device, for_update=True)
floating_ip = common.get_resource("floating-ip", floating_ip_id,
for_update=True)
servers.create_port(vm.userid, floating_ip.network,
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
# without modification, are permitted provided that the following
......@@ -71,8 +71,9 @@ class Command(BaseCommand):
raise CommandError("'owner' is required for floating IP creation")
if network_id is not None:
network = util.get_network(network_id, owner, for_update=True,
non_deleted=True)
network = util.get_resource("network", network_id, for_update=True)
if network.deleted:
raise CommandError("Network '%s' is deleted" % network.id)
if not network.floating_ip_pool:
raise CommandError("Network '%s' is not a floating IP pool."
% 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
# without modification, are permitted provided that the following
......@@ -49,8 +49,8 @@ class Command(BaseCommand):
floating_ip_id = args[0]
#get the floating-ip
floating_ip = common.get_floating_ip_by_id(floating_ip_id,
for_update=True)
floating_ip = common.get_resource("floating-ip", floating_ip_id,
for_update=True)
if not floating_ip.nic:
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
# without modification, are permitted provided that the following
......@@ -57,8 +57,9 @@ class Command(RemoveCommand):
for floating_ip_id in args:
self.stdout.write("\n")
try:
floating_ip = common.get_floating_ip_by_id(floating_ip_id,
for_update=True)
floating_ip = common.get_resource("floating-ip",
floating_ip_id,
for_update=True)
ips.delete_floating_ip(floating_ip)
self.stdout.write("Deleted floating IP '%s'.\n" %
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
# without modification, are permitted provided that the following
......@@ -54,7 +54,7 @@ class Command(BaseCommand):
if len(args) != 1:
raise CommandError("Please provide a network ID.")
network = common.get_network(args[0])
network = common.get_resource("network", args[0])
displayname = options['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
# without modification, are permitted provided that the following
......@@ -36,7 +36,7 @@ from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
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 synnefo.logic import networks, backend as backend_mod
from django.db import transaction
......@@ -95,7 +95,7 @@ class Command(BaseCommand):
if len(args) != 1:
raise CommandError("Please provide a network ID")
network = get_network(args[0])
network = get_resource("network", args[0])
new_name = options.get("name")
if new_name is not None:
......@@ -159,7 +159,7 @@ class Command(BaseCommand):
add_to_backend = options["add_to_backend"]
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,
network.id)
if jobs:
......@@ -168,7 +168,7 @@ class Command(BaseCommand):
remove_from_backend = options["remove_from_backend"]
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,
machine__deleted=False).exists():
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
# modification, are permitted provided that the following conditions
......@@ -32,7 +32,7 @@ from django.core.management.base import CommandError
from snf_django.management.commands import RemoveCommand
from snf_django.lib.api import faults
from synnefo.logic import networks
from synnefo.management.common import get_network, convert_api_faults
from synnefo.management import common
class Command(RemoveCommand):
......@@ -40,7 +40,7 @@ class Command(RemoveCommand):
args = "<Network ID> [<Network ID> ...]"
help = "Remove a network from the Database, and Ganeti"
@convert_api_faults
@common.convert_api_faults
def handle(self, *args, **options):
if not args:
raise CommandError("Please provide a network ID")
......@@ -52,7 +52,8 @@ class Command(RemoveCommand):
for network_id in args:
self.stdout.write("\n")
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' %
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
# without modification, are permitted provided that the following
......@@ -127,12 +127,12 @@ class Command(BaseCommand):
owner = None
if server_id:
owner = "vm"
vm = common.get_vm(server_id, for_update=True)
vm = common.get_resource("server", server_id, for_update=True)
#if vm.router:
# raise CommandError("Server '%s' does not exist." % server_id)
elif router_id:
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:
raise CommandError("Router '%s' does not exist." % router_id)
......@@ -143,21 +143,20 @@ class Command(BaseCommand):
raise CommandError("Please specify the owner of the port.")
# get the network
network = common.get_network(network_id)
network = common.get_resource("network", network_id)
# Get either floating IP or fixed ip address
ipaddress = None
floating_ip_id = options["floating_ip_id"]
ipv4_address = options["ipv4_address"]
if floating_ip_id:
ipaddress = common.get_floating_ip_by_id(floating_ip_id,
for_update=True)
ipaddress = common.get_resource("floating-ip", floating_ip_id,
for_update=True)
if ipv4_address is not None and ipaddress.address != ipv4_address:
raise CommandError("Floating IP address '%s' is different from"
" specified address '%s'" %
(ipaddress.address, ipv4_address))
# validate security groups
sg_list = []
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
# without modification, are permitted provided that the following
......@@ -62,7 +62,7 @@ class Command(BaseCommand):
if len(args) != 1:
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)
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
# modification, are permitted provided that the following conditions
......@@ -62,7 +62,7 @@ class Command(RemoveCommand):
for port_id in args:
self.stdout.write("\n")
try:
port = common.get_port(port_id, for_update=True)
port = common.get_resource("port", port_id, for_update=True)
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
# modification, are permitted provided that the following conditions
......@@ -39,7 +39,7 @@ import logging
import subprocess
from optparse import make_option
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 snf_django.management.utils import parse_bool
......@@ -87,7 +87,7 @@ class Command(BaseCommand):
def handle(self, **options):
backend_id = options['backend-id']
if backend_id:
backends = [get_backend(backend_id)]
backends = [get_resource("backend", backend_id)]
else:
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
# without modification, are permitted provided that the following
......@@ -108,10 +108,10 @@ class Command(BaseCommand):
if not image_id:
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)
if backend_id:
backend = common.get_backend(backend_id)
backend = common.get_resource("backend", backend_id)
else:
backend = None
......@@ -147,7 +147,8 @@ def parse_connections(con_list):
val = {"port": port_id}
elif con_kind == "floatingip":
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}
else:
raise CommandError("Unknown argument for option --port")
......
......@@ -129,8 +129,8 @@ class Command(BaseCommand):
def import_server(instance_name, backend_id, flavor_id, image_id, user_id,
new_public_nic, stream=sys.stdout):
flavor = common.get_flavor(flavor_id)
backend = common.get_backend(backend_id)
flavor = common.get_resource("flavor", flavor_id)
backend = common.get_resource("backend", backend_id)
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
# without modification, are permitted provided that the following
......@@ -61,7 +61,7 @@ class Command(BaseCommand):
if len(args) != 1:
raise CommandError("Please provide a server ID")
vm = common.get_vm(args[0], for_update=True)
vm = common.get_resource("server", args[0], for_update=True)
displayname = options['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
# without modification, are permitted provided that the following
......@@ -36,7 +36,7 @@ from functools import partial
from snf_django.management.commands import ListCommand
from synnefo.db.models import VirtualMachine
from synnefo.management.common import get_backend
from synnefo.management.common import get_resource
from synnefo.api.util import get_image
from synnefo.settings import (CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN,
ASTAKOS_AUTH_URL)
......@@ -120,7 +120,7 @@ class Command(ListCommand):
self.filters["suspended"] = True
if options["backend_id"]:
backend = get_backend(options["backend_id"])
backend = get_resource("backend", options["backend_id"])
self.filters["backend"] = backend.id
if options["build"]:
......
# 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
# without modification, are permitted provided that the following
......@@ -35,7 +35,7 @@ from optparse import make_option
from django.db import transaction
from django.core.management.base import BaseCommand, CommandError
from synnefo.management.common import (get_vm, get_flavor, convert_api_faults,
from synnefo.management.common import (get_resource, convert_api_faults,
wait_server_task)
from snf_django.management.utils import parse_bool
from synnefo.logic import servers
......@@ -93,7 +93,7 @@ class Command(BaseCommand):
if len(args) != 1:
raise CommandError("Please provide a server ID")
server = get_vm(args[0], for_update=True)
server = get_resource("server", args[0], for_update=True)
new_name = options.get("name", None)
if new_name is not None:
......@@ -123,7 +123,7 @@ class Command(BaseCommand):
wait = parse_bool(options["wait"])
new_flavor_id = options.get("flavor")
if new_flavor_id is not None:
new_flavor = get_flavor(new_flavor_id)
new_flavor = get_resource("flavor", new_flavor_id)
old_flavor = server.flavor
msg = "Resizing server '%s' from flavor '%s' to '%s'.\n"
self.stdout.write(msg % (server, old_flavor, new_flavor))
......
# 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
# without modification, are permitted provided that the following
......@@ -34,7 +34,7 @@
from optparse import make_option
from django.core.management.base import CommandError
from synnefo.management.common import (get_vm, convert_api_faults,
from synnefo.management.common import (get_resource, convert_api_faults,
wait_server_task)
from synnefo.logic import servers
from snf_django.management.commands import RemoveCommand
......@@ -68,7 +68,7 @@ class Command(RemoveCommand):
for server_id in args:
self.stdout.write("\n")
try:
server = get_vm(server_id)
server = get_resource("server", server_id, for_update=True)
self.stdout.write("Trying to remove server '%s' from backend "
"'%s' \n" % (server.backend_vm_id,
......
# 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
# without modification, are permitted provided that the following
......@@ -33,7 +33,7 @@
from django.core.management.base import CommandError
from snf_django.management.commands import SynnefoCommand
from synnefo.management.common import (format_vm_state, get_vm,
from synnefo.management.common import (format_vm_state, get_resource,
get_image)
from snf_django.lib.astakos import UserCache
from synnefo.settings import (CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN,
......@@ -49,7 +49,7 @@ class Command(SynnefoCommand):
if len(args) != 1:
raise CommandError("Please provide a server ID")
server = get_vm(args[0])
server = get_resource("server", args[0])
flavor = '%s (%s)' % (server.flavor.id, server.flavor.name)
userid = server.userid
......
# 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
# without modification, are permitted provided that the following
......@@ -31,7 +31,6 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
import json
import string
......@@ -40,7 +39,7 @@ from optparse import make_option
from snf_django.management.utils import pprint_table, parse_bool
from snf_django.management.commands import SynnefoCommand, CommandError
from synnefo.management.common import get_backend
from synnefo.management.common import get_resource
from synnefo.admin import stats as statistics
......@@ -87,7 +86,7 @@ class Command(SynnefoCommand):
def handle(self, *args, **options):
if options["backend"] is not None:
backend = get_backend(options["backend"])
backend = get_resource("backend", options["backend"])
else:
backend = None
......
# 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
# without modification, are permitted provided that the following
......@@ -39,8 +39,6 @@ from snf_django.management.utils import parse_bool
from synnefo.management import pprint
from synnefo.logic import subnets
import ipaddr
HELP_MSG = """
Create a new subnet without authenticating the user. The limit of one
......@@ -103,7 +101,7 @@ class Command(BaseCommand):
if not cidr:
raise CommandError("cidr is mandatory")
user_id = common.get_network(network_id).userid
user_id = common.get_resource("network", network_id).userid
name = options["name"] or ""
allocation_pools = options["allocation_pools"]
ipversion = options["ipversion"] or 4
......
......@@ -31,7 +31,7 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from optparse import make_option
#from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.management import pprint, common
......@@ -46,7 +46,7 @@ class Command(BaseCommand):
if len(args) != 1:
raise CommandError("Please provide a subnet ID.")
subnet = common.get_subnet(args[0])
subnet = common.get_resource("subnet", args[0])
pprint.pprint_subnet_in_db(subnet, stdout=self.stdout)
self.stdout.write("\n\n")
......
# 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
# without modification, are permitted provided that the following
......@@ -66,8 +66,8 @@ class Command(BaseCommand):
if not name:
raise CommandError("--name is mandatory")
subnet = common.get_subnet(subnet_id)
user_id = common.get_network(subnet.network.id).userid
subnet = common.get_resource("subnet", subnet_id, for_update=True)
user_id = common.get_resource("network", subnet.network.id).userid
subnets.update_subnet(sub_id=subnet_id,
name=name,
......
# Copyright 2012 GRNET S.A. All rights reserved.
# Copyright 2012-2014 GRNET S.A. All rights reserved.