
    VhL2                         d dl mZmZmZ dZdZdZd dlZd dlm	Z	 d dl
mZmZ d dlmZ d d	lmZ eZ G d
 de      Z G d de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionu  
---
module: grafana_folder
author:
  - Rémi REY (@rrey)
version_added: "1.0.0"
short_description: Manage Grafana folders
description:
  - Create/update/delete Grafana folders through the folders API.
requirements:
  - The folders API is only available starting Grafana 5 and the module will fail if the server version is lower than version 5.
options:
  name:
    description:
      - The title of the Grafana folder.
    required: true
    type: str
    aliases: [ title ]
  uid:
    description:
      - The folder UID.
    type: str
  parent_uid:
    description:
      - The parent folder UID.
      - Available with subfolder feature of Grafana 11.
    type: str
  state:
    description:
      - Delete the members not found in the C(members) parameters from the
      - list of members found on the folder.
    default: present
    type: str
    choices: ["present", "absent"]
  org_id:
    description:
    - Grafana organization ID in which the datasource should be created.
    - Not used when C(grafana_api_key) is set, because the C(grafana_api_key) only
      belongs to one organization.
    - Mutually exclusive with C(org_name).
    default: 1
    type: int
  org_name:
    description:
    - Grafana organization name in which the datasource should be created.
    - Not used when C(grafana_api_key) is set, because the C(grafana_api_key) only
      belongs to one organization.
    - Mutually exclusive with C(org_id).
    type: str
  skip_version_check:
    description:
      - Skip Grafana version check and try to reach api endpoint anyway.
      - This parameter can be useful if you enabled C(hide_version) in grafana.ini
    required: False
    type: bool
    default: false
    version_added: "1.2.0"
extends_documentation_fragment:
- community.grafana.basic_auth
- community.grafana.api_key
a  
---
- name: Create a folder
  community.grafana.grafana_folder:
      url: "https://grafana.example.com"
      grafana_api_key: "{{ some_api_token_value }}"
      title: "grafana_working_group"
      state: present

- name: Delete a folder
  community.grafana.grafana_folder:
      url: "https://grafana.example.com"
      grafana_api_key: "{{ some_api_token_value }}"
      title: "grafana_working_group"
      state: absent
a
  
---
folder:
    description: Information about the folder
    returned: On success
    type: complex
    contains:
        id:
            description: The folder identifier
            returned: always
            type: int
            sample:
              - 42
        uid:
            description: The folder uid
            returned: always
            type: str
            sample:
              - "nErXDvCkzz"
        orgId:
            description: The organization id
            returned: always
            type: int
            sample:
              - 1
        title:
            description: The folder title
            returned: always
            type: str
            sample:
              - "Department ABC"
        url:
            description: The folder url
            returned: always
            type: str
            sample:
              - "/dashboards/f/nErXDvCkzz/department-abc"
        hasAcl:
            description: Boolean specifying if folder has acl
            returned: always
            type: bool
            sample:
              - false
        canSave:
            description: Boolean specifying if current user can save in folder
            returned: always
            type: bool
            sample:
              - false
        canEdit:
            description: Boolean specifying if current user can edit in folder
            returned: always
            type: bool
            sample:
              - false
        canAdmin:
            description: Boolean specifying if current user can admin in folder
            returned: always
            type: bool
            sample:
              - false
        createdBy:
            description: The name of the user who created the folder
            returned: always
            type: str
            sample:
              - "admin"
        created:
            description: The folder creation date
            returned: always
            type: str
            sample:
              - "2018-01-31T17:43:12+01:00"
        updatedBy:
            description: The name of the user who last updated the folder
            returned: always
            type: str
            sample:
              - "admin"
        updated:
            description: The date the folder was last updated
            returned: always
            type: str
            sample:
              - "2018-01-31T17:43:12+01:00"
        version:
            description: The folder version
            returned: always
            type: int
            sample:
              - 1
        parentUid:
            description: The parent folders uid
            returned: always as subfolder
            type: str
            sample:
              - "76HjcBH2"
