Revision 6dd70a5c

b/snf-cyclades-app/synnefo/api/management/commands/listnetworks.py
99 99
        for network in networks:
100 100
            fields = [str(network.id),
101 101
                      network.name,
102
                      network.type,
102 103
                      network.userid or '',
103 104
                      network.mac_prefix or '',
104 105
                      str(network.dhcp),
b/snf-cyclades-app/synnefo/api/management/commands/network-create.py
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

  
38
from synnefo.db.models import Network
39
from synnefo.api.util import network_specs_from_type
40
from synnefo.logic.backend import create_network
41

  
42
import ipaddr
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
    help = "Create a new network"
51

  
52
    option_list = BaseCommand.option_list + (
53
        make_option('--name',
54
            dest='name',
55
            help="Name of network"),
56
        make_option('--owner',
57
            dest='owner',
58
            help="The owner of the network"),
59
        make_option('--subnet',
60
            dest='subnet',
61
            default=None,
62
            # required=True,
63
            help='Subnet of the network'),
64
        make_option('--gateway',
65
            dest='gateway',
66
            default=None,
67
            help='Gateway of the network'),
68
        make_option('--dhcp',
69
            dest='dhcp',
70
            action='store_true',
71
            default=False,
72
            help='Automatically assign IPs'),
73
        make_option('--public',
74
            dest='public',
75
            action='store_true',
76
            default=False,
77
            help='Network is public'),
78
        make_option('--type',
79
            dest='type',
80
            default='PRIVATE_MAC_FILTERED',
81
            choices=NETWORK_TYPES,
82
            help='Type of network. Choices: ' + ', '.join(NETWORK_TYPES)),
83
        make_option('--subnet6',
84
            dest='subnet6',
85
            default=None,
86
            help='IPv6 subnet of the network'),
87
        make_option('--gateway6',
88
            dest='gateway6',
89
            default=None,
90
            help='IPv6 gateway of the network'),
91
        )
92

  
93
    def handle(self, *args, **options):
94
        if args:
95
            raise CommandError("Command doesn't accept any arguments")
96

  
97
        name = options['name']
98
        subnet = options['subnet']
99

  
100
        if not name:
101
            raise CommandError("Name is required")
102
        if not subnet:
103
            raise CommandError("Subnet is required")
104

  
105
        link, mac_prefix = network_specs_from_type(options['type'])
106

  
107
        subnet, gateway, subnet6, gateway6 = validate_network_info(options)
108

  
109
        if not link:
110
            raise CommandError("Can not create network. No connectivity link")
111

  
112
        network = Network.objects.create(
113
                name=name,
114
                userid=options['owner'],
115
                subnet=subnet,
116
                gateway=gateway,
117
                dhcp=options['dhcp'],
118
                type=options['type'],
119
                public=options['public'],
120
                link=link,
121
                mac_prefix=mac_prefix,
122
                gateway6=gateway6,
123
                subnet6=subnet6,
124
                state='PENDING')
125

  
126
        create_network(network)
127

  
128

  
129
def validate_network_info(options):
130
    subnet = options['subnet']
131
    gateway = options['gateway']
132
    subnet6 = options['subnet6']
133
    gateway6 = options['gateway6']
134

  
135
    try:
136
        ipaddr.IPv4Network(subnet)
137
    except ValueError:
138
        raise CommandError('Malformed subnet')
139
    try:
140
        gateway and ipaddr.IPv4Address(gateway) or None
141
    except ValueError:
142
        raise CommandError('Malformed gateway')
143

  
144
    try:
145
        subnet6 and ipaddr.IPv6Network(subnet6) or None
146
    except ValueError:
147
        raise CommandError('Malformed subnet6')
148

  
149
    try:
150
        gateway6 and ipaddr.IPv6Address(gateway6) or None
151
    except ValueError:
152
        raise CommandError('Malformed gateway6')
153

  
154
    return subnet, gateway, subnet6, gateway6
b/snf-cyclades-app/synnefo/api/networks.py
40 40
from django.template.loader import render_to_string
41 41
from django.utils import simplejson as json
42 42

  
43
from synnefo import settings
44 43
from synnefo.api import util
45 44
from synnefo.api.actions import network_actions
46 45
from synnefo.api.common import method_not_allowed
47
from synnefo.api.faults import (BadRequest, OverLimit,
48
                                Unauthorized, NetworkInUse)
49
from synnefo.db.models import Network, Pool, BridgePool, MacPrefixPool
46
from synnefo.api.faults import (BadRequest, Unauthorized,
47
                                NetworkInUse)
48
from synnefo.db.models import Network
50 49
from synnefo.logic import backend
51 50

  
52 51

  
......
169 168
    if type == 'PUBLIC_ROUTED':
170 169
        raise Unauthorized('Can not create a public network.')
171 170

  
172
    mac_prefix = None
173
    try:
174
        if type == 'PRIVATE_MAC_FILTERED':
175
            link = settings.PRIVATE_MAC_FILTERED_BRIDGE
176
            mac_prefix = MacPrefixPool.get_available().value
177
        elif type == 'PRIVATE_PHYSICAL_VLAN':
178
            link = BridgePool.get_available().value
179
        elif type == 'CUSTOM_ROUTED':
180
            link = settings.CUSTOM_ROUTED_ROUTING_TABLE
181
        elif type == 'CUSTOM_BRIDGED':
182
            link = settings.CUSTOM_BRIDGED_BRIDGE
183
        else:
184
            raise BadRequest('Unknown network type')
185
    except Pool.PoolExhausted:
186
        raise OverLimit('Network count limit exceeded.')
171

  
172
    link, mac_prefix = util.network_specs_from_type(type)
173
    if not link:
174
        raise Exception("Can not create network. No connectivity link.")
187 175

  
188 176
    network = Network.objects.create(
189 177
            name=name,
b/snf-cyclades-app/synnefo/api/util.py
55 55
from django.utils.cache import add_never_cache_headers
56 56

  
57 57
from synnefo.api.faults import (Fault, BadRequest, BuildInProgress,
58
                                ItemNotFound, ServiceUnavailable, Unauthorized, BadMediaType)
58
                                ItemNotFound, ServiceUnavailable, Unauthorized,
59
                                BadMediaType, OverLimit)
59 60
from synnefo.db.models import (Flavor, VirtualMachine, VirtualMachineMetadata,
60
                               Network, NetworkInterface)
61
                               Network, NetworkInterface, BridgePool,
62
                               MacPrefixPool, Pool)
