1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

meraki_config_template - Fix actions when specifying net_id (#51586)

* Allow configuration templates when using net_id...for reals
- Didn't work before, I had bad code. I'm sorry.
- Cleaned up code and simplified functions
- Added integration tests to test for net_id actions

* Add changelog

* Add module name to changelog

* Fix indentation
This commit is contained in:
Kevin Breit 2019-02-22 18:39:01 -06:00 committed by ansibot
parent bb80316afa
commit d65a91ea7e
3 changed files with 114 additions and 69 deletions

View file

@ -0,0 +1,3 @@
bugfixes:
- meraki_config_template - Fix conditions which prevented code from executing when specifying net_id

View file

@ -154,30 +154,19 @@ def delete_template(meraki, org_id, name, data):
return response return response
def bind(meraki, org_name, net_id, nets, name, data): def bind(meraki, net_id, template_id):
template_id = get_template_id(meraki, name, data) path = meraki.construct_path('bind', net_id=net_id)
if is_network_bound(meraki, nets, net_id, template_id) is False: payload = {'configTemplateId': template_id}
path = meraki.construct_path('bind', function='config_template', net_id=net_id) if meraki.params['auto_bind']:
payload = dict() payload['autoBind'] = meraki.params['auto_bind']
payload['configTemplateId'] = template_id r = meraki.request(path, method='POST', payload=json.dumps(payload))
if meraki.params['auto_bind']: return r
payload['autoBind'] = meraki.params['auto_bind']
meraki.result['changed'] = True
r = meraki.request(path, method='POST', payload=json.dumps(payload))
if meraki.status != 200:
meraki.fail_json(msg='Unable to bind configuration template to network')
return r
def unbind(meraki, org_name, net_id, nets, name, data): def unbind(meraki, net_id):
template_id = get_template_id(meraki, name, data) path = meraki.construct_path('unbind', net_id=net_id)
if is_network_bound(meraki, nets, net_id, template_id) is True: meraki.result['changed'] = True
path = meraki.construct_path('unbind', function='config_template', net_id=net_id) return meraki.request(path, method='POST')
meraki.result['changed'] = True
r = meraki.request(path, method='POST')
if meraki.status != 200:
meraki.fail_json(msg='Unable to unbind configuration template from network')
return r
def main(): def main():
@ -213,19 +202,12 @@ def main():
meraki = MerakiModule(module, function='config_template') meraki = MerakiModule(module, function='config_template')
meraki.params['follow_redirects'] = 'all' meraki.params['follow_redirects'] = 'all'
query_urls = {'config_template': '/organizations/{org_id}/configTemplates', query_urls = {'config_template': '/organizations/{org_id}/configTemplates'}
} delete_urls = {'config_template': '/organizations/{org_id}/configTemplates'}
bind_urls = {'config_template': '/networks/{net_id}/bind'}
unbind_urls = {'config_template': '/networks/{net_id}/unbind'}
delete_urls = {'config_template': '/organizations/{org_id}/configTemplates', meraki.url_catalog['get_all'].update(query_urls)
}
bind_urls = {'config_template': '/networks/{net_id}/bind',
}
unbind_urls = {'config_template': '/networks/{net_id}/unbind',
}
meraki.url_catalog['get_all']['config_template'] = '/organizations/{org_id}/configTemplates'
meraki.url_catalog['delete'] = delete_urls meraki.url_catalog['delete'] = delete_urls
meraki.url_catalog['bind'] = bind_urls meraki.url_catalog['bind'] = bind_urls
meraki.url_catalog['unbind'] = unbind_urls meraki.url_catalog['unbind'] = unbind_urls
@ -254,26 +236,36 @@ def main():
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)
elif meraki.params['state'] == 'present': elif meraki.params['state'] == 'present':
if meraki.params['net_name']: template_id = get_template_id(meraki,
meraki.params['config_template'],
get_config_templates(meraki, org_id))
if is_network_bound(meraki, nets, net_id, template_id) is False:
template_bind = bind(meraki, template_bind = bind(meraki,
meraki.params['org_name'],
net_id, net_id,
nets, template_id)
meraki.params['config_template'], if meraki.status != 200:
get_config_templates(meraki, org_id)) meraki.fail_json(msg='Unable to bind configuration template to network')
meraki.result['changed'] = True
meraki.result['data'] = template_bind
elif meraki.params['state'] == 'absent': elif meraki.params['state'] == 'absent':
if not meraki.params['net_name']: if not meraki.params['net_name'] and not meraki.params['net_id']:
meraki.result['data'] = delete_template(meraki, meraki.result['data'] = delete_template(meraki,
org_id, org_id,
meraki.params['config_template'], meraki.params['config_template'],
get_config_templates(meraki, org_id)) get_config_templates(meraki, org_id))
if meraki.status == 200:
meraki.result['changed'] = True
else: else:
config_unbind = unbind(meraki, template_id = get_template_id(meraki,
meraki.params['org_name'], meraki.params['config_template'],
net_id, get_config_templates(meraki, org_id))
nets, if is_network_bound(meraki, nets, net_id, template_id) is True:
meraki.params['config_template'], config_unbind = unbind(meraki,
get_config_templates(meraki, org_id)) net_id)
if meraki.status != 200:
meraki.fail_json(msg='Unable to unbind configuration template from network')
meraki.result['changed'] = True
meraki.result['data'] = config_unbind
# in the event of a successful module execution, you will want to # in the event of a successful module execution, you will want to
# simple AnsibleModule.exit_json(), passing the key/value results # simple AnsibleModule.exit_json(), passing the key/value results

