Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / subnet-create.py @ 9835a70d

History | View | Annotate | Download (5.9 kB)

1 cfc33ff7 Dionysis Grigoropoulos
# Copyright 2013 GRNET S.A. All rights reserved.
2 cfc33ff7 Dionysis Grigoropoulos
#
3 cfc33ff7 Dionysis Grigoropoulos
# Redistribution and use in source and binary forms, with or
4 cfc33ff7 Dionysis Grigoropoulos
# without modification, are permitted provided that the following
5 cfc33ff7 Dionysis Grigoropoulos
# conditions are met:
6 cfc33ff7 Dionysis Grigoropoulos
#
7 cfc33ff7 Dionysis Grigoropoulos
#   1. Redistributions of source code must retain the above
8 cfc33ff7 Dionysis Grigoropoulos
#      copyright notice, this list of conditions and the following
9 cfc33ff7 Dionysis Grigoropoulos
#      disclaimer.
10 cfc33ff7 Dionysis Grigoropoulos
#
11 cfc33ff7 Dionysis Grigoropoulos
#   2. Redistributions in binary form must reproduce the above
12 cfc33ff7 Dionysis Grigoropoulos
#      copyright notice, this list of conditions and the following
13 cfc33ff7 Dionysis Grigoropoulos
#      disclaimer in the documentation and/or other materials
14 cfc33ff7 Dionysis Grigoropoulos
#      provided with the distribution.
15 cfc33ff7 Dionysis Grigoropoulos
#
16 cfc33ff7 Dionysis Grigoropoulos
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 cfc33ff7 Dionysis Grigoropoulos
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 cfc33ff7 Dionysis Grigoropoulos
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 cfc33ff7 Dionysis Grigoropoulos
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 cfc33ff7 Dionysis Grigoropoulos
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 cfc33ff7 Dionysis Grigoropoulos
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 cfc33ff7 Dionysis Grigoropoulos
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 cfc33ff7 Dionysis Grigoropoulos
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 cfc33ff7 Dionysis Grigoropoulos
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 cfc33ff7 Dionysis Grigoropoulos
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 cfc33ff7 Dionysis Grigoropoulos
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 cfc33ff7 Dionysis Grigoropoulos
# POSSIBILITY OF SUCH DAMAGE.
28 cfc33ff7 Dionysis Grigoropoulos
#
29 cfc33ff7 Dionysis Grigoropoulos
# The views and conclusions contained in the software and
30 cfc33ff7 Dionysis Grigoropoulos
# documentation are those of the authors and should not be
31 cfc33ff7 Dionysis Grigoropoulos
# interpreted as representing official policies, either expressed
32 cfc33ff7 Dionysis Grigoropoulos
# or implied, of GRNET S.A.
33 cfc33ff7 Dionysis Grigoropoulos
34 cfc33ff7 Dionysis Grigoropoulos
from optparse import make_option
35 cfc33ff7 Dionysis Grigoropoulos
36 cfc33ff7 Dionysis Grigoropoulos
from django.core.management.base import BaseCommand, CommandError
37 cfc33ff7 Dionysis Grigoropoulos
from synnefo.management import common
38 b7311f3d Dionysis Grigoropoulos
from snf_django.management.utils import parse_bool
39 c52f91ad Dionysis Grigoropoulos
from synnefo.management import pprint
40 cfc33ff7 Dionysis Grigoropoulos
from synnefo.logic import subnets
41 cfc33ff7 Dionysis Grigoropoulos
42 55480205 Dionysis Grigoropoulos
import ipaddr
43 55480205 Dionysis Grigoropoulos
44 cfc33ff7 Dionysis Grigoropoulos
HELP_MSG = """
45 cfc33ff7 Dionysis Grigoropoulos

46 cfc33ff7 Dionysis Grigoropoulos
Create a new subnet without authenticating the user. The limit of one
47 cfc33ff7 Dionysis Grigoropoulos
IPv4/IPv6 subnet per network still applies. Mandatory fields are CIDR and the
48 cfc33ff7 Dionysis Grigoropoulos
Network ID.
49 cfc33ff7 Dionysis Grigoropoulos
"""
50 cfc33ff7 Dionysis Grigoropoulos
51 cfc33ff7 Dionysis Grigoropoulos
52 cfc33ff7 Dionysis Grigoropoulos
class Command(BaseCommand):
53 cfc33ff7 Dionysis Grigoropoulos
    help = "Create a new Subnet." + HELP_MSG
