voxpupuli / pypuppetdb

Python library for working with the PuppetDB API
Apache License 2.0
92 stars 70 forks source link

order_by doesn't work #256

Closed LordKa0S closed 8 months ago

LordKa0S commented 8 months ago

The order_by parameter for api.base.BaseAPI._query doesn't work.

Minimal reproduction

see code comments for details

puppetdb = connect(
    host="<redacted>",
    ssl_key=private_key,
    ssl_cert=hostcert,
    ssl_verify=ca_cert,
)

try:
    print("querying without order_by")
    puppetdb._query(
        endpoint="nodes",
    )
    print("works without order_by")
except Exception as e:
    print("_query doesn't work", e)

try:
    print("querying order_by with key")
    """
    See [tests/test_api_base_query.py](https://github.com/voxpupuli/pypuppetdb/blob/fa74beba4b80ae6f9e5e986ca52efed1c58f99ef/tests/test_api_base_query.py#L141)
    """
    puppetdb._query(
        endpoint="nodes",
        order_by="certname",
    )
except Exception as e:
    print("error with key", e)

try:
    print("querying order_by with list of maps")
    """
    See [order_by in puppetdb docs](https://github.com/puppetlabs/puppetdb/blob/2051e1243980e41f05d2040109bd443bdbbd8e31/documentation/api/query/v4/paging.markdown?plain=1#L21)
    """
    puppetdb._query(
        endpoint="nodes",
        order_by=[
            {
                "field": "certname",
                "order": "desc",
            },
        ],
    )
except Exception as e:
    print("error with list of maps", e)

Current Output

querying without order_by
works without order_by
querying order_by with key
error with key 400 Client Error: Bad Request for url: <redacted>/pdb/query/v4/nodes?order_by=certname
querying order_by with list of maps
error with list of maps 500 Server Error: Server Error for url: <redacted>/pdb/query/v4/nodes?order_by=field&order_by=order

RCA

Query API version 4 expects a JSON array of maps as a application/x-www-form-urlencoded request field.

LordKa0S commented 8 months ago

change to

print("querying order_by with list of maps")
"""
See [order_by in puppetdb docs](https://github.com/puppetlabs/puppetdb/blob/2051e1243980e41f05d2040109bd443bdbbd8e31/documentation/api/query/v4/paging.markdown?plain=1#L21)
"""
puppetdb._query(
    endpoint="nodes",
    order_by=json.dumps(
        obj=[
            {
                "field": "certname",
                "order": "desc",
            },
        ],
    ),
)