blob: b2feb877631aee27d7acd2345fb135b4cfdc852a [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
54 def __hash__(self):
55 return hash(self.id)
56
57
58class DeletableNetwork(DeletableResource):
59
60 def delete(self):
61 self.client.delete_network(self.id)
62
63
64class DeletableSubnet(DeletableResource):
65
66 def __init__(self, *args, **kwargs):
67 super(DeletableSubnet, self).__init__(*args, **kwargs)
68 self._router_ids = set()
69
70 def update(self, *args, **kwargs):
71 _, result = self.client.update_subnet(subnet=self.id, *args, **kwargs)
72 super(DeletableSubnet, self).update(**result['subnet'])
73
74 def add_to_router(self, router_id):
75 self._router_ids.add(router_id)
76 self.client.add_router_interface_with_subnet_id(router_id,
77 subnet_id=self.id)
78
79 def delete(self):
80 for router_id in self._router_ids.copy():
81 self.client.remove_router_interface_with_subnet_id(
82 router_id,
83 subnet_id=self.id)
84 self._router_ids.remove(router_id)
85 self.client.delete_subnet(self.id)
86
87
88class DeletableRouter(DeletableResource):
89
90 def set_gateway(self, network_id):
91 return self.update(external_gateway_info=dict(network_id=network_id))
92
93 def unset_gateway(self):
94 return self.update(external_gateway_info=dict())
95
96 def update(self, *args, **kwargs):
97 _, result = self.client.update_router(self.id,
98 *args,
99 **kwargs)
100 return super(DeletableRouter, self).update(**result['router'])
101
102 def delete(self):
103 self.unset_gateway()
104 self.client.delete_router(self.id)
105
106
107class DeletableFloatingIp(DeletableResource):
108
109 def update(self, *args, **kwargs):
110 _, result = self.client.update_floatingip(self.id,
111 *args,
112 **kwargs)
113 super(DeletableFloatingIp, self).update(**result['floatingip'])
114
115 def __repr__(self):
116 return '<%s addr="%s">' % (self.__class__.__name__,
117 self.floating_ip_address)
118
119 def __str__(self):
120 return '<"FloatingIP" addr="%s" id="%s">' % (self.floating_ip_address,
121 self.id)
122
123 def delete(self):
124 self.client.delete_floatingip(self.id)
125
126
127class DeletablePort(DeletableResource):
128
129 def delete(self):
130 self.client.delete_port(self.id)
131
132
133class DeletableSecurityGroup(DeletableResource):
134
135 def delete(self):
136 self.client.delete_security_group(self.id)
137
138
139class DeletableSecurityGroupRule(DeletableResource):
140
141 def __repr__(self):
142 return '<%s id="%s">' % (self.__class__.__name__, self.id)
143
144 def delete(self):
145 self.client.delete_security_group_rule(self.id)
146
147
148class DeletablePool(DeletableResource):
149
150 def delete(self):
151 self.client.delete_pool(self.id)
152
153
154class DeletableMember(DeletableResource):
155
156 def delete(self):
157 self.client.delete_member(self.id)
158
159
160class DeletableVip(DeletableResource):
161
162 def delete(self):
163 self.client.delete_vip(self.id)