# -*- coding:utf-8 -*-
"""
Function: get_kvm.py moudle. This moudle mainly involves the
Querying KVM Information function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2017-2020
"""
from scripts.common_function import REDFISH_STATUS_CODE_200
from scripts.common_function import REDFISH_STATUS_CODE_404

FORMAT = '%-40s: %s'


def getkvm_init(parser, parser_dict):
    """
    Function Description:initializing the Command for Querying KVM Information
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('getkvm',
                                   help='''get KVM information''')
    parser_dict['getkvm'] = sub_parser

    return 'getkvm'


def getkvm(client, _):
    """
    Function Description:Setting an IP Version
    Parameter:client refishClient:class object
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None
    # Query KVM information
    resp = get_virtualmedia_info(client, slotid)

    return resp


def get_virtualmedia_info(client, slotid):
    """
    Function Description:KVM query information
    Parameter:client refishClient:class object
    slotid str:manager id
    Modify: 2021.3.8 Delete Action Content Display.
    """
    url = "/redfish/v1/Managers/%s/Kvmservice" % slotid
    resp = client.get_resource(url)
    if resp is None:
        return None
    if resp['status_code'] == REDFISH_STATUS_CODE_200:
        kvm_info = resp.get("resource", "")
        del kvm_info["@odata.context"]
        del kvm_info["@odata.id"]
        del kvm_info["@odata.type"]
        del kvm_info["Actions"]
        for key in kvm_info:
            print(FORMAT % (key, kvm_info.get(key, None)))

    elif resp['status_code'] == REDFISH_STATUS_CODE_404:
        print('Failure: resource was not found')
    return resp
