root / snf-cyclades-app / synnefo / api / management / commands / port-create.py @ 0e4ec1f5
History | View | Annotate | Download (4.2 kB)
1 |
# Copyright 2012 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 get_backend, convert_api_faults |
38 |
from snf_django.management.utils import parse_bool |
39 |
|
40 |
from synnefo.db.models import Network, NetworkInterface, SecurityGroup |
41 |
from synnefo.api import util |
42 |
from synnefo.management.common import get_network, get_vm |
43 |
from synnefo.logic import ports |
44 |
|
45 |
|
46 |
class Command(BaseCommand): |
47 |
can_import_settings = True
|
48 |
output_transaction = True
|
49 |
|
50 |
help = "Create a new port"
|
51 |
|
52 |
option_list = BaseCommand.option_list + ( |
53 |
make_option( |
54 |
'--name',
|
55 |
dest='name',
|
56 |
default=None,
|
57 |
help="Name of port"),
|
58 |
make_option( |
59 |
'--network',
|
60 |
dest='network',
|
61 |
default=None,
|
62 |
help='The network to attach the port to'),
|
63 |
make_option( |
64 |
'--server',
|
65 |
dest='server_id',
|
66 |
default=None,
|
67 |
help='The server id the port will be connected to'),
|
68 |
make_option( |
69 |
'--router',
|
70 |
dest='router_id',
|
71 |
default=None,
|
72 |
help='The router id the port will be connected to'),
|
73 |
make_option( |
74 |
'--security-groups',
|
75 |
dest='security-groups',
|
76 |
default=None,
|
77 |
help='Security Groups associated with the port'),
|
78 |
) |
79 |
|
80 |
@convert_api_faults
|
81 |
def handle(self, *args, **options): |
82 |
if args:
|
83 |
raise CommandError("Command doesn't accept any arguments") |
84 |
|
85 |
name = options['name']
|
86 |
network = options['network']
|
87 |
server = options['server_id']
|
88 |
router = options['router_id']
|
89 |
#assume giving security groups comma separated
|
90 |
security_groups = options['security-groups']
|
91 |
|
92 |
if not name: |
93 |
name=""
|
94 |
|
95 |
if (server and router) or not (server or router): |
96 |
raise CommandError('Please give either a server or a router id') |
97 |
|
98 |
if not network: |
99 |
raise CommandError("network is required") |
100 |
|
101 |
if server:
|
102 |
device = server |
103 |
owner = 'vm'
|
104 |
else:
|
105 |
device = router |
106 |
owner = 'router'
|
107 |
|
108 |
#get the network
|
109 |
network = get_network(network) |
110 |
|
111 |
#get the vm
|
112 |
vm = get_vm(device) |
113 |
|
114 |
#validate security groups
|
115 |
sg_list = [] |
116 |
if security_groups:
|
117 |
security_groups = security_groups.split(",")
|
118 |
for gid in security_groups: |
119 |
sg = util.get_security_group(int(gid))
|
120 |
sg_list.append(sg) |
121 |
|
122 |
new_port = ports.create(network, vm, name, security_groups=sg_list, |
123 |
device_owner=owner) |
124 |
self.stdout.write("Created port '%s' in DB.\n" % new_port) |