mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 13:20:23 -07:00
Meraki organization module (#38773)
* Initial commit Query an organization within Meraki. No support is in place for managing or creating yet * Change output_level method and make the state parameter required. * Implemented listing all organizations - Updated documentation - Parse results and return all organizations - Parse results and return specified organization * Framework for creating an organization - Documentation example for organization creation - Framework exists for creating organizations, pending PR 36809 - Created functions for HTTP calls - Renamed from dashboard.meraki.com to api.meraki.com - Added required_if for state * Remove absent state - Meraki API does not support deleting an organization so absent is removed - Updated documentation to call it state instead of status * Small change to documentation * Support all parameters associated to organization - Added all parameters needed for all organization actions. - None of the added ones work at this time. - Added documentation for clone. * Integration test for meraki_organization module * Rename module to meraki for porting to module utility * Meraki documentation fragment - Created initial documentation fragment for Meraki modules * Add meraki module utility to branch. Formerly was on a separate branch. * CRU support for Meraki organization module * CRU is supported for Meraki organizations * There is no DELETE function for organizations in the API * This code is very messy and needs cleanup * Create and Update actions don't show status as updated, must fix * Added Meraki module utility to module utility documentation list * Added support for organization cloning * Renamed use_ssl to use_https * Removed define_method() * Removed is_org() * Added is_org_valid() which does all org sanity checks * Fixes for ansibot - Changed default of use_proxy from true to false - Removed some commented out code - Updated documentation * Changes for ansibot - Removed requirement for state parameter. I may readd this. - Updated formatting diff --git a/lib/ansible/module_utils/network/meraki/meraki.py b/lib/ansible/module_utils/network/meraki/meraki.py index 3acd3d1038..395ac7c4b4 100644 --- a/lib/ansible/module_utils/network/meraki/meraki.py +++ b/lib/ansible/module_utils/network/meraki/meraki.py @@ -42,7 +42,7 @@ def meraki_argument_spec(): return dict(auth_key=dict(type='str', no_log=True, fallback=(env_fallback, ['MERAKI_KEY'])), host=dict(type='str', default='api.meraki.com'), name=dict(type='str'), - state=dict(type='str', choices=['present', 'absent', 'query'], required=True), + state=dict(type='str', choices=['present', 'absent', 'query']), use_proxy=dict(type='bool', default=False), use_https=dict(type='bool', default=True), validate_certs=dict(type='bool', default=True), diff --git a/lib/ansible/modules/network/meraki/meraki_organization.py b/lib/ansible/modules/network/meraki/meraki_organization.py index 923d969366..3789be91d6 100644 --- a/lib/ansible/modules/network/meraki/meraki_organization.py +++ b/lib/ansible/modules/network/meraki/meraki_organization.py @@ -20,11 +20,9 @@ short_description: Manage organizations in the Meraki cloud version_added: "2.6" description: - Allows for creation, management, and visibility into organizations within Meraki - notes: - More information about the Meraki API can be found at U(https://dashboard.meraki.com/api_docs). - Some of the options are likely only used for developers within Meraki - options: name: description: @@ -32,21 +30,18 @@ options: - If C(clone) is specified, C(name) is the name of the new organization. state: description: - - Create or query organizations - choices: ['query', 'present'] + - Create or modify an organization + choices: ['present', 'query'] clone: description: - Organization to clone to a new organization. - type: string org_name: description: - Name of organization. - Used when C(name) should refer to another object. - type: string org_id: description: - ID of organization - author: - Kevin Breit (@kbreit) extends_documentation_fragment: meraki @@ -86,7 +81,6 @@ RETURN = ''' response: description: Data returned from Meraki dashboard. type: dict - state: query returned: info ''' @@ -103,6 +97,7 @@ def main(): argument_spec = meraki_argument_spec() argument_spec.update(clone=dict(type='str'), + state=dict(type='str', choices=['present', 'query']), ) @@ -125,11 +120,9 @@ def main(): meraki.function = 'organizations' meraki.params['follow_redirects'] = 'all' - meraki.required_if=[ - ['state', 'present', ['name']], - ['clone', ['name']], - # ['vpn_PublicIP', ['name']], - ] + meraki.required_if = [['state', 'present', ['name']], + ['clone', ['name']], + ] create_urls = {'organizations': '/organizations', } @@ -162,23 +155,16 @@ def main(): - - # method = None - # org_id = None - - - # meraki.fail_json(msg=meraki.is_org_valid(meraki.get_orgs(), org_name='AnsibleTestOrg')) - if meraki.params['state'] == 'query': - if meraki.params['name'] is None: # Query all organizations, no matter what - orgs = meraki.get_orgs() - meraki.result['organization'] = orgs - elif meraki.params['name'] is not None: # Query by organization name - module.warn('All matching organizations will be returned, even if there are duplicate named organizations') - orgs = meraki.get_orgs() - for o in orgs: - if o['name'] == meraki.params['name']: - meraki.result['organization'] = o + if meraki.params['name'] is None: # Query all organizations, no matter what + orgs = meraki.get_orgs() + meraki.result['organization'] = orgs + elif meraki.params['name'] is not None: # Query by organization name + module.warn('All matching organizations will be returned, even if there are duplicate named organizations') + orgs = meraki.get_orgs() + for o in orgs: + if o['name'] == meraki.params['name']: + meraki.result['organization'] = o elif meraki.params['state'] == 'present': if meraki.params['clone'] is not None: # Cloning payload = {'name': meraki.params['name']} @@ -193,7 +179,10 @@ def main(): payload = {'name': meraki.params['name'], 'id': meraki.params['org_id'], } - meraki.result['response'] = json.loads(meraki.request(meraki.construct_path('update', org_id=meraki.params['org_id']), payload=json.dumps(payload), method='PUT')) + meraki.result['response'] = json.loads(meraki.request(meraki.construct_path('update', + org_id=meraki.params['org_id']), + payload=json.dumps(payload), + method='PUT')) diff --git a/lib/ansible/utils/module_docs_fragments/meraki.py b/lib/ansible/utils/module_docs_fragments/meraki.py index e268d02e68..3569d83b99 100644 --- a/lib/ansible/utils/module_docs_fragments/meraki.py +++ b/lib/ansible/utils/module_docs_fragments/meraki.py @@ -35,6 +35,7 @@ options: description: - Set amount of debug output during module execution choices: ['normal', 'debug'] + default: 'normal' timeout: description: - Time to timeout for HTTP requests. diff --git a/test/integration/targets/meraki_organization/aliases b/test/integration/targets/meraki_organization/aliases new file mode 100644 index 0000000000..ad7ccf7ada --- /dev/null +++ b/test/integration/targets/meraki_organization/aliases @@ -0,0 +1 @@ +unsupported * Formatting fix * Minor updates due to testing - Made state required again - Improved formatting for happier PEP8 - request() now sets instance method * Fix reporting of the result * Enhance idempotency checks - Remove merging functionality as the proposed should be used - Do check and reverse check to look for differences * Rewrote and added additional integration tests. This isn't done. * Updated is_update_required method: - Original and proposed data is passed to method - Added ignored_keys list so it can be skipped if needed * Changes per comments from dag - Optionally assign function on class instantiation - URLs now have {} for substitution method - Move auth_key check to module utility - Remove is_new and get_existing - Minor changes to documentation * Enhancements for future modules and organization - Rewrote construct_path method for simplicity - Increased support for network functionality to be committed * Changes based on Dag feedback and to debug problems * Minor fixes for validitation testing * Small changes for dag and Ansibot - Changed how auth_key is processed - Removed some commented lines - Updated documentation fragment, but that may get reverted * Remove blank line and comment * Improvements for testing and code simplification - Added network integration tests - Modified error handling in request() - More testing to come on this - Rewrote construct_path again. Very simple now. * Remove trailing whitespace * Small changes based on dag's response * Removed certain sections from exit_json and fail_json as they're old
This commit is contained in:
parent
c2d7347819
commit
c8d287fece
9 changed files with 588 additions and 0 deletions
0
lib/ansible/modules/network/meraki/__init__.py
Normal file
0
lib/ansible/modules/network/meraki/__init__.py
Normal file
206
lib/ansible/modules/network/meraki/meraki_organization.py
Normal file
206
lib/ansible/modules/network/meraki/meraki_organization.py
Normal file
|
@ -0,0 +1,206 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright: (c) 2018, 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)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
ANSIBLE_METADATA = {
|
||||
'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'
|
||||
}
|
||||
|
||||
DOCUMENTATION = r'''
|
||||
---
|
||||
module: meraki_organization
|
||||
short_description: Manage organizations in the Meraki cloud
|
||||
version_added: "2.6"
|
||||
description:
|
||||
- Allows for creation, management, and visibility into organizations within Meraki.
|
||||
notes:
|
||||
- More information about the Meraki API can be found at U(https://dashboard.meraki.com/api_docs).
|
||||
- Some of the options are likely only used for developers within Meraki.
|
||||
options:
|
||||
state:
|
||||
description:
|
||||
- Create or modify an organization.
|
||||
choices: ['present', 'query']
|
||||
default: present
|
||||
clone:
|
||||
description:
|
||||
- Organization to clone to a new organization.
|
||||
org_name:
|
||||
description:
|
||||
- Name of organization.
|
||||
- If C(clone) is specified, C(org_name) is the name of the new organization.
|
||||
aliases: [ name, organization ]
|
||||
org_id:
|
||||
description:
|
||||
- ID of organization.
|
||||
aliases: [ id ]
|
||||
author:
|
||||
- Kevin Breit (@kbreit)
|
||||
extends_documentation_fragment: meraki
|
||||
'''
|
||||
|
||||
EXAMPLES = r'''
|
||||
- name: Create a new organization named YourOrg
|
||||
meraki_organization:
|
||||
auth_key: abc12345
|
||||
org_name: YourOrg
|
||||
state: present
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Query information about all organizations associated to the user
|
||||
meraki_organization:
|
||||
auth_key: abc12345
|
||||
state: query
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Query information about a single organization named YourOrg
|
||||
meraki_organization:
|
||||
auth_key: abc12345
|
||||
org_name: YourOrg
|
||||
state: query
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Rename an organization to RenamedOrg
|
||||
meraki_organization:
|
||||
auth_key: abc12345
|
||||
org_id: 987654321
|
||||
org_name: RenamedOrg
|
||||
state: present
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Clone an organization named Org to a new one called ClonedOrg
|
||||
meraki_organization:
|
||||
auth_key: abc12345
|
||||
clone: Org
|
||||
org_name: ClonedOrg
|
||||
state: present
|
||||
delegate_to: localhost
|
||||
'''
|
||||
|
||||
RETURN = r'''
|
||||
response:
|
||||
description: Data returned from Meraki dashboard.
|
||||
type: dict
|
||||
returned: info
|
||||
'''
|
||||
|
||||
import os
|
||||
from ansible.module_utils.basic import AnsibleModule, json, env_fallback
|
||||
from ansible.module_utils.urls import fetch_url
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
# define the available arguments/parameters that a user can pass to
|
||||
# the module
|
||||
argument_spec = meraki_argument_spec()
|
||||
argument_spec.update(clone=dict(type='str'),
|
||||
state=dict(type='str', choices=['present', 'query'], default='present'),
|
||||
org_name=dict(type='str', aliases=['name', 'organization']),
|
||||
org_id=dict(type='int', aliases=['id']),
|
||||
)
|
||||
|
||||
# seed the result dict in the object
|
||||
# we primarily care about changed and state
|
||||
# change is if this module effectively modified the target
|
||||
# state will include any data that you want your module to pass back
|
||||
# for consumption, for example, in a subsequent task
|
||||
result = dict(
|
||||
changed=False,
|
||||
)
|
||||
# the AnsibleModule object will be our abstraction working with Ansible
|
||||
# this includes instantiation, a couple of common attr would be the
|
||||
# args/params passed to the execution, as well as if the module
|
||||
# supports check mode
|
||||
module = AnsibleModule(argument_spec=argument_spec,
|
||||
supports_check_mode=True,
|
||||
)
|
||||
meraki = MerakiModule(module, function='organizations')
|
||||
|
||||
meraki.params['follow_redirects'] = 'all'
|
||||
|
||||
create_urls = {'organizations': '/organizations',
|
||||
}
|
||||
update_urls = {'organizations': '/organizations/{org_id}',
|
||||
}
|
||||
clone_urls = {'organizations': '/organizations/{org_id}/clone',
|
||||
}
|
||||
|
||||
meraki.url_catalog['create'] = create_urls
|
||||
meraki.url_catalog['update'] = update_urls
|
||||
meraki.url_catalog['clone'] = clone_urls
|
||||
|
||||
payload = None
|
||||
|
||||
# if the user is working with this module in only check mode we do not
|
||||
# want to make any changes to the environment, just return the current
|
||||
# state with no modifications
|
||||
# FIXME: Work with Meraki so they can implement a check mode
|
||||
if module.check_mode:
|
||||
meraki.exit_json(**meraki.result)
|
||||
|
||||
# execute checks for argument completeness
|
||||
|
||||
# manipulate or modify the state as needed (this is going to be the
|
||||
# part where your module will do what it needs to do)
|
||||
orgs = meraki.get_orgs()
|
||||
if meraki.params['state'] == 'query':
|
||||
if meraki.params['org_name']: # Query by organization name
|
||||
module.warn('All matching organizations will be returned, even if there are duplicate named organizations')
|
||||
for o in orgs:
|
||||
if o['name'] == meraki.params['org_name']:
|
||||
meraki.result['data'] = o
|
||||
elif meraki.params['org_id']:
|
||||
for o in orgs:
|
||||
if o['id'] == meraki.params['org_id']:
|
||||
meraki.result['data'] = o
|
||||
else: # Query all organizations, no matter what
|
||||
orgs = meraki.get_orgs()
|
||||
meraki.result['data'] = orgs
|
||||
elif meraki.params['state'] == 'present':
|
||||
if meraki.params['clone']: # Cloning
|
||||
payload = {'name': meraki.params['org_name']}
|
||||
meraki.result['data'] = json.loads(
|
||||
meraki.request(
|
||||
meraki.construct_path(
|
||||
'clone',
|
||||
org_name=meraki.params['clone']
|
||||
),
|
||||
payload=json.dumps(payload),
|
||||
method='POST'))
|
||||
elif not meraki.params['org_id'] and meraki.params['org_name']: # Create new organization
|
||||
payload = {'name': meraki.params['org_name']}
|
||||
meraki.result['data'] = json.loads(
|
||||
meraki.request(
|
||||
meraki.construct_path('create'),
|
||||
method='POST',
|
||||
payload=json.dumps(payload)))
|
||||
elif meraki.params['org_id'] and meraki.params['org_name']: # Update an existing organization
|
||||
payload = {'name': meraki.params['org_name'],
|
||||
'id': meraki.params['org_id'],
|
||||
}
|
||||
meraki.result['data'] = json.loads(
|
||||
meraki.request(
|
||||
meraki.construct_path(
|
||||
'update',
|
||||
org_id=meraki.params['org_id']
|
||||
),
|
||||
method='PUT',
|
||||
payload=json.dumps(payload)))
|
||||
|
||||
# in the event of a successful module execution, you will want to
|
||||
# simple AnsibleModule.exit_json(), passing the key/value results
|
||||
meraki.exit_json(**meraki.result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue