Revision 69c8d65d snf-cyclades-app/synnefo/api/ports.py
b/snf-cyclades-app/synnefo/api/ports.py | ||
---|---|---|
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 | 34 |
#from django.conf import settings |
35 |
import ipaddr |
|
35 | 36 |
from django.conf.urls import patterns |
36 | 37 |
from django.http import HttpResponse |
37 | 38 |
from django.utils import simplejson as json |
... | ... | |
39 | 40 |
from django.template.loader import render_to_string |
40 | 41 |
|
41 | 42 |
from snf_django.lib import api |
43 |
from snf_django.lib.api import faults |
|
42 | 44 |
|
43 | 45 |
from synnefo.api import util |
44 | 46 |
from synnefo.db.models import NetworkInterface |
... | ... | |
107 | 109 |
|
108 | 110 |
network = util.get_network(net_id, user_id, non_deleted=True) |
109 | 111 |
|
112 |
# Check if the request contains a valid IPv4 address |
|
113 |
fixed_ips = api.utils.get_attribute(port_dict, "fixed_ips", required=False) |
|
114 |
if fixed_ips is not None and len(fixed_ips) > 0: |
|
115 |
if len(fixed_ips) > 1: |
|
116 |
msg = "'fixed_ips' attribute must contain only one fixed IP." |
|
117 |
raise faults.BadRequest(msg) |
|
118 |
fixed_ip_address = fixed_ips[0].get("ip_address") |
|
119 |
if fixed_ip_address is not None: |
|
120 |
try: |
|
121 |
ip = ipaddr.IPAddress(fixed_ip_address) |
|
122 |
if ip.version == 6: |
|
123 |
msg = "'ip_address' can be only an IPv4 address'" |
|
124 |
raise faults.BadRequest(msg) |
|
125 |
except ValueError: |
|
126 |
msg = "%s is not a valid IPv4 Address" % fixed_ip_address |
|
127 |
raise faults.BadRequest(msg) |
|
128 |
else: |
|
129 |
fixed_ip_address = None |
|
130 |
|
|
110 | 131 |
ipaddress = None |
111 | 132 |
if network.public: |
112 |
fixed_ips = api.utils.get_attribute(port_dict, "fixed_ips", |
|
113 |
required=True) |
|
114 |
fip_address = api.utils.get_attribute(fixed_ips[0], 'ip_address', |
|
115 |
required=True) |
|
116 |
ipaddress = util.get_floating_ip_by_address(user_id, fip_address, |
|
133 |
# Creating a port to a public network is only allowed if the user has |
|
134 |
# already a floating IP address in this network which is specified |
|
135 |
# as the fixed IP address of the port |
|
136 |
if fixed_ip_address is None: |
|
137 |
msg = ("'fixed_ips' attribute must contain a floating IP address" |
|
138 |
" in order to connect to a public network.") |
|
139 |
raise faults.BadRequest(msg) |
|
140 |
ipaddress = util.get_floating_ip_by_address(user_id, fixed_ip_address, |
|
117 | 141 |
for_update=True) |
142 |
elif fixed_ip_address: |
|
143 |
ipaddress = util.allocate_ip(network, user_id, |
|
144 |
address=fixed_ip_address) |
|
118 | 145 |
|
119 | 146 |
vm = util.get_vm(dev_id, user_id, for_update=True, non_deleted=True, |
120 | 147 |
non_suspended=True) |
... | ... | |
134 | 161 |
sg = util.get_security_group(int(gid)) |
135 | 162 |
sg_list.append(sg) |
136 | 163 |
|
137 |
new_port = ports.create(network, vm, ipaddress, security_groups=sg_list) |
|
164 |
new_port = ports.create(network, vm, ipaddress=ipaddress, |
|
165 |
security_groups=sg_list) |
|
138 | 166 |
|
139 | 167 |
response = render_port(request, port_to_dict(new_port), status=201) |
140 | 168 |
|
Also available in: Unified diff