N)AnsibleModule)	fetch_urlbasic_auth_header)base)to_textc                       e Zd Zy)GrafanaErrorN)__name__
__module____qualname__     t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/grafana/plugins/modules/grafana_folder.pyr   r      s    r   r   c                   B    e Zd Zd Zd
dZd Zd Zd ZddZddZ	d	 Z
y)GrafanaFolderInterfacec                    || _         t        j                  |j                  j	                  d            | _        d | _        ddi| _        |j                  j	                  dd       r d|j                  d   z  | j                  d<   nt        |j                  d   |j                  d         | j                  d<   |j                  d	   r| j                  |j                  d	         n|j                  d
   | _        | j                  | j                         |j                  j	                  d      du rm	 | j                         }d   dk  r| j                   j                  dd       |d   dk  r.|j                  d   r| j                   j                  dd       y y y y # t        $ r0}| j                   j                  dt        |             Y d }~d }~ww xY w)NurlzContent-Typezapplication/jsongrafana_api_keyz	Bearer %sAuthorizationurl_usernameurl_passwordorg_nameorg_idskip_version_checkFTfailedmsgmajor   z,folders API is available starting Grafana v5   
parent_uidz/Subfolder API is available starting Grafana v11)_moduler	   	clean_urlparamsgetgrafana_urlr   headersr   organization_by_nameswitch_organizationget_versionr   	fail_jsonr
   )selfmodulegrafana_versiones       r   __init__zGrafanaFolderInterface.__init__   s   >>&--*;*;E*BC&(:;==.5fmm,=>> LL) ->n-v}}^/L-DLL)
 ==, ))&--
*CD]]8, K
 $$T[[1==12e;D"&"2"2"4 w'!+&&%S '  w'",|1L&&%V '  2M, <   D&&d
&CCDs   4F! !	G*&GGNc                 v   |t        j                  |d      }|sg }dj                  | j                  |      }t	        | j
                  ||||      \  }}|d   }|dk(  ry |dk(  r%| j
                  j                  dd	|d
|d       n|dk(  r| j
                  j                  dd       nj|dk(  r1|j                         d   }	| j
                  j                  d|	       n4|dk(  r/|j                         xs d}
| j
                  j                  |
      S | j
                  j                  dd|z         y )NT)	sort_keysz{grafana_url}{path})r)   pathdatar*   methodstatusi  i  z Unauthorized to perform action 'z' on ''r   i  zPermission Deniedi  message   z{}z)Grafana folders API answered with HTTP %d)	jsondumpsformatr)   r   r%   r.   read	from_json)r/   r   r8   r*   r9   full_urlrespinfostatus_code	error_msgresponses              r   _send_requestz$GrafanaFolderInterface._send_request   s;   ::dd3DG(//D<L<LSV/WLL(wv

d 8n#CLL""EKXV #  CLL""$4G"HC		I.ILL""$I">C yy{*dH<<))(33H;V 	 	
r   c                 J    d|z  }| j                  || j                  d       y )Nz/api/user/using/%dPOSTr*   r9   rI   r*   )r/   r   r   s      r   r,   z*GrafanaFolderInterface.switch_organization  s%    "V+3VDr   c                     d}| j                  || j                  d      }t        fd|D              }|r|d   S | j                  j	                  ddz         y )	Nz/api/user/orgsGETrL   c              3   4   K   | ]  }|d    k(  s|  yw)nameNr   ).0orgr   s     r   	<genexpr>z>GrafanaFolderInterface.organization_by_name.<locals>.<genexpr>!  s     MSS[H5LSMs   orgIdTz-Current user isn't member of organization: %sr   )rI   r*   nextr%   r.   )r/   r   r   organizationsorgas    `   r   r+   z+GrafanaFolderInterface.organization_by_name  sb    **3U*SMMMN= LxW 	 	
r   c                     d}| j                  |d | j                  d      }|j                  d      }|6|j                  d      \  }}}t	        |      t	        |      t	        |      dS t        d|z        )Nz/api/healthrO   r7   version.)r!   minorrevz$Failed to retrieve version from '%s')rI   r*   r(   splitintr   )r/   r   rH   rZ   r!   r\   r]   s          r   r-   z"GrafanaFolderInterface.get_version)  s~    %%dDLL & 
 ,,y) 'c 2E5# Z#e*SXNNACGHHr   c                 d    d}t        |||      }| j                  ||| j                  d      }|S )Nz/api/folders)titleuid	parentUidrK   r7   )dictrI   r*   )r/   ra   rb   r$   r   folderrH   s          r   create_folderz$GrafanaFolderInterface.create_folder4  s>    EsjA%%fdll6 & 
 r   c                 &   d|rd|z  ndz  }| j                  || j                  d      }|r[|r%|D cg c]  }|j                  d      |k(  s| }}n-|D cg c]"  }|j                  d      t        |      k(  s!|$ }}|r|d   S y c c}w c c}w )	Nz/api/folders%sz?parentUid=%s rO   rL   rb   ra   r   )rI   r*   r(   r
   )r/   ra   rb   r$   r   rH   itemfolderss           r   
