-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
212 lines (174 loc) · 7.42 KB
/
main.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import json
import sqlalchemy
from database import init_connection_engine
from googleapis import get_published_protos
from service_manager import ServiceManagerClient
from utils import is_cloud_service
def update_status_db(event, context):
print(
f"Successfully triggered by {context.event_id} published at \
{context.timestamp}."
)
# get all services from Service Manager
service_manager_client = ServiceManagerClient()
service_manager = service_manager_client.list()
service_manager_set = {x for x in service_manager}
# get all services with protos published to github/googleapis/googleapis
googleapis = get_published_protos()
googleapis_services = googleapis.keys()
googleapis_set = {x for x in googleapis_services}
# create a superset
all_services_set = service_manager_set | googleapis_set
# prepare SQL statement templates
stmt_select_service = sqlalchemy.text(
"SELECT * FROM services WHERE service_name=:service"
)
stmt_update_public = sqlalchemy.text(
"UPDATE services SET is_public=:in_service_manager, \
in_googleapis=:in_googleapis WHERE service_name=:service"
)
stmt_insert_service = sqlalchemy.text(
"INSERT INTO services (service_name, title, is_cloud, in_googleapis, \
is_public) VALUES (:service, :title, :is_cloud, :in_googleapis, \
:in_service_manager)"
)
stmt_select_star = sqlalchemy.text("SELECT * FROM services")
# initiate database connection
db = init_connection_engine()
with db.connect() as conn:
# iterate over superset of services
for service in all_services_set:
if service in googleapis:
in_googleapis = 1
else:
in_googleapis = 0
if service in service_manager:
in_service_manager = 1
else:
in_service_manager = 0
db_result = conn.execute(stmt_select_service, service=service).fetchone()
# if already in the db: refresh in_googleapis and is_public values
if db_result:
current_in_googleapis = db_result[4]
current_is_public = db_result[5]
if (in_googleapis != current_in_googleapis) or (
in_service_manager != current_is_public
):
conn.execute(
stmt_update_public,
in_googleapis=in_googleapis,
in_service_manager=in_service_manager,
service=service,
)
# if service is in service manager, but not the db: add it
elif service in service_manager:
service_config = service_manager_client.get(service)
is_cloud = is_cloud_service(service_config)
conn.execute(
stmt_insert_service,
service=service,
title=service_config.get("title"),
is_cloud=is_cloud,
in_googleapis=in_googleapis,
in_service_manager=in_service_manager,
)
print(
f"Inserted {service}: is_cloud:{is_cloud}, \
in_googleapis:{in_googleapis}, is_public:{in_service_manager})."
)
# service only exists in googleapis => assume private/allow-listed
# set is_cloud to 0 (conservative)
else:
is_cloud = 0
title = googleapis[service]["title"]
conn.execute(
stmt_insert_service,
service=service,
title=title,
is_cloud=is_cloud,
in_googleapis=in_googleapis,
in_service_manager=in_service_manager,
)
print(
f"Inserted {service}: is_cloud:unknown(0), in_googleapis:{in_googleapis}, \
is_public:{in_service_manager})."
)
# iterate over all services in the database to check for deprecated services
db_all = conn.execute(stmt_select_star).fetchall()
for entry in db_all:
service_name = entry[1]
in_googleapis = entry[4]
in_service_manager = entry[5]
# service is in db but not in service manager, or googleapis
if service_name not in all_services_set:
if in_googleapis == 1 or in_service_manager == 1:
in_googleapis = 0
in_service_manager = 0
conn.execute(
stmt_update_public,
in_googleapis=in_googleapis,
in_service_manager=in_service_manager,
service=service_name,
)
print(
f"Toggled {service_name}: is_public -> 0; in_googleapis -> 0."
)
print("Database update complete.")
def db_refresh_cloud_status(event, context):
# get all services (Cloud + non-Cloud) from Service Manager
service_manager = ServiceManagerClient()
services_list = service_manager.list()
# prepare SQL statement templates
stmt_select_service = sqlalchemy.text(
"SELECT * FROM services WHERE service_name=:service"
)
stmt_update_cloud = sqlalchemy.text(
"UPDATE services SET is_cloud=:is_cloud WHERE service_name=:service"
)
db = init_connection_engine()
with db.connect() as conn:
for service in services_list:
service_config = service_manager.get(service)
is_cloud = is_cloud_service(service_config)
db_result = conn.execute(stmt_select_service, service=service).fetchone()
if db_result:
current_cloud = db_result[3]
if current_cloud != is_cloud:
conn.execute(stmt_update_cloud, is_cloud=is_cloud, service=service)
print(
f"Toggled {service} (is_cloud): {current_cloud} -> {is_cloud}."
)
def reset_apis_tbl(event, context):
print(
f"Successfully triggered by {context.event_id} published at \
{context.timestamp}."
)
stmt_truncate_tbl = sqlalchemy.text("TRUNCATE TABLE apis")
stmt_select_star = sqlalchemy.text("SELECT * FROM services")
stmt_insert_apis = sqlalchemy.text(
"INSERT INTO apis (service_name, apis) VALUES (:service_name, :apis)"
)
# initiate database connection
db = init_connection_engine()
# instantiate Service Manager client
service_manager = ServiceManagerClient()
with db.connect() as conn:
# delete all entries in the table
conn.execute(stmt_truncate_tbl)
# fetch all known services
db_all = conn.execute(stmt_select_star).fetchall()
for record in db_all:
service_name = record[1]
service_config = service_manager.get(service_name)
apis = json.dumps(service_manager.parse_service_apis(service_config))
conn.execute(stmt_insert_apis, service_name=service_name, apis=apis)
# for local execution
if __name__ == "__main__":
# mock event dict
event = {}
# mock google.cloud.functions.Context
class Context:
event_id = None
timestamp = None
context = Context()
reset_apis_tbl(event, context)