blob: 97e120f64c74aafcc1734aa8c30275e8646525cf [file] [log] [blame]
Gavin Brebner0f465a32013-03-14 13:26:09 +00001# 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
Gavin Brebner0f465a32013-03-14 13:26:09 +000016
17class AttributeDict(dict):
18
19 """
20 Provide attribute access (dict.key) to dictionary values.
21 """
22
23 def __getattr__(self, name):
24 """Allow attribute access for all keys in the dict."""
25 if name in self:
26 return self[name]
27 return super(AttributeDict, self).__getattribute__(name)
28
29
30class DeletableResource(AttributeDict):
31
32 """
Mark McClainf2982e82013-07-06 17:48:03 -040033 Support deletion of neutron resources (networks, subnets) via a
Gavin Brebner0f465a32013-03-14 13:26:09 +000034 delete() method, as is supported by keystone and nova resources.
35 """
36
37 def __init__(self, *args, **kwargs):
38 self.client = kwargs.pop('client', None)
39 super(DeletableResource, self).__init__(*args, **kwargs)
40
41 def __str__(self):
42 return '<%s id="%s" name="%s">' % (self.__class__.__name__,
43 self.id, self.name)
44
45 def delete(self):
46 raise NotImplemented()
47
Yair Fried9a551c42013-12-15 14:59:34 +020048 def __hash__(self):
49 return id(self)
50
Gavin Brebner0f465a32013-03-14 13:26:09 +000051
52class DeletableNetwork(DeletableResource):
53
54 def delete(self):
55 self.client.delete_network(self.id)
56
57
58class DeletableSubnet(DeletableResource):
59
Yair Friedca1d9a92013-10-22 11:52:34 +030060 def __init__(self, *args, **kwargs):
61 super(DeletableSubnet, self).__init__(*args, **kwargs)
62 self._router_ids = set()
Gavin Brebner0f465a32013-03-14 13:26:09 +000063
Yair Fried06552292013-11-11 12:10:09 +020064 def update(self, *args, **kwargs):
65 body = dict(subnet=dict(*args, **kwargs))
66 result = self.client.update_subnet(subnet=self.id, body=body)
67 super(DeletableSubnet, self).update(**result['subnet'])
68
Gavin Brebner0f465a32013-03-14 13:26:09 +000069 def add_to_router(self, router_id):
70 self._router_ids.add(router_id)
71 body = dict(subnet_id=self.id)
72 self.client.add_interface_router(router_id, body=body)
73
74 def delete(self):
75 for router_id in self._router_ids.copy():
76 body = dict(subnet_id=self.id)
77 self.client.remove_interface_router(router_id, body=body)
78 self._router_ids.remove(router_id)
79 self.client.delete_subnet(self.id)
80
81
82class DeletableRouter(DeletableResource):
83
84 def add_gateway(self, network_id):
85 body = dict(network_id=network_id)
86 self.client.add_gateway_router(self.id, body=body)
87
88 def delete(self):
89 self.client.remove_gateway_router(self.id)
90 self.client.delete_router(self.id)
91
92
93class DeletableFloatingIp(DeletableResource):
94
Yair Fried9a551c42013-12-15 14:59:34 +020095 def update(self, *args, **kwargs):
96 result = self.client.update_floatingip(floatingip=self.id,
97 body=dict(
98 floatingip=dict(*args,
99 **kwargs)
100 ))
101 super(DeletableFloatingIp, self).update(**result['floatingip'])
102
103 def __repr__(self):
104 return '<%s addr="%s">' % (self.__class__.__name__,
105 self.floating_ip_address)
106
107 def __str__(self):
Yair Fried05db2522013-11-18 11:02:10 +0200108 return '<"FloatingIP" addr="%s" id="%s">' % (self.floating_ip_address,
Yair Fried9a551c42013-12-15 14:59:34 +0200109 self.id)
110
Gavin Brebner0f465a32013-03-14 13:26:09 +0000111 def delete(self):
112 self.client.delete_floatingip(self.id)
113
114
115class DeletablePort(DeletableResource):
116
117 def delete(self):
118 self.client.delete_port(self.id)
Yair Friedeb69f3f2013-10-10 13:18:16 +0300119
120
121class DeletableSecurityGroup(DeletableResource):
122
123 def delete(self):
124 self.client.delete_security_group(self.id)
125
126
127class DeletableSecurityGroupRule(DeletableResource):
128
129 def __repr__(self):
130 return '<%s id="%s">' % (self.__class__.__name__, self.id)
131
132 def delete(self):
133 self.client.delete_security_group_rule(self.id)
Elena Ezhovaa5105e62013-11-26 20:46:52 +0400134
135
136class DeletablePool(DeletableResource):
137
138 def delete(self):
139 self.client.delete_pool(self.id)
140
141
142class DeletableMember(DeletableResource):
143
144 def delete(self):
145 self.client.delete_member(self.id)
146
147
148class DeletableVip(DeletableResource):
149
150 def delete(self):
151 self.client.delete_vip(self.id)