Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / port-create.py @ 9835a70d

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
        if user_id is None:
127
            raise CommandError("Please specify the owner of the port")
128

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

    
142
        # get the network
143
        network = common.get_network(network_id)
144

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

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

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