63

  
61 64
from synnefo.lib.astakos import get_user
62 65
from synnefo.plankton.backend import ImageBackend
63 66

  
......
354 357

  
355 358
def construct_nic_id(nic):
356 359
    return "-".join(["nic", unicode(nic.machine.id), unicode(nic.index)])
360

  
361

  
362
def network_specs_from_type(network_type):
363
    mac_prefix = None
364
    try:
365
        if network_type == 'PRIVATE_MAC_FILTERED':
366
            link = settings.PRIVATE_MAC_FILTERED_BRIDGE
367
            mac_prefix = MacPrefixPool.get_available().value
368
        elif network_type == 'PRIVATE_PHYSICAL_VLAN':
369
            link = BridgePool.get_available().value
370
        elif network_type == 'CUSTOM_ROUTED':
371
            link = settings.CUSTOM_ROUTED_ROUTING_TABLE
372
        elif network_type == 'CUSTOM_BRIDGED':
373
            link = settings.CUSTOM_BRIDGED_BRIDGE
374
        else:
375
            raise BadRequest('Unknown network network_type')
376
    except Pool.PoolExhausted:
377
        raise OverLimit('Network count limit exceeded.')
378

  
379
    return link, mac_prefix
/dev/null
1
[
2
    {
3
        "model": "db.Network",
4
        "pk": 1,
5
        "fields": {
6
            "name": "public",
7
            "created": "2011-04-01",
8
            "updated": "2011-04-01",
9
            "state": "ACTIVE",
10
            "public": 1,
11
            "link": "snf_public",
12
            "dhcp": 1,
13
            "type": "PUBLIC_ROUTED"
14
        }
15
    }
16
]

Also available in: Unified diff