54 cfc33ff7 Dionysis Grigoropoulos
55 cfc33ff7 Dionysis Grigoropoulos
    option_list = BaseCommand.option_list + (
56 cfc33ff7 Dionysis Grigoropoulos
        make_option("--network-id", dest="network_id",
57 cfc33ff7 Dionysis Grigoropoulos
                    help="Specify the Network to attach the subnet. To get the"
58 b7311f3d Dionysis Grigoropoulos
                         " networks of a user, use snf-manage network-list"),
59 cfc33ff7 Dionysis Grigoropoulos
        make_option("--cidr", dest="cidr",
60 cfc33ff7 Dionysis Grigoropoulos
                    help="The CIDR of the subnet, e.g., 192.168.42.0/24"),
61 cfc33ff7 Dionysis Grigoropoulos
        make_option("--allocation-pools", dest="allocation_pools",
62 55480205 Dionysis Grigoropoulos
                    action="append",
63 cfc33ff7 Dionysis Grigoropoulos
                    help="IP allocation pools to be used for assigning IPs to"
64 55480205 Dionysis Grigoropoulos
                    " VMs. Can be used multiple times. Syntax: \n"
65 55480205 Dionysis Grigoropoulos
                    "192.168.42.220,192.168.42.240. Starting IP must proceed "
66 44bd008a Dionysis Grigoropoulos
                    "ending IP.20,192.168.42.240. Starting IP must proceed "
67 44bd008a Dionysis Grigoropoulos
                    "ending IP. If no allocation pools are given, the whole "
68 44bd008a Dionysis Grigoropoulos
                    "subnet range is used, excluding the gateway IP, the "
69 44bd008a Dionysis Grigoropoulos
                    "broadcast address and the network address"),
70 cfc33ff7 Dionysis Grigoropoulos
        make_option("--name", dest="name",
71 cfc33ff7 Dionysis Grigoropoulos
                    help="An arbitrary string for naming the subnet."),
72 b7311f3d Dionysis Grigoropoulos
        make_option("--ip-version", dest="ipversion", choices=["4", "6"],
73 b7311f3d Dionysis Grigoropoulos
                    metavar="4|6",
74 b7311f3d Dionysis Grigoropoulos
                    help="IP version of the CIDR. The value must be in sync"
75 b7311f3d Dionysis Grigoropoulos
                    " with the CIDR. Default value: 4"),
76 cfc33ff7 Dionysis Grigoropoulos
        make_option("--gateway", dest="gateway",
77 cfc33ff7 Dionysis Grigoropoulos
                    help="An IP to use as a gateway for the subnet."
78 cfc33ff7 Dionysis Grigoropoulos
                    " The IP must be inside the CIDR range and cannot be the"
79 b7311f3d Dionysis Grigoropoulos
                    " subnet or broadcast IP. If no value is specified, a"
80 b7311f3d Dionysis Grigoropoulos
                    " gateway will not be set."),
81 b7311f3d Dionysis Grigoropoulos
        make_option("--dhcp", dest="dhcp", default="True",
82 b7311f3d Dionysis Grigoropoulos
                    choices=["True", "False"], metavar="True|False",
83 b7311f3d Dionysis Grigoropoulos
                    help="Value for DHCP/SLAAC. True by default."),
84 cfc33ff7 Dionysis Grigoropoulos
        make_option("--dns", dest="dns",
85 cfc33ff7 Dionysis Grigoropoulos
                    help="DNS nameservers to be used by the VMs in the subnet."
86 cfc33ff7 Dionysis Grigoropoulos
                    " For the time being, this option isn't supported."),
87 cfc33ff7 Dionysis Grigoropoulos
        make_option("--host-routes", dest="host_routes",
88 cfc33ff7 Dionysis Grigoropoulos
                    help="Host routes to be used for advanced routing"
89 cfc33ff7 Dionysis Grigoropoulos
                    "settings. For the time being, this option isn't"
90 cfc33ff7 Dionysis Grigoropoulos
                    " supported.")
91 cfc33ff7 Dionysis Grigoropoulos
    )
