meraki_network - Parameter change for combined network type (#49160)

* Added support for types parameter
- Parameter is used to specify multiple network types

* Fix documentation

* Apply suggestions from code review

Co-Authored-By: kbreit <kevin.breit@kevinbreit.net>

* Reworked type parameter to be a list so types isn't needed

* Re-add tags documentation

* Fix documentation around compatibility

* Convert tags to list from string

* Add changelog fragment
This commit is contained in:
Kevin Breit 2019-03-27 10:10:15 -05:00 committed by Dag Wieers
parent 58849ac228
commit 17fc6c6ff1
3 changed files with 70 additions and 27 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- "meraki_network - type parameter no longer accepts combined. Instead, the network types should be specified in a list."

View file

@ -1,7 +1,7 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright: (c) 2018, Kevin Breit (@kbreit) <kevin.breit@kevinbreit.net> # Copyright: (c) 2018, 2019 Kevin Breit (@kbreit) <kevin.breit@kevinbreit.net>
# 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)
from __future__ import absolute_import, division, print_function from __future__ import absolute_import, division, print_function
@ -28,12 +28,12 @@ options:
state: state:
description: description:
- Create or modify an organization. - Create or modify an organization.
choices: [absent, present, query] choices: [ absent, present, query ]
default: present default: present
net_name: net_name:
description: description:
- Name of a network. - Name of a network.
aliases: [name, network] aliases: [ name, network ]
net_id: net_id:
description: description:
- ID number of a network. - ID number of a network.
@ -47,11 +47,17 @@ options:
description: description:
- Type of network device network manages. - Type of network device network manages.
- Required when creating a network. - Required when creating a network.
choices: [appliance, combined, switch, wireless] - As of Ansible 2.8, C(combined) type is no longer accepted.
aliases: [net_type] - As of Ansible 2.8, changes to this parameter are no longer idempotent.
choices: [ appliance, switch, wireless ]
aliases: [ net_type ]
type: list
tags: tags:
type: list
description: description:
- Comma delimited list of tags to assign to network. - List of tags to assign to network.
- C(tags) name conflicts with the tags parameter in Ansible. Indentation problems may cause unexpected behaviors.
- Ansible 2.8 converts this to a list from a comma separated list.
timezone: timezone:
description: description:
- Timezone associated to network. - Timezone associated to network.
@ -92,6 +98,18 @@ EXAMPLES = r'''
timezone: America/Chicago timezone: America/Chicago
tags: production, chicago tags: production, chicago
delegate_to: localhost delegate_to: localhost
- name: Create combined network named MyNet in the YourOrg organization
meraki_network:
auth_key: abc12345
state: present
org_name: YourOrg
net_name: MyNet
type:
- switch
- appliance
timezone: America/Chicago
tags: production, chicago
delegate_to: localhost
''' '''
RETURN = r''' RETURN = r'''
@ -152,16 +170,18 @@ def is_net_valid(meraki, net_name, data):
def construct_tags(tags): def construct_tags(tags):
''' Assumes tags are a comma separated list ''' formatted_tags = ' '.join(tags)
if tags is not None: return ' {0} '.format(formatted_tags) # Meraki needs space padding
tags = tags.replace(' ', '')
tags = tags.split(',')
tag_list = str() def list_to_string(data):
for t in tags: new_string = str()
tag_list = tag_list + " " + t for i, item in enumerate(data):
tag_list = tag_list + " " if i == len(new_string) - 1:
return tag_list new_string += i
return None else:
new_string = "{0}{1} ".format(new_string, item)
return new_string.strip()
def main(): def main():
@ -172,8 +192,8 @@ def main():
argument_spec = meraki_argument_spec() argument_spec = meraki_argument_spec()
argument_spec.update( argument_spec.update(
net_id=dict(type='str'), net_id=dict(type='str'),
type=dict(type='str', choices=['wireless', 'switch', 'appliance', 'combined'], aliases=['net_type']), type=dict(type='list', choices=['wireless', 'switch', 'appliance'], aliases=['net_type']),
tags=dict(type='str'), tags=dict(type='list'),
timezone=dict(type='str'), timezone=dict(type='str'),
net_name=dict(type='str', aliases=['name', 'network']), net_name=dict(type='str', aliases=['name', 'network']),
state=dict(type='str', choices=['present', 'query', 'absent'], default='present'), state=dict(type='str', choices=['present', 'query', 'absent'], default='present'),
@ -219,9 +239,7 @@ def main():
if meraki.params['net_name']: if meraki.params['net_name']:
payload['name'] = meraki.params['net_name'] payload['name'] = meraki.params['net_name']
if meraki.params['type']: if meraki.params['type']:
payload['type'] = meraki.params['type'] payload['type'] = list_to_string(meraki.params['type'])
if meraki.params['type'] == 'combined':
payload['type'] = 'switch wireless appliance'
if meraki.params['tags']: if meraki.params['tags']:
payload['tags'] = construct_tags(meraki.params['tags']) payload['tags'] = construct_tags(meraki.params['tags'])
if meraki.params['timezone']: if meraki.params['timezone']:
@ -277,6 +295,19 @@ def main():
if meraki.status == 200: if meraki.status == 200:
meraki.result['data'] = r meraki.result['data'] = r
meraki.result['changed'] = True meraki.result['changed'] = True
else:
net = meraki.get_net(meraki.params['org_name'], meraki.params['net_name'], data=nets)
# meraki.fail_json(msg="HERE", net=net, payload=payload)
if meraki.is_update_required(net, payload):
path = meraki.construct_path('update',
net_id=meraki.get_net_id(net_name=meraki.params['net_name'], data=nets)
)
r = meraki.request(path,
method='PUT',
payload=json.dumps(payload))
if meraki.status == 200:
meraki.result['data'] = r
meraki.result['changed'] = True
elif meraki.params['state'] == 'absent': elif meraki.params['state'] == 'absent':
if is_net_valid(meraki, meraki.params['net_name'], nets) is True: if is_net_valid(meraki, meraki.params['net_name'], nets) is True:
net_id = meraki.get_net_id(net_name=meraki.params['net_name'], net_id = meraki.get_net_id(net_name=meraki.params['net_name'],

View file

@ -83,9 +83,11 @@
meraki_network: meraki_network:
auth_key: '{{ auth_key }}' auth_key: '{{ auth_key }}'
state: present state: present
org_name: '{{test_org_name}}' org_name: '{{ test_org_name }}'
net_name: IntTestNetworkCombined net_name: IntTestNetworkCombined
type: combined type:
- appliance
- switch
timezone: America/Chicago timezone: America/Chicago
disable_my_meraki: yes disable_my_meraki: yes
delegate_to: localhost delegate_to: localhost
@ -124,7 +126,9 @@
net_name: IntTestNetworkTags net_name: IntTestNetworkTags
type: switch type: switch
timezone: America/Chicago timezone: America/Chicago
tags: first_tag, second_tag tags:
- first_tag
- second_tag
delegate_to: localhost delegate_to: localhost
register: create_net_tags register: create_net_tags
@ -139,7 +143,10 @@
net_name: IntTestNetworkTags net_name: IntTestNetworkTags
type: switch type: switch
timezone: America/Chicago timezone: America/Chicago
tags: first_tag, second_tag, third_tag tags:
- first_tag
- second_tag
- third_tag
delegate_to: localhost delegate_to: localhost
register: create_net_modified register: create_net_modified
@ -151,7 +158,10 @@
net_name: IntTestNetworkTags net_name: IntTestNetworkTags
type: switch type: switch
timezone: America/Chicago timezone: America/Chicago
tags: first_tag, second_tag, third_tag tags:
- first_tag
- second_tag
- third_tag
delegate_to: localhost delegate_to: localhost
register: create_net_modified_idempotent register: create_net_modified_idempotent
@ -257,4 +267,4 @@
- assert: - assert:
that: that:
- 'delete_all_no_org.msg == "org_name or org_id parameters are required"' - 'delete_all_no_org.msg == "org_name or org_id parameters are required"'