Add net_id support to config_template (#42446)

- Added net_id support to config_template
- Changed integration test to use blocks to cleanup on failure
This commit is contained in:
Kevin Breit 2018-07-09 04:11:16 -05:00 committed by Dag Wieers
parent 9036f846a3
commit eeada86554
2 changed files with 116 additions and 116 deletions

View file

@ -44,6 +44,9 @@ options:
net_name: net_name:
description: description:
- Name of the network to bind or unbind configuration template to. - Name of the network to bind or unbind configuration template to.
net_id:
description:
- ID of the network to bind or unbind configuration template to.
auto_bind: auto_bind:
description: description:
- Optional boolean indicating whether the network's switches should automatically bind to profiles of the same model. - Optional boolean indicating whether the network's switches should automatically bind to profiles of the same model.
@ -95,9 +98,9 @@ def get_template_id(meraki, name, data):
meraki.fail_json(msg='No configuration template named {0} found'.format(name)) meraki.fail_json(msg='No configuration template named {0} found'.format(name))
def is_network_bound(meraki, nets, net_name, template_id): def is_network_bound(meraki, nets, net_id, template_id):
for net in nets: for net in nets:
if net['name'] == net_name: if net['id'] == net_id:
try: try:
if net['configTemplateId'] == template_id: if net['configTemplateId'] == template_id:
return True return True
@ -116,12 +119,9 @@ def delete_template(meraki, org_id, name, data):
return response return response
def bind(meraki, org_name, net_name, name, data): def bind(meraki, org_name, net_id, nets, name, data):
template_id = get_template_id(meraki, name, data) template_id = get_template_id(meraki, name, data)
nets = meraki.get_nets(org_name=org_name) if is_network_bound(meraki, nets, net_id, template_id) is False:
# meraki.fail_json(msg='Nets', nets=nets)
if is_network_bound(meraki, nets, net_name, template_id) is False:
net_id = meraki.get_net_id(net_name=net_name, data=nets)
path = meraki.construct_path('bind', function='config_template', net_id=net_id) path = meraki.construct_path('bind', function='config_template', net_id=net_id)
payload = dict() payload = dict()
payload['configTemplateId'] = template_id payload['configTemplateId'] = template_id
@ -134,11 +134,9 @@ def bind(meraki, org_name, net_name, name, data):
return r return r
def unbind(meraki, org_name, net_name, name, data): def unbind(meraki, org_name, net_id, nets, name, data):
template_id = get_template_id(meraki, name, data) template_id = get_template_id(meraki, name, data)
nets = meraki.get_nets(org_name=org_name) if is_network_bound(meraki, nets, net_id, template_id) is True:
net_id = meraki.get_net_id(net_name=net_name, data=nets)
if is_network_bound(meraki, nets, net_name, template_id) is True:
path = meraki.construct_path('unbind', function='config_template', net_id=net_id) path = meraki.construct_path('unbind', function='config_template', net_id=net_id)
meraki.result['changed'] = True meraki.result['changed'] = True
r = meraki.request(path, method='POST') r = meraki.request(path, method='POST')
@ -157,6 +155,7 @@ def main():
org_id=dict(type='int'), org_id=dict(type='int'),
config_template=dict(type='str', aliases=['name']), config_template=dict(type='str', aliases=['name']),
net_name=dict(type='str'), net_name=dict(type='str'),
net_id=dict(type='str'),
# config_template_id=dict(type='str', aliases=['id']), # config_template_id=dict(type='str', aliases=['id']),
auto_bind=dict(type='bool'), auto_bind=dict(type='bool'),
) )
@ -210,9 +209,12 @@ def main():
# manipulate or modify the state as needed (this is going to be the # manipulate or modify the state as needed (this is going to be the
# part where your module will do what it needs to do) # part where your module will do what it needs to do)
org_id = meraki.params['org_id'] org_id = meraki.params['org_id']
if meraki.params['org_name']: if meraki.params['org_name']:
org_id = meraki.get_org_id(meraki.params['org_name']) org_id = meraki.get_org_id(meraki.params['org_name'])
net_id = meraki.params['net_id']
nets = meraki.get_nets(org_id=org_id)
if meraki.params['net_name']:
net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets)
if meraki.params['state'] == 'query': if meraki.params['state'] == 'query':
meraki.result['data'] = get_config_templates(meraki, org_id) meraki.result['data'] = get_config_templates(meraki, org_id)
@ -220,7 +222,8 @@ def main():
if meraki.params['net_name']: if meraki.params['net_name']:
template_bind = bind(meraki, template_bind = bind(meraki,
meraki.params['org_name'], meraki.params['org_name'],
meraki.params['net_name'], net_id,
nets,
meraki.params['config_template'], meraki.params['config_template'],
get_config_templates(meraki, org_id)) get_config_templates(meraki, org_id))
elif meraki.params['state'] == 'absent': elif meraki.params['state'] == 'absent':
@ -232,7 +235,8 @@ def main():
else: else:
config_unbind = unbind(meraki, config_unbind = unbind(meraki,
meraki.params['org_name'], meraki.params['org_name'],
meraki.params['net_name'], net_id,
nets,
meraki.params['config_template'], meraki.params['config_template'],
get_config_templates(meraki, org_id)) get_config_templates(meraki, org_id))

View file

@ -3,26 +3,27 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
--- ---
# - name: Test an API key is provided - block:
# fail: - name: Test an API key is provided
# msg: Please define an API key fail:
# when: auth_key is not defined msg: Please define an API key
when: auth_key is not defined
# - name: Use an invalid domain - name: Use an invalid domain
# meraki_config_template: meraki_config_template:
# auth_key: '{{ auth_key }}' auth_key: '{{ auth_key }}'
# host: marrrraki.com host: marrrraki.com
# state: query state: query
# org_name: DevTestOrg org_name: DevTestOrg
# output_level: debug output_level: debug
# delegate_to: localhost delegate_to: localhost
# register: invalid_domain register: invalid_domain
# ignore_errors: yes ignore_errors: yes
# - name: Connection assertions - name: Connection assertions
# assert: assert:
# that: that:
# - '"Failed to connect to" in invalid_domain.msg' - '"Failed to connect to" in invalid_domain.msg'
- name: Query all configuration templates - name: Query all configuration templates
meraki_config_template: meraki_config_template:
@ -40,9 +41,6 @@
register: deleted register: deleted
ignore_errors: yes ignore_errors: yes
- debug:
msg: '{{deleted}}'
- assert: - assert:
that: that:
- '"No configuration template named" in deleted.msg' - '"No configuration template named" in deleted.msg'
@ -79,9 +77,6 @@
register: bind_invalid register: bind_invalid
ignore_errors: yes ignore_errors: yes
- debug:
msg: '{{bind_invalid}}'
- assert: - assert:
that: that:
- bind_invalid.changed == False - bind_invalid.changed == False
@ -112,6 +107,7 @@
that: that:
unbind_invalid.changed == False unbind_invalid.changed == False
always:
- name: Delete network - name: Delete network
meraki_network: meraki_network:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'