92 cfc33ff7 Dionysis Grigoropoulos
93 cfc33ff7 Dionysis Grigoropoulos
    @common.convert_api_faults
94 cfc33ff7 Dionysis Grigoropoulos
    def handle(self, *args, **options):
95 cfc33ff7 Dionysis Grigoropoulos
        if args:
96 cfc33ff7 Dionysis Grigoropoulos
            raise CommandError("Command doesn't accept any arguments")
97 cfc33ff7 Dionysis Grigoropoulos
98 cfc33ff7 Dionysis Grigoropoulos
        network_id = options["network_id"]
99 cfc33ff7 Dionysis Grigoropoulos
        cidr = options["cidr"]
100 cfc33ff7 Dionysis Grigoropoulos
101 cfc33ff7 Dionysis Grigoropoulos
        if not network_id:
102 8646e606 Dionysis Grigoropoulos
            raise CommandError("network-id is mandatory")
103 cfc33ff7 Dionysis Grigoropoulos
        if not cidr:
104 cfc33ff7 Dionysis Grigoropoulos
            raise CommandError("cidr is mandatory")
105 cfc33ff7 Dionysis Grigoropoulos
106 c068e75d Dionysis Grigoropoulos
        user_id = common.get_network(network_id).userid
107 b7311f3d Dionysis Grigoropoulos
        name = options["name"] or ""
108 c068e75d Dionysis Grigoropoulos
        allocation_pools = options["allocation_pools"]
109 b7311f3d Dionysis Grigoropoulos
        ipversion = options["ipversion"] or 4
110 b7311f3d Dionysis Grigoropoulos
        ipversion = int(ipversion)
111 c068e75d Dionysis Grigoropoulos
        gateway = options["gateway"]
112 b7311f3d Dionysis Grigoropoulos
        dhcp = parse_bool(options["dhcp"])
113 c068e75d Dionysis Grigoropoulos
        dns = options["dns"]
114 c068e75d Dionysis Grigoropoulos
        host_routes = options["host_routes"]
115 cfc33ff7 Dionysis Grigoropoulos
116 44bd008a Dionysis Grigoropoulos
        alloc = None
117 44bd008a Dionysis Grigoropoulos
        if allocation_pools is not None:
118 44bd008a Dionysis Grigoropoulos
            alloc = subnets.parse_allocation_pools(allocation_pools)
119 44bd008a Dionysis Grigoropoulos
            alloc.sort()
120 55480205 Dionysis Grigoropoulos
121 c52f91ad Dionysis Grigoropoulos
        sub = subnets.create_subnet(name=name,
122 c52f91ad Dionysis Grigoropoulos
                                    network_id=network_id,
123 c52f91ad Dionysis Grigoropoulos
                                    cidr=cidr,
124 44bd008a Dionysis Grigoropoulos
                                    allocation_pools=alloc,
125 c52f91ad Dionysis Grigoropoulos
                                    gateway=gateway,
126 c52f91ad Dionysis Grigoropoulos
                                    ipversion=ipversion,
127 c52f91ad Dionysis Grigoropoulos
                                    dhcp=dhcp,
128 c52f91ad Dionysis Grigoropoulos
                                    slaac=dhcp,
129 c52f91ad Dionysis Grigoropoulos
                                    dns_nameservers=dns,
130 c52f91ad Dionysis Grigoropoulos
                                    host_routes=host_routes,
131 c52f91ad Dionysis Grigoropoulos
                                    user_id=user_id)
132 c52f91ad Dionysis Grigoropoulos
133 c52f91ad Dionysis Grigoropoulos
        pprint.pprint_subnet_in_db(sub, stdout=self.stdout)
134 c52f91ad Dionysis Grigoropoulos
        self.stdout.write("\n\n")
135 c52f91ad Dionysis Grigoropoulos
        pprint.pprint_ippool(sub, stdout=self.stdout)