Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4 kB)

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.db import transaction
37
from django.core.management.base import BaseCommand, CommandError
38
from synnefo.management.common import convert_api_faults
39

    
40
from synnefo.api import util
41
from synnefo.db import pools
42
from synnefo import quotas
43

    
44

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

    
49
    help = "Allocate a new floating IP"
50

    
51
    option_list = BaseCommand.option_list + (
52
        make_option(
53
            '--pool',
54
            dest='pool',
55
            help="The IP pool to allocate the address from"),
56
        make_option(
57
            '--address',
58
            dest='address',
59
            help="The address to be allocated"),
60
        make_option(
61
            '--owner',
62
            dest='owner',
63
            default=None,
64
            # required=True,
65
            help='The owner of the floating IP'),
66
    )
67

    
68
    @transaction.commit_on_success
69
    @convert_api_faults
70
    def handle(self, *args, **options):
71
        if args:
72
            raise CommandError("Command doesn't accept any arguments")
73

    
74
        pool = options['pool']
75
        address = options['address']
76
        owner = options['owner']
77

    
78
        if not owner:
79
            raise CommandError("'owner' is required for floating IP creation")
80

    
81
        if pool is None:
82
            if address:
83
                raise CommandError('Please specify a pool as well')
84

    
85
            # User did not specified a pool. Choose a random public IP
86
            try:
87
                floating_ip = util.allocate_public_ip(userid=owner,
88
                                                      floating_ip=True)
89
            except pools.EmptyPool:
90
                raise faults.Conflict("No more IP addresses available.")
91
        else:
92
            try:
93
                network_id = int(pool)
94
            except ValueError:
95
                raise CommandError("Invalid pool ID.")
96
            network = util.get_network(network_id, owner, for_update=True,
97
                                       non_deleted=True)
98
            if not network.floating_ip_pool:
99
                # Check that it is a floating IP pool
100
                raise CommandError("Floating IP pool %s does not exist." %
101
                                           network_id)
102
            floating_ip = util.allocate_ip(network, owner, address=address,
103
                                           floating_ip=True)
104

    
105
        quotas.issue_and_accept_commission(floating_ip)
106
        transaction.commit()
107

    
108
        self.stdout.write("Created floating IP '%s'.\n" % floating_ip)