/
octavia_v2.py
193 lines (161 loc) · 6.86 KB
/
octavia_v2.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from senlin.drivers import base
from senlin.drivers import sdk
class OctaviaClient(base.DriverBase):
"""Octavia v2 client"""
def __init__(self, params):
super(OctaviaClient, self).__init__(params)
self.conn = sdk.create_connection(params, service_type='load-balancer')
@sdk.translate_exception
def loadbalancer_get(self, name_or_id, ignore_missing=False,
show_deleted=False):
lb = self.conn.load_balancer.find_load_balancer(name_or_id,
ignore_missing)
# TODO(liyi)
# It's unreasonable for octavia don't support filter deleted
# loadbalancers. So if supported, we need to change the function.
if lb and not show_deleted and lb.provisioning_status == 'DELETED':
lb = None
return lb
@sdk.translate_exception
def loadbalancer_create(self, vip_subnet_id=None, vip_network_id=None,
vip_address=None, admin_state_up=True, name=None,
description=None, availability_zone=None,
flavor_id=None):
kwargs = {
'admin_state_up': admin_state_up,
}
if vip_subnet_id is not None:
kwargs['vip_subnet_id'] = vip_subnet_id
if vip_network_id is not None:
kwargs['vip_network_id'] = vip_network_id
if vip_address is not None:
kwargs['vip_address'] = vip_address
if name is not None:
kwargs['name'] = name
if description is not None:
kwargs['description'] = description
if availability_zone is not None:
kwargs['availability_zone'] = availability_zone
if flavor_id is not None:
kwargs['flavor_id'] = flavor_id
res = self.conn.load_balancer.create_load_balancer(**kwargs)
return res
@sdk.translate_exception
def loadbalancer_delete(self, lb_id, ignore_missing=True):
self.conn.load_balancer.delete_load_balancer(
lb_id, ignore_missing=ignore_missing)
return
@sdk.translate_exception
def listener_create(self, loadbalancer_id, protocol, protocol_port,
connection_limit=None,
admin_state_up=True, name=None, description=None):
kwargs = {
'loadbalancer_id': loadbalancer_id,
'protocol': protocol,
'protocol_port': protocol_port,
'admin_state_up': admin_state_up,
}
if connection_limit is not None:
kwargs['connection_limit'] = connection_limit
if name is not None:
kwargs['name'] = name
if description is not None:
kwargs['description'] = description
res = self.conn.load_balancer.create_listener(**kwargs)
return res
@sdk.translate_exception
def listener_delete(self, listener_id, ignore_missing=True):
self.conn.load_balancer.delete_listener(
listener_id, ignore_missing=ignore_missing)
return
@sdk.translate_exception
def pool_create(self, lb_algorithm, listener_id, protocol,
session_persistence, admin_state_up=True,
name=None, description=None):
# Remove cookie_name when type not equal to APP_COOKIE
if session_persistence.get('type') != 'APP_COOKIE':
session_persistence.pop('cookie_name', None)
# When type=NONE set session_persistence to None
if session_persistence.get('type') in ('NONE', None):
session_persistence = None
kwargs = {
'lb_algorithm': lb_algorithm,
'listener_id': listener_id,
'protocol': protocol,
'session_persistence': session_persistence,
'admin_state_up': admin_state_up,
}
if name is not None:
kwargs['name'] = name
if description is not None:
kwargs['description'] = description
res = self.conn.load_balancer.create_pool(**kwargs)
return res
@sdk.translate_exception
def pool_delete(self, pool_id, ignore_missing=True):
self.conn.load_balancer.delete_pool(
pool_id, ignore_missing=ignore_missing)
return
@sdk.translate_exception
def pool_member_create(self, name, pool_id, address, protocol_port,
subnet_id, weight=None, admin_state_up=True):
kwargs = {
'name': name,
'address': address,
'protocol_port': protocol_port,
'admin_state_up': admin_state_up,
'subnet_id': subnet_id,
}
if weight is not None:
kwargs['weight'] = weight
res = self.conn.load_balancer.create_member(pool_id, **kwargs)
return res
@sdk.translate_exception
def pool_member_delete(self, pool_id, member_id, ignore_missing=True):
self.conn.load_balancer.delete_member(
member_id, pool_id, ignore_missing=ignore_missing)
return
@sdk.translate_exception
def healthmonitor_create(self, hm_type, delay, timeout, max_retries,
pool_id, admin_state_up=True,
http_method=None, url_path=None,
expected_codes=None):
kwargs = {
'type': hm_type,
'delay': delay,
'timeout': timeout,
'max_retries': max_retries,
'pool_id': pool_id,
'admin_state_up': admin_state_up,
}
# TODO(anyone): verify if this is correct
if hm_type == 'HTTP':
if http_method is not None:
kwargs['http_method'] = http_method
if url_path is not None:
kwargs['url_path'] = url_path
if expected_codes is not None:
kwargs['expected_codes'] = expected_codes
res = self.conn.load_balancer.create_health_monitor(**kwargs)
return res
@sdk.translate_exception
def healthmonitor_delete(self, hm_id, ignore_missing=True):
self.conn.load_balancer.delete_health_monitor(
hm_id, ignore_missing=ignore_missing)
return
@sdk.translate_exception
def find_flavor(self, flavor_id, ignore_missing=False):
self.conn.load_balancer.find_flavor(
flavor_id, ignore_missing=ignore_missing)
return