~ubuntu-branches/ubuntu/wily/heat/wily-proposed

« back to all changes in this revision

Viewing changes to heat/engine/resources/aws/ec2/route_table.py

  • Committer: Package Import Robot
  • Author(s): James Page, Corey Bryant, James Page
  • Date: 2015-03-30 11:11:18 UTC
  • mfrom: (1.1.23)
  • Revision ID: package-import@ubuntu.com-20150330111118-2qpycylx6swu4yhj
Tags: 2015.1~b3-0ubuntu1
[ Corey Bryant ]
* New upstream milestone release for OpenStack kilo:
  - d/control: Align with upstream dependencies.
  - d/p/sudoers_patch.patch: Rebased.
  - d/p/fix-requirements.patch: Rebased.

[ James Page ]
* d/p/fixup-assert-regex.patch: Tweak test to use assertRegexpMatches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
3
#    not use this file except in compliance with the License. You may obtain
 
4
#    a copy of the License at
 
5
#
 
6
#         http://www.apache.org/licenses/LICENSE-2.0
 
7
#
 
8
#    Unless required by applicable law or agreed to in writing, software
 
9
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
10
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
11
#    License for the specific language governing permissions and limitations
 
12
#    under the License.
 
13
 
 
14
from heat.common.i18n import _
 
15
from heat.engine import constraints
 
16
from heat.engine import properties
 
17
from heat.engine import resource
 
18
from heat.engine.resources.aws.ec2 import vpc
 
19
from heat.engine.resources.openstack.neutron import neutron
 
20
from heat.engine import support
 
21
 
 
22
 
 
23
class RouteTable(resource.Resource):
 
24
 
 
25
    support_status = support.SupportStatus(version='2014.1')
 
26
 
 
27
    PROPERTIES = (
 
28
        VPC_ID, TAGS,
 
29
    ) = (
 
30
        'VpcId', 'Tags',
 
31
    )
 
32
 
 
33
    _TAG_KEYS = (
 
34
        TAG_KEY, TAG_VALUE,
 
35
    ) = (
 
36
        'Key', 'Value',
 
37
    )
 
38
 
 
39
    properties_schema = {
 
40
        VPC_ID: properties.Schema(
 
41
            properties.Schema.STRING,
 
42
            _('VPC ID for where the route table is created.'),
 
43
            required=True
 
44
        ),
 
45
        TAGS: properties.Schema(
 
46
            properties.Schema.LIST,
 
47
            schema=properties.Schema(
 
48
                properties.Schema.MAP,
 
49
                _('List of tags to be attached to this resource.'),
 
50
                schema={
 
51
                    TAG_KEY: properties.Schema(
 
52
                        properties.Schema.STRING,
 
53
                        required=True
 
54
                    ),
 
55
                    TAG_VALUE: properties.Schema(
 
56
                        properties.Schema.STRING,
 
57
                        required=True
 
58
                    ),
 
59
                },
 
60
                implemented=False,
 
61
            )
 
62
        ),
 
63
    }
 
64
 
 
65
    default_client_name = 'neutron'
 
66
 
 
67
    def handle_create(self):
 
68
        client = self.client()
 
69
        props = {'name': self.physical_resource_name()}
 
70
        router = client.create_router({'router': props})['router']
 
71
        self.resource_id_set(router['id'])
 
72
 
 
73
    def check_create_complete(self, *args):
 
74
        client = self.client()
 
75
        attributes = client.show_router(
 
76
            self.resource_id)['router']
 
77
        if not neutron.NeutronResource.is_built(attributes):
 
78
            return False
 
79
 
 
80
        network_id = self.properties.get(self.VPC_ID)
 
81
        default_router = vpc.VPC.router_for_vpc(client, network_id)
 
82
        if default_router and default_router.get('external_gateway_info'):
 
83
            # the default router for the VPC is connected
 
84
            # to the external router, so do it for this too.
 
