Statistics
| Branch: | Tag: | Revision:

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

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

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

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

    
120
        if not name:
121
            name = ""
122

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

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

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

    
145
        # get the network
146
        network = common.get_network(network_id)
147

    
148
        # Get either floating IP or fixed ip address
149
        ipaddress = None
150
        floating_ip_id = options["floating_ip_id"]
151
        ipv4_address = options["ipv4_address"]
152
        if ipv4_address is not None and floating_ip_id is not None:
153
            raise CommandError("Please use either --floating-ip-id or"
154
                               " --ipv4-address option")
155
        elif floating_ip_id:
156
            ipaddress = common.get_floating_ip_by_id(floating_ip_id,
157
                                                     for_update=True)
158

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

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