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 cloudify.decorators import operation |
| 18 | from openstack_plugin_common import with_nova_client |
| 19 | from openstack_plugin_common.floatingip import ( |
| 20 | use_external_floatingip, |
| 21 | set_floatingip_runtime_properties, |
| 22 | delete_floatingip, |
| 23 | floatingip_creation_validation |
| 24 | ) |
| 25 | |
| 26 | |
| 27 | # random note regarding nova floating-ips: floating ips on nova-net have |
| 28 | # pre-assigned ids, and thus a call "nova.floating_ips.get(<fip_id>)" will |
| 29 | # return a value even if the floating-ip isn't even allocated. |
| 30 | # currently all lookups in the code, including by id, use search (i.e. |
| 31 | # nova.<type>.findall) and lists, which won't return such unallocated |
| 32 | # resources. |
| 33 | |
| 34 | @operation |
| 35 | @with_nova_client |
| 36 | def create(nova_client, args, **kwargs): |
| 37 | |
| 38 | if use_external_floatingip(nova_client, 'ip', |
| 39 | lambda ext_fip: ext_fip.ip): |
| 40 | return |
| 41 | |
| 42 | floatingip = { |
| 43 | 'pool': None |
| 44 | } |
| 45 | floatingip.update(ctx.node.properties['floatingip'], **args) |
| 46 | |
| 47 | fip = nova_client.floating_ips.create(floatingip['pool']) |
| 48 | set_floatingip_runtime_properties(fip.id, fip.ip) |
| 49 | |
| 50 | |
| 51 | @operation |
| 52 | @with_nova_client |
| 53 | def delete(nova_client, **kwargs): |
| 54 | delete_floatingip(nova_client) |
| 55 | |
| 56 | |
| 57 | @operation |
| 58 | @with_nova_client |
| 59 | def creation_validation(nova_client, **kwargs): |
| 60 | floatingip_creation_validation(nova_client, 'ip') |