root / snf-cyclades-app / synnefo / api / management / commands / port-create.py @ 6d5c0344
History | View | Annotate | Download (4.7 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 |
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. The port will
|
46 |
get an IP address for each Subnet that is associated with the network."""
|
47 |
|
48 |
|
49 |
class Command(BaseCommand): |
50 |
help = HELP_MSG |
51 |
|
52 |
option_list = BaseCommand.option_list + ( |
53 |
make_option( |
54 |
"--name",
|
55 |
dest="name",
|
56 |
default=None,
|
57 |
help="Name of the port."),
|
58 |
make_option( |
59 |
"--network",
|
60 |
dest="network_id",
|
61 |
default=None,
|
62 |
help="The ID of the network where the port will be created."),
|
63 |
make_option( |
64 |
"--server",
|
65 |
dest="server_id",
|
66 |
default=None,
|
67 |
help="The ID of the server that the port will be connected to."),
|
68 |
make_option( |
69 |
"--router",
|
70 |
dest="router_id",
|
71 |
default=None,
|
72 |
help="The ID of the router that the port will be connected to."),
|
73 |
make_option( |
74 |
"--security-groups",
|
75 |
dest="security-groups",
|
76 |
default=None,
|
77 |
help="Comma separated list of Security Group IDs to associate"
|
78 |
" with the port."),
|
79 |
) |
80 |
|
81 |
@convert_api_faults
|
82 |
def handle(self, *args, **options): |
83 |
if args:
|
84 |
raise CommandError("Command doesn't accept any arguments") |
85 |
|
86 |
name = options["name"]
|
87 |
network_id = options["network_id"]
|
88 |
server_id = options["server_id"]
|
89 |
router_id = options["router_id"]
|
90 |
# assume giving security groups comma separated
|
91 |
security_group_ids = options["security-groups"]
|
92 |
|
93 |
if not name: |
94 |
name = ""
|
95 |
|
96 |
if (server_id and router_id) or not (server_id or router_id): |
97 |
raise CommandError("Please give either a server or a router id") |
98 |
|
99 |
if not network_id: |
100 |
raise CommandError("Please specify a 'network'") |
101 |
|
102 |
if server_id:
|
103 |
owner = "vm"
|
104 |
vm = get_vm(server_id) |
105 |
if vm.router:
|
106 |
raise CommandError("Server '%s' does not exist." % server_id) |
107 |
elif router_id:
|
108 |
owner = "router"
|
109 |
vm = get_vm(router_id) |
110 |
if not vm.router: |
111 |
raise CommandError("Router '%s' does not exist." % router_id) |
112 |
else:
|
113 |
raise CommandError("Neither server or router is specified") |
114 |
|
115 |
# get the network
|
116 |
network = get_network(network_id) |
117 |
|
118 |
# validate security groups
|
119 |
sg_list = [] |
120 |
if security_group_ids:
|
121 |
security_group_ids = security_group_ids.split(",")
|
122 |
for gid in security_group_ids: |
123 |
sg = util.get_security_group(int(gid))
|
124 |
sg_list.append(sg) |
125 |
|
126 |
new_port = ports.create(network, vm, name, security_groups=sg_list, |
127 |
device_owner=owner) |
128 |
self.stdout.write("Created port '%s' in DB.\n" % new_port) |
129 |
# TODO: Display port information, like ip address
|
130 |
# TODO: Add --wait argument to report progress about the Ganeti job.
|