Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.2 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, Backend
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-ids",
131
            dest="backend_ids",
132
            default=None,
133
            help="Comma seperated list of Ganeti backends IDs that the network"
134
                 " will be created. Only for public networks. Use 'all' to"
135
                 " create network in all available backends."),
136
    )
137

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

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

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

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

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

    
176
        backends = []
177
        if backend_ids is not None:
178
            if backend_ids == "all":
179
                backends = Backend.objects.filter(offline=False)
180
            else:
181
                for backend_id in backend_ids.split(","):
182
                    try:
183
                        backend_id = int(backend_id)
184
                    except ValueError:
185
                        raise CommandError("Invalid backend-id: %s"
186
                                           % backend_id)
187
                    backend = get_backend(backend_id)
188
                    backends.append(backend)
189

    
190
        network = networks.create(user_id=userid, name=name, flavor=flavor,
191
                                  subnet=subnet, gateway=gateway,
192
                                  subnet6=subnet6, gateway6=gateway6,
193
                                  dhcp=dhcp, public=public, mode=mode,
194
                                  link=link, mac_prefix=mac_prefix, tags=tags,
195
                                  floating_ip_pool=floating_ip_pool,
196
                                  backends=backends, lazy_create=False)
197

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