query.py 6.17 KB
Newer Older
1
from zephir.i18n import _
2
3
from .error import ServerErrorUnknownServerId

4
5
6
7
8
9
10
11
12
"""
List all servers
"""

FETCH_ALL_SERVERS = '''
    SELECT serverid, servername, serverdescription, servermodelid, zoneid, machineid, automation, lastpeerconnection
    FROM server
 '''

Matthieu Lamalle's avatar
Acl    
Matthieu Lamalle committed
13
14
15
16
17
18
19
20
"""
Fetch one server based on its ID
"""
FETCH_SERVER_ENV = '''
    SELECT serverid, servername, serverdescription, servermodelid, zoneid, machineid, automation, serverenvironment, lastpeerconnection
    FROM server
    WHERE serverid = %s
'''
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
"""
Fetch one server based on its ID
"""
FETCH_SERVER = '''
    SELECT serverid, servername, serverdescription, servermodelid, zoneid, machineid, automation, serverenvironment, lastpeerconnection
    FROM server
    WHERE serverid = %s
'''

"""
Fetch a server automation type
"""
FETCH_SERVER_AUTOMATION = '''
    SELECT automation
    FROM server
    WHERE serverid = %s
'''

"""
Creates a server based on its name and servermodel
"""
SERVER_INSERT = '''
    INSERT INTO server (servername, serverdescription, servermodelid)
    VALUES (%s, %s, %s)
    RETURNING *
'''

"""
Updates a server name based on its ID
"""
SERVER_UPDATE = '''
    UPDATE server
    SET servername = %s,
        serverdescription = %s
    WHERE serverid = %s
    RETURNING *
'''

"""
Set environment content for given server
"""
SERVER_UPDATE_ENVIRONMENT = '''
    UPDATE server
    SET serverenvironment = %s
    WHERE serverid = %s
'''

"""
Set last peer connection for given server
"""
SERVER_UPDATE_PEERCONNECTION = '''
    UPDATE server
    SET lastpeerconnection = %s
    WHERE serverid = %s
'''

"""
Set configuration content for given server
"""
SERVER_UPDATE_CONFIGURATION = '''
    UPDATE server
    SET serverconfiguration = %s
    WHERE serverid = %s
'''
Bruno Boiget's avatar
Bruno Boiget committed
85
86
87
88
89
SERVER_UPDATE_CONFIGURATION_TD = '''
    UPDATE server
    SET serverconfigurationtd = %s
    WHERE serverid = %s
'''
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

"""
Get configuration content for given server
"""
FETCH_SERVER_CONFIGURATION = '''
    SELECT serverconfiguration FROM server
    WHERE serverid = %s
'''

"""
Set automation field to associate server with automation software
"""
SERVER_AUTOMATION_SET = '''
    UPDATE server
    SET automation = %s
    WHERE serverid = %s
'''

"""
Deletes a server based on its ID
"""
SERVER_DELETE = '''
    DELETE
    FROM server
    WHERE serverid = %s
    RETURNING *
'''

"""
Suppression du contenu de la table de jointure servermodelapplicationservice
"""
SERVER_ERASE = """DELETE from server"""


def fetchone(cursor, query: str, query_parameters: tuple=None, raises: bool=False):
    cursor.execute(query, query_parameters)
    fetched = cursor.fetchone()
    if not fetched:
        if raises:
            raise Exception(_("cannot find element with query {} and parameters {}").format(query, query_parameters))
        fetched = None
    return fetched


def server_row_to_dict(server, serverenvironment=False):
    server_obj = {'serverid': server['serverid'],
                  'servername': server['servername'],
                  'serverdescription': server['serverdescription'],
                  'servermodelid': server['servermodelid']}
    if server['lastpeerconnection'] is None:
        server_obj['lastpeerconnection'] = ""
    else:
        server_obj['lastpeerconnection'] = str(server['lastpeerconnection'])
    if serverenvironment:
        # Cas du describe : on veut le serverenvironment
        if server['serverenvironment'] is not None:
            server_obj['serverenvironment'] = server['serverenvironment']
        else:
            server_obj['serverenvironment'] = {}
    if server['automation'] is not None:
        server_obj['automation'] = server['automation']
    if server['zoneid'] is not None:
        server_obj['zoneid'] = server['zoneid']
    if server['machineid'] is not None:
        server_obj['machineid'] = server['machineid']
    return server_obj


def list_all_servers(cursor):
    cursor.execute(FETCH_ALL_SERVERS)
    ret = []
    for server in cursor.fetchall():
        ret.append(server_row_to_dict(server))
    return ret


Matthieu Lamalle's avatar
Acl    
Matthieu Lamalle committed
166
167
168
169
170
def fetch_server_dict(cursor, serverid: int, environment: bool):
    if environment:
        server = fetchone(cursor, FETCH_SERVER_ENV, (serverid,), raises=False)
    else:
        server = fetchone(cursor, FETCH_SERVER, (serverid,), raises=False)
171
172
    if server is None:
        raise ServerErrorUnknownServerId(_('Unable to find a server with ID {}').format(serverid))
Matthieu Lamalle's avatar
Acl    
Matthieu Lamalle committed
173
    return server_row_to_dict(server, serverenvironment=environment)
174
175
176
177
178


def fetch_server(cursor, serverid: int):
    fetched = fetchone(cursor, FETCH_SERVER, (serverid,))
    if fetched is None:
Matthieu Lamalle's avatar
Matthieu Lamalle committed
179
        raise ServerErrorUnknownServerId(_('unable to find a server with ID {}').format(serverid))
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    return fetched


def insert_server(cursor, servername, serverdescription, servermodelid):
    return server_row_to_dict(fetchone(cursor, SERVER_INSERT, (servername, serverdescription, servermodelid), raises=True))


def update_server(cursor, serverid, servername, serverdescription):
    return server_row_to_dict(fetchone(cursor, SERVER_UPDATE, (servername, serverdescription, serverid), raises=True))


def update_environment(cursor, serverid, serverenvironment):
    cursor.execute(SERVER_UPDATE_ENVIRONMENT, (serverenvironment, serverid))
    return cursor.statusmessage


Bruno Boiget's avatar
Bruno Boiget committed
196
197
198
199
200
def update_configuration(cursor, serverid, serverconfiguration, deploy):
    if deploy:
        cursor.execute(SERVER_UPDATE_CONFIGURATION, (serverconfiguration, serverid))
    else:
        cursor.execute(SERVER_UPDATE_CONFIGURATION_TD, (serverconfiguration, serverid))
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    return cursor.statusmessage


def update_peerconnection(cursor, serverid, lastpeerconnection):
    cursor.execute(SERVER_UPDATE_PEERCONNECTION, (lastpeerconnection, serverid))
    return cursor.statusmessage


def fetch_configuration(cursor, serverid):
    fetched = fetchone(cursor, FETCH_SERVER_CONFIGURATION, (serverid,), raises=True)
    return fetched


def delete_server(cursor, serverid):
    return server_row_to_dict(fetchone(cursor, SERVER_DELETE, (serverid,), raises=True))


def set_automation_value(cursor, serverid, automation):
    cursor.execute(SERVER_AUTOMATION_SET, (automation, serverid))
    return cursor.statusmessage


def erase_server(cursor):
    """
    Supprime le contenu de la table Server
    """
    cursor.execute(SERVER_ERASE)