/
loadbalancer_healthmonitor.py
131 lines (106 loc) · 4.64 KB
/
loadbalancer_healthmonitor.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
#
# Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
#
import uuid
from neutron_lbaas.extensions import loadbalancerv2
try:
from neutron.openstack.common import uuidutils
except ImportError:
from oslo_utils import uuidutils
from vnc_api.vnc_api import IdPermsType
from vnc_api.vnc_api import LoadbalancerHealthmonitor
from vnc_api.vnc_api import LoadbalancerHealthmonitorType
from .. resource_manager import ResourceManager
class LoadbalancerHealthmonitorManager(ResourceManager):
_loadbalancer_health_type_mapping = {
'admin_state': 'admin_state_up',
'monitor_type': 'type',
'delay': 'delay',
'timeout': 'timeout',
'max_retries': 'max_retries',
'http_method': 'http_method',
'url_path': 'url_path',
'expected_codes': 'expected_codes'
}
@property
def property_type_mapping(self):
return self._loadbalancer_health_type_mapping
def make_properties(self, healthmonitor):
props = LoadbalancerHealthmonitorType()
for key, mapping in self._loadbalancer_health_type_mapping.iteritems():
if mapping in healthmonitor:
setattr(props, key, healthmonitor[mapping])
return props
def make_dict(self, healthmonitor, fields=None):
res = {'id': healthmonitor.uuid,
'tenant_id': healthmonitor.parent_uuid.replace('-', ''),
'status': self._get_object_status(healthmonitor)}
props = healthmonitor.get_loadbalancer_healthmonitor_properties()
monitor_type = getattr(props, 'monitor_type')
for key, mapping in self._loadbalancer_health_type_mapping.iteritems():
value = getattr(props, key)
if value is not None:
if monitor_type not in ('HTTP', 'HTTPS'):
if mapping in ('http_method', 'url_path', 'expected_codes'):
continue
res[mapping] = value
pool_ids = []
pool_back_refs = healthmonitor.get_loadbalancer_pool_back_refs()
for pool_back_ref in pool_back_refs or []:
pool_id = {}
pool_id['pool_id'] = pool_back_ref['uuid']
pool_ids.append(pool_id)
res['pools'] = pool_ids
return self._fields(res, fields)
def resource_read(self, id):
return self._api.loadbalancer_healthmonitor_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_healthmonitors_list(parent_id=parent_id)
def resource_update(self, obj):
return self._api.loadbalancer_healthmonitor_update(obj)
def resource_delete(self, id):
return self._api.loadbalancer_healthmonitor_delete(id=id)
def get_exception_notfound(self, id=None):
return loadbalancer.HealthMonitorNotFound(monitor_id=id)
def get_exception_inuse(self, id=None):
return loadbalancer.HealthMonitorInUse(monitor_id=id)
@property
def neutron_name(self):
return "healthmonitor"
@property
def resource_name_plural(self):
return "loadbalancer-healthmonitors"
def create(self, context, healthmonitor):
"""
Create a loadbalancer_healtmonitor object.
"""
m = healthmonitor['healthmonitor']
tenant_id = self._get_tenant_id_for_create(context, m)
project = self._project_read(project_id=tenant_id)
uuid = uuidutils.generate_uuid()
props = self.make_properties(m)
id_perms = IdPermsType(enable=True)
monitor_db = LoadbalancerHealthmonitor(
uuid, project, loadbalancer_healthmonitor_properties=props,
id_perms=id_perms)
monitor_db.uuid = uuid
self._api.loadbalancer_healthmonitor_create(monitor_db)
self._api.ref_update('loadbalancer-pool', m['pool_id'],
'loadbalancer-health-monitor', uuid, None, 'ADD')
return self.make_dict(monitor_db)
def delete(self, context, id):
hm_obj = self._api.loadbalancer_healthmonitor_read(id=id)
for pool_back_refs in hm_obj.get_loadbalancer_pool_back_refs() or []:
self._api.ref_update('loadbalancer-pool', pool_back_refs['uuid'],
'loadbalancer-health-monitor', id, None, 'DELETE')
super(LoadbalancerHealthmonitorManager, self).delete(context, id)
def update_properties(self, monitor_db, id, m):
props = monitor_db.get_loadbalancer_healthmonitor_properties()
if self.update_properties_subr(props, m):
monitor_db.set_loadbalancer_healthmonitor_properties(props)
return True
return False