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

« back to all changes in this revision

Viewing changes to heat/engine/resources/neutron/router.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.engine.resources.neutron import neutron
 
18
from heat.engine import scheduler
 
19
 
 
20
if clients.neutronclient is not None:
 
21
    from neutronclient.common.exceptions import NeutronClientException
 
22
 
 
23
from heat.openstack.common import log as logging
 
24
 
 
25
logger = logging.getLogger(__name__)
 
26
 
 
27
 
 
28
class Router(neutron.NeutronResource):
 
29
    properties_schema = {'name': {'Type': 'String'},
 
30
                         'value_specs': {'Type': 'Map',
 
31
                                         'Default': {}},
 
32
                         'admin_state_up': {'Type': 'Boolean',
 
33
                                            'Default': True}}
 
34
    attributes_schema = {
 
35
        "status": "the status of the router",
 
36
        "external_gateway_info": "gateway network for the router",
 
37
        "name": "friendly name of the router",
 
38
        "admin_state_up": "administrative state of the router",
 
39
        "tenant_id": "tenant owning the router",
 
40
        "id": "unique identifier for the router"
 
41
    }
 
42
 
 
43
    def handle_create(self):
 
44
        props = self.prepare_properties(
 
45
            self.properties,
 
46
            self.physical_resource_name())
 
47
        router = self.neutron().create_router({'router': props})['router']
 
48
        self.resource_id_set(router['id'])
 
49
 
 
50
    def _show_resource(self):
 
51
        return self.neutron().show_router(
 
52
            self.resource_id)['router']
 
53
 
 
54
    def check_create_complete(self, *args):
 
55
        attributes = self._show_resource()
 
56
        return self.is_built(attributes)
 
57
 
 
58
    def handle_delete(self):
 
59
        client = self.neutron()
 
60
        try:
 
61
            client.delete_router(self.resource_id)
 
62
        except NeutronClientException as ex:
 
63
            if ex.status_code != 404:
 
64
                raise ex
 
65
        else:
 
66
            return scheduler.TaskRunner(self._confirm_delete)()
 
67
 
 
68
 
 
69
class RouterInterface(neutron.NeutronResource):
 
70
    properties_schema = {'router_id': {'Type': 'String',
 
71
                                       'Required': True},
 
72
                         'subnet_id': {'Type': 'String',
 
73
                                       'Required': True}}
 
74
 
 
75
    def handle_create(self):
 
76
        router_id = self.properties.get('router_id')
 
77
        subnet_id = self.properties.get('subnet_id')
 
78
        self.neutron().add_interface_router(
 
79
            router_id,
 
80
            {'subnet_id': subnet_id})
 
81
        self.resource_id_set('%s:%s' % (router_id, subnet_id))
 
82
 
 
83
    def handle_delete(self):
 
84
        client = self.neutron()
 
85
        (router_id, subnet_id) = self.resource_id.split(':')
 
86
        try:
 
87
            client.remove_interface_router(
 
88
                router_id,
 
89
                {'subnet_id': subnet_id})
 
90
        except NeutronClientException as ex:
 
91
            if ex.status_code != 404:
 
92
                raise ex
 
93
 
 
94
 
 
95
class RouterGateway(neutron.NeutronResource):
 
96
    properties_schema = {'router_id': {'Type': 'String',
 
97
                                       'Required': True},
 
98
                         'network_id': {'Type': 'String',
 
99
                                        'Required': True}}
 
100
 
 
101
    def add_dependencies(self, deps):
 
102
        super(RouterGateway, self).add_dependencies(deps)
 
103
        for resource in self.stack.resources.itervalues():
 
104
            # depend on any RouterInterface in this template with the same
 
105
            # router_id as this router_id
 
106
            if ((resource.type() == 'OS::Neutron::RouterInterface' or
 
107
                resource.type() == 'OS::Quantum::RouterInterface') and
 
108
                resource.properties.get('router_id') ==
 
109
                    self.properties.get('router_id')):
 
110
                        deps += (self, resource)
 
111
            # depend on any subnet in this template with the same network_id
 
112
            # as this network_id, as the gateway implicitly creates a port
 
113
            # on that subnet
 
114
            elif ((resource.type() == 'OS::Neutron::Subnet' or
 
115
                  resource.type() == 'OS::Quantum::Subnet') and
 
116
                  resource.properties.get('network_id') ==
 
117
                    self.properties.get('network_id')):
 
118
                        deps += (self, resource)
 
119
 
 
120
    def handle_create(self):
 
121
        router_id = self.properties.get('router_id')
 
122
        network_id = self.properties.get('network_id')
 
123
        self.neutron().add_gateway_router(
 
124
            router_id,
 
125
            {'network_id': network_id})
 
126
        self.resource_id_set('%s:%s' % (router_id, network_id))
 
127
 
 
128
    def handle_delete(self):
 
129
        client = self.neutron()
 
130
        (router_id, network_id) = self.resource_id.split(':')
 
131
        try:
 
132
            client.remove_gateway_router(router_id)
 
133
        except NeutronClientException as ex:
 
134
            if ex.status_code != 404:
 
135
                raise ex
 
136
 
 
137
 
 
138
def resource_mapping():
 
139
    if clients.neutronclient is None:
 
140
        return {}
 
141
 
 
142
    return {
 
143
        'OS::Neutron::Router': Router,
 
144
        'OS::Neutron::RouterInterface': RouterInterface,
 
145
        'OS::Neutron::RouterGateway': RouterGateway,
 
146
        'OS::Quantum::Router': Router,
 
147
        'OS::Quantum::RouterInterface': RouterInterface,
 
148
        'OS::Quantum::RouterGateway': RouterGateway,
 
149
    }