
    Vh#                        d dl mZmZmZ eZdZdZdZd dl	m
Z
mZ d dlmZ dZ	 d dlZdZd
 Zd Z e edd       edd       edddg       edd       edd       edd       ed       ed       ed       edd       ed       eg dd       eddg        edd	            Zd Zd  Zd! Zd" Zed#k(  r e        yy# e$ r d	ZY w xY w)$    )absolute_importdivisionprint_functiona'  
module: nomad_token
author: Pedro Nascimento (@apecnascimento)
version_added: "8.1.0"
short_description: Manage Nomad ACL tokens
description:
  - This module allows to create Bootstrap tokens, create ACL tokens, update ACL tokens, and delete ACL tokens.
requirements:
  - python-nomad
extends_documentation_fragment:
  - community.general.nomad
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  name:
    description:
      - Name of ACL token to create.
    type: str
  token_type:
    description:
      - The type of the token can be V(client), V(management), or V(bootstrap).
    choices: ["client", "management", "bootstrap"]
    type: str
    default: "client"
  policies:
    description:
      - A list of the policies assigned to the token.
    type: list
    elements: str
    default: []
  global_replicated:
    description:
      - Indicates whether or not the token was created with the C(--global).
    type: bool
    default: false
  state:
    description:
      - Create or remove ACL token.
    choices: ["present", "absent"]
    required: true
    type: str

seealso:
  - name: Nomad ACL documentation
    description: Complete documentation for Nomad API ACL.
    link: https://developer.hashicorp.com/nomad/api-docs/acl/tokens
a  
- name: Create boostrap token
  community.general.nomad_token:
    host: localhost
    token_type: bootstrap
    state: present

- name: Create ACL token
  community.general.nomad_token:
    host: localhost
    name: "Dev token"
    token_type: client
    policies:
      - readonly
    global_replicated: false
    state: absent

- name: Update ACL token Dev token
  community.general.nomad_token:
    host: localhost
    name: "Dev token"
    token_type: client
    policies:
      - readonly
      - devpolicy
    global_replicated: false
    state: absent

- name: Delete ACL token
  community.general.nomad_token:
    host: localhost
    name: "Dev token"
    state: absent
