
    Vh(                         d dl mZmZmZ eZdZdZd dlm	Z	m
Z
 d dlmZmZ d dlmZmZ d dlmZ d dlmZ  e
       Z
 e
j*                   e ed	d
                    G d dee      Zy)    )absolute_importdivisionprint_functionab  
name: meraki
author:
  - Nilashish Chakraborty (@NilashishC)
short_description: Ansible dynamic inventory plugin for Cisco Meraki devices.
requirements:
  - meraki
extends_documentation_fragment:
  - constructed
description:
  - Build inventories using the Cisco Meraki API.
  - Uses a YAML configuration file cisco_meraki.[yml|yaml].
options:
  meraki_org_id:
    description:
      - The organization ID to fetch the networks and devices from.
    type: str
    required: true
  meraki_api_key:
    description:
      - meraki_api_key (string), API key generated in dashboard; can also be set as an environment variable MERAKI_DASHBOARD_API_KEY
    type: str
  meraki_base_url:
    description:
        - meraki_base_url (string), preceding all endpoint resources
    type: str
    default: https://api.meraki.com/api/v1
  meraki_single_request_timeout:
    description:
      - meraki_single_request_timeout (integer), maximum number of seconds for each API call
    type: int
    default: 60
  meraki_certificate_path:
    description:
      - meraki_certificate_path (string), path for TLS/SSL certificate verification if behind local proxy
    type: str
    default: ''
  meraki_requests_proxy:
    description:
      - meraki_requests_proxy (string), proxy server and port, if needed, for HTTPS
    type: str
    default: ''
  meraki_wait_on_rate_limit:
    description:
      - meraki_wait_on_rate_limit (boolean), retry if 429 rate limit error encountered?
    type: bool
    default: true
  meraki_nginx_429_retry_wait_time:
    description:
      - meraki_nginx_429_retry_wait_time (integer), Nginx 429 retry wait time
    type: int
    default: 60
  meraki_action_batch_retry_wait_time:
    description:
      - meraki_action_batch_retry_wait_time (integer), action batch concurrency error retry wait time
    type: int
    default: 60
  meraki_retry_4xx_error:
    description:
      - meraki_retry_4xx_error (boolean), retry if encountering other 4XX error (besides 429)?
    type: bool
    default: false
  meraki_retry_4xx_error_wait_time:
    description:
      - meraki_retry_4xx_error_wait_time (integer), other 4XX error retry wait time
    type: int
    default: 60
  meraki_maximum_retries:
    description:
      - meraki_maximum_retries (integer), retry up to this many times when encountering 429s or other server-side errors
    type: int
    default: 2
  meraki_output_log:
    description:
      - meraki_output_log (boolean), create an output log file?
    type: bool
    default: true
  meraki_log_file_prefix:
    description:
      - meraki_log_file_prefix (string), log file name appended with date and timestamp
    type: str
    default: meraki_api_
  meraki_log_path:
    description:
      - log_path (string), path to output log; by default, working directory of script if not specified
    type: str
    default: ''
  meraki_print_console:
    description:
      - meraki_print_console (boolean), print logging output to console?
    type: bool
    default: true
  meraki_suppress_logging:
    description:
      - meraki_suppress_logging (boolean), disable all logging? you're on your own then!
    type: bool
    default: false
  meraki_simulate:
    description:
      - meraki_simulate (boolean), simulate POST/PUT/DELETE calls to prevent changes?
    type: bool
    default: false
  meraki_be_geo_id:
    description:
      - meraki_be_geo_id (string), optional partner identifier for API usage tracking; can also be set as an environment variable BE_GEO_ID
    type: str
    default: ''
  meraki_use_iterator_for_get_pages:
    description:
      - meraki_use_iterator_for_get_pages (boolean), list* methods will return an iterator with each object instead of a complete list with all items
    type: bool
    default: false
  meraki_inherit_logging_config:
    description:
      - meraki_inherit_logging_config (boolean), Inherits your own logger instance
    type: bool
    default: false
