CiscoDevNet / ansible-mso

Cisco MSO Ansible Collection
https://galaxy.ansible.com/cisco/mso
GNU General Public License v3.0
26 stars 32 forks source link

cisco.mso.mso_schema_template_anp_epg_contract producing UnboundLocalError when querying EPG with no contracts (DCNE-166) #523

Closed justinbrink closed 3 months ago

justinbrink commented 3 months ago

Community Note

Description

Affected Module Name(s):

MSO version and MSO Platform

APIC version and APIC Platform for Site Level Resources

Collection versions

Output/ Error message

Traceback (most recent call last):
  File "/Users/jbrink/.ansible/tmp/ansible-local-9280142kc74ki/ansible-tmp-1724359497.482809-92913-215026567338156/AnsiballZ_mso_schema_template_anp_epg_contract.py", line 259, in <module>
    _ansiballz_main()
  File "/Users/jbrink/.ansible/tmp/ansible-local-9280142kc74ki/ansible-tmp-1724359497.482809-92913-215026567338156/AnsiballZ_mso_schema_template_anp_epg_contract.py", line 249, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/Users/jbrink/.ansible/tmp/ansible-local-9280142kc74ki/ansible-tmp-1724359497.482809-92913-215026567338156/AnsiballZ_mso_schema_template_anp_epg_contract.py", line 122, in invoke_module
    runpy.run_module(mod_name='ansible_collections.cisco.mso.plugins.modules.mso_schema_template_anp_epg_contract', init_globals=dict(_module_fqn='ansible_collections.cisco.mso.plugins.modules.mso_schema_template_anp_epg_contract', _modlib_path=modlib_path),
  File "<frozen runpy>", line 226, in run_module
  File "<frozen runpy>", line 98, in _run_module_code
  File "<frozen runpy>", line 88, in _run_code
  File "/var/folders/x4/lfbrl1gj5hb178gchm8df2dh0000gn/T/ansible_cisco.mso.mso_schema_template_anp_epg_contract_payload_bhz7s79i/ansible_cisco.mso.mso_schema_template_anp_epg_contract_payload.zip/ansible_collections/cisco/mso/plugins/modules/mso_schema_template_anp_epg_contract.py", line 449, in <module>
  File "/var/folders/x4/lfbrl1gj5hb178gchm8df2dh0000gn/T/ansible_cisco.mso.mso_schema_template_anp_epg_contract_payload_bhz7s79i/ansible_cisco.mso.mso_schema_template_anp_epg_contract_payload.zip/ansible_collections/cisco/mso/plugins/modules/mso_schema_template_anp_epg_contract.py", line 345, in main
UnboundLocalError: cannot access local variable 'contract_ref' where it is not associated with a value

Expected Behavior

Actual Behavior

Playbook tasks to Reproduce

  cisco.mso.mso_schema_template_anp_epg_contract:
    hostname: "{{ mso_hostname }}"
    username: "{{ ansible_user }}"
    password: "{{ ansible_pass }}"
    validate_certs: "{{ validate_certs }}"
    schema: "{{ schema }}"
    template: "{{ template }}"
    anp: "{{ app_profile }}"
    epg: "{{ name_of_epg_with_no_contracts }}"
    state: "query"

The fix is easy and I can provide a PR if needed:

        if contracts:
            if len(found_contracts) == len(contracts):
                mso.existing = found_contracts
            else:
                not_found_contracts = [
                    "Contract with Reference '{0}' and type '{1}' not found".format(mso.contract_ref(**contract), contract.get("type"))
                    for contract in contracts
                    if contract not in found_contracts
                ]
                mso.fail_json(msg=not_found_contracts)
-       elif not mso.existing:
+       elif contract and not mso.existing:
            mso.fail_json(msg="Contract with Reference '{0}' and type '{1}' not found".format(contract_ref, contract.get("type")))
        mso.exit_json()
akinross commented 3 months ago

Hi @justinbrink, thank you for raising the bug. I have linked your PR to the issue and have assigned our team to review.