blob: 1a9d0449cae67c715ee7a9c9eb8f2e472912fb5d [file] [log] [blame]
dfilppi9981f552017-08-07 20:10:53 +00001#########
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
16from cloudify import ctx
17from cloudify.decorators import operation
18from cloudify.exceptions import NonRecoverableError
19from openstack_plugin_common import (
20 with_neutron_client,
21 provider,
22 is_external_relationship,
23 is_external_relationship_not_conditionally_created,
24 OPENSTACK_ID_PROPERTY
25)
26from openstack_plugin_common.floatingip import (
27 use_external_floatingip,
28 set_floatingip_runtime_properties,
29 delete_floatingip,
30 floatingip_creation_validation
31)
32
33
34@operation
35@with_neutron_client
36def create(neutron_client, args, **kwargs):
37
38 if use_external_floatingip(neutron_client, 'floating_ip_address',
39 lambda ext_fip: ext_fip['floating_ip_address']):
40 return
41
42 floatingip = {
43 # No defaults
44 }
45 floatingip.update(ctx.node.properties['floatingip'], **args)
46
47 # Sugar: floating_network_name -> (resolve) -> floating_network_id
48 if 'floating_network_name' in floatingip:
49 floatingip['floating_network_id'] = neutron_client.cosmo_get_named(
50 'network', floatingip['floating_network_name'])['id']
51 del floatingip['floating_network_name']
52 elif 'floating_network_id' not in floatingip:
53 provider_context = provider(ctx)
54 ext_network = provider_context.ext_network
55 if ext_network:
56 floatingip['floating_network_id'] = ext_network['id']
57 else:
58 raise NonRecoverableError(
59 'Missing floating network id, name or external network')
60
61 fip = neutron_client.create_floatingip(
62 {'floatingip': floatingip})['floatingip']
63 set_floatingip_runtime_properties(fip['id'], fip['floating_ip_address'])
64
65 ctx.logger.info('Floating IP creation response: {0}'.format(fip))
66
67
68@operation
69@with_neutron_client
70def delete(neutron_client, **kwargs):
71 delete_floatingip(neutron_client)
72
73
74@operation
75@with_neutron_client
76def creation_validation(neutron_client, **kwargs):
77 floatingip_creation_validation(neutron_client, 'floating_ip_address')
78
79
80@operation
81@with_neutron_client
82def connect_port(neutron_client, **kwargs):
83 if is_external_relationship_not_conditionally_created(ctx):
84 return
85
86 port_id = ctx.source.instance.runtime_properties[OPENSTACK_ID_PROPERTY]
87 floating_ip_id = ctx.target.instance.runtime_properties[
88 OPENSTACK_ID_PROPERTY]
89 fip = {'port_id': port_id}
90 neutron_client.update_floatingip(floating_ip_id, {'floatingip': fip})
91
92
93@operation
94@with_neutron_client
95def disconnect_port(neutron_client, **kwargs):
96 if is_external_relationship(ctx):
97 ctx.logger.info('Not disassociating floatingip and port since '
98 'external floatingip and port are being used')
99 return
100
101 floating_ip_id = ctx.target.instance.runtime_properties[
102 OPENSTACK_ID_PROPERTY]
103 fip = {'port_id': None}
104 neutron_client.update_floatingip(floating_ip_id, {'floatingip': fip})