<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic DNAC API trying to search hostname case insensative in Network Platform API</title>
    <link>https://community.cisco.com/t5/network-platform-api/dnac-api-trying-to-search-hostname-case-insensative/m-p/4806806#M8793</link>
    <description>&lt;DIV&gt;Is there a way to do a case-insensitive search using the api?&amp;nbsp; Desired results would be to return devices named TEST, test, Test, etc&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Example code:&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests
import json

USER = 'REMOVED'
PASSWORD = 'REMOVED'
BASE_URL = 'https://dna.server.com/dna'

AUTH = requests.auth.HTTPBasicAuth(USER, PASSWORD)
AUTH_RESPONSE = requests.post(BASE_URL + '/system/api/v1/auth/token', auth=AUTH, verify=False)

TOKEN = json.loads(AUTH_RESPONSE.text)['Token']

HEADERS = { 'X-Auth-Token': TOKEN }
RESOURCE = '/intent/api/v1/network-device' + '?hostname=.*test.*'

call_response = requests.get(BASE_URL + RESOURCE, headers=HEADERS, verify=False)
devices = json.loads(call_response.text)['response']

for device in devices:
    print('Hostname: {hostname}, Type: {type}, MAC: {mac}, Serial: {serial}'.format(serial=device['serialNumber'],hostname=device['hostname'],type=device['type'],mac=device['macAddress']))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
    <pubDate>Mon, 03 Apr 2023 15:05:40 GMT</pubDate>
    <dc:creator>brunop</dc:creator>
    <dc:date>2023-04-03T15:05:40Z</dc:date>
    <item>
      <title>DNAC API trying to search hostname case insensative</title>
      <link>https://community.cisco.com/t5/network-platform-api/dnac-api-trying-to-search-hostname-case-insensative/m-p/4806806#M8793</link>
      <description>&lt;DIV&gt;Is there a way to do a case-insensitive search using the api?&amp;nbsp; Desired results would be to return devices named TEST, test, Test, etc&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Example code:&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests
import json

USER = 'REMOVED'
PASSWORD = 'REMOVED'
BASE_URL = 'https://dna.server.com/dna'

AUTH = requests.auth.HTTPBasicAuth(USER, PASSWORD)
AUTH_RESPONSE = requests.post(BASE_URL + '/system/api/v1/auth/token', auth=AUTH, verify=False)

TOKEN = json.loads(AUTH_RESPONSE.text)['Token']

HEADERS = { 'X-Auth-Token': TOKEN }
RESOURCE = '/intent/api/v1/network-device' + '?hostname=.*test.*'

call_response = requests.get(BASE_URL + RESOURCE, headers=HEADERS, verify=False)
devices = json.loads(call_response.text)['response']

