|
13 | 13 | # * See the License for the specific language governing permissions and |
14 | 14 | # * limitations under the License. |
15 | 15 |
|
| 16 | +import netaddr |
| 17 | + |
16 | 18 | from cloudify import ctx |
17 | 19 | from cloudify.decorators import operation |
18 | 20 | from cloudify.exceptions import NonRecoverableError |
@@ -91,7 +93,7 @@ def create(neutron_client, args, **kwargs): |
91 | 93 | PORT_OPENSTACK_TYPE, |
92 | 94 | args, |
93 | 95 | {'network_id': net_id}) |
94 | | - _handle_fixed_ips(port) |
| 96 | + _handle_fixed_ips(port, neutron_client) |
95 | 97 | _handle_security_groups(port) |
96 | 98 |
|
97 | 99 | p = neutron_client.create_port( |
@@ -273,29 +275,101 @@ def _get_fixed_ip(port): |
273 | 275 | return port['fixed_ips'][0]['ip_address'] if port['fixed_ips'] else None |
274 | 276 |
|
275 | 277 |
|
276 | | -def _handle_fixed_ips(port): |
277 | | - fixed_ips_element = {} |
| 278 | +def _valid_subnet_ip(ip_address, subnet_dict): |
| 279 | + """Check if ip_address is valid for subnet_dict['cidr'] |
| 280 | +
|
| 281 | + :param ip_address: string |
| 282 | + :param subnet_dict: dict with 'cidr' string |
| 283 | + :return: bool |
| 284 | + """ |
278 | 285 |
|
279 | | - # checking for fixed ip property |
280 | | - if ctx.node.properties['fixed_ip']: |
281 | | - fixed_ips_element['ip_address'] = ctx.node.properties['fixed_ip'] |
| 286 | + try: |
| 287 | + if netaddr.IPAddress(ip_address) in \ |
| 288 | + netaddr.IPNetwork(subnet_dict.get('cidr')): |
| 289 | + return True |
| 290 | + except TypeError: |
| 291 | + pass |
| 292 | + return False |
| 293 | + |
| 294 | + |
| 295 | +def _handle_fixed_ips(port, neutron_client): |
| 296 | + """Combine IPs and Subnets for the Port fixed IPs list. |
| 297 | +
|
| 298 | + The Port object looks something this: |
| 299 | + { |
| 300 | + 'port': { |
| 301 | + 'id': 'some-id', |
| 302 | + 'fixed_ips': [ |
| 303 | + {'subnet_id': 'subnet1', 'ip_address': '1.2.3.4'}, |
| 304 | + {'ip_address': '1.2.3.5'}, |
| 305 | + {'subnet_id': 'subnet3'}, |
| 306 | + ] |
| 307 | + ...snip... |
| 308 | + } |
282 | 309 |
|
283 | | - # checking for a connected subnet |
284 | | - subnet_id = get_openstack_id_of_single_connected_node_by_openstack_type( |
285 | | - ctx, SUBNET_OPENSTACK_TYPE, if_exists=True) |
286 | | - if subnet_id: |
287 | | - fixed_ips_element['subnet_id'] = subnet_id |
| 310 | + We need to combine subnets and ips from three sources: |
| 311 | + 1) Fixed IPs and Subnets from the Port object. |
| 312 | + 2) Subnets from relationships to subnets. |
| 313 | + 3) A Fixed IP from node properties. |
| 314 | +
|
| 315 | + There are some issues: |
| 316 | + 1) Users can provide both subnets and relationships to subnets. |
| 317 | + 2) Recurrences of the subnet_id indicate a desire |
| 318 | + for multiple IPs on that subnet. |
| 319 | + 3) If we provide a fixed_ip, we don't also know the |
| 320 | + target subnet because of how the node properties are. |
| 321 | + We should change that. |
| 322 | + Have not yet changed that. |
| 323 | + But will need to support both paths anyway. |
| 324 | +
|
| 325 | + :param port: An Openstack API Port Object. |
| 326 | + :param neutron_client: Openstack Neutron Client. |
| 327 | + :return: None |
| 328 | + """ |
| 329 | + |
| 330 | + fixed_ips = port.get('fixed_ips', []) |
| 331 | + subnet_ids_from_port = [net.get('subnet_id') for net in fixed_ips] |
| 332 | + subnet_ids_from_rels = \ |
| 333 | + get_openstack_ids_of_connected_nodes_by_openstack_type( |
| 334 | + ctx, SUBNET_OPENSTACK_TYPE) |
| 335 | + |
| 336 | + # Add the subnets from relationships to the port subnets. |
| 337 | + for subnet_from_rel in subnet_ids_from_rels: |
| 338 | + if subnet_from_rel not in subnet_ids_from_port: |
| 339 | + fixed_ips.append({'subnet_id': subnet_from_rel}) |
288 | 340 |
|
289 | | - fixed_ips = port.get( |
290 | | - 'fixed_ips', |
291 | | - [] if not fixed_ips_element else [fixed_ips_element]) |
292 | 341 | addresses = [ip.get('ip_address') for ip in fixed_ips] |
293 | | - subnets = [net.get('subnet_id') for net in fixed_ips] |
294 | | - # applying fixed ip parameter, if available |
295 | | - if fixed_ips_element and not \ |
296 | | - (fixed_ips_element.get('ip_address') in addresses or |
297 | | - fixed_ips_element.get('subnet_id') in subnets): |
298 | | - fixed_ips.append(fixed_ips_element) |
| 342 | + fixed_ip_from_props = ctx.node.properties['fixed_ip'] |
| 343 | + |
| 344 | + # If we have a fixed_ip from node props, we need to add it, |
| 345 | + # but first try to match it with one of our subnets. |
| 346 | + # The fixed_ip_element should be one of: |
| 347 | + # 1) {'ip_address': 'x.x.x.x'} |
| 348 | + # 2) {'subnet_id': '....'} |
| 349 | + # 3) {'ip_address': 'x.x.x.x', 'subnet_id': '....'} |
| 350 | + # show_subnet returns something like this: |
| 351 | + # subnet = { |
| 352 | + # 'subnet': { |
| 353 | + # 'id': 'subnet1', |
| 354 | + # 'cidr': '1.2.3.4/24', |
| 355 | + # 'allocation_pools': [], |
| 356 | + # ...snip... |
| 357 | + # } |
| 358 | + # } |
| 359 | + if fixed_ip_from_props and not (fixed_ip_from_props in addresses): |
| 360 | + fixed_ip_element = {'ip_address': fixed_ip_from_props} |
| 361 | + for fixed_ip in fixed_ips: |
| 362 | + subnet_id = fixed_ip.get('subnet_id') |
| 363 | + if not _valid_subnet_ip( |
| 364 | + fixed_ip_from_props, |
| 365 | + neutron_client.show_subnet(subnet_id)): |
| 366 | + continue |
| 367 | + fixed_ip_element['subnet_id'] = subnet_id |
| 368 | + del fixed_ips[fixed_ips.index(fixed_ip)] |
| 369 | + break |
| 370 | + fixed_ips.append(fixed_ip_element) |
| 371 | + |
| 372 | + # Finally update the object. |
299 | 373 | if fixed_ips: |
300 | 374 | port['fixed_ips'] = fixed_ips |
301 | 375 |
|
|
0 commit comments