Search code examples
google-cloud-pythongoogle-cloud-identity

Python: Google Cloud Identity API > Devices > List


Using Google Cloud Identity API > Devices > List request with filter to include a query by serial number works as expected for records where the serial number does not include spaces i.e. 5HCS8767 but will return an empty response if the serial number includes spaces such as in with VMWare VMs i.e. WMWARE-34 98 83 DE 38... etc.

The request is an http get request like so:

device_query = f'{device_endpoint_url}?filter=serial:{serial_number}&view=COMPANY_INVENTORY'

res = requests.get(device_query, headers=headers).json()

A successful response looks like this:

{'devices': [{'name': 'devices/jsihshd98ashd98ahsd', 'createTime': '1000-01-01T01:01:01.011Z', 'lastSyncTime': '1970-01-01T00:00:00Z', 'ownerType': 'COMPANY', 'deviceType': 'CHROME_OS', 'serialNumber': '<SERIAL NUMBER>', 'assetTag': '<ASSET TAG>'}], 'nextPageToken': '<NEXT PAGE TOKEN>'}

A response to querying a serial number with spaces, the return is empty.

I have tried urlencoding the serial with the same results.

Has anyone come across a similar situation?

Thanks,


Solution

  • When querying for serial numbers without spaces the call works as noted in the documentation:

    GET request looks like this and it is encoded:

    https://cloudidentity.googleapis.com/v1/devices?serial:CSD9S7E7W&view=COMPANY_INVENTORY
    

    When querying for serial numbers with spaces: (EMPTY RESPONSE)

    GET request looks like this and is encoded:

    https://cloudidentity.googleapis.com/v1/devices?filter=serial:VMWARE-56 4D F9 5D 52 07 0F CF-E1 0C ED E4 DD 40 2A B2&view=COMPANY_INVENTORY
    https://cloudidentity.googleapis.com/v1/devices?filter=serial:VMWARE-56%204D%20F9%205D%2052%2007%200F%20CF-E1%200C%20ED%20E4%20DD%2040%202A%20B2&view=COMPANY_INVENTORY
    

    When querying for serial numbers with spaces: (EXPECTED RESPONSE - WORKING)

    Adding and encoding quotes before and after the serial number %22

    GET request looks like this and is encoded:

    https://cloudidentity.googleapis.com/v1/devices?filter=serial:"VMWARE-23 4H C9 4D"&view=COMPANY_INVENTORY
    
    https://cloudidentity.googleapis.com/v1/devices?filter=serial:%22VMWARE-23%204H%20C9%204D%22&view=COMPANY_INVENTORY
    

    Using quotes before and after the serial number works with serial numbers with spaces and without them so better just use them.

    Using the python library urllib:

    import urllib.parse
    serial_number = urllib.parse.quote(f'"{serialNumber}"')
    
    "VMWARE-23 4H C9 4D" >>> %22VMWARE-23%204H%20C9%204D%22
    
    res = requests.get(f'https://cloudidentity.googleapis.com/v1/devices?filter=serial:{serial_number}&view=COMPANY_INVENTORY', headers=headers)