for device in devices:
    print('Hostname: {hostname}, Type: {type}, MAC: {mac}, Serial: {serial}'.format(serial=device['serialNumber'],hostname=device['hostname'],type=device['type'],mac=device['macAddress']))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Mon, 03 Apr 2023 15:05:40 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/dnac-api-trying-to-search-hostname-case-insensative/m-p/4806806#M8793</guid>
      <dc:creator>brunop</dc:creator>
      <dc:date>2023-04-03T15:05:40Z</dc:date>
    </item>
    <item>
      <title>Re: DNAC API trying to search hostname case insensative</title>
      <link>https://community.cisco.com/t5/network-platform-api/dnac-api-trying-to-search-hostname-case-insensative/m-p/4807943#M8795</link>
      <description>&lt;P&gt;Python is case sensitive, so search for the "test" won't get the "TEST".&lt;BR /&gt;To search&amp;nbsp;&lt;SPAN&gt;TEST, test, and Test may need to do the search 3 times.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 21:58:41 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/dnac-api-trying-to-search-hostname-case-insensative/m-p/4807943#M8795</guid>
      <dc:creator>yawming</dc:creator>
      <dc:date>2023-04-04T21:58:41Z</dc:date>
    </item>
    <item>
      <title>Re: DNAC API trying to search hostname case insensative</title>
      <link>https://community.cisco.com/t5/network-platform-api/dnac-api-trying-to-search-hostname-case-insensative/m-p/4807971#M8796</link>
      <description>&lt;P&gt;I don't think this is correct.&amp;nbsp; My python code is not doing the comparison, it is the API that is returning results based on the supplied criteria.&lt;/P&gt;&lt;P&gt;In python:&amp;nbsp; "test".casefold() == "TEST".casefold() is a true statement.&lt;/P&gt;&lt;P&gt;In the DNAC web interface the advanced searches are not case sensitive.&amp;nbsp; The API should be capable of doing similar.&lt;/P&gt;&lt;P&gt;I can use '.*TEST.*' to search for devices containing the work for example: 'switchTESTdevice' but only if its an exact match.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 23:19:02 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/dnac-api-trying-to-search-hostname-case-insensative/m-p/4807971#M8796</guid>
      <dc:creator>brunop</dc:creator>
      <dc:date>2023-04-04T23:19:02Z</dc:date>
    </item>
    <item>
      <title>Re: DNAC API trying to search hostname case insensative</title>
      <link>https://community.cisco.com/t5/network-platform-api/dnac-api-trying-to-search-hostname-case-insensative/m-p/4807994#M8797</link>
      <description>&lt;P&gt;Sorry that misunderstood you, you are asking for case -INsensitive not sensitive. Which I don't know if we can do it within DNAC API, but if we do the search from the response then we can do that of cause.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 00:42:15 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/dnac-api-trying-to-search-hostname-case-insensative/m-p/4807994#M8797</guid>
      <dc:creator>yawming</dc:creator>
      <dc:date>2023-04-05T00:42:15Z</dc:date>
    </item>
    <item>
      <title>Re: DNAC API trying to search hostname case insensative</title>
      <link>https://community.cisco.com/t5/network-platform-api/dnac-api-trying-to-search-hostname-case-insensative/m-p/4808744#M8798</link>
      <description>&lt;P&gt;Cross-posted here:&amp;nbsp; &lt;A href="https://networkengineering.stackexchange.com/questions/82824/cisco-dnac-case-insensitive-search/82827#82827" target="_blank"&gt;https://networkengineering.stackexchange.com/questions/82824/cisco-dnac-case-insensitive-search/82827#82827&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 11:38:38 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/dnac-api-trying-to-search-hostname-case-insensative/m-p/4808744#M8798</guid>
      <dc:creator>brunop</dc:creator>
      <dc:date>2023-04-05T11:38:38Z</dc:date>
    </item>
    <item>
      <title>Re: DNAC API trying to search hostname case insensative</title>
      <link>https://community.cisco.com/t5/network-platform-api/dnac-api-trying-to-search-hostname-case-insensative/m-p/4809457#M8799</link>
      <description>&lt;P&gt;At least on 2.3.3.6-70045 I see the API is not doing a case insensitive match.&lt;/P&gt;
&lt;P&gt;I'm thinking of the following options. One could be to use a simpler query like ".*est.* instead of ".*test.* so you can get results from "test" and "Test".&lt;/P&gt;
&lt;P&gt;Another option is to calculate the combinations you have available between uppercase and lowercase for the query you want. For example "TEST, test, Test, tesT, ..." and perform an API call for each one. If you are interested in this option, I did this small test.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;import json
import requests
from itertools import product
from typing import List

USER = "USERNAME"
PASSWORD = "PASSWORD"
BASE_URL = "https://DNA/dna"

AUTH = requests.auth.HTTPBasicAuth(USER, PASSWORD)
AUTH_RESPONSE = requests.post(
    BASE_URL + "/system/api/v1/auth/token", auth=AUTH, verify=False
)

TOKEN = json.loads(AUTH_RESPONSE.text)["Token"]
HEADERS = {"X-Auth-Token": TOKEN}

# https://stackoverflow.com/a/69782247/922218
def case_insensitive(pattern: str) -&amp;gt; List[str]:
    result: List = []
    cases = zip(*[pattern, pattern.swapcase()])
    for permutation in product(*cases):
        result.append("".join(permutation))
    return result


devices: List = []
hosts = case_insensitive(pattern="edge")

for host in hosts:
    RESOURCE = f"/intent/api/v1/network-device?hostname=.*{host}.*"
    call_response = requests.get(BASE_URL + RESOURCE, headers=HEADERS, verify=False)
    devices += json.loads(call_response.text)["response"]


for device in devices:
    print(
        "Hostname: {hostname}, Type: {type}, MAC: {mac}, Serial: {serial}".format(
            serial=device["serialNumber"],
            hostname=device["hostname"],
            type=device["type"],
            mac=device["macAddress"],
        )
    )&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, this is not optimal. For an 4 letter input, you will have a 16 possible combinations, so 16 APIs call. In Big O notation, this will be a &lt;STRONG&gt;&lt;EM&gt;O(n²)&lt;/EM&gt;&lt;/STRONG&gt;, which is not good. So if you go with this option, be careful with larger inputs.&lt;/P&gt;
&lt;P&gt;Probably the best approach is to normalize hostnames, so this situation can be minimized.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Apr 2023 12:21:05 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/dnac-api-trying-to-search-hostname-case-insensative/m-p/4809457#M8799</guid>
      <dc:creator>Jesus Illescas</dc:creator>
      <dc:date>2023-04-06T12:21:05Z</dc:date>
    </item>
  </channel>
</rss>