View file

@ -4,26 +4,26 @@
# 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)
--- ---
- block: - block:
- name: Test an API key is provided # - name: Test an API key is provided
fail: # fail:
msg: Please define an API key # msg: Please define an API key
when: auth_key is not defined # 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:
@ -37,7 +37,7 @@
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
state: absent state: absent
org_name: DevTestOrg org_name: DevTestOrg
config_template: DevConfigTemplateInvalid config_template: FakeConfigTemplate
register: deleted register: deleted
ignore_errors: yes ignore_errors: yes
@ -54,13 +54,24 @@
type: appliance type: appliance
delegate_to: localhost delegate_to: localhost
- name: Get network id
meraki_network:
auth_key: '{{auth_key}}'
state: query
org_name: '{{test_org_name}}'
net_name: '{{test_net_name}}'
register: net_info
- set_fact:
net_id: '{{net_info.data.id}}'
- name: Bind a template to a network - name: Bind a template to a network
meraki_config_template: meraki_config_template:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
state: present state: present
org_name: '{{ test_org_name }}' org_name: '{{ test_org_name }}'
net_name: '{{ test_net_name }}' net_name: '{{ test_net_name }}'
config_template: DevConfigTemplate config_template: '{{test_template_name}}'
register: bind register: bind
- assert: - assert:
@ -73,7 +84,7 @@
state: present state: present
org_name: '{{ test_org_name }}' org_name: '{{ test_org_name }}'
net_name: '{{ test_net_name }}' net_name: '{{ test_net_name }}'
config_template: DevConfigTemplate config_template: '{{test_template_name}}'
register: bind_invalid register: bind_invalid
ignore_errors: yes ignore_errors: yes
@ -87,7 +98,7 @@
state: absent state: absent
org_name: '{{ test_org_name }}' org_name: '{{ test_org_name }}'
net_name: '{{ test_net_name }}' net_name: '{{ test_net_name }}'
config_template: DevConfigTemplate config_template: '{{test_template_name}}'
register: unbind register: unbind
- assert: - assert:
@ -100,13 +111,52 @@
state: absent state: absent
org_name: '{{ test_org_name }}' org_name: '{{ test_org_name }}'
net_name: '{{ test_net_name }}' net_name: '{{ test_net_name }}'
config_template: DevConfigTemplate config_template: '{{test_template_name}}'
register: unbind_invalid register: unbind_invalid
- assert: - assert:
that: that:
unbind_invalid.changed == False unbind_invalid.changed == False
- name: Bind a template to a network via id
meraki_config_template:
auth_key: '{{auth_key}}'
state: present
org_name: '{{test_org_name}}'
net_id: '{{net_id}}'
config_template: '{{test_template_name}}'
register: bind_id
- assert:
that:
bind_id.changed == True
- name: Bind a template to a network via id for idempotency
meraki_config_template:
auth_key: '{{auth_key}}'
state: present
org_name: '{{test_org_name}}'
net_id: '{{net_id}}'
config_template: '{{test_template_name}}'
register: bind_id_idempotent
- assert:
that:
bind_id_idempotent.changed == False
- name: Unbind a template to a network via id
meraki_config_template:
auth_key: '{{auth_key}}'
state: absent
org_name: '{{test_org_name}}'
net_id: '{{net_id}}'
config_template: '{{test_template_name}}'
register: unbind_id
- assert:
that:
unbind_id.changed == True
always: always:
- name: Delete network - name: Delete network
meraki_network: meraki_network: