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

Don't throw an exception if API's JSON response lacks an expected key #272

Closed
coreywright opened this issue Jun 24, 2022 · 0 comments · Fixed by #273
Closed

Don't throw an exception if API's JSON response lacks an expected key #272

coreywright opened this issue Jun 24, 2022 · 0 comments · Fixed by #273

Comments

@coreywright
Copy link
Contributor

coreywright commented Jun 24, 2022

SUMMARY

If the DO API returns a JSON response with a missing key (eg droplet) or no keys, then don't set the associated variable to None (ie droplet = json_data.get("droplet", None)) and immediately call get() on it as if it's a dict (ie droplet_status = droplet.get("status", None)).

ISSUE TYPE
  • Bug Report
COMPONENT NAME

digital_ocean_droplet

ANSIBLE VERSION
$ ansible --version
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the 
controller starting with Ansible 2.12. Current version: 3.6.9 (default, Jan 26 
2021, 15:33:00) [GCC 8.4.0]. This feature will be removed from ansible-core in 
version 2.12. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
ansible [core 2.11.6] 
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/user/ansible-venv/lib/python3.6/site-packages/ansible
  ansible collection location = /home/user/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/user/ansible-venv/bin/ansible
  python version = 3.6.9 (default, Jan 26 2021, 15:33:00) [GCC 8.4.0]
  jinja version = 3.0.2
  libyaml = True
COLLECTION VERSION
$ ansible-galaxy collection list community.digitalocean
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the 
controller starting with Ansible 2.12. Current version: 3.6.9 (default, Jan 26 
2021, 15:33:00) [GCC 8.4.0]. This feature will be removed from ansible-core in 
version 2.12. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.

# /home/user/ansible-venv/lib/python3.6/site-packages/ansible_collections
Collection             Version
---------------------- -------
community.digitalocean 1.10.0 

# /home/user/.ansible/collections/ansible_collections
Collection             Version
---------------------- -------
community.digitalocean 1.19.0 
CONFIGURATION
$ ansible-config dump --only-changed
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the 
controller starting with Ansible 2.12. Current version: 3.6.9 (default, Jan 26 
2021, 15:33:00) [GCC 8.4.0]. This feature will be removed from ansible-core in 
version 2.12. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
OS / ENVIRONMENT
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.5 LTS
Release:        18.04
Codename:       bionic

$ python -VV
Python 3.6.9 (default, Jan 26 2021, 15:33:00) 
[GCC 8.4.0]
STEPS TO REPRODUCE
  1. Try to create a droplet.
  2. Receive an empty or incomplete JSON respose from DO API.
EXPECTED RESULTS

digital_ocean_droplet doesn't throw an exception, but gracefully handles the empty or incomplete API JSON response.

ACTUAL RESULTS
...
  File \"/tmp/ansible_community.digitalocean.digital_ocean_droplet_payload_0upuk5u4/ansible_community.digitalocean.digital_ocean_droplet_payload.zip/ansible_collections/community/digitalocean/plugins/modules/digital_ocean_droplet.py\", line 919, in <module>
  File \"/tmp/ansible_community.digitalocean.digital_ocean_droplet_payload_0upuk5u4/ansible_community.digitalocean.digital_ocean_droplet_payload.zip/ansible_collections/community/digitalocean/plugins/modules/digital_ocean_droplet.py\", line 915, in main
  File \"/tmp/ansible_community.digitalocean.digital_ocean_droplet_payload_0upuk5u4/ansible_community.digitalocean.digital_ocean_droplet_payload.zip/ansible_collections/community/digitalocean/plugins/modules/digital_ocean_droplet.py\", line 868, in core
  File \"/tmp/ansible_community.digitalocean.digital_ocean_droplet_payload_0upuk5u4/ansible_community.digitalocean.digital_ocean_droplet_payload.zip/ansible_collections/community/digitalocean/plugins/modules/digital_ocean_droplet.py\", line 770, in create
  File \"/tmp/ansible_community.digitalocean.digital_ocean_droplet_payload_0upuk5u4/ansible_community.digitalocean.digital_ocean_droplet_payload.zip/ansible_collections/community/digitalocean/plugins/modules/digital_ocean_droplet.py\", line 650, in ensure_power_on
  File \"/tmp/ansible_community.digitalocean.digital_ocean_droplet_payload_0upuk5u4/ansible_community.digitalocean.digital_ocean_droplet_payload.zip/ansible_collections/community/digitalocean/plugins/modules/digital_ocean_droplet.py\", line 545, in wait_status
AttributeError: 'NoneType' object has no attribute 'get'
coreywright added a commit to coreywright/community.digitalocean that referenced this issue Jun 24, 2022
Instead of throwing an exception, be more defensive in the code: if
the API's JSON response lacks a key and we correspondingly set a
variable to `None`, then don't call `get()` on the variable, but test
the variable first.

    AttributeError: 'NoneType' object has no attribute 'get'

Fixes ansible-collections#272.
coreywright added a commit to coreywright/community.digitalocean that referenced this issue Jun 24, 2022
Instead of throwing an exception, be more defensive in the code: if
the API's JSON response lacks a key and we correspondingly set a
variable to `None`, then don't call `get()` on the variable, but test
the variable first.

    AttributeError: 'NoneType' object has no attribute 'get'

Fixes ansible-collections#272.
coreywright added a commit to coreywright/community.digitalocean that referenced this issue Jun 26, 2022
Instead of throwing an exception, be more defensive in the code: if
the API's JSON response lacks a key and we correspondingly set a
variable to `None`, then don't call `get()` on the variable, but test
the variable first.

    AttributeError: 'NoneType' object has no attribute 'get'

Fixes ansible-collections#272.
@coreywright coreywright changed the title Don't throw an exception if API's JSON respose lacks an expected key Don't throw an exception if API's JSON response lacks an expected key Jun 26, 2022
mamercad pushed a commit that referenced this issue Jun 27, 2022
…#273)

Instead of throwing an exception, be more defensive in the code: if
the API's JSON response lacks a key and we correspondingly set a
variable to `None`, then don't call `get()` on the variable, but test
the variable first.

    AttributeError: 'NoneType' object has no attribute 'get'

Fixes #272.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant