From b4a6c4209f496a5f5940d469171aaa76538e040e Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Wed, 15 Jun 2022 21:14:24 -0500 Subject: [PATCH 1/4] refactor st2-generate-schemas so that logic is in an importable module --- st2common/bin/st2-generate-schemas | 42 ++++-------- st2common/st2common/cmd/generate_schemas.py | 74 +++++++++++++++++++++ 2 files changed, 88 insertions(+), 28 deletions(-) create mode 100644 st2common/st2common/cmd/generate_schemas.py diff --git a/st2common/bin/st2-generate-schemas b/st2common/bin/st2-generate-schemas index 376d1fc5f6..dce637499a 100755 --- a/st2common/bin/st2-generate-schemas +++ b/st2common/bin/st2-generate-schemas @@ -17,43 +17,29 @@ # See the License for the specific language governing permissions and # limitations under the License. +""" +A script that generates st2 metadata (pack, action, rule, ...) schemas. +`st2-generate-schemas` is used to to update contrib/schemas/*.json. + +USAGE: st2-generate-schemas +""" + from __future__ import absolute_import -import json import os -import six - -from st2common.models.api import action as action_models -from st2common.models.api import pack as pack_models -from st2common.models.api import policy as policy_models -from st2common.models.api import rule as rule_models -from st2common.models.api import sensor as sensor_models +import sys -content_models = { - "pack": pack_models.PackAPI, - "action": action_models.ActionAPI, - "alias": action_models.ActionAliasAPI, - "policy": policy_models.PolicyAPI, - "rule": rule_models.RuleAPI, - "sensor": sensor_models.SensorTypeAPI, -} +from st2common.cmd import generate_schemas -def main(): +def init(): scripts_dir = os.path.dirname(os.path.abspath(__file__)) schemas_dir = os.path.abspath(os.path.join(scripts_dir, "../../contrib/schemas")) - for name, model in six.iteritems(content_models): - schema_text = json.dumps(model.schema, indent=4) - print('Generated schema for the "%s" model.' % name) - - schema_file = os.path.join(schemas_dir, name + ".json") - print('Schema will be written to "%s".' % schema_file) - - with open(schema_file, "w") as f: - f.write(schema_text) - f.write("\n") + # set the default for backwards compatibility + generate_schemas.default_scchemas_dir = schemas_dir if __name__ == "__main__": - main() + init() + sys.exit(generate_schemas.main()) diff --git a/st2common/st2common/cmd/generate_schemas.py b/st2common/st2common/cmd/generate_schemas.py new file mode 100644 index 0000000000..bdb4754383 --- /dev/null +++ b/st2common/st2common/cmd/generate_schemas.py @@ -0,0 +1,74 @@ +# Copyright 2021 The StackStorm Authors. +# +# 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 +# +# http://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. + +""" +A script that generates st2 metadata (pack, action, rule, ...) schemas. +This is used by `st2-generate-schemas` to update contrib/schemas/*.json. +""" + +from __future__ import absolute_import + +import json +import os +import sys + +from st2common.models.api import action as action_models +from st2common.models.api import pack as pack_models +from st2common.models.api import policy as policy_models +from st2common.models.api import rule as rule_models +from st2common.models.api import sensor as sensor_models + +__all__ = ["generate_schemas", "write_schemas"] + +content_models = { + "pack": pack_models.PackAPI, + "action": action_models.ActionAPI, + "alias": action_models.ActionAliasAPI, + "policy": policy_models.PolicyAPI, + "rule": rule_models.RuleAPI, + "sensor": sensor_models.SensorTypeAPI, +} + + +default_schemas_dir = "." + + +def generate_schemas(): + for name, model in content_models.items(): + schema_text = json.dumps(model.schema, indent=4) + + yield name, schema_text + + +def write_schemas(schemas_dir): + for name, schema_text in generate_schemas(): + print('Generated schema for the "%s" model.' % name) + + schema_file = os.path.join(schemas_dir, name + ".json") + print('Schema will be written to "%s".' % schema_file) + + with open(schema_file, "w") as f: + f.write(schema_text) + f.write("\n") + + +def main(): + argv = sys.argv[1:] + + # 1st positional parameter is the destination directory + schemas_dir = argv[0] if argv else default_schemas_dir + + write_schemas(schemas_dir) + + return 0 From d6794b3d9b9e6c346e204aedb0db1b85ff9cea21 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Wed, 15 Jun 2022 23:13:19 -0500 Subject: [PATCH 2/4] run st2-generate-schemas --- contrib/schemas/sensor.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/schemas/sensor.json b/contrib/schemas/sensor.json index 3622b6d8ee..6193bf521f 100644 --- a/contrib/schemas/sensor.json +++ b/contrib/schemas/sensor.json @@ -11,7 +11,7 @@ "uid": { "type": "string" }, - "name": { + "class_name": { "type": "string", "required": true }, From 789b1d057fe3722a8632c17b510e37f195fe2099 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Fri, 12 Aug 2022 14:43:11 -0500 Subject: [PATCH 3/4] add changelog entry --- CHANGELOG.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index fae190eca7..1e8d09b0da 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -94,6 +94,9 @@ Changed * Refactor tests to use python imports to identify test fixtures. #5699 #5702 #5703 #5704 #5705 #5706 Contributed by @cognifloyd +* Refactor ``st2-generate-schemas`` so that logic is in an importable module. #5708 + Contributed by @cognifloyd + Removed ~~~~~~~ From 5db734bb2458031d291df75c6e65749c90a75501 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Fri, 12 Aug 2022 14:48:15 -0500 Subject: [PATCH 4/4] typo fix --- st2common/bin/st2-generate-schemas | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/st2common/bin/st2-generate-schemas b/st2common/bin/st2-generate-schemas index dce637499a..8f5d49dfea 100755 --- a/st2common/bin/st2-generate-schemas +++ b/st2common/bin/st2-generate-schemas @@ -37,7 +37,7 @@ def init(): schemas_dir = os.path.abspath(os.path.join(scripts_dir, "../../contrib/schemas")) # set the default for backwards compatibility - generate_schemas.default_scchemas_dir = schemas_dir + generate_schemas.default_schemas_dir = schemas_dir if __name__ == "__main__":