Scott Baker | d5deaa3 | 2014-08-11 17:52:39 -0700 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | import json |
| 4 | import os |
| 5 | import requests |
| 6 | import sys |
| 7 | |
| 8 | REST_API="http://alpha.opencloud.us:8000/plstackapi/" |
| 9 | |
| 10 | NODES_API = REST_API + "nodes/" |
| 11 | SLICES_API = REST_API + "slices/" |
| 12 | SLIVERS_API = REST_API + "slivers/" |
| 13 | |
| 14 | opencloud_auth=("demo@onlab.us", "demo") |
| 15 | |
| 16 | def get_slice_id(slice_name): |
| 17 | r = requests.get(SLICES_API + "?name=%s" % slice_name, auth=opencloud_auth) |
| 18 | return r.json()[0]["id"] |
| 19 | |
| 20 | def get_node_id(host_name): |
| 21 | r = requests.get(NODES_API + "?name=%s" % host_name, auth=opencloud_auth) |
| 22 | return r.json()[0]["id"] |
| 23 | |
| 24 | def get_slivers(slice_id=None, node_id=None): |
| 25 | queries = [] |
| 26 | if slice_id: |
| 27 | queries.append("slice=%s" % str(slice_id)) |
| 28 | if node_id: |
| 29 | queries.append("node=%s" % str(node_id)) |
| 30 | |
| 31 | if queries: |
| 32 | query_string = "?" + "&".join(queries) |
| 33 | else: |
| 34 | query_string = "" |
| 35 | |
| 36 | r = requests.get(SLIVERS_API + query_string, auth=opencloud_auth) |
| 37 | return r.json() |
| 38 | |
| 39 | def main(): |
| 40 | global opencloud_auth |
| 41 | |
| 42 | if len(sys.argv)!=5: |
| 43 | print >> sys.stderr, "syntax: get_instance_name.py <username>, <password>, <hostname> <slicename>" |
| 44 | sys.exit(-1) |
| 45 | |
| 46 | username = sys.argv[1] |
| 47 | password = sys.argv[2] |
| 48 | hostname = sys.argv[3] |
| 49 | slice_name = sys.argv[4] |
| 50 | |
| 51 | opencloud_auth=(username, password) |
| 52 | |
| 53 | slice_id = get_slice_id(slice_name) |
| 54 | node_id = get_node_id(hostname) |
| 55 | slivers = get_slivers(slice_id, node_id) |
| 56 | |
| 57 | instance_names = [x["instance_name"] for x in slivers if x["instance_name"]] |
| 58 | |
| 59 | # return the last one in the list (i.e. the newest one) |
| 60 | |
| 61 | print sorted(instance_names)[-1] |
| 62 | |
| 63 | if __name__ == "__main__": |
| 64 | main() |
| 65 | |