blob: 8b0cf5c61cf680373d885f7ae41646acc382c2f5 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
S.Çağlar Onura95895d2015-02-09 13:34:11 -050017#! /usr/bin/env python
Scott Bakerd5deaa32014-08-11 17:52:39 -070018
19import json
20import os
21import requests
22import sys
23
Scott Bakerf476a972015-02-02 15:56:31 -080024REST_API="http://alpha.opencloud.us:8000/xos/"
Scott Bakerd5deaa32014-08-11 17:52:39 -070025
26NODES_API = REST_API + "nodes/"
27SLICES_API = REST_API + "slices/"
Tony Mack3de59e32015-08-19 11:58:18 -040028SLIVERS_API = REST_API + "instances/"
Scott Bakerd5deaa32014-08-11 17:52:39 -070029
30opencloud_auth=("demo@onlab.us", "demo")
31
32def get_slice_id(slice_name):
33 r = requests.get(SLICES_API + "?name=%s" % slice_name, auth=opencloud_auth)
34 return r.json()[0]["id"]
35
36def get_node_id(host_name):
Scott Baker7fbf1db2014-09-05 15:02:43 -070037 r = requests.get(NODES_API)
38 nodes = r.json()
39 for node in nodes:
40 if node["name"].lower() == host_name.lower():
41 return node["id"]
42 print >> sys.stderr, "Error: failed to find node %s" % host_name
43 sys.exit(-1)
Scott Bakerd5deaa32014-08-11 17:52:39 -070044
Tony Mack3de59e32015-08-19 11:58:18 -040045def get_instances(slice_id=None, node_id=None):
Scott Bakerd5deaa32014-08-11 17:52:39 -070046 queries = []
47 if slice_id:
48 queries.append("slice=%s" % str(slice_id))
49 if node_id:
50 queries.append("node=%s" % str(node_id))
51
52 if queries:
53 query_string = "?" + "&".join(queries)
54 else:
55 query_string = ""
56
57 r = requests.get(SLIVERS_API + query_string, auth=opencloud_auth)
58 return r.json()
59
60def main():
61 global opencloud_auth
62
63 if len(sys.argv)!=5:
64 print >> sys.stderr, "syntax: get_instance_name.py <username>, <password>, <hostname> <slicename>"
65 sys.exit(-1)
66
67 username = sys.argv[1]
68 password = sys.argv[2]
69 hostname = sys.argv[3]
70 slice_name = sys.argv[4]
71
72 opencloud_auth=(username, password)
73
74 slice_id = get_slice_id(slice_name)
75 node_id = get_node_id(hostname)
Tony Mack3de59e32015-08-19 11:58:18 -040076 instances = get_instances(slice_id, node_id)
Scott Bakerd5deaa32014-08-11 17:52:39 -070077
Tony Mack3de59e32015-08-19 11:58:18 -040078 instance_names = [x["instance_name"] for x in instances if x["instance_name"]]
Scott Bakerd5deaa32014-08-11 17:52:39 -070079
80 # return the last one in the list (i.e. the newest one)
81
82 print sorted(instance_names)[-1]
83
84if __name__ == "__main__":
85 main()
86