dfilppi | 9981f55 | 2017-08-07 20:10:53 +0000 | [diff] [blame^] | 1 | ######### |
| 2 | # Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # * See the License for the specific language governing permissions and |
| 14 | # * limitations under the License. |
| 15 | |
| 16 | from cloudify import ctx |
| 17 | from openstack_plugin_common import ( |
| 18 | delete_resource_and_runtime_properties, |
| 19 | use_external_resource, |
| 20 | validate_resource, |
| 21 | COMMON_RUNTIME_PROPERTIES_KEYS, |
| 22 | OPENSTACK_ID_PROPERTY, |
| 23 | OPENSTACK_TYPE_PROPERTY) |
| 24 | |
| 25 | |
| 26 | FLOATINGIP_OPENSTACK_TYPE = 'floatingip' |
| 27 | |
| 28 | # Runtime properties |
| 29 | IP_ADDRESS_PROPERTY = 'floating_ip_address' # the actual ip address |
| 30 | RUNTIME_PROPERTIES_KEYS = COMMON_RUNTIME_PROPERTIES_KEYS + \ |
| 31 | [IP_ADDRESS_PROPERTY] |
| 32 | |
| 33 | |
| 34 | def use_external_floatingip(client, ip_field_name, ext_fip_ip_extractor): |
| 35 | external_fip = use_external_resource( |
| 36 | ctx, client, FLOATINGIP_OPENSTACK_TYPE, ip_field_name) |
| 37 | if external_fip: |
| 38 | ctx.instance.runtime_properties[IP_ADDRESS_PROPERTY] = \ |
| 39 | ext_fip_ip_extractor(external_fip) |
| 40 | return True |
| 41 | |
| 42 | return False |
| 43 | |
| 44 | |
| 45 | def set_floatingip_runtime_properties(fip_id, ip_address): |
| 46 | ctx.instance.runtime_properties[OPENSTACK_ID_PROPERTY] = fip_id |
| 47 | ctx.instance.runtime_properties[OPENSTACK_TYPE_PROPERTY] = \ |
| 48 | FLOATINGIP_OPENSTACK_TYPE |
| 49 | ctx.instance.runtime_properties[IP_ADDRESS_PROPERTY] = ip_address |
| 50 | |
| 51 | |
| 52 | def delete_floatingip(client, **kwargs): |
| 53 | delete_resource_and_runtime_properties(ctx, client, |
| 54 | RUNTIME_PROPERTIES_KEYS) |
| 55 | |
| 56 | |
| 57 | def floatingip_creation_validation(client, ip_field_name, **kwargs): |
| 58 | validate_resource(ctx, client, FLOATINGIP_OPENSTACK_TYPE, |
| 59 | ip_field_name) |
| 60 | |
| 61 | |
| 62 | def get_server_floating_ip(neutron_client, server_id): |
| 63 | |
| 64 | floating_ips = neutron_client.list_floatingips() |
| 65 | |
| 66 | floating_ips = floating_ips.get('floatingips') |
| 67 | if not floating_ips: |
| 68 | return None |
| 69 | |
| 70 | for floating_ip in floating_ips: |
| 71 | port_id = floating_ip.get('port_id') |
| 72 | if not port_id: |
| 73 | # this floating ip is not attached to any port |
| 74 | continue |
| 75 | |
| 76 | port = neutron_client.show_port(port_id)['port'] |
| 77 | device_id = port.get('device_id') |
| 78 | if not device_id: |
| 79 | # this port is not attached to any server |
| 80 | continue |
| 81 | |
| 82 | if server_id == device_id: |
| 83 | return floating_ip |
| 84 | return None |