def lambda_handler(event,context):
    logger.info("Got Event: {}".format(event))
    try:
        config = fetchFromTransitConfigTable(transitConfigTable)
        logger.info("Got config: {}".format(config))
        if config:
            #deleteVpnConfigurationFromPaGroup() this will be from pan_vpn_generic file
            vpcResult = getItemFromVpcTable(config['TransitVpcTable'],event['VpcId'])
            logger.info("Got vpcResult: {}".format(vpcResult))
            if vpcResult:
                paGroupResult = getItemFromPaGroupInfo(config['TransitPaGroupInfo'],vpcResult['PaGroupName'])
                if paGroupResult:
                    api_key = pan_vpn_generic.getApiKey(paGroupResult['N1Mgmt'], config['UserName'],config['Password'])
                    #Deleting the VPN connections with the PA Group
                    pan_vpn_generic.paGroupDeleteVpn(api_key, paGroupResult, vpcResult['Node1VpnId'],vpcResult['Node2VpnId'])
                    logger.info("Successfully deleted VPN connections VPN1: {}, VPN2: {} with PaGroup: {} ".format(vpcResult['Node1VpnId'],vpcResult['Node2VpnId'],paGroupResult['PaGroupName']))
                    #Delete Item from TransitVpcTable with
                    deleteItemFromVpcTable(config['TransitVpcTable'],event['VpcId'])
                    updatePaGroupInfoTable(config['TransitPaGroupInfo'],vpcResult['PaGroupName'])
                    updateBgpTunnleIpPool(config['TransitBgpTunnelIpPool'], event['VpcId'])
                    if 'VgwAsn' in event: updateVgwAsn(config['TransitVgwAsn'],event['VgwAsn'])
        else:
            logger.error("Not Received any data from TransitConfig table")
    except Exception as e:
        logger.error("Error from deleteTransitVpnConfiguration, Error: {}".format(str(e)))
def lambda_handler(event, context):
    if 'queryStringParameters' in event.keys():
        event = event['queryStringParameters']
    logger.info("Got Event: {}".format(event))
    try:
        config = fetchFromTransitConfigTable(transitConfigTable)
        creds = get_secret(secretName,endpointUrl,region)
        logger.info("Got creds {}".format(creds))
        username = creds['username']
        password = creds['password']
        logger.info("Got config: {}".format(config))
        if config:
            # deleteVpnConfigurationFromPaGroup() this will be from pan_vpn_generic file
            vpcResult = getItemFromVpcTable(config['TransitVpcTable'], event['VpcId'])
            logger.info("Got vpcResult from {} table {}".format(vpcResult, config['TransitVpcTable']))
            if vpcResult:
                paGroupResult = getItemFromPaGroupInfo(config['TransitPaGroupInfo'], vpcResult['PaGroupName'])
                logger.info('Got paGroupResult {} from {}'.format(paGroupResult, config['TransitPaGroupInfo']))
                if paGroupResult:
                    api_key = pan_vpn_generic.getApiKey(paGroupResult['N1Mgmt'], username, password)
                    logger.info('Got apikey ')
                    # Deleting the VPN connections with the PA Group
                    logger.info('Calling paGroupDeleteVpn with {} {} {}'.format(paGroupResult, vpcResult['Node1VpnId'],
                                                                                vpcResult['Node2VpnId']))
                    pan_vpn_generic.paGroupDeleteVpn(api_key, paGroupResult, vpcResult['Node1VpnId'],
                                                     vpcResult['Node2VpnId'])
                    logger.info("Successfully deleted VPN connections VPN1: {}, VPN2: {} with PaGroup: {} ".format(
                        vpcResult['Node1VpnId'], vpcResult['Node2VpnId'], paGroupResult['PaGroupName']))
                    # Delete Item from TransitVpcTable with
                    res = deleteItemFromVpcTable(config['TransitVpcTable'], event['VpcId'])
                    logger.info('Deleted Item from table {}'.format(config['TransitVpcTable']))
                    updatePaGroupInfoTable(config['TransitPaGroupInfo'], vpcResult['PaGroupName'])

                    updateBgpTunnleIpPool(config['TransitBgpTunnelIpPool'], event['VpcId'])
                    if 'VpcId' in event:
                        updateVgwAsn(config['TransitVgwAsn'], event['VpcId'])
                        logger.info('Deleted VgwAsn from table {}'.format(config['TransitVgwAsn']))
                    data1 = {
                        'Result': 'Success',
                        'Reason': 'Updated deleted the VPN and updated the tables' + config['TransitVpcTable']
                    }
                    apioutput = response(data1, 200)
                    logger.info("Sending response={}, hence proceeding  ".format(apioutput))
                    return apioutput
                else:
                    logger.info("Sending response={}, hence proceeding  ".format(apioutput))
                    data2 = {
                        'Result': 'Success',
                        'Reason': 'No Items matched with the GroupName: {}' + vpcResult['PaGroupName']
                    }
                    apioutput = response(data2, 200)
                    return apioutput
            else:
                data3 = {
                    'Result': 'Failure',
                    'Reason': "No Item matched with VpcId {}".format(event['VpcId'])
                }
                apioutput = response(data3, 200)
                return apioutput
        else:
            logger.error("Not Received any data from TransitConfig table")
    except Exception as e:
        logger.error("Error from deleteTransitVpnConfiguration, Error: {}".format(str(e)))