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