# -*- coding: utf-8 -*-
"""
Function: get_syslog_cert.py moudle. This moudle mainly involves the
 querying a Syslog Certificate function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2020-2021
"""


def getsyslogcert_init(parser, parser_list):
    """
    #=====================================================================
    #   @Description:  get syslog certificate init
    #   @Method:  getstateless_init
    #   @Param:
    #   @Return:
    #   @Date:
    #=====================================================================
    """

    sub_parser = parser.add_parser('getsyslogcert',
                                   help='''get syslog certificate''')
    parser_list['getsyslogcert'] = sub_parser

    return 'getsyslogcert'


def _printcertificateinformation(certinformation):
    """
    #=====================================================================
    #   @Description: _printcertificateinformation
    #   @Method:  _printcertificateinformation
    #   @Param:
    #   @Return:
    #   @Date:
    #=====================================================================
    """
    proplength = 20
    m_format = '%-*s:%-*s'

    for k in certinformation:
        print(m_format % (proplength, k, proplength, certinformation[k]))


def getsyslogcert(client, _):
    """
    #=====================================================================
    #   @Description:  get syslog certificate
    #   @Method:  getsyslogcert
    #   @Param:
    #   @Return:
    #   @Date:
    #=====================================================================
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None

    url = "/redfish/v1/Managers/%s/SyslogService" % slotid
    resp = client.get_resource(url)

    if resp is None:
        return None

    if resp['status_code'] == 200:
        print_result(resp)

    return resp


def print_result(resp):
    """
    功能描述：打印 请求结果
    参数：resp (dict):redfish 接口返回值
    返回值：None
    异常描述：None
    修改：None
    """
    rootcertificate = resp['resource'].get("RootCertificate", None)
    clientcertificate = resp['resource'].get("ClientCertificate", None)
    if rootcertificate is None and clientcertificate is None:
        print("no data available for the resource")
        return
    if rootcertificate is not None:
        print('[RootCertificate]')
        _printcertificateinformation(rootcertificate)
        print('')
    if clientcertificate is not None:
        print('[ClientCertificate]')
        _printcertificateinformation(clientcertificate)
