root / snf-cyclades-app / synnefo / logic / management / commands / floating-ip-create.py @ a6e6fe48
History | View | Annotate | Download (3.3 kB)
1 | a6e6fe48 | Christos Stavrakakis | # Copyright 2012-2014 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | f29f1326 | Marios Kogias | #
|
3 | f29f1326 | Marios Kogias | # Redistribution and use in source and binary forms, with or
|
4 | f29f1326 | Marios Kogias | # without modification, are permitted provided that the following
|
5 | f29f1326 | Marios Kogias | # conditions are met:
|
6 | f29f1326 | Marios Kogias | #
|
7 | f29f1326 | Marios Kogias | # 1. Redistributions of source code must retain the above
|
8 | f29f1326 | Marios Kogias | # copyright notice, this list of conditions and the following
|
9 | f29f1326 | Marios Kogias | # disclaimer.
|
10 | f29f1326 | Marios Kogias | #
|
11 | f29f1326 | Marios Kogias | # 2. Redistributions in binary form must reproduce the above
|
12 | f29f1326 | Marios Kogias | # copyright notice, this list of conditions and the following
|
13 | f29f1326 | Marios Kogias | # disclaimer in the documentation and/or other materials
|
14 | f29f1326 | Marios Kogias | # provided with the distribution.
|
15 | f29f1326 | Marios Kogias | #
|
16 | f29f1326 | Marios Kogias | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | f29f1326 | Marios Kogias | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | f29f1326 | Marios Kogias | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | f29f1326 | Marios Kogias | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | f29f1326 | Marios Kogias | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | f29f1326 | Marios Kogias | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | f29f1326 | Marios Kogias | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | f29f1326 | Marios Kogias | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | f29f1326 | Marios Kogias | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | f29f1326 | Marios Kogias | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | f29f1326 | Marios Kogias | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | f29f1326 | Marios Kogias | # POSSIBILITY OF SUCH DAMAGE.
|
28 | f29f1326 | Marios Kogias | #
|
29 | f29f1326 | Marios Kogias | # The views and conclusions contained in the software and
|
30 | f29f1326 | Marios Kogias | # documentation are those of the authors and should not be
|
31 | f29f1326 | Marios Kogias | # interpreted as representing official policies, either expressed
|
32 | f29f1326 | Marios Kogias | # or implied, of GRNET S.A.
|
33 | f29f1326 | Marios Kogias | |
34 | f29f1326 | Marios Kogias | from optparse import make_option |
35 | f29f1326 | Marios Kogias | |
36 | f29f1326 | Marios Kogias | from django.core.management.base import BaseCommand, CommandError |
37 | f29f1326 | Marios Kogias | from synnefo.management.common import convert_api_faults |
38 | 0292883e | Christos Stavrakakis | from synnefo.logic import ips |
39 | f29f1326 | Marios Kogias | from synnefo.api import util |
40 | f29f1326 | Marios Kogias | |
41 | f29f1326 | Marios Kogias | |
42 | f29f1326 | Marios Kogias | class Command(BaseCommand): |
43 | f29f1326 | Marios Kogias | help = "Allocate a new floating IP"
|
44 | f29f1326 | Marios Kogias | |
45 | f29f1326 | Marios Kogias | option_list = BaseCommand.option_list + ( |
46 | f29f1326 | Marios Kogias | make_option( |
47 | 1229a55f | Christos Stavrakakis | '--network',
|
48 | 1229a55f | Christos Stavrakakis | dest='network_id',
|
49 | 1229a55f | Christos Stavrakakis | help="The ID of the network to allocate the address from"),
|
50 | f29f1326 | Marios Kogias | make_option( |
51 | f29f1326 | Marios Kogias | '--address',
|
52 | f29f1326 | Marios Kogias | dest='address',
|
53 | f29f1326 | Marios Kogias | help="The address to be allocated"),
|
54 | f29f1326 | Marios Kogias | make_option( |
55 | f29f1326 | Marios Kogias | '--owner',
|
56 | f29f1326 | Marios Kogias | dest='owner',
|
57 | f29f1326 | Marios Kogias | default=None,
|
58 | f29f1326 | Marios Kogias | help='The owner of the floating IP'),
|
59 | f29f1326 | Marios Kogias | ) |
60 | f29f1326 | Marios Kogias | |
61 | f29f1326 | Marios Kogias | @convert_api_faults
|
62 | f29f1326 | Marios Kogias | def handle(self, *args, **options): |
63 | f29f1326 | Marios Kogias | if args:
|
64 | f29f1326 | Marios Kogias | raise CommandError("Command doesn't accept any arguments") |
65 | f29f1326 | Marios Kogias | |
66 | 1229a55f | Christos Stavrakakis | network_id = options['network_id']
|
67 | f29f1326 | Marios Kogias | address = options['address']
|
68 | f29f1326 | Marios Kogias | owner = options['owner']
|
69 | f29f1326 | Marios Kogias | |
70 | f29f1326 | Marios Kogias | if not owner: |
71 | f29f1326 | Marios Kogias | raise CommandError("'owner' is required for floating IP creation") |
72 | f29f1326 | Marios Kogias | |
73 | 0292883e | Christos Stavrakakis | if network_id is not None: |
74 | a6e6fe48 | Christos Stavrakakis | network = util.get_resource("network", network_id, for_update=True) |
75 | a6e6fe48 | Christos Stavrakakis | if network.deleted:
|
76 | a6e6fe48 | Christos Stavrakakis | raise CommandError("Network '%s' is deleted" % network.id) |
77 | 1229a55f | Christos Stavrakakis | if not network.floating_ip_pool: |
78 | 1229a55f | Christos Stavrakakis | raise CommandError("Network '%s' is not a floating IP pool." |
79 | 1229a55f | Christos Stavrakakis | % network) |
80 | 0292883e | Christos Stavrakakis | else:
|
81 | 0292883e | Christos Stavrakakis | network = None
|
82 | f29f1326 | Marios Kogias | |
83 | 0292883e | Christos Stavrakakis | floating_ip = ips.create_floating_ip(userid=owner, |
84 | 0292883e | Christos Stavrakakis | network=network, |
85 | 0292883e | Christos Stavrakakis | address=address) |
86 | f29f1326 | Marios Kogias | |
87 | f29f1326 | Marios Kogias | self.stdout.write("Created floating IP '%s'.\n" % floating_ip) |