Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intersight_rest_api Module - $filter Query Parameter formatting problem #109

Open
miarond opened this issue Feb 6, 2024 · 2 comments
Open

Comments

@miarond
Copy link

miarond commented Feb 6, 2024

I have been testing API calls to Intersight using the intersight_rest_api module, and I have encountered a problem with the $filter Query Parameter, specifically when using the in Operator. According to the API documentation, the $filter Query Parameter can filter results based on the presence of a field value in a list of possible values, like this:

$filter=Name in ('hostname1', 'hostname2', 'hostname3')

I have tested this syntax using the Intersight API Docs Swagger UI and it definitely works as expected. Using the asset/Targets API endpoint, I can provide a list of possible hostnames in the format above, contained in parenthesis with each element surrounded by single quotes, and the API response will contain only those Targets included in my search query.

When attempting this using the Intersight Ansible Collection, I'm finding that only the first search element is recognized and passed to the API Endpoint - I only get one search result back from each API call. I have tried several methods of formatting this $filter string, including escaping the single quotes and the parenthesis, but I have been unable to get a successful result from the module.

Environment Information:

  • Python v3.12.0
  • Ansible v2.16.3
  • Intersight Collection v2.0.7

Example Task:

- name: Call Intersight
  cisco.intersight.intersight_rest_api:
    api_private_key: "{{ api_private_key }}"
    api_key_id: "{{ api_key_id }}"
    api_uri: "{{ api_uri | default(omit) }}"
    resource_path: /asset/Targets
    query_params:
      $filter: "Name in ('Pod1-DNACenter','Pod2-DNACenter','Pod2-Services','Pod1-Services')"
      $select: "Moid,TargetId,Name,Tags"
    api_body: {}
  register: serial_asset_target_result

Response:

ok: [localhost] => {
    "api_response": {
        "ClassId": "asset.Target",
        "Moid": "<omitted>",
        "Name": "Pod1-DNACenter",
        "ObjectType": "asset.Target",
        "Tags": [],
        "TargetId": [
            "<omitted>"
        ]
    },
    "changed": false,
    "invocation": {
        "module_args": {
            "api_body": {},
            "api_key_id": "<omitted>",
            "api_private_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "api_uri": "https://<omitted>.intersight.com/api/v1",
            "list_body": null,
            "query_params": {
                "$filter": "Name in ('Pod1-DNACenter','Pod2-DNACenter','Pod2-Services','Pod1-Services')",
                "$select": "Moid,TargetId,Name,Tags"
            },
            "resource_path": "/asset/Targets",
            "return_list": false,
            "state": "present",
            "update_method": "patch",
            "use_proxy": true,
            "validate_certs": true
        }
    },
    "trace_id": "<omitted>"
}

Example Response from API Docs Swagger UI:

{
  "ObjectType": "asset.Target.List",
  "Results": [
    {
      "ClassId": "asset.Target",
      "Moid": "<omitted>",
      "Name": "Pod1-DNACenter",
      "ObjectType": "asset.Target",
      "Tags": [],
      "TargetId": [
        "<omitted>"
      ]
    },
    {
      "ClassId": "asset.Target",
      "Moid": "<omitted>",
      "Name": "Pod1-Services",
      "ObjectType": "asset.Target",
      "Tags": [],
      "TargetId": [
        "<omitted>"
      ]
    },
    {
      "ClassId": "asset.Target",
      "Moid": "<omitted>",
      "Name": "Pod2-DNACenter",
      "ObjectType": "asset.Target",
      "Tags": [],
      "TargetId": [
        "<omitted>"
      ]
    },
    {
      "ClassId": "asset.Target",
      "Moid": "<omitted>",
      "Name": "Pod2-Services",
      "ObjectType": "asset.Target",
      "Tags": [],
      "TargetId": [
        "<omitted>"
      ]
    }
  ]
}

I don't know where exactly the problem is occurring, but it seems like a string formatting issue in the Collection code itself, or some quirk in Ansible. Please look into this and determine if it can be fixed or a workaround put in place. Thank you!

@dsoper2
Copy link
Collaborator

dsoper2 commented Feb 21, 2024

By default the intersight_rest_api module only returns a single result based on the query_params. The return_list parameter can be used to get all results from the API call. Here's an example that's working in my environment:

- name: "Get targets with filter in query"
  cisco.intersight.intersight_rest_api:
    <<: *api_info
    resource_path: /asset/Targets
    query_params:
      $filter: "Name in ('hx-mgmt-node-2','hx-mgmt-node-3','hx-mgmt-node-4')"
      $select: "Moid,TargetId,Name,Tags"
    return_list: true
  register: serial_asset_target_result

"api_response": [
    {
        "ClassId": "asset.Target",
        "Moid": "636d5e5e6f72612d326297d2",
        "Name": "hx-mgmt-node-2",
        "ObjectType": "asset.Target",
        "Tags": [],
        "TargetId": [
            "WZP23300RXM"
        ]
    },
    {
        "ClassId": "asset.Target",
        "Moid": "636d5e166f72612d3262942d",
        "Name": "hx-mgmt-node-3",
        "ObjectType": "asset.Target",
        "Tags": [],
        "TargetId": [
            "WZP23300RXN"
        ]
    },
    {
        "ClassId": "asset.Target",
        "Moid": "636d5e2e6f72612d32629566",
        "Name": "hx-mgmt-node-4",
        "ObjectType": "asset.Target",
        "Tags": [],
        "TargetId": [
            "WZP23300P22"
        ]
    }
]

@miarond
Copy link
Author

miarond commented Feb 26, 2024

@dsoper2 Thanks for the advice! I didn't notice that option - I'll give it a try :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants