Newer
Older
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
import logging as log
from sqlalchemy.orm import Session
from pilab.crud.Util import from_json, update_attrs
from pilab.crud.Host import Host
from pilab.schemas.Cube import CubesType
from pilab.schemas.Pi import PiType
from pilab.schemas.Hardware import SwitchType, ControllerType
from pilab.events import meta
log.getLogger('sqlalchemy.engine').setLevel(log.WARNING)
class Switch(object):
@staticmethod
def create(db: Session, switch: meta.Switch):#DONE
host = Host.create(db, switch)
db_switch = from_json(SwitchType, switch.dict(), host_id=host.id)
db.add(db_switch)
db.flush()
switch = meta.Switch(**vars(db_switch), **host.dict())
return switch
@staticmethod
def get(db: Session, host_id: int = None, cube_id: int = None, pi_id: int = None):
if host_id:
db_switch = db.query(SwitchType).filter(
SwitchType.host_id == host_id
).first()
elif cube_id:
db_cube = db.query(CubesType).filter(
CubesType.switch_id == host_id
).first()
db_switch = db.query(SwitchType).filter(
SwitchType.host_id == db_cube.switch_id
).first()
elif pi_id:
db_pi= db.query(PiType).filter(
PiType.host_id == pi_id
).first()
db_cube = db.query(CubesType).filter(
CubesType.id == db_pi.cube_id
).first()
db_switch = db.query(SwitchType).filter(
SwitchType.host_id == db_cube.switch_id
).first()
host = Host.get(db, db_switch.host_id)
return meta.Switch(**vars(db_switch), **host.dict())
@staticmethod#DONE
def delete(db: Session, host_id: int):
db_switch = db.query(SwitchType).filter(
SwitchType.host_id == host_id
).first()
db.delete(db_switch)
db.flush()
Host.delete(db, host_id)
@staticmethod
def update(db: Session, switch: meta.HostUpdate, _id: int):
host = Host.update(db, switch, _id)
return host
@staticmethod
def getAll(db: Session):
switches = []
db_switches = db.query(SwitchType).all()
for db_switch in db_switches:
host = Host.get(db, db_switch.host_id)
switches.append(meta.Switch(**vars(db_switch), **host.dict()))
return switches
class Controller(object):
@staticmethod
def create(db: Session, controller: meta.Controller):
host = Host.create(db, controller)
db_controller = from_json(ControllerType, controller.dict(), host_id=host.id)
db.add(db_controller)
db.flush()
controller = meta.Controller(**vars(db_controller), **host.dict())
return controller
@staticmethod#DONE
def delete(db: Session, host_id: int):
db_controller = db.query(ControllerType).filter(
ControllerType.host_id == host_id
).first()
db.delete(db_controller)
db.flush()
Host.delete(db, host_id)
@staticmethod
def get(db: Session, host_id: int = None, cube_id: int = None, pi_id: int = None):
if host_id:
db_controller = db.query(ControllerType).filter(
ControllerType.host_id == host_id
).first()
elif cube_id:
db_cube = db.query(CubesType).filter(
CubesType.controller_id == host_id
).first()
db_controller = db.query(ControllerType).filter(
ControllerType.host_id == db_cube.controller_id
).first()
elif pi_id:
db_pi= db.query(PiType).filter(
PiType.host_id == pi_id
).first()
db_cube = db.query(CubesType).filter(
CubesType.id == db_pi.cube_id
).first()
db_controller= db.query(ControllerType).filter(
ControllerType.host_id == db_cube.controller_id
).first()
host = Host.get(db, db_controller.host_id)
return meta.Controller(**vars(db_controller), **host.dict())
@staticmethod
def update(db: Session, controller: meta.HostUpdate, _id: int):
host = Host.update(db, controller, _id)
return host
@staticmethod
def getAll(db: Session):
controllers = []
db_controllers = db.query(ControllerType).all()
for db_controller in db_controllers:
host = Host.get(db, db_controller.host_id)
controllers.append(meta.Controller(**vars(db_controller), **host.dict()))
return controllers