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

cyclades: snf-manage floating-ip-attach

snf-manage floating-ip-attach <address> --machine <server_id/router_id>
parent a040fe36
# Copyright 2012 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
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A 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 optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.management.common import convert_api_faults
from synnefo.api import util
from synnefo.management.common import get_network, get_vm
from synnefo.logic import servers
class Command(BaseCommand):
can_import_settings = True
output_transaction = True
help = "Attach a floating IP to a VM or router"
option_list = BaseCommand.option_list + (
make_option(
'--machine',
dest='machine',
default=None,
help='The server id the floating-ip will be attached to'),
)
@convert_api_faults
def handle(self, *args, **options):
if not args or len(args) > 1:
raise CommandError("Command accepts exactly one argument")
floating_ip = args[0] # this is the floating-ip address
device = options['machine']
if not device:
raise CommandError('Please give either a server or a router id')
#get the vm
vm = get_vm(device)
servers.add_floating_ip(vm, floating_ip)
self.stdout.write("Attached %s to %s.\n" % (floating_ip, vm))
......@@ -270,11 +270,7 @@ def get_floating_ip_by_id(userid, floating_ip_id, for_update=False):
objects = IPAddress.objects
if for_update:
objects = objects.select_for_update()
if not userid:
return objects.get(id=floating_ip_id, floating_ip=True,
deleted=False)
else:
return objects.get(id=floating_ip_id, floating_ip=True,
return objects.get(id=floating_ip_id, floating_ip=True,
userid=userid, deleted=False)
except IPAddress.DoesNotExist:
raise faults.ItemNotFound("Floating IP %s does not exist." %
......
......@@ -313,6 +313,7 @@ class VirtualMachine(models.Model):
serial = models.ForeignKey(QuotaHolderSerial,
related_name='virtual_machine', null=True,
on_delete=models.SET_NULL)
router = models.BooleanField('Router', default=False)
# VM State
# The following fields are volatile data, in the sense
......@@ -375,7 +376,8 @@ class VirtualMachine(models.Model):
get_latest_by = 'created'
def __unicode__(self):
return u"<vm:%s@backend:%s>" % (self.id, self.backend_id)
return u"<vm:%s@backend:%s router:%s>" % (self.id, self.backend_id,
self.router)
# Error classes
class InvalidBackendIdError(Exception):
......
......@@ -56,7 +56,7 @@ def port_command(action):
@transaction.commit_on_success
def create(network, machine, name="", security_groups=None,
device_owner='vm'):
"""Create a new port by giving a vm and a network"""
if network.state != 'ACTIVE':
raise faults.Conflict('Network build in process')
......@@ -87,7 +87,6 @@ def create(network, machine, name="", security_groups=None,
return port
@transaction.commit_on_success
def delete(port):
"""Delete a port by removing the NIC card the instance.
......
......@@ -306,7 +306,10 @@ def create_nic(vm, network=None, ipaddress=None, address=None):
raise faults.BuildInProgress('Network not active yet')
nic = NetworkInterface.objects.create(machine=vm, network=network,
state="BUILDING")
state="BUILDING",
device_owner='router'
if vm.router
else 'vm')
if ipaddress is not None:
ipaddress.nic = nic
ipaddress.save()
......
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