
    Vh|&                         d dl mZmZmZ dZdZdZd dlZd dlm	Z	 e
Z G d de      Zd	 Zd
 Zd Zd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionu  module: grafana_plugin
author:
- Thierry Sallé (@seuf)
short_description: Manage Grafana plugins via grafana-cli
description:
- Install and remove Grafana plugins.
- See U(https://grafana.com/docs/plugins/installation/) for upstream documentation.
options:
  name:
    description:
    - Name of the plugin.
    required: true
    type: str
  version:
    description:
    - Version of the plugin to install.
    - Defaults to C(latest).
    type: str
  grafana_plugins_dir:
    description:
    - Directory where the Grafana plugin will be installed.
    - If omitted, defaults to C(/var/lib/grafana/plugins).
    type: str
  grafana_repo:
    description:
    - URL to the Grafana plugin repository.
    - 'If omitted, grafana-cli will use the default value: U(https://grafana.com/api/plugins).'
    type: str
  grafana_plugin_url:
    description:
    - Full URL to the plugin zip file instead of downloading the file from U(https://grafana.com/api/plugins).
    - Requires grafana 4.6.x or later.
    type: str
  state:
    description:
    - Whether the plugin should be installed.
    choices:
    - present
    - absent
    default: present
    type: str
  validate_certs:
    description:
    - Boolean variable to include --insecure while installing pluging
    default: false
    type: bool
z
---
- name: Install/update Grafana piechart panel plugin
  community.grafana.grafana_plugin:
    name: grafana-piechart-panel
    version: latest
    state: present
zm
---
version:
  description: version of the installed/removed/updated plugin.
  type: str
  returned: always
N)AnsibleModulec                       e Zd Zy)GrafanaCliExceptionN)__name__
__module____qualname__     t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/grafana/plugins/modules/grafana_plugin.pyr   r   P   s    r   r   c                 j    | j                  d      \  }}|j                         |j                         fS )N@)splitstrip)stringnameversions      r   parse_versionr   T   s+    LL%MD'::<((r   c                    d}d}d }t         j                  j                  |      \  }}|r ||      ru|}nrt         j                  d   j                  t         j                        D ]?  }|j                  d      }t         j                  j                  ||      } ||      s=|} n |t        d      d| v r| d   rdj                  |d	| d         }d
| v r| d
   rdj                  |d| d
         }d| v r| d   rdj                  |d| d         }d| v r| d   du rdj                  |d      }dj                  |d      S )z
    Get the grafana-cli binary path with global options.
    Raise a GrafanaCliException if the grafana-cli is not present or not in PATH

    :param params: ansible module params. Used to fill grafana-cli global params.
    zgrafana-cliNc                     t         j                  j                  |       xr$ t        j                  | t         j                        S )N)ospathisfileaccessX_OK)fpaths    r   is_exezgrafana_cli_bin.<locals>.is_exec   s)    ww~~e$B5"'')BBr   PATH"z0grafana-cli binary is not present or not in PATHgrafana_plugin_urlz{0} {1} {2}z--pluginUrlgrafana_plugins_dirz--pluginsDirgrafana_repoz--repovalidate_certsFz{0} {1}z
--insecureplugins)	r   r   r   environpathsepr   joinr   format)paramsprogramgrafana_clir   r   fnamer   exe_files           r   grafana_cli_binr0   Y   sl    GKC 77==)LE5'?!KJJv&,,RZZ8 	D::c?Dww||D'2Hh&	 !"TUU6)f5I.J'..]F3G,HK !F*v6K/L'..^V4I-JK V#~(>'..Xvn'=K v%&1A*Be*K#**;EKY77r   c                    t        |      }| j                  dj                  |            \  }}}|j                  d      }|D ]A  }|j	                  d      dk7  s|j                         }t        |      \  }}	||d   k(  s?|	c S  y)z
    Fetch grafana installed plugin version. Return None if plugin is not installed.

    :param module: ansible module object. used to run system commands.
    :param params: ansible module params.
    z{0} ls
 @ r   N)r0   run_commandr*   r   findrstripr   )
