blob: ae303123315af709069b82e1c78a98be3f5b0df6 [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 Warren3961acd2015-10-02 14:38:53 -040044 self.network_client = kwargs.pop('network_client', None)
John Warren94d8faf2015-09-15 12:22:24 -040045 self.networks_client = kwargs.pop('networks_client', None)
John Warren3961acd2015-10-02 14:38:53 -040046 self.subnets_client = kwargs.pop('subnets_client', None)
John Warren49c0fe52015-10-22 12:35:54 -040047 self.ports_client = kwargs.pop('ports_client', None)
Yair Fried1fc32a12014-08-04 09:11:30 +030048 super(DeletableResource, self).__init__(*args, **kwargs)
49
50 def __str__(self):
51 return '<%s id="%s" name="%s">' % (self.__class__.__name__,
52 self.id, self.name)
53
54 @abc.abstractmethod
55 def delete(self):
56 return
57
Miguel Lavalle02ba8cd2014-09-01 19:23:22 -050058 @abc.abstractmethod
Yair Fried45f92952014-06-26 05:19:19 +030059 def refresh(self):
Miguel Lavalle02ba8cd2014-09-01 19:23:22 -050060 return
61
Yair Fried1fc32a12014-08-04 09:11:30 +030062 def __hash__(self):
63 return hash(self.id)
64
Miguel Lavalle02ba8cd2014-09-01 19:23:22 -050065 def wait_for_status(self, status):
66 if not hasattr(self, 'status'):
67 return
68
Yair Fried45f92952014-06-26 05:19:19 +030069 def helper_get():
70 self.refresh()
71 return self
72
73 return self.client.wait_for_resource_status(helper_get, status)
Miguel Lavalle02ba8cd2014-09-01 19:23:22 -050074
Yair Fried1fc32a12014-08-04 09:11:30 +030075
76class DeletableNetwork(DeletableResource):
77
78 def delete(self):
John Warren94d8faf2015-09-15 12:22:24 -040079 self.networks_client.delete_network(self.id)
Yair Fried1fc32a12014-08-04 09:11:30 +030080
81
82class DeletableSubnet(DeletableResource):
83
84 def __init__(self, *args, **kwargs):
85 super(DeletableSubnet, self).__init__(*args, **kwargs)
86 self._router_ids = set()
87
88 def update(self, *args, **kwargs):
John Warren3961acd2015-10-02 14:38:53 -040089 result = self.subnets_client.update_subnet(self.id,
90 *args,
91 **kwargs)
Yair Fried413bf2d2014-11-19 17:07:11 +020092 return super(DeletableSubnet, self).update(**result['subnet'])
Yair Fried1fc32a12014-08-04 09:11:30 +030093
94 def add_to_router(self, router_id):
95 self._router_ids.add(router_id)
John Warren3961acd2015-10-02 14:38:53 -040096 self.network_client.add_router_interface_with_subnet_id(
97 router_id, subnet_id=self.id)
Yair Fried1fc32a12014-08-04 09:11:30 +030098
99 def delete(self):
100 for router_id in self._router_ids.copy():
John Warren3961acd2015-10-02 14:38:53 -0400101 self.network_client.remove_router_interface_with_subnet_id(
Yair Fried1fc32a12014-08-04 09:11:30 +0300102 router_id,
103 subnet_id=self.id)
104 self._router_ids.remove(router_id)
John Warren3961acd2015-10-02 14:38:53 -0400105 self.subnets_client.delete_subnet(self.id)
Yair Fried1fc32a12014-08-04 09:11:30 +0300106
107
108class DeletableRouter(DeletableResource):
109
110 def set_gateway(self, network_id):
111 return self.update(external_gateway_info=dict(network_id=network_id))
112
113 def unset_gateway(self):
114 return self.update(external_gateway_info=dict())
115
116 def update(self, *args, **kwargs):
David Kranz34e88122014-12-11 15:24:05 -0500117 result = self.client.update_router(self.id,
118 *args,
119 **kwargs)
Yair Fried1fc32a12014-08-04 09:11:30 +0300120 return super(DeletableRouter, self).update(**result['router'])
121
122 def delete(self):
123 self.unset_gateway()
124 self.client.delete_router(self.id)
125
126
127class DeletableFloatingIp(DeletableResource):
128
Yair Fried45f92952014-06-26 05:19:19 +0300129 def refresh(self, *args, **kwargs):
David Kranz34e88122014-12-11 15:24:05 -0500130 result = self.client.show_floatingip(self.id,
131 *args,
132 **kwargs)
Yair Fried45f92952014-06-26 05:19:19 +0300133 super(DeletableFloatingIp, self).update(**result['floatingip'])
134
Yair Fried1fc32a12014-08-04 09:11:30 +0300135 def update(self, *args, **kwargs):
David Kranz34e88122014-12-11 15:24:05 -0500136 result = self.client.update_floatingip(self.id,
137 *args,
138 **kwargs)
Yair Fried1fc32a12014-08-04 09:11:30 +0300139 super(DeletableFloatingIp, self).update(**result['floatingip'])
140
141 def __repr__(self):
142 return '<%s addr="%s">' % (self.__class__.__name__,
143 self.floating_ip_address)
144
145 def __str__(self):
146 return '<"FloatingIP" addr="%s" id="%s">' % (self.floating_ip_address,
147 self.id)
148
149 def delete(self):
150 self.client.delete_floatingip(self.id)
151
152
153class DeletablePort(DeletableResource):
154
155 def delete(self):
John Warren49c0fe52015-10-22 12:35:54 -0400156 self.ports_client.delete_port(self.id)
Yair Fried1fc32a12014-08-04 09:11:30 +0300157
158
159class DeletableSecurityGroup(DeletableResource):
160
161 def delete(self):
162 self.client.delete_security_group(self.id)
163
164
165class DeletableSecurityGroupRule(DeletableResource):
166
167 def __repr__(self):
168 return '<%s id="%s">' % (self.__class__.__name__, self.id)
169
170 def delete(self):
171 self.client.delete_security_group_rule(self.id)
172
173
174class DeletablePool(DeletableResource):
175
176 def delete(self):
177 self.client.delete_pool(self.id)
178
179
180class DeletableMember(DeletableResource):
181
182 def delete(self):
183 self.client.delete_member(self.id)
184
185
186class DeletableVip(DeletableResource):
187
188 def delete(self):
189 self.client.delete_vip(self.id)
Miguel Lavalle02ba8cd2014-09-01 19:23:22 -0500190
Yair Fried45f92952014-06-26 05:19:19 +0300191 def refresh(self):
David Kranz34e88122014-12-11 15:24:05 -0500192 result = self.client.show_vip(self.id)
193 super(DeletableVip, self).update(**result['vip'])