Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.1 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 validate_network_info, get_backend
38

    
39
from synnefo.db.models import Network
40
from synnefo.logic.backend import create_network
41

    
42
from synnefo.api.util import net_resources
43

    
44
NETWORK_TYPES = ['PUBLIC_ROUTED', 'PRIVATE_MAC_FILTERED',
45
                 'PRIVATE_PHYSICAL_VLAN', 'CUSTOM_ROUTED',
46
                 'CUSTOM_BRIDGED']
47

    
48

    
49
class Command(BaseCommand):
50
    can_import_settings = True
51
    output_transaction = True
52

    
53
    help = "Create a new network"
54

    
55
    option_list = BaseCommand.option_list + (
56
        make_option('--name',
57
            dest='name',
58
            help="Name of network"),
59
        make_option('--owner',
60
            dest='owner',
61
            help="The owner of the network"),
62
        make_option('--subnet',
63
            dest='subnet',
64
            default=None,
65
            # required=True,
66
            help='Subnet of the network'),
67
        make_option('--gateway',
68
            dest='gateway',
69
            default=None,
70
            help='Gateway of the network'),
71
        make_option('--dhcp',
72
            dest='dhcp',
73
            action='store_true',
74
            default=False,
75
            help='Automatically assign IPs'),
76
        make_option('--public',
77
            dest='public',
78
            action='store_true',
79
            default=False,
80
            help='Network is public'),
81
        make_option('--type',
82
            dest='type',
83
            default='PRIVATE_MAC_FILTERED',
84
            choices=NETWORK_TYPES,
85
            help='Type of network. Choices: ' + ', '.join(NETWORK_TYPES)),
86
        make_option('--subnet6',
87
            dest='subnet6',
88
            default=None,
89
            help='IPv6 subnet of the network'),
90
        make_option('--gateway6',
91
            dest='gateway6',
92
            default=None,
93
            help='IPv6 gateway of the network'),
94
        make_option('--backend-id',
95
            dest='backend_id',
96
            default=None,
97
            help='ID of the backend that the network will be created. Only for'
98
                 ' public networks'),
99
        make_option('--link',
100
            dest='link',
101
            default=None,
102
            help="Connectivity link of the Network. None for default."),
103
        make_option('--mac-prefix',
104
            dest='mac_prefix',
105
            default=None,
106
            help="MAC prefix of the network. None for default")
107
        )
108

    
109
    def handle(self, *args, **options):
110
        if args:
111
            raise CommandError("Command doesn't accept any arguments")
112

    
113
        name = options['name']
114
        subnet = options['subnet']
115
        net_type = options['type']
116
        backend_id = options['backend_id']
117
        public = options['public']
118
        link = options['link']
119
        mac_prefix = options['mac_prefix']
120

    
121
        if not name:
122
            raise CommandError("Name is required")
123
        if not subnet:
124
            raise CommandError("Subnet is required")
125
        if public and not backend_id:
126
            raise CommandError("backend-id is required")
127
        if backend_id and not public:
128
            raise CommandError("Private networks must be created to"
129
                               " all backends")
130

    
131
        if mac_prefix and net_type == "PRIVATE_MAC_FILTERED":
132
            raise CommandError("Can not override MAC_FILTERED mac-prefix")
133
        if link and net_type == "PRIVATE_PHYSICAL_VLAN":
134
            raise CommandError("Can not override PHYSICAL_VLAN link")
135

    
136
        if backend_id:
137
            backend = get_backend(backend_id)
138

    
139
        default_link, default_mac_prefix = net_resources(net_type)
140
        if not link:
141
            link = default_link
142
        if not mac_prefix:
143
            mac_prefix = default_mac_prefix
144

    
145
        subnet, gateway, subnet6, gateway6 = validate_network_info(options)
146

    
147
        if not link:
148
            raise CommandError("Can not create network. No connectivity link")
149

    
150
        network = Network.objects.create(
151
                name=name,
152
                userid=options['owner'],
153
                subnet=subnet,
154
                gateway=gateway,
155
                dhcp=options['dhcp'],
156
                type=net_type,
157
                public=public,
158
                link=link,
159
                mac_prefix=mac_prefix,
160
                gateway6=gateway6,
161
                subnet6=subnet6,
162
                state='PENDING')
163

    
164
        if public:
165
            # Create BackendNetwork only to the specified Backend
166
            network.create_backend_network(backend)
167
            create_network(network, backends=[backend])
168
        else:
169
            # Create BackendNetwork entries for all Backends
170
            network.create_backend_network()
171
            create_network(network)