From bc4ff8aed9265b412f57c6228998113eb25a2782 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Fri, 30 Jun 2017 04:41:28 +0530 Subject: [PATCH] Modify flask_control for vmware_host test suites (#26175) Fix adds additional helper methods to get information about host system from vcsim Signed-off-by: Abhijeet Kasurde --- .../docker/vcenter-simulator/flask_control.py | 63 ++++++++++++++++--- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/test/utils/docker/vcenter-simulator/flask_control.py b/test/utils/docker/vcenter-simulator/flask_control.py index 9a6b45e83d9..1ea8cb5b34e 100755 --- a/test/utils/docker/vcenter-simulator/flask_control.py +++ b/test/utils/docker/vcenter-simulator/flask_control.py @@ -190,30 +190,75 @@ def get_govc_vm_info(): return jsonify(vm_output) +@app.route('/govc_host_info') +def get_govc_host_info(): + """ Run govc host.info """ + host_name = request.args.get("host_name") or None + host_output = {} + if host_name: + all_hosts = [host_name] + else: + all_hosts = _get_all_objs(ofilter='H') + for host_system in all_hosts: + host_info = _get_host_info(host_name=host_system) + name = host_info.get('Name', host_system) + host_output[name] = host_info + + return jsonify(host_output) + + +def _get_host_info(host_name=None): + """ + Get all information of host from vcsim + :param vm_name: Name of host + :return: Dictionary containing information about VM, + where KEY represent attributes and VALUE represent attribute's value + """ + cmd = '%s host.info -host=%s 2>&1' % (GOVCPATH, host_name) + + host_info = {} + if host_name is None: + return host_info + host_info = parse_govc_info(cmd) + + return host_info + + def _get_vm_info(vm_name=None): """ Get all information of VM from vcsim :param vm_name: Name of VM - :return: Dictionary containing inforamtion about VM, + :return: Dictionary containing information about VM, where KEY represent attributes and VALUE represent attribute's value """ cmd = '%s vm.info %s 2>&1' % (GOVCPATH, vm_name) - so, se = run_cmd(cmd) - stdout_lines = so.splitlines() vm_info = {} if vm_name is None: return vm_info - - for line in stdout_lines: - if ":" in line: - key, value = line.split(":") - key = key.lstrip() - vm_info[key] = value.strip() + vm_info = parse_govc_info(cmd) return vm_info +def parse_govc_info(cmd): + """ + Helper function to parse output of govc info commands + :param cmd: command variable to run and parse output for + :return: Dictionary containing information about object + """ + so, se = run_cmd(cmd) + stdout_lines = so.splitlines() + info = {} + for line in stdout_lines: + if ":" in line: + key, value = line.split(":", 1) + key = key.lstrip() + info[key] = value.strip() + + return info + + def _get_all_objs(ofilter=None): """ Get all VM Objects from vcsim