a  
# cisco_meraki.yml
---
plugin: cisco.meraki.meraki
meraki_api_key: "<enter Meraki API key or set the MERAKI_DASHBOARD_API_KEY env var>"
meraki_org_id: "<enter Meraki Org ID>"
keyed_groups:
  # group devices based on network ID
  - prefix: meraki_network_id
    key: network_id
  # group devices based on network name
  - prefix: meraki_network
    key: network
  # group devices based on device type
  - prefix: meraki_device_type
    key: device_type
  # group devices based on meraki device tag
  - prefix: meraki_tag
    key: tags
)MERAKImeraki_argument_spec)BaseInventoryPluginConstructable)	to_nativeto_text)ArgumentSpecValidator)AnsibleErrorstrT)typerequired)meraki_org_idc                   :     e Zd ZdZ fdZd Zd Zd fd	Z xZS )InventoryModulezcisco.meraki.merakic                 X    d}t         t        |   |      r|j                  d      rd}|S )zMreturn true/false if this is possibly a valid file for this plugin to consumeF)zcisco_meraki.yamlzcisco_meraki.ymlT)superr   verify_fileendswith)selfpathvalid	__class__s      i/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/meraki/plugins/inventory/meraki.pyr   zInventoryModule.verify_file   s.    $3D9}}FG    c                     i }|j                  dd|dd      }|D ]#  }t        |j                  dd            ||d   <   % |S )	z7Build a dictionary mapping network ID to network names.organizationsgetOrganizationNetworksallorganizationIdtotal_pagesfamilyfunctionparamsname id)exec_merakir   get)r   	dashboardorg_idnetwork_mapnetworksnetworks         r   _build_network_mapz"InventoryModule._build_network_map   sc    ((".&,UC ) 

   	JG)0VR1H)IK&	J r   c                     t        t              }|j                  |      }|j                  r't	        ddj                  |j                               |j                  S )z&Validate the inventory plugin argspec.zValidation failed: z, )r   r   validateerror_messagesr   joinvalidated_parameters)r   
parametersargspec_validatorresults       r   _validate_argspecz!InventoryModule._validate_argspec   s^    12FG"++J7  %dii0E0E&F%GHJ J ***r   c                    t         t        |   ||||       | j                  |      }|D ci c]  }|t        v s|||    }}| j                  |      }|j                  d      }	| j                  d      }
t        |      }	 |j                  dd|	dd	      }|r| j                  ||	      }|D ]  }|j                  d
      }|s&| j                  j                  d|d           |d   }| j                  j                  |d       |j                  d      rD| j                  j!                  |d|d          | j                  j!                  |d||d             n| j                  j#                  d| d       |j                  d      r!| j                  j!                  |d|d          n| j                  j                  d| d       | j                  j!                  |d|d          | j                  j!                  |d|d          | j                  j!                  |d|d          | j%                  | j                  d      ||d       | j'                  | j                  d      |||
       | j)                  | j                  d       |||
!        y#y#c c}w # t*        $ r}t-        d"t/        |             d#}~ww xY w)$z/Talk to the Meraki API and build the inventory.)r9   r   strict)r(   r   getOrganizationDevicesr!   r"   r%   r)   z No name set for device with MAC mac)group	networkId
network_idr2   zDevice z" is not associated with a network.lanIpansible_hostzNo lanIp found for device with z<. The `ansible_host` variable will not be set for this host.device_typemodelansible_product_serialserial
macaddresscomposeT)r>   groupskeyed_groups)keys	variableshostr>   z'Failed to get devices from Meraki API: N)r   r   parse_read_config_datar   r<   pop
get_optionr   r,   r3   r-   displaywarning	inventoryadd_hostset_variablevvvv_set_composite_vars_add_host_to_composed_groups_add_host_to_keyed_groups	Exceptionr   r
   )r   rW   loaderr   cacheconfigkmeraki_connect_configvalidated_configr   r>   r.   devicesr0   devicehostnameer   s                    r   rQ   zInventoryModule.parse   s    	ot*9fdEJ''-"(!
A1E,EAvayL!
 !
  11, 2 . ),,_=*"23	B	J++&1*7N , G
 "55iO% 6F%zz&1H#,,>ve}oN $*%=NN++HE+Bzz+.33$lF;4G 33$iVK=P1Q ))%hZ/QR zz'*33$nfWo ,,=hZ HZ Z
 NN// - NN// ":F8<L NN// ,u? ,,T__!.#$*HT - C 55doo 7"#)8F 6 D 22!__^<"(%%	 3 c6 '!
Z  	J9)A,HJ J	Js#   J$J$HJ) )	K2K		K)T)	__name__
__module____qualname__NAMEr   r3   r<   rQ   __classcell__)r   s   @r   r   r      s%     D	+VJ VJr   r   N)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLES<ansible_collections.cisco.meraki.plugins.plugin_utils.merakir   r   ansible.plugins.inventoryr   r	   +ansible.module_utils.common.text.convertersr
   r   $ansible.module_utils.common.arg_specr   ansible.errorsr   updatedictr    r   r   <module>rz      su   
 C Bun* I J F ' ,-     t679zJ)= zJr   