Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / port-create.py @ 8b178e6b

History | View | Annotate | Download (5.9 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

    
38
from synnefo.api import util
39
from synnefo.management import common, pprint
40
from snf_django.management.utils import parse_bool
41
from synnefo.logic import ports
42

    
43
HELP_MSG = """Create a new port.
44

45
Connect a server/router to a network by creating a new port. If 'floating_ip'
46
option is used, the specified floating IP will be assigned to the new port.
47
Otherwise, the port will get an IP address for each Subnet that is associated
48
with the network."""
49

    
50

    
51
class Command(BaseCommand):
52
    help = HELP_MSG
53

    
54
    option_list = BaseCommand.option_list + (
55
        make_option(
56
            "--name",
57
            dest="name",
58
            default=None,
59
            help="Name of the port."),
60
        make_option(
61
            "--network",
62
            dest="network_id",
63
            default=None,
64
            help="The ID of the network where the port will be created."),
65
        make_option(
66
            "--server",
67
            dest="server_id",
68
            default=None,
69
            help="The ID of the server that the port will be connected to."),
70
        #make_option(
71
        #    "--router",
72
        #    dest="router_id",
73
        #    default=None,
74
        #    help="The ID of the router that the port will be connected to."),
75
        make_option(
76
            "--floating-ip",
77
            dest="floating_ip_id",
78
            default=None,
79
            help="The ID of the floating IP to use for the port."),
80
        make_option(
81
            "--security-groups",
82
            dest="security-groups",
83
            default=None,
84
            help="Comma separated list of Security Group IDs to associate"
85
                 " with the port."),
86
        make_option(
87
            "--wait",
88
            dest="wait",
89
            default="True",
90
            choices=["True", "False"],
91
            metavar="True|False",
92
            help="Wait for Ganeti jobs to complete."),
93
    )
94

    
95
    @common.convert_api_faults
96
    def handle(self, *args, **options):
97
        if args:
98
            raise CommandError("Command doesn't accept any arguments")
99

    
100
        name = options["name"]
101
        network_id = options["network_id"]
102
        server_id = options["server_id"]
103
        #router_id = options["router_id"]
104
        router_id = None
105
        floating_ip_id = options["floating_ip_id"]
106
        # assume giving security groups comma separated
107
        security_group_ids = options["security-groups"]
108
        wait = parse_bool(options["wait"])
109

    
110
        if not name:
111
            name = ""
112

    
113
        if (server_id and router_id) or not (server_id or router_id):
114
            raise CommandError("Please give either a server or a router id")
115

    
116
        if not network_id:
117
            raise CommandError("Please specify a 'network'")
118

    
119
        if server_id:
120
            owner = "vm"
121
            vm = common.get_vm(server_id)
122
            #if vm.router:
123
            #    raise CommandError("Server '%s' does not exist." % server_id)
124
        elif router_id:
125
            owner = "router"
126
            vm = common.get_vm(router_id)
127
            if not vm.router:
128
                raise CommandError("Router '%s' does not exist." % router_id)
129
        else:
130
            raise CommandError("Neither server or router is specified")
131

    
132
        floating_ip = None
133
        if floating_ip_id:
134
            floating_ip = common.get_floating_ip_by_id(floating_ip_id,
135
                                                       for_update=True)
136
            if floating_ip.userid != vm.userid:
137
                msg = "Floating IP %s does not belong to server/router owner."
138
                raise CommandError(msg % floating_ip)
139

    
140
        # get the network
141
        network = common.get_network(network_id)
142

    
143
        # validate security groups
144
        sg_list = []
145
        if security_group_ids:
146
            security_group_ids = security_group_ids.split(",")
147
            for gid in security_group_ids:
148
                sg = util.get_security_group(int(gid))
149
                sg_list.append(sg)
150

    
151
        new_port = ports.create(network, vm, name=name, ipaddress=floating_ip,
152
                                security_groups=sg_list,
153
                                device_owner=owner)
154
        self.stdout.write("Created port '%s' in DB:\n" % new_port)
155
        pprint.pprint_port(new_port, stdout=self.stdout)
156
        pprint.pprint_port_ips(new_port, stdout=self.stdout)
157
        self.stdout.write("\n")
158
        common.wait_server_task(new_port.machine, wait, stdout=self.stdout)