Statistics
| Branch: | Tag: | Revision:

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

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

    
40
from synnefo.db.models import Network, Backend
41
from synnefo.logic import networks
42
from synnefo.management import pprint
43

    
44
NETWORK_FLAVORS = Network.FLAVORS.keys()
45

    
46

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

    
51
    help = "Create a new network"
52

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

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

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

    
160
        if name is None:
161
            name = ""
162
        if flavor is None:
163
            raise CommandError("flavor is required")
164

    
165
        if (subnet is None) and (subnet6 is None):
166
            raise CommandError("subnet or subnet6 is required")
167
        if subnet is None and gateway is not None:
168
            raise CommandError("Can not use gateway without subnet")
169
        if subnet6 is None and gateway6 is not None:
170
            raise CommandError("Can not use gateway6 without subnet6")
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(userid=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)
199
        pprint.pprint_network(network, stdout=self.stdout)
200
        pprint.pprint_network_subnets(network, stdout=self.stdout)