-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_manager.py
110 lines (92 loc) · 3.85 KB
/
service_manager.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
import json
import os
import backoff
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
def backoff_hdlr(details):
print(
" // Backing off {wait:0.1f} seconds afters {tries} tries.\r".format(**details)
)
def success_hdlr(details):
print(" // Success")
class ServiceManagerClient:
client = None
def __init__(self):
self.client = build("servicemanagement", "v1").services()
@backoff.on_exception(backoff.expo, HttpError, on_backoff=backoff_hdlr)
def list(self):
services = []
request = self.client.list()
print("Service Manager: fetching services ... ")
while request is not None:
response = request.execute()
for item in response["services"]:
services.append(item["serviceName"])
request = self.client.list_next(request, response)
print(f"Service Manager: found {len(services)} services.")
return services
@backoff.on_exception(
backoff.expo, HttpError, on_backoff=backoff_hdlr, on_success=success_hdlr
)
def get(self, service_name):
print(f"Fetching config for service: {service_name}", end="")
try:
service_config = self.client.getConfig(serviceName=service_name).execute()
return service_config
except HttpError as he:
if he.resp.status == 403:
return None
else:
raise HttpError(he.resp, he.content)
def parse_service_apis(self, service_config):
# service_name = service_config.get("name")
# service_shortname = service_name[0 : service_name.find(".")]
# apis = []
if service_config and service_config.get("apis"):
return service_config.get("apis")
# for api in service_config.get("apis"):
# api_version = api.get("version")
# api_name = api.get("name")
# # only keep service-specific apis
# if api_name.find(service_shortname) == -1:
# continue
# if api_name not in apis:
# apis.append((api_version, api_name))
# return apis
def download_service_config_json(self, service_config):
cdir = os.getcwd() + "/service_configs/"
service_name = service_config.get("name")
short_name = service_name[0 : service_name.find(".googleapis.com")]
with open(cdir + short_name + ".json", "w") as fp:
fp.write(json.dumps(service_config, sort_keys=True, indent=4))
def create_apis_json(self, service_list):
all_apis = {}
cdir = os.getcwd() + "/service_configs/"
for service in service_list:
service_name = service[0 : service.find(".googleapis.com")]
try:
with open(cdir + service_name + ".json", "r") as fp:
fp.close()
except (FileNotFoundError):
try:
new_service_config = sm.get(service)
self.download_service_config_json(new_service_config)
except (Exception):
continue
try:
with open(cdir + service_name + ".json", "r") as fp:
service_config = json.loads(fp.read())
service_apis = self.parse_service_apis(service_config)
for api in service_apis:
all_apis[api] = service
except (FileNotFoundError):
continue
return json.dumps(all_apis, indent=4, sort_keys=True)
if __name__ == "__main__":
import pprint
sm = ServiceManagerClient()
redis = sm.get("multiclusteringress.googleapis.com")
redis_apis = sm.parse_service_apis(redis)
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(redis_apis)
# apis = sm.parse_service_apis(config)