Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / network-create.py @ b47f167a

History | View | Annotate | Download (6.8 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.core.management.base import BaseCommand, CommandError
37
from synnefo.management.common import get_backend, convert_api_faults
38
from synnefo.webproject.management.utils import parse_bool
39

    
40
from synnefo.db.models import Network
41
from synnefo.logic import networks
42

    
43
NETWORK_FLAVORS = Network.FLAVORS.keys()
44

    
45

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

    
50
    help = "Create a new network"
51

    
52
    option_list = BaseCommand.option_list + (
53
        make_option(
54
            '--name',
55
            dest='name',
56
            help="Name of network"),
57
        make_option(
58
            '--owner',
59
            dest='owner',
60
            help="The owner of the network"),
61
        make_option(
62
            '--subnet',
63
            dest='subnet',
64
            default=None,
65
            # required=True,
66
            help='Subnet of the network'),
67
        make_option(
68
            '--gateway',
69
            dest='gateway',
70
            default=None,
71
            help='Gateway of the network'),
72
        make_option(
73
            '--subnet6',
74
            dest='subnet6',
75
            default=None,
76
            help='IPv6 subnet of the network'),
77
        make_option(
78
            '--gateway6',
79
            dest='gateway6',
80
            default=None,
81
            help='IPv6 gateway of the network'),
82
        make_option(
83
            '--dhcp',
84
            dest='dhcp',
85
            default="False",
86
            choices=["True", "False"],
87
            metavar="True|False",
88
            help='Automatically assign IPs'),
89
        make_option(
90
            '--public',
91
            dest='public',
92
            action='store_true',
93
            default=False,
94
            help='Network is public'),
95
        make_option(
96
            '--flavor',
97
            dest='flavor',
98
            default=None,
99
            choices=NETWORK_FLAVORS,
100
            help='Network flavor. Choices: ' + ', '.join(NETWORK_FLAVORS)),
101
        make_option(
102
            '--mode',
103
            dest='mode',
104
            default=None,
105
            help="Overwrite flavor connectivity mode."),
106
        make_option(
107
            '--link',
108
            dest='link',
109
            default=None,
110
            help="Overwrite flavor connectivity link."),
111
        make_option(
112
            '--mac-prefix',
113
            dest='mac_prefix',
114
            default=None,
115
            help="Overwrite flavor connectivity MAC prefix"),
116
        make_option(
117
            '--tags',
118
            dest='tags',
119
            default=None,
120
            help='The tags of the Network (comma separated strings)'),
121
        make_option(
122
            '--floating-ip-pool',
123
            dest='floating_ip_pool',
124
            default="False",
125
            choices=["True", "False"],
126
            metavar="True|False",
127
            help="Use the network as a Floating IP pool. Floating IP pools"
128
                 " are created in all available backends."),
129
        make_option(
130
            '--backend-id',
131
            dest='backend_id',
132
            default=None,
133
            help='ID of the backend that the network will be created. Only for'
134
                 ' public networks'),
135
    )
136

    
137
    @convert_api_faults
138
    def handle(self, *args, **options):
139
        if args:
140
            raise CommandError("Command doesn't accept any arguments")
141

    
142
        name = options['name']
143
        subnet = options['subnet']
144
        gateway = options['gateway']
145
        subnet6 = options['subnet6']
146
        gateway6 = options['gateway6']
147
        backend_id = options['backend_id']
148
        public = options['public']
149
        flavor = options['flavor']
150
        mode = options['mode']
151
        link = options['link']
152
        mac_prefix = options['mac_prefix']
153
        tags = options['tags']
154
        userid = options["owner"]
155
        floating_ip_pool = parse_bool(options["floating_ip_pool"])
156
        dhcp = parse_bool(options["dhcp"])
157

    
158
        if not name:
159
            raise CommandError("name is required")
160
        if not flavor:
161
            raise CommandError("flavor is required")
162

    
163
        if (subnet is None) and (subnet6 is None):
164
            raise CommandError("subnet or subnet6 is required")
165
        if subnet is None and gateway is not None:
166
            raise CommandError("Can not use gateway without subnet")
167
        if subnet6 is None and gateway6 is not None:
168
            raise CommandError("Can not use gateway6 without subnet6")
169

    
170
        if public and not (backend_id or floating_ip_pool):
171
            raise CommandError("backend-id is required")
172
        if not userid and not public:
173
            raise CommandError("'owner' is required for private networks")
174

    
175
        if backend_id is not None:
176
            try:
177
                backend_id = int(backend_id)
178
            except ValueError:
179
                raise CommandError("Invalid backend-id: %s" % backend_id)
180
            backend = get_backend(backend_id)
181
        else:
182
            backend = None
183

    
184
        network = networks.create(user_id=userid, name=name, flavor=flavor,
185
                                  subnet=subnet, gateway=gateway,
186
                                  subnet6=subnet6, gateway6=gateway6,
187
                                  dhcp=dhcp, public=public, mode=mode,
188
                                  link=link, mac_prefix=mac_prefix, tags=tags,
189
                                  floating_ip_pool=floating_ip_pool,
190
                                  backend=backend, lazy_create=False)
191

    
192
        self.stdout.write("Created network '%s' in DB.\n" % network)