blob: 3450df59a241496b3062f8f611172705dc84f176 [file] [log] [blame]
S.Çağlar Onura95895d2015-02-09 13:34:11 -05001#! /usr/bin/env python
Scott Bakerd5deaa32014-08-11 17:52:39 -07002
3import json
4import os
5import requests
6import sys
7
Scott Bakerf476a972015-02-02 15:56:31 -08008REST_API="http://alpha.opencloud.us:8000/xos/"
Scott Bakerd5deaa32014-08-11 17:52:39 -07009
10NODES_API = REST_API + "nodes/"
11SLICES_API = REST_API + "slices/"
12SLIVERS_API = REST_API + "slivers/"
13
14opencloud_auth=("demo@onlab.us", "demo")
15
16def 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
20def get_node_id(host_name):
Scott Baker7fbf1db2014-09-05 15:02:43 -070021 r = requests.get(NODES_API)
22 nodes = r.json()
23 for node in nodes:
24 if node["name"].lower() == host_name.lower():
25 return node["id"]
26 print >> sys.stderr, "Error: failed to find node %s" % host_name
27 sys.exit(-1)
Scott Bakerd5deaa32014-08-11 17:52:39 -070028
29def get_slivers(slice_id=None, node_id=None):
30 queries = []
31 if slice_id:
32 queries.append("slice=%s" % str(slice_id))
33 if node_id:
34 queries.append("node=%s" % str(node_id))
35
36 if queries:
37 query_string = "?" + "&".join(queries)
38 else:
39 query_string = ""
40
41 r = requests.get(SLIVERS_API + query_string, auth=opencloud_auth)
42 return r.json()
43
44def main():
45 global opencloud_auth
46
47 if len(sys.argv)!=5:
48 print >> sys.stderr, "syntax: get_instance_name.py <username>, <password>, <hostname> <slicename>"
49 sys.exit(-1)
50
51 username = sys.argv[1]
52 password = sys.argv[2]
53 hostname = sys.argv[3]
54 slice_name = sys.argv[4]
55
56 opencloud_auth=(username, password)
57
58 slice_id = get_slice_id(slice_name)
59 node_id = get_node_id(hostname)
60 slivers = get_slivers(slice_id, node_id)
61
62 instance_names = [x["instance_name"] for x in slivers if x["instance_name"]]
63
64 # return the last one in the list (i.e. the newest one)
65
66 print sorted(instance_names)[-1]
67
68if __name__ == "__main__":
69 main()
70