get_folderz!GrafanaFolderInterface.get_folder<  s    */J">RTU%%c4<<%N,4OD38N4OO &.!'1Bgen1TD  qz! Ps   B	B	"B;Bc                 L    d|z  }| j                  || j                  d      }|S )Nz/api/folders/%sDELETErL   rM   )r/   
folder_uidr   rH   s       r   delete_folderz$GrafanaFolderInterface.delete_folderL  s,    *,%%c4<<%Qr   )NNrO   )NN)r   r   r   r3   rI   r,   r+   r-   rf   rk   ro   r   r   r   r   r      s.    !F
@E	
	I r   r   c                     t        j                         } | j                  t        ddgd      t        dd      t        d      t        d      t        d	d
      t        ddddg      t        d             t	        | d
t        j
                         g dgz   t        j                         ddggz         }|j                  d   }|j                  d   }|j                  d   }|j                  d   }t        j                  |j                  d         |j                  d<   t        |      }d
}|j                  |||      }|dk(  r>|(|j                  |||       |j                  |||      }d}|j                  ||       y |dk(  rI||j                  d
d       |j                  |j                  d            }	|j                  d|	       y y )Nstrra   T)typealiasesrequired   r_   )defaultrr   )rr   boolF)rr   rv   presentabsent)rr   rv   choices)rQ   r   r   r$   r   staterb   )r   r   r   r   r   )argument_specsupports_check_moderequired_togethermutually_exclusiver{   rQ   r$   rb   r   )changedre   zNo folder found)r   r<   )r	   grafana_argument_specupdaterd   r   grafana_required_togethergrafana_mutually_exclusiver'   r&   r   rk   rf   	exit_jsonro   r(   )
r|   r0   r{   ra   r$   rb   grafana_ifacer   re   results
             r   mainr   R  s   ..0Muwi$?AE*5!U#VU;y9h:OPe   #!88:5
67::<z"

	F MM'"EMM&!E|,J
--
C>>&--*>?FMM%*62MG%%eS*=F	>''sJ?"--eS*EFG8	(	>U4EF,,VZZ->?v6	 
r   __main__)
__future__r   r   r   DOCUMENTATIONEXAMPLESRETURNr>   ansible.module_utils.basicr   ansible.module_utils.urlsr   r   :ansible_collections.community.grafana.plugins.module_utilsr	   ansible.module_utils._textr
   rr   __metaclass__	Exceptionr   objectr   r   r   r   r   r   <module>r      sq   ( A @<|"a
F  4 B K .	9 	yV yx+7\ zF r   