-
Notifications
You must be signed in to change notification settings - Fork 69
/
listener.py
162 lines (131 loc) · 5.39 KB
/
listener.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#
# Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
#
from neutron.api.v2 import attributes
from neutron.common import exceptions as n_exc
from neutron_lbaas.extensions import loadbalancerv2
try:
from neutron.openstack.common import log as logging
except ImportError:
from oslo_log import log as logging
try:
from neutron.openstack.common import uuidutils
except ImportError:
from oslo_utils import uuidutils
from vnc_api.vnc_api import IdPermsType, NoIdError
from vnc_api.vnc_api import InstanceIp, VirtualMachineInterface
from vnc_api.vnc_api import SecurityGroup
from vnc_api.vnc_api import LoadbalancerListener, LoadbalancerListenerType
from .. resource_manager import ResourceManager, EntityInUse
from .. import utils
import uuid
class ListenerManager(ResourceManager):
_listener_type_mapping = {
'protocol': 'protocol',
'protocol_port': 'protocol_port',
'admin_state': 'admin_state_up',
}
@property
def property_type_mapping(self):
return self._listener_type_mapping
def make_properties(self, lb):
props = LoadbalancerListenerType()
for key, mapping in self._listener_type_mapping.iteritems():
if mapping in lb and lb[mapping] != attributes.ATTR_NOT_SPECIFIED:
setattr(props, key, lb[mapping])
return props
def _get_loadbalancers(self, ll):
lb_refs = ll.get_loadbalancer_refs()
if lb_refs is None:
return None
return [lb_refs[0]['uuid']]
def make_dict(self, ll, fields=None):
props = ll.get_loadbalancer_listener_properties()
res = {'id': ll.uuid,
'tenant_id': ll.parent_uuid.replace('-', ''),
'name': ll.display_name,
'description': self._get_object_description(ll),
'protocol': props.protocol,
'protocol_port': props.protocol_port,
'admin_state_up': props.admin_state,
'loadbalancers': self._get_loadbalancers(ll)}
return self._fields(res, fields)
def resource_read(self, id):
return self._api.loadbalancer_listener_read(id=id)
def resource_list(self, tenant_id=None):
if tenant_id:
parent_id = str(uuid.UUID(tenant_id))
else:
parent_id = None
return self._api.loadbalancer_listeners_list(parent_id=parent_id)
def resource_update(self, obj):
return self._api.loadbalancer_listener_update(obj)
def resource_delete(self, id):
return self._api.loadbalancer_listener_delete(id=id)
def get_exception_notfound(self, id=None):
return loadbalancerv2.EntityNotFound(name=self.neutron_name, id=id)
def get_exception_inuse(self, id=None):
return EntityInUse(name=self.neutron_name, id=id)
@property
def neutron_name(self):
return "listener"
@property
def resource_name_plural(self):
return "loadbalancer-listeners"
def create(self, context, listener):
l = listener['listener']
tenant_id = self._get_tenant_id_for_create(context, l)
project = self._project_read(project_id=tenant_id)
if l['loadbalancer_id']:
try:
lb = self._api.loadbalancer_read(id=l['loadbalancer_id'])
except NoIdError:
raise loadbalancerv2.EntityNotFound(name='Loadbalancer',
id=v['loadbalancer_id'])
project_id = lb.parent_uuid
if str(uuid.UUID(tenant_id)) != project_id:
raise n_exc.NotAuthorized()
else:
lb = None
obj_uuid = uuidutils.generate_uuid()
name = self._get_resource_name('listener', project,
l['name'], obj_uuid)
id_perms = IdPermsType(enable=True, description=l['description'])
ll = LoadbalancerListener(name, project, id_perms=id_perms,
display_name=l['name'])
ll.uuid = obj_uuid
if lb:
ll.set_loadbalancer(lb)
props = self.make_properties(l)
ll.set_loadbalancer_listener_properties(props)
self._api.loadbalancer_listener_create(ll)
return self.make_dict(ll)
def delete_listener(self, context, id):
try:
ll = self._api.loadbalancer_listener_read(id=id)
except NoIdError:
loadbalancerv2.EntityNotFound(name=self.neutron_name, id=id)
super(ListenerManager, self).delete(context, id)
def _update_listener_properties(self, props, id, ll):
"""
Update listener properties and return True if the have been
modified
"""
# according to the spec:
# port and protocol are immutable
immutable = ['protocol', 'protocol_port']
for field in immutable:
if field not in ll:
continue
if getattr(props, field) != ll[field]:
msg = 'Attribute %s in listener %s is immutable' % (field, id)
raise n_exc.BadRequest(resource='listener', msg=msg)
# update
change = self.update_properties_subr(props, ll)
return change
def update_properties(self, ll_db, id, ll):
props = ll_db.get_loadbalancer_listener_properties()
if self._update_listener_properties(props, id, ll):
ll_db.set_loadbalancer_listener_properties(props)
return True
return False