85
            external_network_id = default_router[
 
86
                'external_gateway_info']['network_id']
 
87
            client.add_gateway_router(self.resource_id, {
 
88
                'network_id': external_network_id})
 
89
        return True
 
90
 
 
91
    def handle_delete(self):
 
92
        client = self.client()
 
93
 
 
94
        router_id = self.resource_id
 
95
        try:
 
96
            client.delete_router(router_id)
 
97
        except Exception as ex:
 
98
            self.client_plugin().ignore_not_found(ex)
 
99
 
 
100
        # just in case this router has been added to a gateway, remove it
 
101
        try:
 
102
            client.remove_gateway_router(router_id)
 
103
        except Exception as ex:
 
104
            self.client_plugin().ignore_not_found(ex)
 
105
 
 
106
 
 
107
class SubnetRouteTableAssociation(resource.Resource):
 
108
 
 
109
    PROPERTIES = (
 
110
        ROUTE_TABLE_ID, SUBNET_ID,
 
111
    ) = (
 
112
        'RouteTableId', 'SubnetId',
 
113
    )
 
114
 
 
115
    properties_schema = {
 
116
        ROUTE_TABLE_ID: properties.Schema(
 
117
            properties.Schema.STRING,
 
118
            _('Route table ID.'),
 
119
            required=True
 
120
        ),
 
121
        SUBNET_ID: properties.Schema(
 
122
            properties.Schema.STRING,
 
123
            _('Subnet ID.'),
 
124
            required=True,
 
125
            constraints=[
 
126
                constraints.CustomConstraint('neutron.subnet')
 
127
            ]
 
128
        ),
 
129
    }
 
130
 
 
131
    default_client_name = 'neutron'
 
132
 
 
133
    def handle_create(self):
 
134
        client = self.client()
 
135
        subnet_id = self.properties.get(self.SUBNET_ID)
 
136
 
 
137
        router_id = self.properties.get(self.ROUTE_TABLE_ID)
 
138
 
 
139
        # remove the default router association for this subnet.
 
140
        try:
 
141
            previous_router = self._router_for_subnet(subnet_id)
 
142
            if previous_router:
 
143
                client.remove_interface_router(
 
144
                    previous_router['id'],
 
145
                    {'subnet_id': subnet_id})
 
146
        except Exception as ex:
 
147
            self.client_plugin().ignore_not_found(ex)
 
148
 
 
149
        client.add_interface_router(
 
150
            router_id, {'subnet_id': subnet_id})
 
151
 
 
152
    def _router_for_subnet(self, subnet_id):
 
153
        client = self.client()
 
154
        subnet = client.show_subnet(
 
155
            subnet_id)['subnet']
 
156
        network_id = subnet['network_id']
 
157
        return vpc.VPC.router_for_vpc(client, network_id)
 
158
 
 
159
    def handle_delete(self):
 
160
        client = self.client()
 
161
        subnet_id = self.properties.get(self.SUBNET_ID)
 
162
 
 
163
        router_id = self.properties.get(self.ROUTE_TABLE_ID)
 
164
 
 
165
        try:
 
166
            client.remove_interface_router(router_id, {
 
167
                'subnet_id': subnet_id})
 
168
        except Exception as ex:
 
169
            self.client_plugin().ignore_not_found(ex)
 
170
 
 
171
        # add back the default router
 
172
        try:
 
173
            default_router = self._router_for_subnet(subnet_id)
 
174
            if default_router:
 
175
                client.add_interface_router(
 
176
                    default_router['id'], {'subnet_id': subnet_id})
 
177
        except Exception as ex:
 
178
            self.client_plugin().ignore_not_found(ex)
 
179
 
 
180
 
 
181
def resource_mapping():
 
182
    return {
 
183
        'AWS::EC2::RouteTable': RouteTable,
 
184
        'AWS::EC2::SubnetRouteTableAssociation': SubnetRouteTableAssociation,
 
185
    }