# -*- coding:utf-8 -*-
"""
Function: del_session.py moudle. This moudle mainly involves the
Deleting a Specified Session function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2019-2020
"""
from scripts import common_function


def delsession_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Deleting a Specified Session
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('delsession',
                                   help='''delete a specified session''')
    sub_parser.add_argument('-I', dest='id', required=True,
                            help='''specifies the session ID to be deleted''')

    parser_dict['delsession'] = sub_parser

    return 'delsession'


def delsession(client, args):
    """
    Function Description:Deleting a Specified Session
    Parameter:client refishClient: class object
    args object:CLI command
    """
    url = "/redfish/v1/SessionService/Sessions/" + args.id
    resp = client.get_resource(url)
    if resp is None:
        return None
    if resp['status_code'] == common_function.REDFISH_STATUS_CODE_404:
        print('Failure: the session does not exist')
        return resp
    if resp['status_code'] != common_function.REDFISH_STATUS_CODE_200:
        common_function.get_error_info(resp)
        return resp
    resp = client.delete_resource(url)
    print_result(resp)
    return resp


def print_result(resp):
    """
    Function Description:print result
    Parameter:resp dict: Redfish Interface Value
    """
    if resp is None:
        return
    if resp['status_code'] == common_function.REDFISH_STATUS_CODE_200:
        print('Success: successfully completed request')
    else:
        common_function.get_error_info(resp)
