jumpserver/apps/assets/automations/methods.py

67 lines
2.0 KiB
Python
Raw Normal View History

2022-09-09 11:17:25 +00:00
import os
import yaml
2022-10-09 12:54:11 +00:00
import json
2022-09-09 11:17:25 +00:00
from functools import partial
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
def check_platform_method(manifest, manifest_path):
required_keys = ['category', 'method', 'name', 'id', 'type']
less_key = set(required_keys) - set(manifest.keys())
if less_key:
raise ValueError("Manifest missing keys: {}, {}".format(less_key, manifest_path))
if not isinstance(manifest['type'], list):
raise ValueError("Manifest type must be a list: {}".format(manifest_path))
return True
def check_platform_methods(methods):
ids = [m['id'] for m in methods]
for i, _id in enumerate(ids):
if _id in ids[i+1:]:
raise ValueError("Duplicate id: {}".format(_id))
2022-09-20 09:33:15 +00:00
def get_platform_automation_methods():
2022-09-09 11:17:25 +00:00
methods = []
for root, dirs, files in os.walk(BASE_DIR, topdown=False):
for name in files:
path = os.path.join(root, name)
if not path.endswith('manifest.yml'):
continue
with open(path, 'r') as f:
manifest = yaml.safe_load(f)
check_platform_method(manifest, path)
manifest['dir'] = os.path.dirname(path)
methods.append(manifest)
check_platform_methods(methods)
return methods
def filter_key(manifest, attr, value):
manifest_value = manifest.get(attr, '')
if isinstance(manifest_value, str):
manifest_value = [manifest_value]
return value in manifest_value or 'all' in manifest_value
2022-09-20 09:33:15 +00:00
def filter_platform_methods(category, tp, method=None):
methods = platform_automation_methods
2022-09-09 11:17:25 +00:00
if category:
methods = filter(partial(filter_key, attr='category', value=category), methods)
if tp:
methods = filter(partial(filter_key, attr='type', value=tp), methods)
if method:
methods = filter(lambda x: x['method'] == method, methods)
return methods
2022-09-20 09:33:15 +00:00
platform_automation_methods = get_platform_automation_methods()
2022-09-09 11:17:25 +00:00
if __name__ == '__main__':
2022-10-09 12:54:11 +00:00
print(json.dumps(platform_automation_methods, indent=4))