Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / management / commands / port-create.py @ b6426ead

History | View | Annotate | Download (6.8 kB)

1
# Copyright 2013-2014 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

    
38
from synnefo.api import util
39
from synnefo.management import common, pprint
40
from snf_django.management.utils import parse_bool
41
from snf_django.management.commands import SynnefoCommand
42
from synnefo.logic import servers
43

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

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

    
51

    
52
class Command(SynnefoCommand):
53
    help = HELP_MSG
54

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

    
106
    @common.convert_api_faults
107
    def handle(self, *args, **options):
108
        if args:
109
            raise CommandError("Command doesn't accept any arguments")
110

    
111
        name = options["name"]
112
        user_id = options["user_id"]
113
        network_id = options["network_id"]
114
        server_id = options["server_id"]
115
        #router_id = options["router_id"]
116
        router_id = None
117
        # assume giving security groups comma separated
118
        security_group_ids = options["security-groups"]
119
        wait = parse_bool(options["wait"])
120

    
121
        if not name:
122
            name = ""
123

    
124
        if not network_id:
125
            raise CommandError("Please specify a 'network'")
126

    
127
        vm = None
128
        owner = None
129
        if server_id:
130
            owner = "vm"
131
            vm = common.get_resource("server", server_id, for_update=True)
132
            #if vm.router:
133
            #    raise CommandError("Server '%s' does not exist." % server_id)
134
        elif router_id:
135
            owner = "router"
136
            vm = common.get_resource("server", router_id, for_update=True)
137
            if not vm.router:
138
                raise CommandError("Router '%s' does not exist." % router_id)
139

    
140
        if user_id is None:
141
            if vm is not None:
142
                user_id = vm.userid
143
            else:
144
                raise CommandError("Please specify the owner of the port.")
145

    
146
        # get the network
147
        network = common.get_resource("network", network_id)
148

    
149
        # Get either floating IP or fixed ip address
150
        ipaddress = None
151
        floating_ip_id = options["floating_ip_id"]
152
        ipv4_address = options["ipv4_address"]
153
        if floating_ip_id:
154
            ipaddress = common.get_resource("floating-ip", floating_ip_id,
155
                                            for_update=True)
156
            if ipv4_address is not None and ipaddress.address != ipv4_address:
157
                raise CommandError("Floating IP address '%s' is different from"
158
                                   " specified address '%s'" %
159
                                   (ipaddress.address, ipv4_address))
160

    
161
        # validate security groups
162
        sg_list = []
163
        if security_group_ids:
164
            security_group_ids = security_group_ids.split(",")
165
            for gid in security_group_ids:
166
                sg = util.get_security_group(int(gid))
167
                sg_list.append(sg)
168

    
169
        new_port = servers.create_port(user_id, network, machine=vm,
170
                                       name=name,
171
                                       use_ipaddress=ipaddress,
172
                                       address=ipv4_address,
173
                                       security_groups=sg_list,
174
                                       device_owner=owner)
175
        self.stdout.write("Created port '%s' in DB:\n" % new_port)
176
        pprint.pprint_port(new_port, stdout=self.stdout)
177
        pprint.pprint_port_ips(new_port, stdout=self.stdout)
178
        self.stdout.write("\n")
179
        if vm is not None:
180
            common.wait_server_task(new_port.machine, wait, stdout=self.stdout)