blob: 23d936e5f6bf46fcf8fe58375a16d7f765d3abc3 [file] [log] [blame]
Yair Fried1fc32a12014-08-04 09:11:30 +03001# Copyright 2013 Hewlett-Packard Development Company, L.P.
2# All Rights Reserved.
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
16import abc
17
18import six
19
20
21class AttributeDict(dict):
22
23 """
24 Provide attribute access (dict.key) to dictionary values.
25 """
26
27 def __getattr__(self, name):
28 """Allow attribute access for all keys in the dict."""
29 if name in self:
30 return self[name]
31 return super(AttributeDict, self).__getattribute__(name)
32
33
34@six.add_metaclass(abc.ABCMeta)
35class DeletableResource(AttributeDict):
36
37 """
38 Support deletion of neutron resources (networks, subnets) via a
39 delete() method, as is supported by keystone and nova resources.
40 """
41
42 def __init__(self, *args, **kwargs):
43 self.client = kwargs.pop('client', None)
John Warren94d8faf2015-09-15 12:22:24 -040044 self.networks_client = kwargs.pop('networks_client', None)
Yair Fried1fc32a12014-08-04 09:11:30 +030045 super(DeletableResource, self).__init__(*args, **kwargs)
46
47 def __str__(self):
48 return '<%s id="%s" name="%s">' % (self.__class__.__name__,
49 self.id, self.name)
50
51 @abc.abstractmethod
52 def delete(self):
53 return
54
Miguel Lavalle02ba8cd2014-09-01 19:23:22 -050055 @abc.abstractmethod
Yair Fried45f92952014-06-26 05:19:19 +030056 def refresh(self):
Miguel Lavalle02ba8cd2014-09-01 19:23:22 -050057 return
58
Yair Fried1fc32a12014-08-04 09:11:30 +030059 def __hash__(self):
60 return hash(self.id)
61
Miguel Lavalle02ba8cd2014-09-01 19:23:22 -050062 def wait_for_status(self, status):
63 if not hasattr(self, 'status'):
64 return
65
Yair Fried45f92952014-06-26 05:19:19 +030066 def helper_get():
67 self.refresh()
68 return self
69
70 return self.client.wait_for_resource_status(helper_get, status)
Miguel Lavalle02ba8cd2014-09-01 19:23:22 -050071
Yair Fried1fc32a12014-08-04 09:11:30 +030072
73class DeletableNetwork(DeletableResource):
74
75 def delete(self):
John Warren94d8faf2015-09-15 12:22:24 -040076 self.networks_client.delete_network(self.id)
Yair Fried1fc32a12014-08-04 09:11:30 +030077
78
79class DeletableSubnet(DeletableResource):
80
81 def __init__(self, *args, **kwargs):
82 super(DeletableSubnet, self).__init__(*args, **kwargs)
83 self._router_ids = set()
84
85 def update(self, *args, **kwargs):
Yair Fried413bf2d2014-11-19 17:07:11 +020086 result = self.client.update_subnet(self.id,
87 *args,
88 **kwargs)
89 return super(DeletableSubnet, self).update(**result['subnet'])
Yair Fried1fc32a12014-08-04 09:11:30 +030090
91 def add_to_router(self, router_id):
92 self._router_ids.add(router_id)
93 self.client.add_router_interface_with_subnet_id(router_id,
94 subnet_id=self.id)
95
96 def delete(self):
97 for router_id in self._router_ids.copy():
98 self.client.remove_router_interface_with_subnet_id(
99 router_id,
100 subnet_id=self.id)
101 self._router_ids.remove(router_id)
102 self.client.delete_subnet(self.id)
103
104
105class DeletableRouter(DeletableResource):
106
107 def set_gateway(self, network_id):
108 return self.update(external_gateway_info=dict(network_id=network_id))
109
110 def unset_gateway(self):
111 return self.update(external_gateway_info=dict())
112
113 def update(self, *args, **kwargs):
David Kranz34e88122014-12-11 15:24:05 -0500114 result = self.client.update_router(self.id,
115 *args,
116 **kwargs)
Yair Fried1fc32a12014-08-04 09:11:30 +0300117 return super(DeletableRouter, self).update(**result['router'])
118
119 def delete(self):
120 self.unset_gateway()
121 self.client.delete_router(self.id)
122
123
124class DeletableFloatingIp(DeletableResource):
125
Yair Fried45f92952014-06-26 05:19:19 +0300126 def refresh(self, *args, **kwargs):
David Kranz34e88122014-12-11 15:24:05 -0500127 result = self.client.show_floatingip(self.id,
128 *args,
129 **kwargs)
Yair Fried45f92952014-06-26 05:19:19 +0300130 super(DeletableFloatingIp, self).update(**result['floatingip'])
131
Yair Fried1fc32a12014-08-04 09:11:30 +0300132 def update(self, *args, **kwargs):
David Kranz34e88122014-12-11 15:24:05 -0500133 result = self.client.update_floatingip(self.id,
134 *args,
135 **kwargs)
Yair Fried1fc32a12014-08-04 09:11:30 +0300136 super(DeletableFloatingIp, self).update(**result['floatingip'])
137
138 def __repr__(self):
139 return '<%s addr="%s">' % (self.__class__.__name__,
140 self.floating_ip_address)
141
142 def __str__(self):
143 return '<"FloatingIP" addr="%s" id="%s">' % (self.floating_ip_address,
144 self.id)
145
146 def delete(self):
147 self.client.delete_floatingip(self.id)
148
149
150class DeletablePort(DeletableResource):
151
152 def delete(self):
153 self.client.delete_port(self.id)
154
155
156class DeletableSecurityGroup(DeletableResource):
157
158 def delete(self):
159 self.client.delete_security_group(self.id)
160
161
162class DeletableSecurityGroupRule(DeletableResource):
163
164 def __repr__(self):
165 return '<%s id="%s">' % (self.__class__.__name__, self.id)
166
167 def delete(self):
168 self.client.delete_security_group_rule(self.id)
169
170
171class DeletablePool(DeletableResource):
172
173 def delete(self):
174 self.client.delete_pool(self.id)
175
176
177class DeletableMember(DeletableResource):
178
179 def delete(self):
180 self.client.delete_member(self.id)
181
182
183class DeletableVip(DeletableResource):
184
185 def delete(self):
186 self.client.delete_vip(self.id)
Miguel Lavalle02ba8cd2014-09-01 19:23:22 -0500187
Yair Fried45f92952014-06-26 05:19:19 +0300188 def refresh(self):
David Kranz34e88122014-12-11 15:24:05 -0500189 result = self.client.show_vip(self.id)
190 super(DeletableVip, self).update(**result['vip'])