diff options
author | asteroide <thomas.duval@orange.com> | 2017-06-16 17:05:46 +0200 |
---|---|---|
committer | asteroide <thomas.duval@orange.com> | 2017-06-16 17:05:46 +0200 |
commit | 4bcec5c4296280c7831a88a7fcf655a477777cc7 (patch) | |
tree | 120c41ce273281a13401f0de3a5800e649a2acd1 /moonv4/moon_utilities | |
parent | c25a7ed290780368f1e8a68fa047b8618d794bb3 (diff) |
Add tools to get API list of several OpenStack components
Change-Id: I2b2b9a43df1054448d643cde3b362f3fcd059621
Diffstat (limited to 'moonv4/moon_utilities')
-rw-r--r-- | moonv4/moon_utilities/moon_utilities/get_os_apis.py | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/moonv4/moon_utilities/moon_utilities/get_os_apis.py b/moonv4/moon_utilities/moon_utilities/get_os_apis.py new file mode 100644 index 00000000..dea2a878 --- /dev/null +++ b/moonv4/moon_utilities/moon_utilities/get_os_apis.py @@ -0,0 +1,122 @@ +import json +import logging +import requests +import argparse + +URLS = { + "keystone": "https://api.github.com/repos/openstack/keystone/contents/api-ref/source/v3", + "nova": "https://api.github.com/repos/openstack/nova/contents/api-ref/source", + "neutron": "https://api.github.com/repos/openstack/neutron-lib/contents/api-ref/source/v2", + "glance": "https://api.github.com/repos/openstack/glance/contents/api-ref/source/v2", + "swift": "https://api.github.com/repos/openstack/swift/contents/api-ref/source", + "cinder": "https://api.github.com/repos/openstack/cinder/contents/api-ref/source/v3", + +} + +logger = None + +USER = "" +PASS = "" + + +def init(): + global logger, USER, PASS + parser = argparse.ArgumentParser() + parser.add_argument("--verbose", "-v", action='store_true', help="verbose mode") + parser.add_argument("--debug", "-d", action='store_true', help="debug mode") + parser.add_argument("--format", "-f", help="Output format (txt, json)", default="json") + parser.add_argument("--output", "-o", help="Output filename") + parser.add_argument("--credentials", "-c", help="Github credential filename (inside format user:pass)") + args = parser.parse_args() + + FORMAT = '%(levelname)s %(message)s' + + if args.verbose: + logging.basicConfig( + format=FORMAT, + level=logging.INFO) + elif args.debug: + logging.basicConfig( + format=FORMAT, + level=logging.DEBUG) + else: + logging.basicConfig( + format=FORMAT, + level=logging.WARNING) + + if args.credentials: + cred = open(args.credentials).read() + USER = cred.split(":")[0] + PASS = cred.split(":")[1] + + logger = logging.getLogger(__name__) + + return args + + +def get_api_item(url): + if USER: + r = requests.get(url, auth=(USER, PASS)) + else: + r = requests.get(url) + items = [] + for line in r.text.splitlines(): + if ".. rest_method::" in line: + items.append(line.replace(".. rest_method::", "").strip()) + logger.debug("\n\t".join(items)) + return items + + +def get_content(key, args): + logger.info("Analysing {}".format(key)) + if USER: + r = requests.get(URLS[key], auth=(USER, PASS)) + else: + r = requests.get(URLS[key]) + data = r.json() + results = {} + for item in data: + try: + logger.debug("{} {}".format(item['name'], item['download_url'])) + if item['type'] == "file" and ".inc" in item['name']: + results[item['name'].replace(".inc", "")] = get_api_item(item['download_url']) + except TypeError: + logger.error("Error with {}".format(item)) + except requests.exceptions.MissingSchema: + logger.error("MissingSchema error {}".format(item)) + return results + + +def to_str(results): + output = "" + for key in results: + output += "{}\n".format(key) + for item in results[key]: + output += "\t{}\n".format(item) + for value in results[key][item]: + output += "\t\t{}\n".format(value) + return output + + +def save(results, args): + if args.output: + if args.format == 'json': + json.dump(results, open(args.output, "w"), indent=4) + elif args.format == 'txt': + open(args.output, "w").write(to_str(results)) + else: + if args.format == 'json': + print(json.dumps(results, indent=4)) + elif args.format in ('txt', 'text'): + print(to_str(results)) + + +def main(): + args = init() + results = {} + for key in URLS: + results[key] = get_content(key, args) + save(results, args) + +if __name__ == "__main__": + main()
\ No newline at end of file |