Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / floating-ip-release.py @ 8b178e6b

History | View | Annotate | Download (2.8 kB)

1 9d1e6480 Marios Kogias
# Copyright 2012 GRNET S.A. All rights reserved.
2 9d1e6480 Marios Kogias
#
3 9d1e6480 Marios Kogias
# Redistribution and use in source and binary forms, with or
4 9d1e6480 Marios Kogias
# without modification, are permitted provided that the following
5 9d1e6480 Marios Kogias
# conditions are met:
6 9d1e6480 Marios Kogias
#
7 9d1e6480 Marios Kogias
#   1. Redistributions of source code must retain the above
8 9d1e6480 Marios Kogias
#      copyright notice, this list of conditions and the following
9 9d1e6480 Marios Kogias
#      disclaimer.
10 9d1e6480 Marios Kogias
#
11 9d1e6480 Marios Kogias
#   2. Redistributions in binary form must reproduce the above
12 9d1e6480 Marios Kogias
#      copyright notice, this list of conditions and the following
13 9d1e6480 Marios Kogias
#      disclaimer in the documentation and/or other materials
14 9d1e6480 Marios Kogias
#      provided with the distribution.
15 9d1e6480 Marios Kogias
#
16 9d1e6480 Marios Kogias
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 9d1e6480 Marios Kogias
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 9d1e6480 Marios Kogias
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 9d1e6480 Marios Kogias
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 9d1e6480 Marios Kogias
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 9d1e6480 Marios Kogias
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 9d1e6480 Marios Kogias
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 9d1e6480 Marios Kogias
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 9d1e6480 Marios Kogias
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 9d1e6480 Marios Kogias
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 9d1e6480 Marios Kogias
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 9d1e6480 Marios Kogias
# POSSIBILITY OF SUCH DAMAGE.
28 9d1e6480 Marios Kogias
#
29 9d1e6480 Marios Kogias
# The views and conclusions contained in the software and
30 9d1e6480 Marios Kogias
# documentation are those of the authors and should not be
31 9d1e6480 Marios Kogias
# interpreted as representing official policies, either expressed
32 9d1e6480 Marios Kogias
# or implied, of GRNET S.A.
33 9d1e6480 Marios Kogias
34 9d1e6480 Marios Kogias
from optparse import make_option
35 9d1e6480 Marios Kogias
36 9d1e6480 Marios Kogias
from django.db import transaction
37 9d1e6480 Marios Kogias
from django.core.management.base import BaseCommand, CommandError
38 9d1e6480 Marios Kogias
39 9d1e6480 Marios Kogias
from synnefo.management.common import get_floating_ip_by_address
40 9d1e6480 Marios Kogias
from synnefo import quotas
41 9d1e6480 Marios Kogias
42 9d1e6480 Marios Kogias
class Command(BaseCommand):
43 9d1e6480 Marios Kogias
    can_import_settings = True
44 9d1e6480 Marios Kogias
    output_transaction = True
45 9d1e6480 Marios Kogias
46 9d1e6480 Marios Kogias
    help = "Release a floating IP"
47 9d1e6480 Marios Kogias
48 9d1e6480 Marios Kogias
    @transaction.commit_on_success
49 9d1e6480 Marios Kogias
    def handle(self, *args, **options):
50 9d1e6480 Marios Kogias
        if not args:
51 9d1e6480 Marios Kogias
            raise CommandError("Please provide a floating-ip address")
52 9d1e6480 Marios Kogias
53 9d1e6480 Marios Kogias
        address = args[0]
54 9d1e6480 Marios Kogias
55 9d1e6480 Marios Kogias
        floating_ip = get_floating_ip_by_address(address, for_update=True)
56 9d1e6480 Marios Kogias
        if floating_ip.nic:
57 9d1e6480 Marios Kogias
            # This is safe, you also need for_update to attach floating IP to
58 9d1e6480 Marios Kogias
            # instance.
59 9d1e6480 Marios Kogias
            msg = "Floating IP '%s' is attached to instance." % floating_ip.id
60 9d1e6480 Marios Kogias
            raise CommandError(msg)
61 9d1e6480 Marios Kogias
62 9d1e6480 Marios Kogias
        # Return the address of the floating IP back to pool
63 9d1e6480 Marios Kogias
        floating_ip.release_address()
64 9d1e6480 Marios Kogias
        # And mark the floating IP as deleted
65 9d1e6480 Marios Kogias
        floating_ip.deleted = True
66 9d1e6480 Marios Kogias
        floating_ip.save()
67 9d1e6480 Marios Kogias
        # Release quota for floating IP
68 9d1e6480 Marios Kogias
        quotas.issue_and_accept_commission(floating_ip, delete=True)
69 9d1e6480 Marios Kogias
        transaction.commit()
70 9d1e6480 Marios Kogias
        # Delete the floating IP from DB
71 9d1e6480 Marios Kogias
        floating_ip.delete()
72 9d1e6480 Marios Kogias
73 9d1e6480 Marios Kogias
        self.stdout.write("Deleted floating IP '%s'.\n" % address)