blob: 0ce1769ef71711361dd3e52a63e1809d114be4b6 [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
64 def add_to_router(self, router_id):
65 self._router_ids.add(router_id)
66 body = dict(subnet_id=self.id)
67 self.client.add_interface_router(router_id, body=body)
68
69 def delete(self):
70 for router_id in self._router_ids.copy():
71 body = dict(subnet_id=self.id)
72 self.client.remove_interface_router(router_id, body=body)
73 self._router_ids.remove(router_id)
74 self.client.delete_subnet(self.id)
75
76
77class DeletableRouter(DeletableResource):
78
79 def add_gateway(self, network_id):
80 body = dict(network_id=network_id)
81 self.client.add_gateway_router(self.id, body=body)
82
83 def delete(self):
84 self.client.remove_gateway_router(self.id)
85 self.client.delete_router(self.id)
86
87
88class DeletableFloatingIp(DeletableResource):
89
Yair Fried9a551c42013-12-15 14:59:34 +020090 def update(self, *args, **kwargs):
91 result = self.client.update_floatingip(floatingip=self.id,
92 body=dict(
93 floatingip=dict(*args,
94 **kwargs)
95 ))
96 super(DeletableFloatingIp, self).update(**result['floatingip'])
97
98 def __repr__(self):
99 return '<%s addr="%s">' % (self.__class__.__name__,
100 self.floating_ip_address)
101
102 def __str__(self):
Yair Fried05db2522013-11-18 11:02:10 +0200103 return '<"FloatingIP" addr="%s" id="%s">' % (self.floating_ip_address,
Yair Fried9a551c42013-12-15 14:59:34 +0200104 self.id)
105
Gavin Brebner0f465a32013-03-14 13:26:09 +0000106 def delete(self):
107 self.client.delete_floatingip(self.id)
108
109
110class DeletablePort(DeletableResource):
111
112 def delete(self):
113 self.client.delete_port(self.id)
Yair Friedeb69f3f2013-10-10 13:18:16 +0300114
115
116class DeletableSecurityGroup(DeletableResource):
117
118 def delete(self):
119 self.client.delete_security_group(self.id)
120
121
122class DeletableSecurityGroupRule(DeletableResource):
123
124 def __repr__(self):
125 return '<%s id="%s">' % (self.__class__.__name__, self.id)
126
127 def delete(self):
128 self.client.delete_security_group_rule(self.id)