diff --git a/synthtool/languages/common.py b/synthtool/languages/common.py new file mode 100644 index 000000000..8e91075f3 --- /dev/null +++ b/synthtool/languages/common.py @@ -0,0 +1,52 @@ +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +from pathlib import Path +import re + + +def update_library_version(version: str, root_dir: str): + """ + Rewrites all metadata files in ./samples/generated to the version number argument + + """ + root_dir_path = Path(root_dir) + + snippet_metadata_files = get_sample_metadata_files(root_dir_path) + for file in snippet_metadata_files: + with open(file, "r+") as f: + data = json.load(f) + data["clientLibrary"]["version"] = version + f.seek(0) + json.dump(data, f, indent=4) + f.truncate() + + +def get_sample_metadata_files(dir: Path): + """ + Walks through samples/generated to find all snippet metadata files, appends them to a list + + Returns: + A list of all metadata files. + """ + metadata_files = [] + for path_object in dir.glob("**/*"): + if path_object.is_file(): + if re.search(r"snippet_metadata", str(path_object)): + metadata_files.append(str(Path.resolve(path_object))) + if path_object.is_dir(): + get_sample_metadata_files(path_object) + + return metadata_files diff --git a/synthtool/languages/node.py b/synthtool/languages/node.py index 281c9d4cd..ee27ae8f7 100644 --- a/synthtool/languages/node.py +++ b/synthtool/languages/node.py @@ -23,9 +23,11 @@ from typing import Any, Dict, List, Optional, Callable import logging import shutil +from synthtool.languages import common _REQUIRED_FIELDS = ["name", "repository", "engines"] _TOOLS_DIRECTORY = "/synthtool" +_GENERATED_SAMPLES_DIRECTORY = "./samples/generated" def read_metadata(): @@ -338,6 +340,9 @@ def owlbot_main( templates = common_templates.node_library(source_location="build/src") s_copy([templates], excludes=templates_excludes) + library_version = template_metadata()["version"] + common.update_library_version(library_version, _GENERATED_SAMPLES_DIRECTORY) + if __name__ == "__main__": owlbot_main() diff --git a/tests/fixtures/samples/generated/v1/snippet_metadata.google.cloud.asset.v1.json b/tests/fixtures/samples/generated/v1/snippet_metadata.google.cloud.asset.v1.json new file mode 100644 index 000000000..0f5508662 --- /dev/null +++ b/tests/fixtures/samples/generated/v1/snippet_metadata.google.cloud.asset.v1.json @@ -0,0 +1,127 @@ +{ + "clientLibrary": { + "name": "nodejs-asset", + "version": "0.1.0", + "language": "TYPESCRIPT", + "apis": [ + { + "id": "google.cloud.asset.v1p1beta1", + "version": "v1" + } + ] + }, + "snippets": [ + { + "regionTag": "cloudasset_v1p1beta1_generated_AssetService_SearchAllResources_async", + "title": "AssetService searchAllResources Sample", + "origin": "API_DEFINITION", + "description": " Searches all the resources under a given accessible CRM scope (project/folder/organization). This RPC gives callers especially admins the ability to search all the resources under a scope, even if they don't have .get permission of all the resources. Callers should have cloud.assets.SearchAllResources permission on the requested scope, otherwise it will be rejected.", + "canonical": true, + "file": "asset_service.search_all_resources.js", + "language": "JAVASCRIPT", + "segments": [ + { + "start": 25, + "end": 87, + "type": "FULL" + } + ], + "clientMethod": { + "shortName": "SearchAllResources", + "fullName": "google.cloud.asset.v1p1beta1.AssetService.SearchAllResources", + "async": true, + "parameters": [ + { + "name": "scope", + "type": "TYPE_STRING" + }, + { + "name": "query", + "type": "TYPE_STRING" + }, + { + "name": "asset_types", + "type": "TYPE_STRING[]" + }, + { + "name": "page_size", + "type": "TYPE_INT32" + }, + { + "name": "page_token", + "type": "TYPE_STRING" + }, + { + "name": "order_by", + "type": "TYPE_STRING" + } + ], + "resultType": ".google.cloud.asset.v1p1beta1.SearchAllResourcesResponse", + "client": { + "shortName": "AssetServiceClient", + "fullName": "google.cloud.asset.v1p1beta1.AssetServiceClient" + }, + "method": { + "shortName": "SearchAllResources", + "fullName": "google.cloud.asset.v1p1beta1.AssetService.SearchAllResources", + "service": { + "shortName": "AssetService", + "fullName": "google.cloud.asset.v1p1beta1.AssetService" + } + } + } + }, + { + "regionTag": "cloudasset_v1p1beta1_generated_AssetService_SearchAllIamPolicies_async", + "title": "AssetService searchAllIamPolicies Sample", + "origin": "API_DEFINITION", + "description": " Searches all the IAM policies under a given accessible CRM scope (project/folder/organization). This RPC gives callers especially admins the ability to search all the IAM policies under a scope, even if they don't have .getIamPolicy permission of all the IAM policies. Callers should have cloud.assets.SearchAllIamPolicies permission on the requested scope, otherwise it will be rejected.", + "canonical": true, + "file": "asset_service.search_all_iam_policies.js", + "language": "JAVASCRIPT", + "segments": [ + { + "start": 25, + "end": 78, + "type": "FULL" + } + ], + "clientMethod": { + "shortName": "SearchAllIamPolicies", + "fullName": "google.cloud.asset.v1p1beta1.AssetService.SearchAllIamPolicies", + "async": true, + "parameters": [ + { + "name": "scope", + "type": "TYPE_STRING" + }, + { + "name": "query", + "type": "TYPE_STRING" + }, + { + "name": "page_size", + "type": "TYPE_INT32" + }, + { + "name": "page_token", + "type": "TYPE_STRING" + } + ], + "resultType": ".google.cloud.asset.v1p1beta1.SearchAllIamPoliciesResponse", + "client": { + "shortName": "AssetServiceClient", + "fullName": "google.cloud.asset.v1p1beta1.AssetServiceClient" + }, + "method": { + "shortName": "SearchAllIamPolicies", + "fullName": "google.cloud.asset.v1p1beta1.AssetService.SearchAllIamPolicies", + "service": { + "shortName": "AssetService", + "fullName": "google.cloud.asset.v1p1beta1.AssetService" + } + } + } + } + ] +} \ No newline at end of file diff --git a/tests/fixtures/samples/generated/v2/snippet_metadata.google.cloud.asset.v2.json b/tests/fixtures/samples/generated/v2/snippet_metadata.google.cloud.asset.v2.json new file mode 100644 index 000000000..3673a74de --- /dev/null +++ b/tests/fixtures/samples/generated/v2/snippet_metadata.google.cloud.asset.v2.json @@ -0,0 +1,127 @@ +{ + "clientLibrary": { + "name": "nodejs-asset", + "version": "0.1.0", + "language": "TYPESCRIPT", + "apis": [ + { + "id": "google.cloud.asset.v1p1beta1", + "version": "v2" + } + ] + }, + "snippets": [ + { + "regionTag": "cloudasset_v1p1beta1_generated_AssetService_SearchAllResources_async", + "title": "AssetService searchAllResources Sample", + "origin": "API_DEFINITION", + "description": " Searches all the resources under a given accessible CRM scope (project/folder/organization). This RPC gives callers especially admins the ability to search all the resources under a scope, even if they don't have .get permission of all the resources. Callers should have cloud.assets.SearchAllResources permission on the requested scope, otherwise it will be rejected.", + "canonical": true, + "file": "asset_service.search_all_resources.js", + "language": "JAVASCRIPT", + "segments": [ + { + "start": 25, + "end": 87, + "type": "FULL" + } + ], + "clientMethod": { + "shortName": "SearchAllResources", + "fullName": "google.cloud.asset.v1p1beta1.AssetService.SearchAllResources", + "async": true, + "parameters": [ + { + "name": "scope", + "type": "TYPE_STRING" + }, + { + "name": "query", + "type": "TYPE_STRING" + }, + { + "name": "asset_types", + "type": "TYPE_STRING[]" + }, + { + "name": "page_size", + "type": "TYPE_INT32" + }, + { + "name": "page_token", + "type": "TYPE_STRING" + }, + { + "name": "order_by", + "type": "TYPE_STRING" + } + ], + "resultType": ".google.cloud.asset.v1p1beta1.SearchAllResourcesResponse", + "client": { + "shortName": "AssetServiceClient", + "fullName": "google.cloud.asset.v1p1beta1.AssetServiceClient" + }, + "method": { + "shortName": "SearchAllResources", + "fullName": "google.cloud.asset.v1p1beta1.AssetService.SearchAllResources", + "service": { + "shortName": "AssetService", + "fullName": "google.cloud.asset.v1p1beta1.AssetService" + } + } + } + }, + { + "regionTag": "cloudasset_v1p1beta1_generated_AssetService_SearchAllIamPolicies_async", + "title": "AssetService searchAllIamPolicies Sample", + "origin": "API_DEFINITION", + "description": " Searches all the IAM policies under a given accessible CRM scope (project/folder/organization). This RPC gives callers especially admins the ability to search all the IAM policies under a scope, even if they don't have .getIamPolicy permission of all the IAM policies. Callers should have cloud.assets.SearchAllIamPolicies permission on the requested scope, otherwise it will be rejected.", + "canonical": true, + "file": "asset_service.search_all_iam_policies.js", + "language": "JAVASCRIPT", + "segments": [ + { + "start": 25, + "end": 78, + "type": "FULL" + } + ], + "clientMethod": { + "shortName": "SearchAllIamPolicies", + "fullName": "google.cloud.asset.v1p1beta1.AssetService.SearchAllIamPolicies", + "async": true, + "parameters": [ + { + "name": "scope", + "type": "TYPE_STRING" + }, + { + "name": "query", + "type": "TYPE_STRING" + }, + { + "name": "page_size", + "type": "TYPE_INT32" + }, + { + "name": "page_token", + "type": "TYPE_STRING" + } + ], + "resultType": ".google.cloud.asset.v1p1beta1.SearchAllIamPoliciesResponse", + "client": { + "shortName": "AssetServiceClient", + "fullName": "google.cloud.asset.v1p1beta1.AssetServiceClient" + }, + "method": { + "shortName": "SearchAllIamPolicies", + "fullName": "google.cloud.asset.v1p1beta1.AssetService.SearchAllIamPolicies", + "service": { + "shortName": "AssetService", + "fullName": "google.cloud.asset.v1p1beta1.AssetService" + } + } + } + } + ] +} \ No newline at end of file diff --git a/tests/test_languages_common.py b/tests/test_languages_common.py new file mode 100644 index 000000000..1963e3e04 --- /dev/null +++ b/tests/test_languages_common.py @@ -0,0 +1,69 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from synthtool.languages import common +from pathlib import Path +import json +import shutil + +FIXTURES_SAMPLES = Path(__file__).parent / "fixtures" / "samples" / "generated" + + +def test_get_sample_metadata_files(): + metadataFiles = [ + str( + Path.resolve( + FIXTURES_SAMPLES / "v1" / "snippet_metadata.google.cloud.asset.v1.json" + ) + ), + str( + Path.resolve( + FIXTURES_SAMPLES / "v2" / "snippet_metadata.google.cloud.asset.v2.json" + ) + ), + ] + assert set(common.get_sample_metadata_files(Path.resolve(FIXTURES_SAMPLES))) == set( + metadataFiles + ) + + +def test_update_library_version(): + shutil.copytree(FIXTURES_SAMPLES, Path.resolve(FIXTURES_SAMPLES / "temp")) + with open( + Path.resolve( + FIXTURES_SAMPLES + / "temp" + / "v1" + / "snippet_metadata.google.cloud.asset.v1.json" + ), + "r+", + ) as n: + common.update_library_version("1234", FIXTURES_SAMPLES / "temp") + new_data = json.load(n) + assert new_data["clientLibrary"]["version"] == "1234" + + with open( + Path.resolve( + FIXTURES_SAMPLES + / "temp" + / "v2" + / "snippet_metadata.google.cloud.asset.v2.json" + ), + "r+", + ) as n: + common.update_library_version("1234", FIXTURES_SAMPLES / "temp") + new_data = json.load(n) + assert new_data["clientLibrary"]["version"] == "1234" + + shutil.rmtree(Path.resolve(FIXTURES_SAMPLES / "temp"))