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


def getcsr_init(parser, parser_dict):
    """
    Function Description:initializing the Command for Querying a CSR File
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('getcsr',
                                   help='''get certificate signing request''')
    parser_dict['getcsr'] = sub_parser

    return 'getcsr'


def getcsr(client, _):
    """
    Function Description: Querying a CSR File
    Parameter:client refishClient:class object
    """
    slotid = client.get_slotid()
    if slotid is None:
        return None

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

    if resp is None:
        return None

    if resp['status_code'] == REDFISH_STATUS_CODE_200:

        csrequest = resp['resource'].get('CertificateSigningRequest', None)
        if csrequest is not None:
            print("CertificateSigningRequest:")
            print('%s' % csrequest)
        else:
            print("CertificateSigningRequest: None")

    return resp