aM  
result:
  description: Result returned by nomad.
  returned: always
  type: dict
  sample:
    {
      "accessor_id": "0d01c55f-8d63-f832-04ff-1866d4eb594e",
      "create_index": 14,
      "create_time": "2023-11-12T18:48:34.248857001Z",
      "expiration_time": null,
      "expiration_ttl": "",
      "global": true,
      "hash": "eSn8H8RVqh8As8WQNnC2vlBRqXy6DECogc5umzX0P30=",
      "modify_index": 836,
      "name": "devs",
      "policies": [
        "readonly"
      ],
      "roles": null,
      "secret_id": "12e878ab-e1f6-e103-b4c4-3b5173bb4cea",
      "type": "client"
    }
)AnsibleModulemissing_required_lib)	to_nativeNTFc                 f     |j                   j                         }t         fd|D        d       }|S )Nc              3   J   K   | ]  }|j                  d       k(  r|  yw)NameN)get).0tokennames     q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/nomad_token.py	<genexpr>zget_token.<locals>.<genexpr>   s)      0EYYv&$.  0s    #)acl
get_tokensnext)r   nomad_clienttokensr   s   `   r   	get_tokenr      s5    ((*F 0V 0157EL    c                 t    | d   | d   | d   | d   | d   | d   | d   | d   | d	   | d
   | d   | d   | d   d}|S )N
AccessorIDCreateIndex
CreateTimeExpirationTTLExpirationTimeGlobalHashModifyIndexr   PoliciesRolesSecretIDType)accessor_idcreate_indexcreate_timeexpiration_ttlexpiration_timeglobalhashmodify_indexr   policiesroles	secret_idtype )nomad_responsetransformed_responses     r   transform_responser5      s{    %l3&}5%l3(9)*:; *v&&}5v&":.(#J/v&   r   str)requiredr1   inti&  )r1   defaultpresentabsent)r7   choicesbool   path)r1   )r1   no_log)client
management	bootstraprA   )r<   r9   list)r1   elementsr9   )hostportstateuse_ssltimeoutvalidate_certsclient_cert
client_key	namespacer   r   
token_typer.   global_replicatedc                  6    t        t        dddggddg      } | S )NFr   rO   )rO   rA   r   )rO   rB   rR   )argument_specsupports_check_moderequired_one_ofrequired_if)r   rS   modules    r   setup_module_objectrY      s3    #!\"
 03

F Mr   c                 L   t         s| j                  t        d             | j                  j	                  d      | j                  j	                  d      f}t        j                  | j                  j	                  d      | j                  j	                  d      | j                  j	                  d      | j                  j	                  d      | j                  j	                  d	      || j                  j	                  d
      | j                  j	                  d            }|S )Nzpython-nomadmsgrL   rM   rF   rG   rI   rJ   rK   rN   r   )rF   rG   securerJ   verifycertrN   r   )import_nomad	fail_jsonr   paramsr   nomadNomad)rX   certificate_sslr   s      r   setup_nomad_clientrf      s    1.AB}}((79J9J<9XYO;;]]v&]]v&}}  +!!),}}  !12--##K0mm(	L r   c                 
   t        |       }d}i }d}| j                  j                  d      dk(  r8| j                  j                  d      dk(  r<	 t        d|      }|rd}n)|j                  j                         }d	}t        |      }d
}n	 | j                  j                  d      | j                  j                  d      | j                  j                  d      | j                  j                  d      d}t        |d   |      }|r7|d   |d<   |j                  j                  |d   |      }d}t        |      }d
}n*|j                  j                  |      }d}t        |      }d
}| j                  j                  d      dk(  r| j                  j                  d      s| j                  d       | j                  j                  d      dk(  s| j                  j                  d      dk(  r| j                  d       	 t        | j                  j                  d      |      }	|	r/|j                  j!                  |	j                  d             d}d
}n*dj#                  | j                  j                  d            }| j%                  |||       y # t        j                  j                  j                  $ r` 	 |j                  j                         }d	}t        |      }d
}n1# t        $ r%}| j                  t        |             Y d }~nd }~ww xY wY w xY w# t        $ r&}| j                  t        |             Y d }~d }~ww xY w# t        $ r%}| j                  t        |             Y d }~d }~ww xY w)N FrH   r:   rO   rC   zBootstrap TokenzACL bootstrap already exist.zBoostrap token created.Tr[   r   r.   rP   )r   r%   r"   r   r   r   zACL token updated.zACL token Created.r;   zname is needed to delete token.z*Delete ACL bootstrap token is not allowed.zACL token deleted.zNo token with name '{0}' found)changedr\   result)rf   rb   r   r   r   generate_bootstrapr5   rc   api
exceptionsURLNotAuthorizedNomadException	Exceptionra   r   update_tokencreate_tokendelete_tokenformat	exit_json)
rX   r   r\   rj   ri   current_tokennomad_resulte
token_infor   s
             r   runry      s,   %f-L
CFG}}!Y.==\*k97 )*;\ J 8C#/#3#3#F#F#HL3C/=F"G3"MM--f5"MM--l; & 1 1* =$mm//0CD	
 !**V*<l K /<\/JJ|,#/#3#3#@#@|A\^h#iL.C/=F"G $0#3#3#@#@#LL.C/=F"G
 }}!X-}}  (!BC==\*k9V]]=N=Nv=VZk=k!MN
	/fmm//7FE  --eii.EF*6==fmm>O>OPV>WX
 W#f=q 99''FF 77#/#3#3#F#F#HL3C/=F"G  7$$1$6677B  3  Yq\ 223(  	/1..	/sn   :J CL" B M 'L )K*)L*	L3LLLLL"	M+MM	NM==Nc                  .    t               } t        |        y )N)rY   ry   rW   s    r   mainr{   '  s     "FKr   __main__)
__future__r   r   r   r1   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   r`   rc   ImportErrorr   r5   dictrS   rY   rf   ry   r{   __name__r2   r   r   <module>r      s(   A @2h!F
2 K AL
 ( 	t%	(	5$	'
y(&;
<fd+eQ'VT2&! 
E$
'	5	A8Tvr:6$(K>\
 zF O  Ls   C CC