-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.py
166 lines (139 loc) · 5.01 KB
/
queries.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
# -*- coding: utf-8 -*-
#
# 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 connection \
import Session
import models
def assign_rd_for(attr):
def assign_route_domain(func):
def warpper(db, *args, **kwargs):
ret = func(db, *args, **kwargs)
if attr == "loadbalancers":
assign_lbs_rd(db, ret)
if attr == "pools_members":
assign_pools_rd(db, ret)
return ret
return warpper
return assign_route_domain
def assign_lbs_rd(db, lbs):
for res in lbs:
subnet_id = res.subnet_id
rd = db.get_rd_by_subnet(subnet_id)
res.vip_address = res.vip_address + '%' + str(rd)
def assign_pools_rd(db, pools):
for pl in pools:
for mb in pl.members:
subnet_id = mb.subnet_id
rd = db.get_rd_by_subnet(subnet_id)
mb.address = mb.address + '%' + str(rd)
class Queries(object):
def __init__(self):
self.connection = models.con
self.lb = models.Loadbalancer
self.ls = models.Listener
self.pl = models.Pool
self.mn = models.Monitor
self.mb = models.Member
self.bindings = models.Loadbalanceragentbindings
self.subnet = models.Subnet
self.net = models.Network
# @assign_rd_for("loadbalancers")
def get_loadbalancers_by_agent_id(self, agent_id):
with Session(self.connection) as se:
# join is LEFT OUTER JOIN by default
ret = se.query(self.lb).join(self.bindings).filter(
self.bindings.agent_id == agent_id).all()
return ret
def get_loadbalancer(self, lb_id):
with Session(self.connection) as se:
ret = se.query(self.lb).get(lb_id)
return ret
def get_loadbalancers_by_project_id(self, pj_id):
with Session(self.connection) as se:
ret = se.query(self.lb).filter(
self.lb.project_id == pj_id
).all()
return ret
def get_listener(self, ls_id):
with Session(self.connection) as se:
ret = se.query(self.ls).get(ls_id)
return ret
def get_listeners_by_lb_id(self, lb_id):
with Session(self.connection) as se:
ret = se.query(self.ls).filter(
self.ls.loadbalancer_id == lb_id
).all()
return ret
def get_listeners_by_project_id(self, lb_id):
with Session(self.connection) as se:
ret = se.query(self.ls).filter(
self.ls.project_id == lb_id
).all()
return ret
def get_pool(self, pl_id):
with Session(self.connection) as se:
ret = se.query(self.pl).get(pl_id)
return ret
# @assign_rd_for("pools_members")
def get_pools_by_lb_id(self, lb_id):
with Session(self.connection) as se:
ret = se.query(self.pl).filter(
models.Pool.loadbalancer_id == lb_id
).all()
return ret
# @assign_rd_for("pools_members")
def get_pools_by_project_id(self, pj_id):
with Session(self.connection) as se:
ret = se.query(self.pl).filter(
models.Pool.project_id == pj_id
).all()
return ret
def get_mn(self, mn_id):
with Session(self.connection) as se:
ret = se.query(self.mn).get(mn_id)
return ret
def get_member(self, mb_id):
with Session(self.connection) as se:
ret = se.query(self.mb).get(mb_id)
return ret
def get_members_by_pool_id(self, pl_id):
with Session(self.connection) as se:
ret = se.query(self.mb).filter(
models.Member.pool_id == pl_id
).all()
return ret
def get_net(self, net_id):
with Session(self.connection) as se:
ret = se.query(self.net).get(
net_id)
return ret
def get_subnet(self, subnet_id):
with Session(self.connection) as se:
ret = se.query(self.subnet).get(
subnet_id)
return ret
def get_subnets_by_network_id(self, net_id):
with Session(self.connection) as se:
ret = se.query(self.subnet).filter(
models.Subnet.network_id == net_id
).all()
return ret
def get_rd_by_subnet(self, subnet_id):
with Session(self.connection) as se:
ret = se.query(self.subnet).get(
subnet_id
)
sgement = ret.network.segments[0]
ret = sgement.segmentation_id
return ret