Gavin Brebner | 0f465a3 | 2013-03-14 13:26:09 +0000 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2013 Hewlett-Packard Development Company, L.P. |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
Gavin Brebner | 0f465a3 | 2013-03-14 13:26:09 +0000 | [diff] [blame] | 18 | |
| 19 | class AttributeDict(dict): |
| 20 | |
| 21 | """ |
| 22 | Provide attribute access (dict.key) to dictionary values. |
| 23 | """ |
| 24 | |
| 25 | def __getattr__(self, name): |
| 26 | """Allow attribute access for all keys in the dict.""" |
| 27 | if name in self: |
| 28 | return self[name] |
| 29 | return super(AttributeDict, self).__getattribute__(name) |
| 30 | |
| 31 | |
| 32 | class DeletableResource(AttributeDict): |
| 33 | |
| 34 | """ |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 35 | Support deletion of neutron resources (networks, subnets) via a |
Gavin Brebner | 0f465a3 | 2013-03-14 13:26:09 +0000 | [diff] [blame] | 36 | delete() method, as is supported by keystone and nova resources. |
| 37 | """ |
| 38 | |
| 39 | def __init__(self, *args, **kwargs): |
| 40 | self.client = kwargs.pop('client', None) |
| 41 | super(DeletableResource, self).__init__(*args, **kwargs) |
| 42 | |
| 43 | def __str__(self): |
| 44 | return '<%s id="%s" name="%s">' % (self.__class__.__name__, |
| 45 | self.id, self.name) |
| 46 | |
| 47 | def delete(self): |
| 48 | raise NotImplemented() |
| 49 | |
| 50 | |
| 51 | class DeletableNetwork(DeletableResource): |
| 52 | |
| 53 | def delete(self): |
| 54 | self.client.delete_network(self.id) |
| 55 | |
| 56 | |
| 57 | class DeletableSubnet(DeletableResource): |
| 58 | |
| 59 | _router_ids = set() |
| 60 | |
| 61 | def add_to_router(self, router_id): |
| 62 | self._router_ids.add(router_id) |
| 63 | body = dict(subnet_id=self.id) |
| 64 | self.client.add_interface_router(router_id, body=body) |
| 65 | |
| 66 | def delete(self): |
| 67 | for router_id in self._router_ids.copy(): |
| 68 | body = dict(subnet_id=self.id) |
| 69 | self.client.remove_interface_router(router_id, body=body) |
| 70 | self._router_ids.remove(router_id) |
| 71 | self.client.delete_subnet(self.id) |
| 72 | |
| 73 | |
| 74 | class DeletableRouter(DeletableResource): |
| 75 | |
| 76 | def add_gateway(self, network_id): |
| 77 | body = dict(network_id=network_id) |
| 78 | self.client.add_gateway_router(self.id, body=body) |
| 79 | |
| 80 | def delete(self): |
| 81 | self.client.remove_gateway_router(self.id) |
| 82 | self.client.delete_router(self.id) |
| 83 | |
| 84 | |
| 85 | class DeletableFloatingIp(DeletableResource): |
| 86 | |
| 87 | def delete(self): |
| 88 | self.client.delete_floatingip(self.id) |
| 89 | |
| 90 | |
| 91 | class DeletablePort(DeletableResource): |
| 92 | |
| 93 | def delete(self): |
| 94 | self.client.delete_port(self.id) |