blob: ae0eda1127888e57eb56db38e879aba9c3b7f3b3 [file] [log] [blame]
Gavin Brebner0f465a32013-03-14 13:26:09 +00001# 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 Brebner0f465a32013-03-14 13:26:09 +000018
19class 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
32class DeletableResource(AttributeDict):
33
34 """
Mark McClainf2982e82013-07-06 17:48:03 -040035 Support deletion of neutron resources (networks, subnets) via a
Gavin Brebner0f465a32013-03-14 13:26:09 +000036 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
Yair Fried9a551c42013-12-15 14:59:34 +020050 def __hash__(self):
51 return id(self)
52
Gavin Brebner0f465a32013-03-14 13:26:09 +000053
54class DeletableNetwork(DeletableResource):
55
56 def delete(self):
57 self.client.delete_network(self.id)
58
59
60class DeletableSubnet(DeletableResource):
61
Yair Friedca1d9a92013-10-22 11:52:34 +030062 def __init__(self, *args, **kwargs):
63 super(DeletableSubnet, self).__init__(*args, **kwargs)
64 self._router_ids = set()
Gavin Brebner0f465a32013-03-14 13:26:09 +000065
66 def add_to_router(self, router_id):
67 self._router_ids.add(router_id)
68 body = dict(subnet_id=self.id)
69 self.client.add_interface_router(router_id, body=body)
70
71 def delete(self):
72 for router_id in self._router_ids.copy():
73 body = dict(subnet_id=self.id)
74 self.client.remove_interface_router(router_id, body=body)
75 self._router_ids.remove(router_id)
76 self.client.delete_subnet(self.id)
77
78
79class DeletableRouter(DeletableResource):
80
81 def add_gateway(self, network_id):
82 body = dict(network_id=network_id)
83 self.client.add_gateway_router(self.id, body=body)
84
85 def delete(self):
86 self.client.remove_gateway_router(self.id)
87 self.client.delete_router(self.id)
88
89
90class DeletableFloatingIp(DeletableResource):
91
Yair Fried9a551c42013-12-15 14:59:34 +020092 def update(self, *args, **kwargs):
93 result = self.client.update_floatingip(floatingip=self.id,
94 body=dict(
95 floatingip=dict(*args,
96 **kwargs)
97 ))
98 super(DeletableFloatingIp, self).update(**result['floatingip'])
99
100 def __repr__(self):
101 return '<%s addr="%s">' % (self.__class__.__name__,
102 self.floating_ip_address)
103
104 def __str__(self):
Yair Fried05db2522013-11-18 11:02:10 +0200105 return '<"FloatingIP" addr="%s" id="%s">' % (self.floating_ip_address,
Yair Fried9a551c42013-12-15 14:59:34 +0200106 self.id)
107
Gavin Brebner0f465a32013-03-14 13:26:09 +0000108 def delete(self):
109 self.client.delete_floatingip(self.id)
110
111
112class DeletablePort(DeletableResource):
113
114 def delete(self):
115 self.client.delete_port(self.id)
Yair Friedeb69f3f2013-10-10 13:18:16 +0300116
117
118class DeletableSecurityGroup(DeletableResource):
119
120 def delete(self):
121 self.client.delete_security_group(self.id)
122
123
124class DeletableSecurityGroupRule(DeletableResource):
125
126 def __repr__(self):
127 return '<%s id="%s">' % (self.__class__.__name__, self.id)
128
129 def delete(self):
130 self.client.delete_security_group_rule(self.id)