summaryrefslogtreecommitdiffstats
path: root/ogcli
diff options
context:
space:
mode:
Diffstat (limited to 'ogcli')
-rwxr-xr-xogcli41
-rw-r--r--ogcli/__init__.py1
-rw-r--r--ogcli/objects/__init__.py0
-rw-r--r--ogcli/objects/modes.py6
-rw-r--r--ogcli/objects/og_client.py6
-rw-r--r--ogcli/objects/og_scopes.py6
-rw-r--r--ogcli/ogcli.py39
7 files changed, 41 insertions, 58 deletions
diff --git a/ogcli b/ogcli
new file mode 100755
index 0000000..33d740a
--- /dev/null
+++ b/ogcli
@@ -0,0 +1,41 @@
+#!/usr/bin/python
+
+from cli.cli import OgCLI
+import argparse
+import json
+import sys
+
+OG_CLI_CFG_PATH="/opt/opengnsys/etc/ogcli.json"
+
+class CLI():
+ def __init__(self):
+ try:
+ with open(OG_CLI_CFG_PATH, 'r') as json_file:
+ self.cfg = json.load(json_file)
+ except json.JSONDecodeError:
+ sys.exit(f'ERROR: Failed parse malformed JSON file '
+ f'{OG_CLI_CFG_PATH}')
+ except:
+ sys.exit(f'ERROR: cannot open {OG_CLI_CFG_PATH}')
+
+ required_cfg_params = {'api_token', 'ip', 'port'}
+ difference_cfg_params = required_cfg_params - self.cfg.keys()
+ if len(difference_cfg_params) > 0:
+ sys.exit(f'Missing {difference_cfg_params} key in '
+ f'json config file')
+
+ self.ogcli = OgCLI(self.cfg)
+
+ parser = argparse.ArgumentParser(prog='ogcli')
+ parser.add_argument('command', help='Subcommand to run')
+ args = parser.parse_args(sys.argv[1:2])
+
+ if not hasattr(self.ogcli, args.command):
+ parser.print_help()
+ sys.exit('Unknown command')
+
+ # Call the command with the same name.
+ getattr(self.ogcli, args.command)(sys.argv[2:])
+
+if __name__ == "__main__":
+ CLI()
diff --git a/ogcli/__init__.py b/ogcli/__init__.py
deleted file mode 100644
index 8b13789..0000000
--- a/ogcli/__init__.py
+++ /dev/null
@@ -1 +0,0 @@
-
diff --git a/ogcli/objects/__init__.py b/ogcli/objects/__init__.py
deleted file mode 100644
index e69de29..0000000
--- a/ogcli/objects/__init__.py
+++ /dev/null
diff --git a/ogcli/objects/modes.py b/ogcli/objects/modes.py
deleted file mode 100644
index bf175d6..0000000
--- a/ogcli/objects/modes.py
+++ /dev/null
@@ -1,6 +0,0 @@
-class OgModes():
-
- @staticmethod
- def list_available_modes(rest):
- r = rest.get('/modes')
- print(r.json())
diff --git a/ogcli/objects/og_client.py b/ogcli/objects/og_client.py
deleted file mode 100644
index ca91036..0000000
--- a/ogcli/objects/og_client.py
+++ /dev/null
@@ -1,6 +0,0 @@
-class OgClient():
-
- @staticmethod
- def list_clients(rest):
- r = rest.get('/clients')
- print(r.json())
diff --git a/ogcli/objects/og_scopes.py b/ogcli/objects/og_scopes.py
deleted file mode 100644
index 0bf3ca8..0000000
--- a/ogcli/objects/og_scopes.py
+++ /dev/null
@@ -1,6 +0,0 @@
-class OgScope():
-
- @staticmethod
- def list_scopes(rest):
- r = rest.get('/scopes')
- print(r.json())
diff --git a/ogcli/ogcli.py b/ogcli/ogcli.py
deleted file mode 100644
index d8e66b7..0000000
--- a/ogcli/ogcli.py
+++ /dev/null
@@ -1,39 +0,0 @@
-from ogcli.objects.og_client import OgClient
-from ogcli.objects.og_scopes import OgScope
-from ogcli.objects.modes import OgModes
-import argparse
-import requests
-import sys
-
-class OgREST():
- def __init__(self, ip, port, api_token):
- self.URL = f'http://{ip}:{port}'
- self.HEADERS = {'Authorization' : api_token}
-
- def get(self, path):
- try:
- r = requests.get(f'{self.URL}{path}',
- headers=self.HEADERS)
- if r.status_code != 200:
- sys.exit(f"Cannot connect to ogServer: "
- f"{r.status_code} HTTP status code")
- except IOError as e:
- sys.exit(f"Cannot connect to ogServer: {e}")
- return r
-
-class OgCLI():
- def __init__(self, cfg):
- self.rest = OgREST(cfg['ip'], cfg['port'], cfg['api_token'])
-
- def list(self, args):
- choices = ['clients', 'scopes', 'modes']
- parser = argparse.ArgumentParser()
- parser.add_argument('item', choices=choices)
- args = parser.parse_args(args)
-
- if args.item == 'clients':
- OgClient.list_clients(self.rest)
- elif args.item == 'modes':
- OgModes.list_available_modes(self.rest)
- elif args.item == 'scopes':
- OgScope.list_scopes(self.rest)