Scott Baker | 86581fd | 2018-05-10 11:06:23 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2017-present Open Networking Foundation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # show-instances.py |
| 18 | # Show the SimpleExampleServiceInstances and their ip addresses in a human-readable table. |
| 19 | # Syntax: show-instances.py <base_url> <username> <password> |
| 20 | # |
| 21 | # Example: show-instances.py http://192.168.42.253:30006 admin@opencord.org letmein |
| 22 | |
| 23 | import sys |
| 24 | import time |
| 25 | import requests |
| 26 | from requests.auth import HTTPBasicAuth |
| 27 | |
| 28 | DELAY=1 |
| 29 | |
| 30 | def main(): |
| 31 | if len(sys.argv)<4: |
| 32 | print "Syntax: xos-instances.py <base_url> <username> <password>" |
| 33 | sys.exit(-1) |
| 34 | |
| 35 | base_url = sys.argv[1] |
| 36 | username = sys.argv[2] |
| 37 | password = sys.argv[3] |
| 38 | |
| 39 | auth = HTTPBasicAuth(username, password) |
| 40 | |
| 41 | r = requests.get(base_url + "/xosapi/v1/simpleexampleservice/simpleexampleserviceinstances", auth=auth) |
| 42 | |
| 43 | if r.status_code != 200: |
| 44 | print "Received error response", r.status_code |
| 45 | print r.text |
| 46 | sys.exit(-1) |
| 47 | |
| 48 | |
| 49 | print "%-4s %-40s %-4s %-4s" % ("id", "Name", "Comp", "IP") |
| 50 | for item in r.json()["items"]: |
| 51 | name = item.get("name") |
| 52 | compute_id = item["compute_instance_id"] |
| 53 | if compute_id: |
| 54 | r_compute = requests.get(base_url + "/xosapi/v1/kubernetes/kubernetesserviceinstances/%s" % compute_id, auth=auth) |
| 55 | if r_compute.status_code != 200: |
| 56 | print "Received error response when fetching compute instance", r_compute.status_code |
| 57 | print r_compute.text |
| 58 | sys.exit(-1) |
| 59 | pod_ip = r_compute.json().get("pod_ip", "") |
| 60 | else: |
| 61 | pod_ip = "" |
| 62 | print "%4s %-40s %4s %s" % (item["id"], name, compute_id, pod_ip) |
| 63 | |
| 64 | |
| 65 | if __name__=="__main__": |
| 66 | main() |