Repository: ansible/ansible

Issue:
meraki_network module doesn't always use org_id and net_id when provided
##### SUMMARY
`org_id` and `net_id` can be provided to improve playbook execution performance since less lookups are needed. `org_id` and `net_id` should be used within the module, when possible to avoid unnecessarily API calls.

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
meraki_network

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.7.0.dev0 (meraki/meraki_device 387c37e255) last updated 2018/06/06 20:11:36 (GMT -500)
  config file = None
  configured module search path = ['/Users/kbreit/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/kbreit/Documents/Programming/ansible/lib/ansible
  executable location = /Users/kbreit/Documents/Programming/ansible/bin/ansible
  python version = 3.5.4 (default, Feb 25 2018, 14:56:02) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->


Discussion:
Files identified in the description:
* [lib/ansible/modules/network/meraki/meraki_network.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/network/meraki/meraki_network.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @dagwieers
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/network/meraki/meraki_network.py b/lib/ansible/modules/network/meraki/meraki_network.py
--- a/lib/ansible/modules/network/meraki/meraki_network.py
+++ b/lib/ansible/modules/network/meraki/meraki_network.py
@@ -199,10 +199,10 @@ def main():
     # manipulate or modify the state as needed (this is going to be the
     # part where your module will do what it needs to do)
 
-    if meraki.params['org_name']:
-        nets = meraki.get_nets(org_name=meraki.params['org_name'])
-    elif meraki.params['org_id']:
-        nets = meraki.get_nets(org_id=meraki.params['org_id'])
+    org_id = meraki.params['org_id']
+    if not org_id:
+        org_id = meraki.get_org_id(meraki.params['org_name'])
+    nets = meraki.get_nets(org_id=org_id)
 
     if meraki.params['state'] == 'query':
         if not meraki.params['net_name'] and not meraki.params['net_id']:
@@ -215,14 +215,9 @@ def main():
     elif meraki.params['state'] == 'present':
         if meraki.params['net_name']:  # FIXME: Idempotency check is ugly here, improve
             if is_net_valid(meraki, meraki.params['net_name'], nets) is False:
-                if meraki.params['org_name']:  # FIXME: This can be cleaned up...maybe
-                    path = meraki.construct_path('create',
-                                                 org_name=meraki.params['org_name']
-                                                 )
-                elif meraki.params['org_id']:
-                    path = meraki.construct_path('create',
-                                                 org_id=meraki.params['org_id']
-                                                 )
+                path = meraki.construct_path('create',
+                                             org_id=org_id
+                                             )
                 r = meraki.request(path,
                                    method='POST',
                                    payload=json.dumps(payload)
@@ -242,8 +237,7 @@ def main():
                     meraki.result['changed'] = True
     elif meraki.params['state'] == 'absent':
         if is_net_valid(meraki, meraki.params['net_name'], nets) is True:
-            net_id = meraki.get_net_id(org_name=meraki.params['org_name'],
-                                       net_name=meraki.params['net_name'],
+            net_id = meraki.get_net_id(net_name=meraki.params['net_name'],
                                        data=nets)
             path = meraki.construct_path('delete', net_id=net_id)
             r = meraki.request(path, method='DELETE')


Test patch:
diff --git a/test/integration/targets/meraki_network/tasks/main.yml b/test/integration/targets/meraki_network/tasks/main.yml
--- a/test/integration/targets/meraki_network/tasks/main.yml
+++ b/test/integration/targets/meraki_network/tasks/main.yml
@@ -232,4 +232,4 @@
     that:
       - '"org_name or org_id parameters are required" in delete_all_no_org.msg'
       # - query_deleted_org_id.data | length == 6     
-      - query_deleted.data | length == 0
\ No newline at end of file
+      # - query_deleted.data | length == 0
\ No newline at end of file