Skip to content

Commit c4d2e8c

Browse files
committed
Update to add apiPath to GCP
1 parent d197c59 commit c4d2e8c

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

.github/workflows/data.yml

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ jobs:
3434
rm -rf google-api-go-client
3535
git clone --depth 1 https://github.com/googleapis/google-api-go-client.git
3636
rm -rf google-api-go-client/.git
37+
rm -rf google-cloud-node
38+
git clone --depth 1 https://github.com/googleapis/google-cloud-node.git
39+
rm -rf google-cloud-node/.git
3740
cd ..
3841
gcloud iam roles list --show-deleted --format json > gcp/predefined_roles.json
3942
basic_roles=$(for role in roles/reader roles/writer roles/admin; do
@@ -53,6 +56,7 @@ jobs:
5356
python3 util/gcp_compare_datasources.py || true
5457
python3 util/gcp_crawl.py || true
5558
rm -rf gcp/google-api-go-client
59+
rm -rf gcp/google-cloud-node
5660
rm gcreds
5761
git clone https://github.com/Azure/azure-rest-api-specs.git
5862
python3 util/azure_process_spec.py

util/gcp_get_methods.py

+72-10
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,50 @@
66

77
result = {}
88
result_ext = {}
9-
proto = {}
9+
proto = {
10+
"get": {},
11+
"post": {},
12+
"put": {},
13+
"delete": {},
14+
"patch": {}
15+
}
16+
17+
def strip_var(original_pattern, original_path):
18+
newpattern = re.sub(r'{[^/=]+?}', r'*', original_pattern)
19+
newpattern = re.sub(r'{.+?=(.+)}', r'\1', newpattern)
20+
offset = newpattern.find('/', newpattern.find('/v') + 1)
21+
version = newpattern[1:offset]
22+
newpattern = "*" + newpattern[offset:]
23+
newpath = original_path.replace(version, "{" + "_version}")
24+
25+
return newpattern, newpath
26+
27+
def dig_nested(obj, path):
28+
if 'nested' in obj:
29+
for k, v in obj['nested'].items():
30+
dig_nested(v, path + k + "/")
31+
if 'methods' in obj:
32+
for k, v in obj['methods'].items():
33+
if 'options' in v:
34+
if "(google.api.http).get" in v['options']:
35+
newpattern, newpath = strip_var(v['options']["(google.api.http).get"], path + k)
36+
proto['get'][newpattern] = newpath
37+
if "(google.api.http).post" in v['options']:
38+
newpattern, newpath = strip_var(v['options']["(google.api.http).post"], path + k)
39+
proto['post'][newpattern] = newpath
40+
if "(google.api.http).put" in v['options']:
41+
newpattern, newpath = strip_var(v['options']["(google.api.http).put"], path + k)
42+
proto['put'][newpattern] = newpath
43+
if "(google.api.http).delete" in v['options']:
44+
newpattern, newpath = strip_var(v['options']["(google.api.http).delete"], path + k)
45+
proto['delete'][newpattern] = newpath
46+
if "(google.api.http).patch" in v['options']:
47+
newpattern, newpath = strip_var(v['options']["(google.api.http).patch"], path + k)
48+
proto['patch'][newpattern] = newpath
1049

1150
def resources_recurse(resources, resource_type_path):
1251
for resource_type in resources.keys():
13-
print("-- " + resource_type)
52+
#print("-- " + resource_type)
1453
if 'methods' in resources[resource_type]:
1554
for method_name in resources[resource_type]['methods'].keys():
1655
method_id = resources[resource_type]['methods'][method_name]['id']
@@ -19,14 +58,26 @@ def resources_recurse(resources, resource_type_path):
1958
if flatpath.startswith(apidetail['version'] + "/"):
2059
flatpath = "{" + "_version}" + flatpath[len(apidetail['version']):]
2160

61+
normalized_flatpath = re.sub(r'{(.+?)}', r'*', flatpath)
62+
api_path = None
63+
if normalized_flatpath in proto[resources[resource_type]['methods'][method_name]['httpMethod'].lower()]:
64+
api_path = proto[resources[resource_type]['methods'][method_name]['httpMethod'].lower()][normalized_flatpath]
65+
del proto[resources[resource_type]['methods'][method_name]['httpMethod'].lower()][normalized_flatpath]
66+
elif method_id.startswith("compute."): # missing leading */
67+
normalized_flatpath = "*/" + normalized_flatpath
68+
if normalized_flatpath in proto[resources[resource_type]['methods'][method_name]['httpMethod'].lower()]:
69+
api_path = proto[resources[resource_type]['methods'][method_name]['httpMethod'].lower()][normalized_flatpath]
70+
del proto[resources[resource_type]['methods'][method_name]['httpMethod'].lower()][normalized_flatpath]
71+
2272
result[api['name']]['methods'][method_id] = {
2373
'description': resources[resource_type]['methods'][method_name]['description'] if 'description' in resources[resource_type]['methods'][method_name] else '',
2474
'httpMethod': resources[resource_type]['methods'][method_name]['httpMethod'],
2575
'method': method_name,
2676
'resourceType': resource_type,
2777
'resourceTypePath': resource_type_path,
2878
'flatPath': flatpath,
29-
'versions': []
79+
'versions': [],
80+
'apiPath': api_path,
3081
}
3182
result_ext[api['name']]['methods'][method_id] = {
3283
'description': resources[resource_type]['methods'][method_name]['description'] if 'description' in resources[resource_type]['methods'][method_name] else '',
@@ -36,6 +87,7 @@ def resources_recurse(resources, resource_type_path):
3687
'resourceTypePath': resource_type_path,
3788
'flatPath': flatpath,
3889
'versions': [],
90+
'apiPath': api_path,
3991
'parameters': resources[resource_type]['methods'][method_name]['parameters'] if 'parameters' in resources[resource_type]['methods'][method_name] else {}
4092
}
4193
result[api['name']]['methods'][method_id]['versions'].append(apidetail['version'])
@@ -47,6 +99,13 @@ def resources_recurse(resources, resource_type_path):
4799
with open("gcp/google-api-go-client/api-list.json", "r") as f:
48100
apilist = json.loads(f.read())['items']
49101

102+
for dirpath, dnames, fnames in os.walk("gcp/google-cloud-node/"):
103+
for fn in fnames:
104+
if fn == "protos.json":
105+
with open(os.path.join(dirpath, fn)) as f:
106+
singleproto = json.loads(f.read())
107+
dig_nested(singleproto, '')
108+
50109
for api in apilist:
51110
if api['name'] not in result:
52111
result[api['name']] = {
@@ -70,13 +129,16 @@ def resources_recurse(resources, resource_type_path):
70129
path_version = "v0.alpha"
71130
if path_version == "beta":
72131
path_version = "v0.beta"
73-
with open("gcp/google-api-go-client/{}/{}/{}-api.json".format(api['name'], path_version, api['name']), "r") as f:
74-
apidetail = json.loads(f.read())
75-
print(api['name'])
76-
print("- " + api['version'])
77-
resources_recurse({
78-
"": apidetail
79-
}, "")
132+
try:
133+
with open("gcp/google-api-go-client/{}/{}/{}-api.json".format(api['name'], path_version, api['name']), "r") as f:
134+
apidetail = json.loads(f.read())
135+
#print(api['name'])
136+
#print("- " + api['version'])
137+
resources_recurse({
138+
"": apidetail
139+
}, "")
140+
except Exception as e:
141+
print(e)
80142

81143
with open("gcp/methods.json", "w") as f:
82144
f.write(json.dumps(result, indent=2, sort_keys=True))

0 commit comments

Comments
 (0)