# -*- utf-8 -*-
"""
Function: del_remote_https_server_cert.py moudle. This moudle mainly
 involves the Deleting the Root Certificate of a Remote HTTPS Server
 function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2021
"""
import sys

from scripts import common_function

sub_command = 'delremotehttpsservercert'
url = "/redfish/v1/Managers/{}/SecurityService/Actions/" \
      "SecurityService.DeleteRemoteHttpsServerRootCA"
delete_error = "failed to delete the root certificate of" \
               " the remote HTTPS server."


def del_remote_https_server_cert_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    deleting the Root Certificate of a Remote HTTPS Server.
    Parameter:parser object: action object
              parser_dict dict: dictionary format of subcommand and
               ArgumentParser
    """
    sub_parser = parser.add_parser(sub_command,
                                   help="deleting the root certificate of a"
                                        " remote https server")
    sub_parser.add_argument("-RCI",
                            dest="root_cert_id",
                            choices=["1", "2", "3", "4", "5", "6", "7", "8"],
                            help="id of the root certificate used to"
                                 " authenticate the remote https server.",
                            required=True)
    parser_dict[sub_command] = sub_parser
    return sub_command


def del_remote_https_server_cert(client, args):
    """
    Function Description:del remote https server root ca
    Parameter:client refishClient: class object
              args object:CLI command
    """
    slot_id = client.get_slotid()
    if slot_id is None:
        sys.exit(common_function.UREST_STATUS_CODE_127)

    resp = client.create_resource(url.format(slot_id),
                                  {"RootCertId": int(args.root_cert_id)})
    if resp is None:
        raise common_function.CustomError(delete_error)

    if resp['status_code'] != common_function.REDFISH_STATUS_CODE_200:
        raise common_function.CustomError(resp)

    print('Success: successfully completed request')
    return resp
