TypeError: unhashable type: 'list' python rest api

425 Views Asked by At

Please see my sample code and help me to fix it.

I am getting TypeError: unhashable type: 'list'.

How do I edit this script to fix the problem?

def test_configure_destination_port(omx_ip, verb, path, header):
    # start common1
    thisfile = os.path.basename(__file__)
    # filesize = os.path.getsize('log_pass.log')
    start_time = datetime.now()
    uri = "https://" + omx_ip + '/api' + path
    
# the error starts here on the payload line.
File "/test_configure_destination_port.py", line 24, in test_configure_destination_port
    payload = {
TypeError: unhashable type: 'list'
    payload = {

        [
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 1,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990301
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.101",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 1
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 2,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990302
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.105",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 2
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 5,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990305
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.201",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 3
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 6,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990306
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.205",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 4
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 1,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990301
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.31",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 5
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 2,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990302
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.35",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 6
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 5,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990305
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.41",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 7
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 6,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990306
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.45",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 8
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 1,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990301
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.101",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 9
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 2,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990302
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.105",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 10
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 5,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990305
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.201",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 11
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 6,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990306
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.205",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 12
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 1,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990301
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.31",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 13
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 2,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990302
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.35",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 14
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 5,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990305
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.41",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 15
                    }
                ]
            },
            {
                "IsPresent": True,
                "Input": {
                    "InputType": "Port",
                    "Port": 6,
                    "VlanIdentifier": 0,
                    "ObservationDomainId": 990306
                },
                "Destinations": [
                    {
                        "DestinationHostIp": "192.168.1.45",
                        "DestinationHostPort": 4739,
                        "DisableArp": False,
                        "DestMac": "E8:EA:6A:1E:85:0C",
                        "DestinationId": 16
                    }
                ]
            }
        ]
       
    }

    # get put response
    response = requests.request(verb, uri, verify=False, headers=header, json=payload)
    r = response.status_code
    pretty_data = json.dumps(response.json(), indent=4, sort_keys=True)
    sentrequest = json.dumps(payload, indent=4, sort_keys=True)
        
    fileConfig('logging_pass_config.ini')
    logger = logging.getLogger()
    file_stat = os.stat('log_pass.log')     

    # print request status
    if r == 200:
        print('PASS\nTest Case: ' + verb + " " + uri + '\n' + 'Response Code = ' + str(r) + '\n\n'
                    + "Path returned: " + pretty_data)

        print('\nSent Request: ' + sentrequest + '\n')
        fileConfig('logging_pass_config.ini')
        logger = logging.getLogger()
        file_stat = os.stat('log_pass.log')
        logger.debug('\n\nPASS\n\nTest Case: ' + verb + " " + uri + '\n' +
        '\nResponse = ' + str(r) + '\n\nPath returned: ' + pretty_data + '\n')

        fileConfig('logging_pass_config.ini')
        logger = logging.getLogger()
        logger.debug('\n\nYou ran ' + str(next(iteration)) + ' possible combinations for ' + thisfile + '\n')
         
        fileConfig('logging_pass_config.ini')
        logger = logging.getLogger()
    
    else:

        print('what\n')

        # start_timit was imported
        end_timit = timeit.default_timer()          
        elapsed_time = (end_timit - start_timit)    
        convert_float_to_str = str(elapsed_time)    
        split_string_at_period = convert_float_to_str.split(".")
        elapsed_seconds = (split_string_at_period[0])           
        elapsed_seconds = int(elapsed_seconds)
        print('Elapsed time is: ' + str(elapsed_seconds) + ' second' )
        logger.debug('Elapsed time is : ' + str(elapsed_seconds) + ' second') 
        
        if r == 401:
            print (r)
            fileConfig('logging_fail_config.ini')
            logger = logging.getLogger()
            print('Your Token has expired. Generate a new one')
            logger.debug('Your Token has expired. Generate a new one')
            header = generate_token.acquire_token(omx_ip, 'system', 'system')
        
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        check for expired token
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        # setup logger
        fileConfig('logging_pass_config.ini')
        logger = logging.getLogger()
        
        # start_timit was imported
        end_timit = timeit.default_timer()          
        elapsed_time = (end_timit - start_timit)    
        convert_float_to_str = str(elapsed_time)    
        split_string_at_period = convert_float_to_str.split(".")
        elapsed_seconds = (split_string_at_period[0])           
        elapsed_seconds = int(elapsed_seconds)
            
    time_elapsed = datetime.now() - start_time
    print(f'\n\nTest Time for ' + thisfile + '  is: ' + str(time_elapsed) + '\n')
    logger.debug(f'\n\nTest Time for ' + thisfile + '  is: ' + str(time_elapsed) + '\n')


def test_configure_destination_port_main(omx_ip, slotnum, portnum, header_token):
    test_configure_destination_port(omx_ip, 'PUT', '/v2/Systems/Slot/' + str(slotnum) + '/Ipfix/Config/DestGrp', header_token)
0

There are 0 best solutions below