Revision a1713485
b/snf-cyclades-app/synnefo/api/management/commands/port-create.py | ||
---|---|---|
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.logic import ports |
|
43 |
|
|
44 |
|
|
45 |
class Command(BaseCommand): |
|
46 |
can_import_settings = True |
|
47 |
output_transaction = True |
|
48 |
|
|
49 |
help = "Create a new port" |
|
50 |
|
|
51 |
option_list = BaseCommand.option_list + ( |
|
52 |
make_option( |
|
53 |
'--name', |
|
54 |
dest='name', |
|
55 |
default=None, |
|
56 |
help="Name of port"), |
|
57 |
make_option( |
|
58 |
'--owner', |
|
59 |
dest='owner', |
|
60 |
default=None, |
|
61 |
help="The owner of the port"), |
|
62 |
make_option( |
|
63 |
'--network', |
|
64 |
dest='network', |
|
65 |
default=None, |
|
66 |
help='The network to attach the port to'), |
|
67 |
make_option( |
|
68 |
'--device', |
|
69 |
dest='device_id', |
|
70 |
default=None, |
|
71 |
help='The VM id the port will be connected to'), |
|
72 |
make_option( |
|
73 |
'--security-groups', |
|
74 |
dest='security-groups', |
|
75 |
default=None, |
|
76 |
help='Security Groups associated with the port'), |
|
77 |
) |
|
78 |
|
|
79 |
@convert_api_faults |
|
80 |
def handle(self, *args, **options): |
|
81 |
if args: |
|
82 |
raise CommandError("Command doesn't accept any arguments") |
|
83 |
|
|
84 |
name = options['name'] |
|
85 |
network = options['network'] |
|
86 |
device = options['device_id'] |
|
87 |
#assume giving security groups comma separated |
|
88 |
security_groups = options['security-groups'] |
|
89 |
userid = options["owner"] |
|
90 |
|
|
91 |
if not name: |
|
92 |
name="" |
|
93 |
|
|
94 |
if not network: |
|
95 |
raise CommandError("network is required") |
|
96 |
if not device: |
|
97 |
raise CommandError("device is required") |
|
98 |
if not userid: |
|
99 |
raise CommandError("owner is required") |
|
100 |
|
|
101 |
#get the network |
|
102 |
network = util.get_network(network, userid, non_deleted=True) |
|
103 |
|
|
104 |
#get the vm |
|
105 |
vm = util.get_vm(device, userid) |
|
106 |
|
|
107 |
#validate security groups |
|
108 |
sg_list = [] |
|
109 |
if security_groups: |
|
110 |
security_groups = security_groups.split(",") |
|
111 |
for gid in security_groups: |
|
112 |
sg = util.get_security_group(int(gid)) |
|
113 |
sg_list.append(sg) |
|
114 |
|
|
115 |
new_port = ports.create(userid, network, vm, security_groups=sg_list) |
|
116 |
self.stdout.write("Created port '%s' in DB.\n" % new_port) |
b/snf-cyclades-app/synnefo/api/ports.py | ||
---|---|---|
121 | 121 |
if name is None: |
122 | 122 |
name = "" |
123 | 123 |
|
124 |
sg_list = [] |
|
125 | 124 |
security_groups = api.utils.get_attribute(port_dict, |
126 | 125 |
"security_groups", |
127 | 126 |
required=False) |
128 | 127 |
#validate security groups |
129 | 128 |
# like get security group from db |
129 |
sg_list = [] |
|
130 | 130 |
if security_groups: |
131 | 131 |
for gid in security_groups: |
132 |
try: |
|
133 |
sg = SecurityGroup.objects.get(id=int(gid)) |
|
134 |
sg_list.append(sg) |
|
135 |
except (ValueError, SecurityGroup.DoesNotExist): |
|
136 |
raise api.faults.ItemNotFound("Not valid security group") |
|
132 |
sg = util.get_security_group(int(gid)) |
|
133 |
sg_list.append(sg) |
|
137 | 134 |
|
138 | 135 |
new_port = ports.create(user_id, network, vm, security_groups=sg_list) |
139 | 136 |
|
... | ... | |
168 | 165 |
if security_groups: |
169 | 166 |
sg_list = [] |
170 | 167 |
#validate security groups |
171 |
# like get security group from db |
|
172 | 168 |
for gid in security_groups: |
173 |
try: |
|
174 |
sg = SecurityGroup.objects.get(id=int(gid)) |
|
175 |
sg_list.append(sg) |
|
176 |
except (ValueError, SecurityGroup.DoesNotExist): |
|
177 |
raise api.faults.ItemNotFound("Not valid security group") |
|
169 |
sg = util.get_security_group(int(gid)) |
|
170 |
sg_list.append(sg) |
|
178 | 171 |
|
179 | 172 |
#clear the old security groups |
180 | 173 |
port.security_groups.clear() |
b/snf-cyclades-app/synnefo/api/util.py | ||
---|---|---|
47 | 47 |
|
48 | 48 |
from snf_django.lib.api import faults |
49 | 49 |
from synnefo.db.models import (Flavor, VirtualMachine, VirtualMachineMetadata, |
50 |
Network, NetworkInterface, BridgePoolTable, |
|
51 |
MacPrefixPoolTable, IPAddress, IPPoolTable) |
|
50 |
Network, NetworkInterface, SecurityGroup, |
|
51 |
BridgePoolTable, MacPrefixPoolTable, IPAddress, |
|
52 |
IPPoolTable) |
|
52 | 53 |
from synnefo.db import pools |
53 | 54 |
|
54 | 55 |
from synnefo.plankton.utils import image_backend |
... | ... | |
243 | 244 |
except (ValueError, NetworkInterface.DoesNotExist): |
244 | 245 |
raise faults.ItemNotFound('Port not found.') |
245 | 246 |
|
247 |
def get_security_group(sg_id): |
|
248 |
try: |
|
249 |
sg = SecurityGroup.objects.get(id=sg_id) |
|
250 |
return sg |
|
251 |
except (ValueError, SecurityGroup.DoesNotExist): |
|
252 |
raise faults.ItemNotFound("Not valid security group") |
|
246 | 253 |
|
247 | 254 |
def get_floating_ip_by_address(userid, address, for_update=False): |
248 | 255 |
try: |
Also available in: Unified diff