~ubuntu-branches/ubuntu/trusty/heat/trusty

« back to all changes in this revision

Viewing changes to heat/engine/resources/neutron/floatingip.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short
  • Date: 2013-08-08 01:08:42 UTC
  • Revision ID: package-import@ubuntu.com-20130808010842-77cni2v4vlib7rus
Tags: 2013.2~b2-0ubuntu4
[ Chuck Short ]
* debian/rules: Enable testsuite during builds.
* debian/patches/fix-sqlalchemy-0.8.patch: Build against sqlalchemy 0.8.
* debian/patches/rename-quantumclient.patch: quantumclient -> neutronclient.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
#
 
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
 
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, WITHOUT
 
12
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
13
#    License for the specific language governing permissions and limitations
 
14
#    under the License.
 
15
 
 
16
from heat.engine import clients
 
17
from heat.openstack.common import log as logging
 
18
from heat.engine.resources.neutron import neutron
 
19
 
 
20
if clients.neutronclient is not None:
 
21
    from neutronclient.common.exceptions import NeutronClientException
 
22
 
 
23
logger = logging.getLogger(__name__)
 
24
 
 
25
 
 
26
class FloatingIP(neutron.NeutronResource):
 
27
    properties_schema = {'floating_network_id': {'Type': 'String',
 
28
                                                 'Required': True},
 
29
                         'value_specs': {'Type': 'Map',
 
30
                                         'Default': {}},
 
31
                         'port_id': {'Type': 'String'},
 
32
                         'fixed_ip_address': {'Type': 'String'}}
 
33
 
 
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)
 
44
 
 
45
    def handle_create(self):
 
46
        props = self.prepare_properties(
 
47
            self.properties,
 
48
            self.physical_resource_name())
 
49
        fip = self.neutron().create_floatingip({
 
50
            'floatingip': props})['floatingip']
 
51
        self.resource_id_set(fip['id'])
 
52
 
 
53
    def handle_delete(self):
 
54
        client = self.neutron()
 
55
        try:
 
56
            client.delete_floatingip(self.resource_id)
 
57
        except NeutronClientException as ex:
 
58
            if ex.status_code != 404:
 
59
                raise ex
 
60
 
 
61
    def FnGetAtt(self, key):
 
62
        try:
 
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))
 
67
            return None
 
68
        return self.handle_get_attributes(self.name, key, attributes)
 
69
 
 
70
 
 
71
class FloatingIPAssociation(neutron.NeutronResource):
 
72
    properties_schema = {'floatingip_id': {'Type': 'String',
 
73
                                           'Required': True},
 
74
                         'port_id': {'Type': 'String',
 
75
                                     'Required': True},
 
76
                         'fixed_ip_address': {'Type': 'String'}}
 
77
 
 
78
    def handle_create(self):
 
79
        props = self.prepare_properties(self.properties, self.name)
 
80
 
 
81
        floatingip_id = props.pop('floatingip_id')
 
82
 
 
83
        self.neutron().update_floatingip(floatingip_id, {
 
84
            'floatingip': props})['floatingip']
 
85
        self.resource_id_set('%s:%s' % (floatingip_id, props['port_id']))
 
86
 
 
87
    def handle_delete(self):
 
88
        if not self.resource_id:
 
89
            return
 
90
        client = self.neutron()
 
91
        (floatingip_id, port_id) = self.resource_id.split(':')
 
92
        try:
 
93
            client.update_floatingip(
 
94
                floatingip_id,
 
95
                {'floatingip': {'port_id': None}})
 
96
        except NeutronClientException as ex:
 
97
            if ex.status_code != 404:
 
98
                raise ex
 
99
 
 
100
 
 
101
def resource_mapping():
 
102
    if clients.neutronclient is None:
 
103
        return {}
 
104
 
 
105
    return {
 
106
        'OS::Neutron::FloatingIP': FloatingIP,
 
107
        'OS::Neutron::FloatingIPAssociation': FloatingIPAssociation,
 
108
        'OS::Quantum::FloatingIP': FloatingIP,
 
109
        'OS::Quantum::FloatingIPAssociation': FloatingIPAssociation,
 
110
    }