root / snf-cyclades-app / synnefo / api / management / commands / port-create.py @ 014df29c
History | View | Annotate | Download (5.6 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.common import convert_api_faults |
38 |
|
39 |
from synnefo.api import util |
40 |
from synnefo.management.common import (get_network, get_vm, |
41 |
get_floating_ip_by_id) |
42 |
from synnefo.logic import ports |
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(BaseCommand): |
53 |
help = HELP_MSG |
54 |
|
55 |
option_list = BaseCommand.option_list + ( |
56 |
make_option( |
57 |
"--name",
|
58 |
dest="name",
|
59 |
default=None,
|
60 |
help="Name of the port."),
|
61 |
make_option( |
62 |
"--network",
|
63 |
dest="network_id",
|
64 |
default=None,
|
65 |
help="The ID of the network where the port will be created."),
|
66 |
make_option( |
67 |
"--server",
|
68 |
dest="server_id",
|
69 |
default=None,
|
70 |
help="The ID of the server that the port will be connected to."),
|
71 |
#make_option(
|
72 |
# "--router",
|
73 |
# dest="router_id",
|
74 |
# default=None,
|
75 |
# help="The ID of the router that the port will be connected to."),
|
76 |
make_option( |
77 |
"--floating-ip",
|
78 |
dest="floating_ip_id",
|
79 |
default=None,
|
80 |
help="The ID of the floating IP to use for the port."),
|
81 |
make_option( |
82 |
"--security-groups",
|
83 |
dest="security-groups",
|
84 |
default=None,
|
85 |
help="Comma separated list of Security Group IDs to associate"
|
86 |
" with the port."),
|
87 |
) |
88 |
|
89 |
@convert_api_faults
|
90 |
def handle(self, *args, **options): |
91 |
if args:
|
92 |
raise CommandError("Command doesn't accept any arguments") |
93 |
|
94 |
name = options["name"]
|
95 |
network_id = options["network_id"]
|
96 |
server_id = options["server_id"]
|
97 |
#router_id = options["router_id"]
|
98 |
router_id = None
|
99 |
floating_ip_id = options["floating_ip_id"]
|
100 |
# assume giving security groups comma separated
|
101 |
security_group_ids = options["security-groups"]
|
102 |
|
103 |
if not name: |
104 |
name = ""
|
105 |
|
106 |
if (server_id and router_id) or not (server_id or router_id): |
107 |
raise CommandError("Please give either a server or a router id") |
108 |
|
109 |
if not network_id: |
110 |
raise CommandError("Please specify a 'network'") |
111 |
|
112 |
if server_id:
|
113 |
owner = "vm"
|
114 |
vm = get_vm(server_id) |
115 |
#if vm.router:
|
116 |
# raise CommandError("Server '%s' does not exist." % server_id)
|
117 |
elif router_id:
|
118 |
owner = "router"
|
119 |
vm = get_vm(router_id) |
120 |
if not vm.router: |
121 |
raise CommandError("Router '%s' does not exist." % router_id) |
122 |
else:
|
123 |
raise CommandError("Neither server or router is specified") |
124 |
|
125 |
floating_ip = None
|
126 |
if floating_ip_id:
|
127 |
floating_ip = get_floating_ip_by_id(floating_ip_id, |
128 |
for_update=True)
|
129 |
if floating_ip.userid != vm.userid:
|
130 |
msg = "Floating IP %s does not belong to server/router owner."
|
131 |
raise CommandError(msg % floating_ip)
|
132 |
|
133 |
# get the network
|
134 |
network = get_network(network_id) |
135 |
|
136 |
# validate security groups
|
137 |
sg_list = [] |
138 |
if security_group_ids:
|
139 |
security_group_ids = security_group_ids.split(",")
|
140 |
for gid in security_group_ids: |
141 |
sg = util.get_security_group(int(gid))
|
142 |
sg_list.append(sg) |
143 |
|
144 |
new_port = ports.create(network, vm, name=name, ipaddress=floating_ip, |
145 |
security_groups=sg_list, |
146 |
device_owner=owner) |
147 |
self.stdout.write("Created port '%s' in DB.\n" % new_port) |
148 |
# TODO: Display port information, like ip address
|
149 |
# TODO: Add --wait argument to report progress about the Ganeti job.
|