~ubuntu-branches/ubuntu/saucy/heat/saucy-updates

« back to all changes in this revision

Viewing changes to heat/engine/resources/quantum/router.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-08 15:23:59 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20130808152359-187gmaw0nx1oduxy
Tags: upstream-2013.2~b2.a186.g2b4b248
ImportĀ upstreamĀ versionĀ 2013.2~b2.a186.g2b4b248

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.quantum import quantum
18
 
from heat.engine import scheduler
19
 
 
20
 
if clients.quantumclient is not None:
21
 
    from quantumclient.common.exceptions import QuantumClientException
22
 
 
23
 
from heat.openstack.common import log as logging
24
 
 
25
 
logger = logging.getLogger(__name__)
26
 
 
27
 
 
28
 
class Router(quantum.QuantumResource):
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.quantum().create_router({'router': props})['router']
48
 
        self.resource_id_set(router['id'])
49
 
 
50
 
    def _show_resource(self):
51
 
        return self.quantum().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.quantum()
60
 
        try:
61
 
            client.delete_router(self.resource_id)
62
 
        except QuantumClientException 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(quantum.QuantumResource):
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.quantum().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.quantum()
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 QuantumClientException as ex:
91
 
            if ex.status_code != 404:
92
 
                raise ex
93
 
 
94
 
 
95
 
class RouterGateway(quantum.QuantumResource):
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::Quantum::RouterInterface' and
107
 
                resource.properties.get('router_id') ==
108
 
                    self.properties.get('router_id')):
109
 
                        deps += (self, resource)
110
 
            # depend on any subnet in this template with the same network_id
111
 
            # as this network_id, as the gateway implicitly creates a port
112
 
            # on that subnet
113
 
            elif (resource.type() == 'OS::Quantum::Subnet' and
114
 
                  resource.properties.get('network_id') ==
115
 
                    self.properties.get('network_id')):
116
 
                        deps += (self, resource)
117
 
 
118
 
    def handle_create(self):
119
 
        router_id = self.properties.get('router_id')
120
 
        network_id = self.properties.get('network_id')
121
 
        self.quantum().add_gateway_router(
122
 
            router_id,
123
 
            {'network_id': network_id})
124
 
        self.resource_id_set('%s:%s' % (router_id, network_id))
125
 
 
126
 
    def handle_delete(self):
127
 
        client = self.quantum()
128
 
        (router_id, network_id) = self.resource_id.split(':')
129
 
        try:
130
 
            client.remove_gateway_router(router_id)
131
 
        except QuantumClientException as ex:
132
 
            if ex.status_code != 404:
133
 
                raise ex
134
 
 
135
 
 
136
 
def resource_mapping():
137
 
    if clients.quantumclient is None:
138
 
        return {}
139
 
 
140
 
    return {
141
 
        'OS::Quantum::Router': Router,
142
 
        'OS::Quantum::RouterInterface': RouterInterface,
143
 
        'OS::Quantum::RouterGateway': RouterGateway,
144
 
    }