Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.4 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 BaseCommand, CommandError
37
from synnefo.management import common
38
from snf_django.management.utils import parse_bool
39
from synnefo.management import pprint
40

    
41
from synnefo.logic import subnets
42

    
43
HELP_MSG = """
44

45
Create a new subnet without authenticating the user. The limit of one
46
IPv4/IPv6 subnet per network still applies. Mandatory fields are CIDR and the
47
Network ID.
48
"""
49

    
50

    
51
class Command(BaseCommand):
52
    help = "Create a new Subnet." + HELP_MSG
53

    
54
    option_list = BaseCommand.option_list + (
55
        make_option("--network-id", dest="network_id",
56
                    help="Specify the Network to attach the subnet. To get the"
57
                         " networks of a user, use snf-manage network-list"),
58
        make_option("--cidr", dest="cidr",
59
                    help="The CIDR of the subnet, e.g., 192.168.42.0/24"),
60
        make_option("--allocation-pools", dest="allocation_pools",
61
                    help="IP allocation pools to be used for assigning IPs to"
62
                    " VMs. Syntax: \n"
63
                    "'[[192.168.42.100,192.168.42.200],"
64
                    "[192.168.42.220,129.168.42.240]]'"),
65
        make_option("--name", dest="name",
66
                    help="An arbitrary string for naming the subnet."),
67
        make_option("--ip-version", dest="ipversion", choices=["4", "6"],
68
                    metavar="4|6",
69
                    help="IP version of the CIDR. The value must be in sync"
70
                    " with the CIDR. Default value: 4"),
71
        make_option("--gateway", dest="gateway",
72
                    help="An IP to use as a gateway for the subnet."
73
                    " The IP must be inside the CIDR range and cannot be the"
74
                    " subnet or broadcast IP. If no value is specified, a"
75
                    " gateway will not be set."),
76
        make_option("--dhcp", dest="dhcp", default="True",
77
                    choices=["True", "False"], metavar="True|False",
78
                    help="Value for DHCP/SLAAC. True by default."),
79
        make_option("--dns", dest="dns",
80
                    help="DNS nameservers to be used by the VMs in the subnet."
81
                    " For the time being, this option isn't supported."),
82
        make_option("--host-routes", dest="host_routes",
83
                    help="Host routes to be used for advanced routing"
84
                    "settings. For the time being, this option isn't"
85
                    " supported.")
86
    )
87

    
88
    @common.convert_api_faults
89
    def handle(self, *args, **options):
90
        if args:
91
            raise CommandError("Command doesn't accept any arguments")
92

    
93
        network_id = options["network_id"]
94
        cidr = options["cidr"]
95

    
96
        if not network_id:
97
            raise CommandError("network-id is mandatory")
98
        if not cidr:
99
            raise CommandError("cidr is mandatory")
100

    
101
        user_id = common.get_network(network_id).userid
102
        name = options["name"] or ""
103
        allocation_pools = options["allocation_pools"]
104
        ipversion = options["ipversion"] or 4
105
        ipversion = int(ipversion)
106
        gateway = options["gateway"]
107
        dhcp = parse_bool(options["dhcp"])
108
        dns = options["dns"]
109
        host_routes = options["host_routes"]
110

    
111
        sub = subnets.create_subnet(name=name,
112
                                    network_id=network_id,
113
                                    cidr=cidr,
114
                                    allocation_pools=allocation_pools,
115
                                    gateway=gateway,
116
                                    ipversion=ipversion,
117
                                    dhcp=dhcp,
118
                                    slaac=dhcp,
119
                                    dns_nameservers=dns,
120
                                    host_routes=host_routes,
121
                                    user_id=user_id)
122

    
123
        pprint.pprint_subnet_in_db(sub, stdout=self.stdout)
124
        self.stdout.write("\n\n")
125
        pprint.pprint_ippool(sub, stdout=self.stdout)