Commit eac82857 authored by Marios Kogias's avatar Marios Kogias
Browse files

cyclades: Add snf-manage port-remove command

parent 6a145e4f
......@@ -39,6 +39,7 @@ from snf_django.management.utils import parse_bool
from synnefo.db.models import Network, NetworkInterface, SecurityGroup
from synnefo.api import util
from synnefo.management.common import get_network, get_vm
from synnefo.logic import ports
......@@ -54,11 +55,6 @@ class Command(BaseCommand):
dest='name',
default=None,
help="Name of port"),
make_option(
'--owner',
dest='owner',
default=None,
help="The owner of the port"),
make_option(
'--network',
dest='network',
......@@ -86,7 +82,6 @@ class Command(BaseCommand):
device = options['device_id']
#assume giving security groups comma separated
security_groups = options['security-groups']
userid = options["owner"]
if not name:
name=""
......@@ -95,14 +90,12 @@ class Command(BaseCommand):
raise CommandError("network is required")
if not device:
raise CommandError("device is required")
if not userid:
raise CommandError("owner is required")
#get the network
network = util.get_network(network, userid, non_deleted=True)
network = get_network(network)
#get the vm
vm = util.get_vm(device, userid)
vm = get_vm(device)
#validate security groups
sg_list = []
......@@ -112,5 +105,5 @@ class Command(BaseCommand):
sg = util.get_security_group(int(gid))
sg_list.append(sg)
new_port = ports.create(userid, network, vm, security_groups=sg_list)
new_port = ports.create(network, vm, security_groups=sg_list)
self.stdout.write("Created port '%s' in DB.\n" % new_port)
......@@ -77,4 +77,4 @@ class Command(ListCommand):
}
fields = ["id", "name", "user.uuid", "mac_address", "network",
"fixed_ips"]
"device_id", "fixed_ips"]
# Copyright 2011-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 THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# 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.
#
from django.core.management.base import BaseCommand, CommandError
from synnefo.logic import ports
from synnefo.api.util import get_port
from synnefo.management.common import convert_api_faults
class Command(BaseCommand):
can_import_settings = True
help = "Remove a port from the Database and from the VMs attached to"
@convert_api_faults
def handle(self, *args, **options):
if len(args) < 1:
raise CommandError("Please provide a port ID")
port = get_port(args[0], None)
ports.delete(port)
self.stdout.write("Successfully removed port\n")
......@@ -132,7 +132,7 @@ def create_port(request):
sg = util.get_security_group(int(gid))
sg_list.append(sg)
new_port = ports.create(user_id, network, vm, security_groups=sg_list)
new_port = ports.create(network, vm, security_groups=sg_list)
response = render_port(request, port_to_dict(new_port), status=201)
......
......@@ -235,7 +235,10 @@ def get_port(port_id, user_id, for_update=False):
if for_update:
objects = objects.select_for_update()
port = objects.get(network__userid=user_id, id=port_id)
if not user_id:
port = objects.get(id=port_id)
else:
port = objects.get(network__userid=user_id, id=port_id)
if (port.device_owner != "vm") and for_update:
raise faults.BadRequest('Can not update non vm port')
......
......@@ -55,7 +55,7 @@ def port_command(action):
return decorator
@transaction.commit_on_success
def create(userid, network, machine, name="", security_groups=None,
def create(network, machine, name="", security_groups=None,
device_owner='vm'):
if network.state != 'ACTIVE':
......@@ -65,6 +65,7 @@ def create(userid, network, machine, name="", security_groups=None,
port = NetworkInterface.objects.create(name=name,
network=network,
machine=machine,
userid=machine.userid,
device_owner=device_owner,
state="BUILDING")
#add the security groups if any
......@@ -76,7 +77,7 @@ def create(userid, network, machine, name="", security_groups=None,
IPAddress.objects.create(subnet=subn,
network=network,
nic=port,
userid=userid,
userid=machine.userid,
# FIXME
address="192.168.0." + str(subn.id))
......@@ -86,3 +87,8 @@ def create(userid, network, machine, name="", security_groups=None,
#quotas.issue_and_accept_commission(new_port)
return port
@transaction.commit_on_success
def delete(port):
port.ips.all().delete()
port.delete()
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