blob: 01fc841044d4b07be2718e3526a47d669ca5d7cd [file] [log] [blame]
Scott Bakerd5deaa32014-08-11 17:52:39 -07001#! /usr/bin/python
2
3import json
4import os
5import requests
6import sys
7
8REST_API="http://alpha.opencloud.us:8000/plstackapi/"
9
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 + "?name=%s" % host_name, auth=opencloud_auth)
22# return r.json()[0]["id"]
23 r = requests.get(NODES_API)
24 nodes = r.json()
25 for node in nodes:
26 if node["name"].lower() == host_name.lower():
27 return node["id"]
28 print >> sys.stderr, "Error: failed to find node %s" % host_name
29 sys.exit(-1)
Scott Bakerd5deaa32014-08-11 17:52:39 -070030
31def get_slivers(slice_id=None, node_id=None):
32 queries = []
33 if slice_id:
34 queries.append("slice=%s" % str(slice_id))
35 if node_id:
36 queries.append("node=%s" % str(node_id))
37
38 if queries:
39 query_string = "?" + "&".join(queries)
40 else:
41 query_string = ""
42
43 r = requests.get(SLIVERS_API + query_string, auth=opencloud_auth)
44 return r.json()
45
46def main():
47 global opencloud_auth
48
49 if len(sys.argv)!=5:
50 print >> sys.stderr, "syntax: get_instance_name.py <username>, <password>, <hostname> <slicename>"
51 sys.exit(-1)
52
53 username = sys.argv[1]
54 password = sys.argv[2]
55 hostname = sys.argv[3]
56 slice_name = sys.argv[4]
57
58 opencloud_auth=(username, password)
59
60 slice_id = get_slice_id(slice_name)
61 node_id = get_node_id(hostname)
62 slivers = get_slivers(slice_id, node_id)
63
64 instance_names = [x["instance_name"] for x in slivers if x["instance_name"]]
65
66 # return the last one in the list (i.e. the newest one)
67
68 print sorted(instance_names)[-1]
69
70if __name__ == "__main__":
71 main()
72