blob: 2b182d03e767d2b37933228ee79e00138c32bdb9 [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)
44 super(DeletableResource, self).__init__(*args, **kwargs)
45
46 def __str__(self):
47 return '<%s id="%s" name="%s">' % (self.__class__.__name__,
48 self.id, self.name)
49
50 @abc.abstractmethod
51 def delete(self):
52 return
53
Miguel Lavalle02ba8cd2014-09-01 19:23:22 -050054 @abc.abstractmethod
55 def show(self):
56 return
57
Yair Fried1fc32a12014-08-04 09:11:30 +030058 def __hash__(self):
59 return hash(self.id)
60
Miguel Lavalle02ba8cd2014-09-01 19:23:22 -050061 def wait_for_status(self, status):
62 if not hasattr(self, 'status'):
63 return
64
65 return self.client.wait_for_resource_status(self.show, status)
66
Yair Fried1fc32a12014-08-04 09:11:30 +030067
68class DeletableNetwork(DeletableResource):
69
70 def delete(self):
71 self.client.delete_network(self.id)
72
73
74class DeletableSubnet(DeletableResource):
75
76 def __init__(self, *args, **kwargs):
77 super(DeletableSubnet, self).__init__(*args, **kwargs)
78 self._router_ids = set()
79
80 def update(self, *args, **kwargs):
81 _, result = self.client.update_subnet(subnet=self.id, *args, **kwargs)
82 super(DeletableSubnet, self).update(**result['subnet'])
83
84 def add_to_router(self, router_id):
85 self._router_ids.add(router_id)
86 self.client.add_router_interface_with_subnet_id(router_id,
87 subnet_id=self.id)
88
89 def delete(self):
90 for router_id in self._router_ids.copy():
91 self.client.remove_router_interface_with_subnet_id(
92 router_id,
93 subnet_id=self.id)
94 self._router_ids.remove(router_id)
95 self.client.delete_subnet(self.id)
96
97
98class DeletableRouter(DeletableResource):
99
100 def set_gateway(self, network_id):
101 return self.update(external_gateway_info=dict(network_id=network_id))
102
103 def unset_gateway(self):
104 return self.update(external_gateway_info=dict())
105
106 def update(self, *args, **kwargs):
107 _, result = self.client.update_router(self.id,
108 *args,
109 **kwargs)
110 return super(DeletableRouter, self).update(**result['router'])
111
112 def delete(self):
113 self.unset_gateway()
114 self.client.delete_router(self.id)
115
116
117class DeletableFloatingIp(DeletableResource):
118
119 def update(self, *args, **kwargs):
120 _, result = self.client.update_floatingip(self.id,
121 *args,
122 **kwargs)
123 super(DeletableFloatingIp, self).update(**result['floatingip'])
124
125 def __repr__(self):
126 return '<%s addr="%s">' % (self.__class__.__name__,
127 self.floating_ip_address)
128
129 def __str__(self):
130 return '<"FloatingIP" addr="%s" id="%s">' % (self.floating_ip_address,
131 self.id)
132
133 def delete(self):
134 self.client.delete_floatingip(self.id)
135
136
137class DeletablePort(DeletableResource):
138
139 def delete(self):
140 self.client.delete_port(self.id)
141
142
143class DeletableSecurityGroup(DeletableResource):
144
145 def delete(self):
146 self.client.delete_security_group(self.id)
147
148
149class DeletableSecurityGroupRule(DeletableResource):
150
151 def __repr__(self):
152 return '<%s id="%s">' % (self.__class__.__name__, self.id)
153
154 def delete(self):
155 self.client.delete_security_group_rule(self.id)
156
157
158class DeletablePool(DeletableResource):
159
160 def delete(self):
161 self.client.delete_pool(self.id)
162
163
164class DeletableMember(DeletableResource):
165
166 def delete(self):
167 self.client.delete_member(self.id)
168
169
170class DeletableVip(DeletableResource):
171
172 def delete(self):
173 self.client.delete_vip(self.id)
Miguel Lavalle02ba8cd2014-09-01 19:23:22 -0500174
175 def show(self):
176 _, result = self.client.show_vip(self.id)
177 super(DeletableVip, self).update(**result['vip'])
178 return self