Add tools to get API list of several OpenStack components 53/36153/1
authorasteroide <thomas.duval@orange.com>
Fri, 16 Jun 2017 15:05:46 +0000 (17:05 +0200)
committerasteroide <thomas.duval@orange.com>
Fri, 16 Jun 2017 15:05:46 +0000 (17:05 +0200)
Change-Id: I2b2b9a43df1054448d643cde3b362f3fcd059621

moonv4/moon_utilities/moon_utilities/get_os_apis.py [new file with mode: 0644]

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 (file)
index 0000000..dea2a87
--- /dev/null
@@ -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