Scott Baker | 44d9114 | 2016-07-27 09:50:15 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | import json |
| 4 | import os |
| 5 | import requests |
| 6 | import sys |
| 7 | import traceback |
| 8 | |
| 9 | def check_status(r): |
| 10 | if (r.status_code != 200): |
| 11 | print >> sys.stderr, "ERR: recieved status %d" % r.status_code |
| 12 | print >> sys.stderr, r.text |
| 13 | sys.exit(-1) |
| 14 | |
| 15 | def main(): |
| 16 | global opencloud_auth |
| 17 | |
| 18 | if len(sys.argv)!=7: |
| 19 | print >> sys.stderr, "syntax: run_tosca.py <port> <username> <password> [get | set] app attribute" |
| 20 | print >> sys.stderr, " 'get' will print attribute to stdout" |
| 21 | print >> sys.stderr, " 'set' will read attribute from stdin" |
| 22 | print >> sys.stderr, "example: onos_attribute.py 80 padmin@vicci.org letmein get VTN_ONOS_app rest_onos/v1/network/configuration/" |
| 23 | sys.exit(-1) |
| 24 | |
| 25 | port = int(sys.argv[1]) |
| 26 | username = sys.argv[2] |
| 27 | password = sys.argv[3] |
| 28 | operation = sys.argv[4] |
| 29 | app_name = sys.argv[5] |
| 30 | attr_name = sys.argv[6] |
| 31 | |
| 32 | xos_auth=(username, password) |
| 33 | hostname = "127.0.0.1" |
| 34 | |
| 35 | url = "http://%s:%d/api/tenant/onos/app/?name=%s" % (hostname, port, app_name) |
| 36 | r = requests.get(url, auth=xos_auth) |
| 37 | check_status(r) |
| 38 | |
| 39 | app_id = r.json()[0]["id"] |
| 40 | |
| 41 | url = "http://%s:%d/api/tenant/onos/app/%s/attributes/?attribute_name=%s" % (hostname, port, app_id, attr_name) |
| 42 | r = requests.get(url, auth=xos_auth) |
| 43 | check_status(r) |
| 44 | |
| 45 | if operation == "get": |
| 46 | if r.json() != []: |
| 47 | print r.json()[0]["value"] |
| 48 | |
| 49 | elif operation == "set": |
| 50 | if r.json() == []: |
| 51 | url = "http://%s:%d/api/tenant/onos/app/%s/attributes/" % (hostname, port, app_id) |
| 52 | r = requests.post(url, data={"name": attr_name, "value": sys.stdin.read()}, auth=xos_auth) |
| 53 | check_status(r) |
| 54 | else: |
| 55 | attr_id = r.json()[0]["id"] |
| 56 | url = "http://%s:%d/api/tenant/onos/app/%s/attributes/%s/" % (hostname, port, app_id, attr_id) |
| 57 | r = requests.put(url, data={"value": sys.stdin.read()}, auth=xos_auth) |
| 58 | check_status(r) |
| 59 | |
| 60 | if __name__ == "__main__": |
| 61 | main() |
| 62 | |