1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
16
from heat.engine import clients
17
from heat.openstack.common import log as logging
18
from heat.engine.resources.neutron import neutron
20
if clients.neutronclient is not None:
21
from neutronclient.common.exceptions import NeutronClientException
23
logger = logging.getLogger(__name__)
26
class FloatingIP(neutron.NeutronResource):
27
properties_schema = {'floating_network_id': {'Type': 'String',
29
'value_specs': {'Type': 'Map',
31
'port_id': {'Type': 'String'},
32
'fixed_ip_address': {'Type': 'String'}}
34
def add_dependencies(self, deps):
35
super(FloatingIP, self).add_dependencies(deps)
36
# depend on any RouterGateway in this template with the same
37
# network_id as this floating_network_id
38
for resource in self.stack.resources.itervalues():
39
if ((resource.type() == 'OS::Neutron::RouterGateway' or
40
resource.type() == 'OS::Quantum::RouterGateway') and
41
resource.properties.get('network_id') ==
42
self.properties.get('floating_network_id')):
43
deps += (self, resource)
45
def handle_create(self):
46
props = self.prepare_properties(
48
self.physical_resource_name())
49
fip = self.neutron().create_floatingip({
50
'floatingip': props})['floatingip']
51
self.resource_id_set(fip['id'])
53
def handle_delete(self):
54
client = self.neutron()
56
client.delete_floatingip(self.resource_id)
57
except NeutronClientException as ex:
58
if ex.status_code != 404:
61
def FnGetAtt(self, key):
63
attributes = self.neutron().show_floatingip(
64
self.resource_id)['floatingip']
65
except NeutronClientException as ex:
66
logger.warn("failed to fetch resource attributes: %s" % str(ex))
68
return self.handle_get_attributes(self.name, key, attributes)
71
class FloatingIPAssociation(neutron.NeutronResource):
72
properties_schema = {'floatingip_id': {'Type': 'String',
74
'port_id': {'Type': 'String',
76
'fixed_ip_address': {'Type': 'String'}}
78
def handle_create(self):
79
props = self.prepare_properties(self.properties, self.name)
81
floatingip_id = props.pop('floatingip_id')
83
self.neutron().update_floatingip(floatingip_id, {
84
'floatingip': props})['floatingip']
85
self.resource_id_set('%s:%s' % (floatingip_id, props['port_id']))
87
def handle_delete(self):
88
if not self.resource_id:
90
client = self.neutron()
91
(floatingip_id, port_id) = self.resource_id.split(':')
93
client.update_floatingip(
95
{'floatingip': {'port_id': None}})
96
except NeutronClientException as ex:
97
if ex.status_code != 404:
101
def resource_mapping():
102
if clients.neutronclient is None:
106
'OS::Neutron::FloatingIP': FloatingIP,
107
'OS::Neutron::FloatingIPAssociation': FloatingIPAssociation,
108
'OS::Quantum::FloatingIP': FloatingIP,
109
'OS::Quantum::FloatingIPAssociation': FloatingIPAssociation,