moduler+   r-   rcstdoutstderrstdout_lineslineplugin_nameplugin_versions
             r   get_grafana_plugin_versionr@      s     "&)K++HOOK,HIB<<%L &99Ur!;;=D*7*='KfVn,%%& r   c                     t        |      }| j                  dj                  ||d               \  }}}|j                  d      }|d   r|d   j	                         S y)z
    Fetch the latest version available from grafana-cli.
    Return the newest version number or None not found.

    :param module: ansible module object. used to run system commands.
    :param params: ansible module params.
    z{0} list-versions {1}r   r2   r   N)r0   r5   r*   r   r7   )r8   r+   r-   r9   r:   r;   r<   s          r   !get_grafana_plugin_version_latestrB      sf     "&)K++&&{F6NCB <<%LAA%%''r   c                    t        |      }|d   dk(  rt        | |      }|qd|v rg|d   rb|d   |k(  rdd|dS |d   dk(  s|d   -t        | |      }||k(  rdd|dS d	j                  ||d
         }ndj                  ||d
   |d         }nrdd|dS d|v r=|d   dk(  s|d   dj                  ||d
         }nEdj                  ||d
   |d         }n+dj                  ||d
         }ndj                  ||d
         }| j	                  |d      \  }}}|dk(  r|j                  d      }	|	D ]  }
|
j                  |d
         s|
j                  d      dk7  r|
j                         }
t        |
      \  }}nd}|d   dk(  rdj                  |d
   |      d|dc S dj                  |d
   |      ddc S  y|d   dk(  r)|j                  d      rdj                  |d
   |      ddS t        dj                  ||||            )z
    Install update or remove grafana plugin

    :param module: ansible module object. used to run system commands.
    :param params: ansible module params.
    statepresentNr   z Grafana plugin already installedF)msgchangedr   latestz{0} update {1}r   z{0} install {1} {2}z{0} install {1}z{0} uninstall {1}   )umaskr   r2   r3   r4   z"Grafana plugin {0} installed : {1}Tz$Grafana plugin {0} uninstalled : {1})rF   rG   absentzplugin does not existz,Grafana plugin {0} already uninstalled : {1}z1'{0}' execution returned an error : [{1}] {2} {3})
r0   r@   rB   r*   r5   r   r6   r7   r   r   )r8   r+   r-   grafana_plugin_versionlatest_versioncmdr9   r:   r;   r<   r=   r>   r?   s                r   grafana_pluginrO      s    "&)Kg)#!;FF!K!-F"vi'8)$(>>A#(#9  i(H4y8I8Q)J"F* *-CC'I+0+A$ 
 /55k6&>R3::'	9J
 >$5  F")$0F94E4M+22;vOC/66#VF^VI5FC (..{F6NK!((fVnE++Cv+>B	Qw||D)  	Dyy(99U#r);;=D2?2E/K%)N'?i/CJJ"6NC  $(#1   FLL"6NC  $(	 #	0 '?h&6;;7N+OELL6NC !	  "?FFR
 	
r   c                     t        t        t        dd      t        d      t        d      t        d      t        d      t        dd      t        dd	gd
            d      } 	 t        | | j                        } | j                  dddi| y # t        $ r,}| j                  ddj                  |             Y d }~y d }~wt        $ r6}| j                  ddj                  t        |      |             Y d }~y d }~ww xY w)NTstr)requiredtype)rS   boolF)rS   defaultrE   rK   )choicesrU   )r   r   r#   r$   r"   r%   rD   )argument_specsupports_check_modez{0})failedrF   z
{0} : {1} rY   r   )
r   dictrO   r+   r   	fail_jsonr*   	ExceptionrS   	exit_json)r8   resultes      r   mainr`     s    t%0e$ $% 05)#/VU;	84iH
 "F6 F,E,V,
  %,,q/: ,*=*=d1gq*IJs$   *B 	D"CD,DD__main__)
__future__r   r   r   DOCUMENTATIONEXAMPLESRETURNr   ansible.module_utils.basicr   rS   __metaclass__r\   r   r   r0   r@   rB   rO   r`   r	   r   r   r   <module>rh      sr    A @.`
 
 4	) 	)
+8\&$\
~6 zF r   