#! /usr/bin/python import sys import requests import json import os token = None iotronic_url = "http://192.168.17.102:1288/v1" try: os.environ['OS_AUTH_URL'] except Exception: print("load the rc") sys.exit(1) url = os.environ['OS_AUTH_URL'] + "/auth/tokens" def get_token(user, project, psw): payload = {"auth": { "identity": { "methods": ["password"], "password": { "user": { "name": user, "domain": {"id": "default"}, "password": psw } } }, "scope": { "project": { "name": project, "domain": {"id": "default"} } } } } headers = { 'content-type': "application/json", } response = requests.request("POST", url, data=json.dumps(payload), headers=headers) token=response.headers.get('X-Subject-Token') if token: r=json.loads(response.text)['token']['roles'] roles=[str(x['name']) for x in r] print(user + " in " + project + ' with roles: '+ " ".join(roles) ) return token def node_manager(argv): actions = ['show', 'list', 'create', 'delete', 'update'] if argv[0] not in actions or len(argv) == 0: print("node list|create|delete|show|update") sys.exit() global iotronic_url, token if not token: token = get_token(os.environ['OS_USERNAME'], os.environ['OS_PROJECT_NAME'], os.environ['OS_PASSWORD']) # print(token) headers = {'content-type': "application/json", 'x-auth-token': token, } if argv[0] == 'list': url = iotronic_url + "/nodes" response = requests.request("GET", url, headers=headers) elif argv[0] == 'create': code = argv[1] name = argv[2] lat = argv[3] lon = argv[4] alt = argv[5] typ = argv[6] url = iotronic_url + "/nodes" payload = { "code": code, "mobile": False, "location": [ { "latitude": lat, "altitude": alt, "longitude": lon } ], "type": typ, "name": name } response = requests.request("POST", url, data=json.dumps(payload), headers=headers) elif argv[0] == 'delete': node = argv[1] url = iotronic_url + "/nodes/" + node response = requests.request("DELETE", url, headers=headers) elif argv[0] == 'show': node = argv[1] url = iotronic_url + "/nodes/" + node response = requests.request("GET", url, headers=headers) elif argv[0] == 'update': node = argv[1] values = {} for opt in argv[2:]: key, val = opt.split(':') values[key] = val url = iotronic_url + "/nodes/" + node payload = values response = requests.request("PATCH", url, data=json.dumps(payload), headers=headers) else: print ("node list|create|delete|show|update") sys.exit() try: print(json.dumps(response.json(), indent=2)) except Exception: pass def plugin_manager(argv): actions = ['show', 'list', 'create', 'delete', 'update', 'inject'] if argv[0] not in actions or len(argv) == 0: print("plugin list|create|delete|show|update|inject") sys.exit() global iotronic_url, token if not token: token = get_token(os.environ['OS_USERNAME'], os.environ['OS_PROJECT_NAME'], os.environ['OS_PASSWORD']) headers = {'content-type': "application/json", 'x-auth-token': token, } if argv[0] == 'list': url = iotronic_url + "/plugins" response = requests.request("GET", url, headers=headers) elif argv[0] == 'create': f=argv[1] with open(f, 'r') as fil: t = fil.read() url = iotronic_url + "/plugins" payload={"name": f, "config": t} response = requests.request("POST", url, data=json.dumps(payload), headers=headers) elif argv[0] == 'delete': plugin = argv[1] url = iotronic_url + "/plugins/" + plugin response = requests.request("DELETE", url, headers=headers) elif argv[0] == 'show': plugin = argv[1] url = iotronic_url + "/plugins/" + plugin response = requests.request("GET", url, headers=headers) elif argv[0] == 'update': plugin = argv[1] values = {} for opt in argv[2:]: key, val = opt.split(':') values[key] = val url = iotronic_url + "/plugins/" + plugin payload = values response = requests.request("PATCH", url, data=json.dumps(payload), headers=headers) else: print ("node list|create|delete|show|update") sys.exit() try: print(json.dumps(response.json(), indent=2)) except Exception: pass if __name__ == "__main__": argv = sys.argv if len(argv) <= 2: print("USAGE: iotronic node|plugin [OPTIONS]") sys.exit() if argv[1] == 'node': node_manager(argv[2:]) elif argv[1] == 'plugin': plugin_manager(argv[2:]) else: print("USAGE: iotronic node|plugin [OPTIONS]")