Statistics
| Branch: | Tag: | Revision:

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

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

    
39
from synnefo.logic import subnets
40

    
41
HELP_MSG = """
42

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

    
48

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

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

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

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

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

    
100
        user_id = common.get_network(network_id).userid
101
        name = options["name"]
102
        allocation_pools = options["allocation_pools"]
103
        ipversion = options["ipversion"]
104
        if not ipversion:
105
            ipversion = 4
106
        else:
107
            try:
108
                ipversion = int(ipversion)
109
            except ValueError:
110
                raise CommandError("ip-version must be 4 or 6")
111

    
112
        gateway = options["gateway"]
113
        if not gateway:
114
            gateway = ""
115
        dhcp = options["dhcp"]
116
        dhcp = False if dhcp else True
117
        dns = options["dns"]
118
        host_routes = options["host_routes"]
119

    
120
        subnets.create_subnet(name=name,
121
                              network_id=network_id,
122
                              cidr=cidr,
123
                              allocation_pools=allocation_pools,
124
                              gateway=gateway,
125
                              ipversion=ipversion,
126
                              dhcp=dhcp,
127
                              slac=dhcp,
128
                              dns_nameservers=dns,
129
                              host_routes=host_routes,
130
                              user_id=user_id)