Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 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 Onur | a95895d | 2015-02-09 13:34:11 -0500 | [diff] [blame] | 17 | #! /usr/bin/env python |
Scott Baker | d5deaa3 | 2014-08-11 17:52:39 -0700 | [diff] [blame] | 18 | |
| 19 | import json |
| 20 | import os |
| 21 | import requests |
| 22 | import sys |
| 23 | |
Scott Baker | f476a97 | 2015-02-02 15:56:31 -0800 | [diff] [blame] | 24 | REST_API="http://alpha.opencloud.us:8000/xos/" |
Scott Baker | d5deaa3 | 2014-08-11 17:52:39 -0700 | [diff] [blame] | 25 | |
| 26 | NODES_API = REST_API + "nodes/" |
| 27 | SLICES_API = REST_API + "slices/" |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 28 | SLIVERS_API = REST_API + "instances/" |
Scott Baker | d5deaa3 | 2014-08-11 17:52:39 -0700 | [diff] [blame] | 29 | |
| 30 | opencloud_auth=("demo@onlab.us", "demo") |
| 31 | |
| 32 | def 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 | |
| 36 | def get_node_id(host_name): |
Scott Baker | 7fbf1db | 2014-09-05 15:02:43 -0700 | [diff] [blame] | 37 | 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 Baker | d5deaa3 | 2014-08-11 17:52:39 -0700 | [diff] [blame] | 44 | |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 45 | def get_instances(slice_id=None, node_id=None): |
Scott Baker | d5deaa3 | 2014-08-11 17:52:39 -0700 | [diff] [blame] | 46 | 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 | |
| 60 | def 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 Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 76 | instances = get_instances(slice_id, node_id) |
Scott Baker | d5deaa3 | 2014-08-11 17:52:39 -0700 | [diff] [blame] | 77 | |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 78 | instance_names = [x["instance_name"] for x in instances if x["instance_name"]] |
Scott Baker | d5deaa3 | 2014-08-11 17:52:39 -0700 | [diff] [blame] | 79 | |
| 80 | # return the last one in the list (i.e. the newest one) |
| 81 | |
| 82 | print sorted(instance_names)[-1] |
| 83 | |
| 84 | if __name__ == "__main__": |
| 85 | main() |
| 86 | |