cisco-en-programmability / dnacentersdk

Cisco DNA Center Python SDK
https://dnacentersdk.readthedocs.io/en/latest/
MIT License
70 stars 33 forks source link

ApiError 404 when using DNAC system other than the sandbox #19

Closed dannysheaffer closed 3 years ago

dannysheaffer commented 3 years ago

I am trying to get the sdk working with my lab system. The lab system is using a cert from an inter Microsoft CA server. The root certificate from this CA had to be appended to the cacert.pem file from the certifi package. Using verify=false would allow an API connection but subsequents calls would fail. This problem was resolved when I updated the capers.pem file

(https://geekcontainer.wordpress.com/2018/07/20/sslerror-ssl-certificate_verify_failed-certificate-verify-failed-_ssl-c661-requests-gethttps-somedomain-com/)

After resolving that problem I can establish an API connection with verify=True enabled by subsequent calls are failing...

devices = dnac.devices.get_device_list() Traceback (most recent call last): File "", line 1, in File "/Users/dsheaffe/mycode/dnac-env/lib/python3.7/site-packages/dnacentersdk/api/v1_3_3/devices.py", line 395, in get_device_list json_data = self._session.get(endpoint_full_url, params=params) File "/Users/dsheaffe/mycode/dnac-env/lib/python3.7/site-packages/dnacentersdk/restsession.py", line 398, in get with self.request('GET', url, erc, 0, params=params, **kwargs) as resp: File "/Users/dsheaffe/mycode/dnac-env/lib/python3.7/site-packages/dnacentersdk/restsession.py", line 320, in request check_response_code(response, erc) File "/Users/dsheaffe/mycode/dnac-env/lib/python3.7/site-packages/dnacentersdk/utils.py", line 216, in check_response_code raise ApiError(response) dnacentersdk.exceptions.ApiError: [404] Not Found - The URI requested is invalid or the resource requested, such as a user, does not exist. Also returned when the requested format is not supported by the requested method.

How do I fix this problem? I tried using the debug environment variable to get more info but it doesn't seem to work.

jbogarin commented 3 years ago

@dannysheaffer could you share your code here and the version of DNAC you are testing against?

dannysheaffer commented 3 years ago

Hi Jose

The 404 error happens whenever I try to access an API call.

% source dnac-env/bin/activate

% pip list Package Version


attrs 20.3.0 certifi 2020.11.8 chardet 3.0.4 click 7.1.2 dnacentercli 2.0.0 dnacentersdk 2.0.2 fastjsonschema 2.14.5 future 0.18.2 idna 2.10 importlib-metadata 2.0.0 iniconfig 1.1.1 packaging 20.4 pip 20.2.4 pluggy 0.13.1 py 1.9.0 pyparsing 2.4.7 pytest 6.1.2 requests 2.24.0 requests-toolbelt 0.9.1 setuptools 50.3.2 six 1.15.0 toml 0.10.2 urllib3 1.26.0 zipp 3.4.0

% python Python 3.7.6 (default, Dec 30 2019, 19:38:36) [Clang 10.0.0 (clang-1000.11.45.5)] on darwin Type "help", "copyright", "credits" or "license" for more information.

from dnacentersdk import api /Users/dsheaffe/mycode/dnac-env/lib/python3.7/site-packages/requests/init.py:91: RequestsDependencyWarning: urllib3 (1.26.0) or chardet (3.0.4) doesn't match a supported version! RequestsDependencyWarning)

dnac = api.DNACenterAPI(username="admin", ... password="xxxxxxxx", ... base_url="https://dnac.satlab.cisco.com:443", ... version='1.3.3', ... verify=True)

devices = dnac.devices.get_device_list(family='Switches and Hubs') Traceback (most recent call last): File "", line 1, in File "/Users/dsheaffe/mycode/dnac-env/lib/python3.7/site-packages/dnacentersdk/api/v1_3_3/devices.py", line 395, in get_device_list json_data = self._session.get(endpoint_full_url, params=params) File "/Users/dsheaffe/mycode/dnac-env/lib/python3.7/site-packages/dnacentersdk/restsession.py", line 398, in get with self.request('GET', url, erc, 0, params=params, **kwargs) as resp: File "/Users/dsheaffe/mycode/dnac-env/lib/python3.7/site-packages/dnacentersdk/restsession.py", line 320, in request check_response_code(response, erc) File "/Users/dsheaffe/mycode/dnac-env/lib/python3.7/site-packages/dnacentersdk/utils.py", line 216, in check_response_code raise ApiError(response) dnacentersdk.exceptions.ApiError: [404] Not Found - The URI requested is invalid or the resource requested, such as a user, does not exist. Also returned when the requested format is not supported by the requested method.

Danny

jbogarin commented 3 years ago

@dannysheaffer I see that you closed the issue. Do you still need us to investigate?

dannysheaffer commented 3 years ago

Hi Jose

Yes please, I am still having the problem.

Danny

jbogarin commented 3 years ago

@dannysheaffer I tested with three different versions of DNA Center: 1.3.1 (Sandbox), 2.1.1, and 2.1.2 (internal labs), and all of them worked.

Do you have other instances you can test against to rule out there is a problem with the instance you are using for testing?

You could try to do this to improve logging and see if we get more information about the problem:

import logging
import warnings
from dnacentersdk import DNACenterAPI

# Another way to disable warnings caused by (verify=False)
warnings.filterwarnings('ignore', message='Unverified HTTPS request')

logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)

dna_ch = logging.StreamHandler()
dnac = DNACenterAPI(verify=False, debug=True)
logging.getLogger('dnacentersdk').addHandler(dna_ch)

try:
    devices = dnac.devices.get_device_list(family='Switches and Hubs')
    for device in devices.response:
        print('{:20s}{}'.format(device.hostname, device.upTime))
except ApiError as e:
    print(e)
jordanjms17 commented 3 years ago

@dannysheaffer you can also try making the equivalent API call from the developer toolkit in DNA-C. I think what you're trying to hit is probably Get Device list. Sometimes I get errors even from there and the solution is to disable and re-enable the DNA Center REST API bundle in DNA-C.

dannysheaffer commented 3 years ago

My calls to the API are working now. They always worked from the DNAC platform UI or Postman. I disabled the bundled and renabled the bundle and I believe that's what ultimately fixed the issue. Thank you for the help.