Revision d9c36253

b/snf-cyclades-app/synnefo/api/management/commands/floating-ip-attach.py
1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

  
34
from optparse import make_option
35

  
36
from django.core.management.base import BaseCommand, CommandError
37
from synnefo.management.common import convert_api_faults
38

  
39
from synnefo.api import util
40
from synnefo.management.common import get_network, get_vm
41
from synnefo.logic import servers
42

  
43

  
44
class Command(BaseCommand):
45
    can_import_settings = True
46
    output_transaction = True
47

  
48
    help = "Attach a floating IP to a VM or router"
49

  
50
    option_list = BaseCommand.option_list + (
51
        make_option(
52
            '--machine',
53
            dest='machine',
54
            default=None,
55
            help='The server id the floating-ip will be attached to'),
56
    )
57

  
58
    @convert_api_faults
59
    def handle(self, *args, **options):
60
        if not args or len(args) > 1:
61
            raise CommandError("Command accepts exactly one argument")
62

  
63
        floating_ip = args[0]  # this is the floating-ip address
64
        device = options['machine']
65

  
66
        if not device:
67
            raise CommandError('Please give either a server or a router id')
68

  
69
        #get the vm
70
        vm = get_vm(device)
71

  
72
        servers.add_floating_ip(vm, floating_ip)
73

  
74
        self.stdout.write("Attached %s to %s.\n" % (floating_ip, vm))
b/snf-cyclades-app/synnefo/api/util.py
270 270
        objects = IPAddress.objects
271 271
        if for_update:
272 272
            objects = objects.select_for_update()
273
        if not userid:
274
            return objects.get(id=floating_ip_id, floating_ip=True,
275
                               deleted=False)
276
        else:
277
            return objects.get(id=floating_ip_id, floating_ip=True,
273
        return objects.get(id=floating_ip_id, floating_ip=True,
278 274
                               userid=userid, deleted=False)
279 275
    except IPAddress.DoesNotExist:
280 276
        raise faults.ItemNotFound("Floating IP %s does not exist." %
b/snf-cyclades-app/synnefo/db/models.py
313 313
    serial = models.ForeignKey(QuotaHolderSerial,
314 314
                               related_name='virtual_machine', null=True,
315 315
                               on_delete=models.SET_NULL)
316
    router = models.BooleanField('Router', default=False)
316 317

  
317 318
    # VM State
318 319
    # The following fields are volatile data, in the sense
......
375 376
        get_latest_by = 'created'
376 377

  
377 378
    def __unicode__(self):
378
        return u"<vm:%s@backend:%s>" % (self.id, self.backend_id)
379
        return u"<vm:%s@backend:%s router:%s>" % (self.id, self.backend_id,
380
                                                  self.router)
379 381

  
380 382
    # Error classes
381 383
    class InvalidBackendIdError(Exception):
b/snf-cyclades-app/synnefo/logic/ports.py
56 56
@transaction.commit_on_success
57 57
def create(network, machine, name="", security_groups=None,
58 58
           device_owner='vm'):
59

  
59
    """Create a new port by giving a vm and a network"""
60 60
    if network.state != 'ACTIVE':
61 61
        raise faults.Conflict('Network build in process')
62 62

  
......
87 87

  
88 88
    return port
89 89

  
90

  
91 90
@transaction.commit_on_success
92 91
def delete(port):
93 92
    """Delete a port by removing the NIC card the instance.
b/snf-cyclades-app/synnefo/logic/servers.py
306 306
        raise faults.BuildInProgress('Network not active yet')
307 307

  
308 308
    nic = NetworkInterface.objects.create(machine=vm, network=network,
309
                                          state="BUILDING")
309
                                          state="BUILDING",
310
                                          device_owner='router'
311
                                                          if vm.router
312
                                                            else 'vm')
310 313
    if ipaddress is not None:
311 314
        ipaddress.nic = nic
312 315
        ipaddress.save()

Also available in: Unified diff