1 | # -*- coding: utf-8 -*- |
---|
2 | ################################################################################# |
---|
3 | # @file opengngys.py |
---|
4 | # @brief Module that get and store credential for access Rest API opengnsys and |
---|
5 | # get Opengnsys OUs. Suply global access to server opengnsys entity. |
---|
6 | # import vs singleton pattern. |
---|
7 | # @warning None |
---|
8 | # @note Use: None |
---|
9 | # @license GNU GPLv3+ |
---|
10 | # @author David Fuertes, EUPT, University of Zaragoza. |
---|
11 | # Opengnsys Api Rest support provided by Juan Carlos García, EUPT, University of Zaragoza. |
---|
12 | # @version 1.1.0 - First version |
---|
13 | # @date 2019-15-11 |
---|
14 | ################################################################################# |
---|
15 | |
---|
16 | import threading |
---|
17 | |
---|
18 | |
---|
19 | from http_requests import HttpRequest, UsingPoolManagerConnector |
---|
20 | from ognsys_actions import DoLogin, GetOUS, GetLabsOu |
---|
21 | import ognsys_globals |
---|
22 | import ou |
---|
23 | from ados import adoDB_ous |
---|
24 | |
---|
25 | |
---|
26 | class Ognsys: |
---|
27 | |
---|
28 | def __init__(self, db): |
---|
29 | self.db = db |
---|
30 | |
---|
31 | ognsys_globals.init(db) |
---|
32 | |
---|
33 | |
---|
34 | self.http_request = HttpRequest() |
---|
35 | |
---|
36 | # ---- Return a list of dictionaries {OU_ID: OU_NAME} --- |
---|
37 | def get_ous(self): |
---|
38 | self.http_request.set_connector(UsingPoolManagerConnector(ognsys_globals.local.__POOL_MANAGER__)) |
---|
39 | return self.http_request.do_action(GetOUS()) |
---|
40 | |
---|
41 | |
---|
42 | def set_apikey(self, ou_id): |
---|
43 | |
---|
44 | ou_credentials = ou.get_ou_credentials(self.db, ou_id) |
---|
45 | |
---|
46 | if ou_credentials: |
---|
47 | |
---|
48 | self.http_request.set_connector(UsingPoolManagerConnector(ognsys_globals.local.__POOL_MANAGER__)) |
---|
49 | credentials = self.http_request.do_action(DoLogin(ou_credentials)) |
---|
50 | |
---|
51 | if 'apikey' in credentials: |
---|
52 | ognsys_globals.set_apikey(credentials['apikey']) |
---|
53 | |
---|
54 | return True |
---|
55 | else: |
---|
56 | |
---|
57 | return False |
---|
58 | else: |
---|
59 | return False |
---|
60 | |
---|
61 | def get_labs(self): |
---|
62 | ous = self.get_ous() |
---|
63 | |
---|
64 | self.http_request.set_connector(UsingPoolManagerConnector(ognsys_globals.local.__POOL_MANAGER__)) |
---|
65 | |
---|
66 | labs = {} |
---|
67 | for ou in ous: |
---|
68 | self.set_apikey(ou['id']) |
---|
69 | labs_ognsys = self.http_request.do_action(GetLabsOu(ou['id'])) |
---|
70 | labs_ognsys_on = [] |
---|
71 | |
---|
72 | if isinstance(labs_ognsys, list): |
---|
73 | for lab in labs_ognsys: |
---|
74 | if lab['inremotepc'] == True: |
---|
75 | labs_ognsys_on.append(lab) |
---|
76 | |
---|
77 | labs[ou['name']] = sorted(labs_ognsys_on, key = lambda i: i['name']) |
---|
78 | |
---|
79 | |
---|
80 | return labs |
---|
81 | |
---|
82 | def synchronize_table_ous(self, db): |
---|
83 | ou = self.get_ous() |
---|
84 | adoDB_ous.update_ous_in_table(db, ou) |
---|
85 | adoDB_ous.delete_ous_not_in_ous_server(db, ou) |
---|
86 | |
---|
87 | |
---|
88 | def get_query_ous(self, db): |
---|
89 | return adoDB_ous.get_queryOUs(db) |
---|
90 | |
---|
91 | |
---|