Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / management / commands / subnet-create.py @ a6e6fe48

History | View | Annotate | Download (5.9 kB)

1 a6e6fe48 Christos Stavrakakis
# Copyright 2013-2014 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 cfc33ff7 Dionysis Grigoropoulos
HELP_MSG = """
43 cfc33ff7 Dionysis Grigoropoulos

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