Forum Discussion

syncretism's avatar
syncretism
Icon for Nimbostratus rankNimbostratus
Jan 22, 2019

Searching and Filtering Objects by Metadata

I've been adding some metadata to the WIPs in our f5 GTMs, eg

 

"metadata": [ { "name": "xxAppName", "persist": "true", "value": "Web Service" }, { "name": "xxAppOwner", "persist": "true", "value": "myGroup2" }, { "name": "xxAppSupport", "persist": "true", "value": "support_email@mydomain.com" }, { "name": "xxServiceName", "persist": "true", "value": "myService2" }, { "name": "xxWIPStatus", "persist": "true", "value": "active" } ],

 

This is useful when I'm using Splunk or jq to parse the results, but what if I want to limit the scope of the returned data in the original request, analogous to a filter like "where xxAppSupport == support_email@mydomain.com?" Do any versions of iControl REST (and by extension, the SDK) support this? It'll be really useful when this effort extends to our LTMs. Thanks.

 

2 Replies

  • You can use $select. See "About query parameters" in iControl REST User Guide Version 13.1 (pp. 28-29).

    For example, GET /mgmt/tm/sys/version (equivalent to tmsh show sys version) returns Build, Edition, Product, Title and Version. When you add the query string $select=Edition, the call returns only the Edition field (and its parents). e.g.,

     

     curl -sku : https://localhost/mgmt/tm/sys/version?\$select=Edition | python -m json.tool
    {
        "entries": {
            "https://localhost/mgmt/tm/sys/version/0": {
                "nestedStats": {
                    "entries": {
                        "Edition": {
                            "description": "Point Release 7"
                        }
                    }
                }
            }
        }
    }
    

     

    The above is from LTM but it should also work on GTM/DNS.

  • I am not familiar with the GTM data structure, but generally speaking, you need to come up with a programmatic solution to selectively obtain the element inside the array (from your example, the value of metadata is an array of dict elements).

    Allow me to talk about LTM's datagroup because it has a similar data structure to metadata. Assume that I am looking for the datagroup name that has the records (metadata equivalent) containing the name 'age'.

    Here's how you get a list of all the datagroup names:

     

     curl -sku : https:///mgmt/tm/ltm/data-group/internal?\$select=name \
     | python -m json.tool | grep name\"
                "name": "aol"
                "name": "images"
                "name": "private_net"
                "name": "sys_APM_MS_Office_OFBA_DG"
                "name": "test"
    

     

    So, there are five datagroups on this box. The list of records in 'test' looks like this:

     

     curl -sku : https:///mgmt/tm/ltm/data-group/internal/test?\$select=records
    {"records":[{"name":"addKey","data":"addVal"},{"name":"age","data":"100"},{"name":"name","data":"Satoshi"}]}
    

     

    So the correct answer is "the name 'age' is in the 'test' datagroup and its value is 100". A python code to achieve this goal is below:

     

    from f5.bigip import ManagementRoot
    mgmt = ManagementRoot('host', 'user', 'pass')
    
     Get all the datagroup names
    dataGroupAll = mgmt.tm.ltm.data_group.internals.get_collection()
    names = [d.name for d in dataGroupAll]
    print('List of internal dgs[{}]: {}'.format(len(names), ', '.join(names)))
    
     Scan all the records in all the datagroups until it finds the matching value in the name field
    dataFound = None
    dataIn = None
    for dgName in names:
        print 'Checking ', dgName
        dg =mgmt.tm.ltm.data_group.internals.internal.load(name=dgName)
        for ddg in dg.records:
            if ddg['name'] == :
                dataFound = ddg['data']
                dataIn = dgName
                break
        if dataIn != None:
            break
    
    print('key {}, value {} found in {}'.format(, dataFound, dataIn))
    

     

    Example output below:

     

     

    Checking  aol
    Checking  images
    Checking  private_net
    Checking  sys_APM_MS_Office_OFBA_DG
    Checking  test
    key age, value 100 found in test
    

     

    I hope you can tailor the code to suit your needs.

    See also F5 Python SDK Documentation.