| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Kevin Breit (@kbreit) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
ANSIBLE_METADATA = { |
| 11 |
'metadata_version': '1.1', |
| 12 |
"status": ['deprecated'], |
| 13 |
'supported_by': 'community' |
| 14 |
} |
| 15 |
|
| 16 |
DOCUMENTATION = r''' |
| 17 |
--- |
| 18 |
module: meraki_organization |
| 19 |
short_description: Manage organizations in the Meraki cloud |
| 20 |
description: |
| 21 |
- Allows for creation, management, and visibility into organizations within Meraki. |
| 22 |
deprecated: |
| 23 |
removed_in: '3.0.0' |
| 24 |
why: Updated modules released with increased functionality |
| 25 |
alternative: cisco.meraki.organizations |
| 26 |
options: |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Create or modify an organization. |
| 30 |
- C(org_id) must be specified if multiple organizations of the same name exist. |
| 31 |
- C(absent) WILL DELETE YOUR ENTIRE ORGANIZATION, AND ALL ASSOCIATED OBJECTS, WITHOUT CONFIRMATION. USE WITH CAUTION. |
| 32 |
choices: ['absent', 'present', 'query'] |
| 33 |
default: present |
| 34 |
type: str |
| 35 |
clone: |
| 36 |
description: |
| 37 |
- Organization to clone to a new organization. |
| 38 |
type: str |
| 39 |
org_name: |
| 40 |
description: |
| 41 |
- Name of organization. |
| 42 |
- If C(clone) is specified, C(org_name) is the name of the new organization. |
| 43 |
aliases: [ name, organization ] |
| 44 |
type: str |
| 45 |
org_id: |
| 46 |
description: |
| 47 |
- ID of organization. |
| 48 |
aliases: [ id ] |
| 49 |
type: str |
| 50 |
delete_confirm: |
| 51 |
description: |
| 52 |
- ID of organization required for confirmation before deletion. |
| 53 |
type: str |
| 54 |
author: |
| 55 |
- Kevin Breit (@kbreit) |
| 56 |
extends_documentation_fragment: cisco.meraki.meraki |
| 57 |
''' |
| 58 |
|
| 59 |
EXAMPLES = r''' |
| 60 |
- name: Create a new organization named YourOrg |
| 61 |
meraki_organization: |
| 62 |
auth_key: abc12345 |
| 63 |
org_name: YourOrg |
| 64 |
state: present |
| 65 |
delegate_to: localhost |
| 66 |
|
| 67 |
- name: Delete an organization named YourOrg |
| 68 |
meraki_organization: |
| 69 |
auth_key: abc12345 |
| 70 |
org_name: YourOrg |
| 71 |
state: absent |
| 72 |
delegate_to: localhost |
| 73 |
|
| 74 |
- name: Query information about all organizations associated to the user |
| 75 |
meraki_organization: |
| 76 |
auth_key: abc12345 |
| 77 |
state: query |
| 78 |
delegate_to: localhost |
| 79 |
|
| 80 |
- name: Query information about a single organization named YourOrg |
| 81 |
meraki_organization: |
| 82 |
auth_key: abc12345 |
| 83 |
org_name: YourOrg |
| 84 |
state: query |
| 85 |
delegate_to: localhost |
| 86 |
|
| 87 |
- name: Rename an organization to RenamedOrg |
| 88 |
meraki_organization: |
| 89 |
auth_key: abc12345 |
| 90 |
org_id: 987654321 |
| 91 |
org_name: RenamedOrg |
| 92 |
state: present |
| 93 |
delegate_to: localhost |
| 94 |
|
| 95 |
- name: Clone an organization named Org to a new one called ClonedOrg |
| 96 |
meraki_organization: |
| 97 |
auth_key: abc12345 |
| 98 |
clone: Org |
| 99 |
org_name: ClonedOrg |
| 100 |
state: present |
| 101 |
delegate_to: localhost |
| 102 |
''' |
| 103 |
|
| 104 |
RETURN = r''' |
| 105 |
data: |
| 106 |
description: Information about the organization which was created or modified |
| 107 |
returned: success |
| 108 |
type: complex |
| 109 |
contains: |
| 110 |
id: |
| 111 |
description: Unique identification number of organization |
| 112 |
returned: success |
| 113 |
type: int |
| 114 |
sample: 2930418 |
| 115 |
name: |
| 116 |
description: Name of organization |
| 117 |
returned: success |
| 118 |
type: str |
| 119 |
sample: YourOrg |
| 120 |
|
| 121 |
''' |
| 122 |
|
| 123 |
from ansible.module_utils.basic import AnsibleModule, json |
| 124 |
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec |
| 125 |
|
| 126 |
|
| 127 |
def get_org(meraki, org_id, data): |
| 128 |
# meraki.fail_json(msg=str(org_id), data=data, oid0=data[0]['id'], oid1=data[1]['id']) |
| 129 |
for o in data: |
| 130 |
# meraki.fail_json(msg='o', data=o['id'], type=str(type(o['id']))) |
| 131 |
if o['id'] == org_id: |
| 132 |
return o |
| 133 |
return -1 |
| 134 |
|
| 135 |
|
| 136 |
def main(): |
| 137 |
|
| 138 |
# define the available arguments/parameters that a user can pass to |
| 139 |
# the module |
| 140 |
argument_spec = meraki_argument_spec() |
| 141 |
argument_spec.update(clone=dict(type='str'), |
| 142 |
state=dict(type='str', choices=['absent', 'present', 'query'], default='present'), |
| 143 |
org_name=dict(type='str', aliases=['name', 'organization']), |
| 144 |
org_id=dict(type='str', aliases=['id']), |
| 145 |
delete_confirm=dict(type='str'), |
| 146 |
) |
| 147 |
|
| 148 |
# the AnsibleModule object will be our abstraction working with Ansible |
| 149 |
# this includes instantiation, a couple of common attr would be the |
| 150 |
# args/params passed to the execution, as well as if the module |
| 151 |
# supports check mode |
| 152 |
module = AnsibleModule(argument_spec=argument_spec, |
| 153 |
supports_check_mode=True, |
| 154 |
) |
| 155 |
meraki = MerakiModule(module, function='organizations') |
| 156 |
|
| 157 |
meraki.params['follow_redirects'] = 'all' |
| 158 |
|
| 159 |
create_urls = {'organizations': '/organizations'} |
| 160 |
update_urls = {'organizations': '/organizations/{org_id}'} |
| 161 |
delete_urls = {'organizations': '/organizations/{org_id}'} |
| 162 |
clone_urls = {'organizations': '/organizations/{org_id}/clone'} |
| 163 |
|
| 164 |
meraki.url_catalog['create'] = create_urls |
| 165 |
meraki.url_catalog['update'] = update_urls |
| 166 |
meraki.url_catalog['clone'] = clone_urls |
| 167 |
meraki.url_catalog['delete'] = delete_urls |
| 168 |
|
| 169 |
payload = None |
| 170 |
|
| 171 |
# manipulate or modify the state as needed (this is going to be the |
| 172 |
# part where your module will do what it needs to do) |
| 173 |
orgs = meraki.get_orgs() |
| 174 |
if meraki.params['state'] == 'query': |
| 175 |
if meraki.params['org_name']: # Query by organization name |
| 176 |
module.warn('All matching organizations will be returned, even if there are duplicate named organizations') |
| 177 |
for o in orgs: |
| 178 |
if o['name'] == meraki.params['org_name']: |
| 179 |
meraki.result['data'] = o |
| 180 |
elif meraki.params['org_id']: |
| 181 |
for o in orgs: |
| 182 |
if o['id'] == meraki.params['org_id']: |
| 183 |
meraki.result['data'] = o |
| 184 |
else: # Query all organizations, no matter what |
| 185 |
meraki.result['data'] = orgs |
| 186 |
elif meraki.params['state'] == 'present': |
| 187 |
if meraki.params['clone']: # Cloning |
| 188 |
payload = {'name': meraki.params['org_name']} |
| 189 |
response = meraki.request(meraki.construct_path('clone', |
| 190 |
org_name=meraki.params['clone'] |
| 191 |
), |
| 192 |
payload=json.dumps(payload), |
| 193 |
method='POST') |
| 194 |
if meraki.status != 201: |
| 195 |
meraki.fail_json(msg='Organization clone failed') |
| 196 |
meraki.result['data'] = response |
| 197 |
meraki.result['changed'] = True |
| 198 |
elif not meraki.params['org_id'] and meraki.params['org_name']: # Create new organization |
| 199 |
payload = {'name': meraki.params['org_name']} |
| 200 |
response = meraki.request(meraki.construct_path('create'), |
| 201 |
method='POST', |
| 202 |
payload=json.dumps(payload)) |
| 203 |
if meraki.status == 201: |
| 204 |
meraki.result['data'] = response |
| 205 |
meraki.result['changed'] = True |
| 206 |
elif meraki.params['org_id'] and meraki.params['org_name']: # Update an existing organization |
| 207 |
payload = {'name': meraki.params['org_name'], |
| 208 |
'id': meraki.params['org_id'], |
| 209 |
} |
| 210 |
original = get_org(meraki, meraki.params['org_id'], orgs) |
| 211 |
if meraki.is_update_required(original, payload, optional_ignore=['url']): |
| 212 |
response = meraki.request(meraki.construct_path('update', |
| 213 |
org_id=meraki.params['org_id'] |
| 214 |
), |
| 215 |
method='PUT', |
| 216 |
payload=json.dumps(payload)) |
| 217 |
if meraki.status != 200: |
| 218 |
meraki.fail_json(msg='Organization update failed') |
| 219 |
meraki.result['data'] = response |
| 220 |
meraki.result['changed'] = True |
| 221 |
else: |
| 222 |
meraki.result['data'] = original |
| 223 |
elif meraki.params['state'] == 'absent': |
| 224 |
if meraki.params['org_name'] is not None: |
| 225 |
org_id = meraki.get_org_id(meraki.params['org_name']) |
| 226 |
elif meraki.params['org_id'] is not None: |
| 227 |
org_id = meraki.params['org_id'] |
| 228 |
if meraki.params['delete_confirm'] != org_id: |
| 229 |
meraki.fail_json(msg="delete_confirm must match the network ID of the network to be deleted.") |
| 230 |
if meraki.check_mode is True: |
| 231 |
meraki.result['data'] = {} |
| 232 |
meraki.result['changed'] = True |
| 233 |
meraki.exit_json(**meraki.result) |
| 234 |
path = meraki.construct_path('delete', org_id=org_id) |
| 235 |
response = meraki.request(path, method='DELETE') |
| 236 |
if meraki.status == 204: |
| 237 |
meraki.result['data'] = {} |
| 238 |
meraki.result['changed'] = True |
| 239 |
|
| 240 |
# in the event of a successful module execution, you will want to |
| 241 |
# simple AnsibleModule.exit_json(), passing the key/value results |
| 242 |
meraki.exit_json(**meraki.result) |
| 243 |
|
| 244 |
|
| 245 |
if __name__ == '__main__': |
| 246 |
main() |