
    Vh)                     D   d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZmZ d Z G d d	e      Z e ed
       ed
            Z e ed
d       ed
d            Z e ed
d       edd
            Z e ed
d       ed            Z e        e        ed       eddedgdg       eddedgdg       edde       edde       edde       ed       ed
       edddg      dZej-                  e       d Zedk(  r e        yy)     )absolute_importdivisionprint_functiona  
module: consul_token
short_description: Manipulate Consul tokens
version_added: 8.3.0
description:
  - Allows the addition, modification and deletion of tokens in a Consul cluster using the agent. For more details on using
    and configuring ACLs, see U(https://www.consul.io/docs/guides/acl.html).
author:
  - Florian Apolloner (@apollo13)
extends_documentation_fragment:
  - community.general.consul
  - community.general.consul.token
  - community.general.consul.actiongroup_consul
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: partial
    details:
      - In check mode the diff will miss operational attributes.
  action_group:
    version_added: 8.3.0
options:
  state:
    description:
      - Whether the token should be present or absent.
    choices: ['present', 'absent']
    default: present
    type: str
  accessor_id:
    description:
      - Specifies a UUID to use as the token's Accessor ID. If not specified a UUID will be generated for this field.
    type: str
  secret_id:
    description:
      - Specifies a UUID to use as the token's Secret ID. If not specified a UUID will be generated for this field.
    type: str
  description:
    description:
      - Free form human readable description of the token.
    type: str
  policies:
    type: list
    elements: dict
    description:
      - List of policies to attach to the token. Each policy is a dict.
      - If the parameter is left blank, any policies currently assigned will not be changed.
      - Any empty array (V([])) will clear any policies previously set.
    suboptions:
      name:
        description:
          - The name of the policy to attach to this token; see M(community.general.consul_policy) for more info.
          - Either this or O(policies[].id) must be specified.
        type: str
      id:
        description:
          - The ID of the policy to attach to this token; see M(community.general.consul_policy) for more info.
          - Either this or O(policies[].name) must be specified.
        type: str
  roles:
    type: list
    elements: dict
    description:
      - List of roles to attach to the token. Each role is a dict.
      - If the parameter is left blank, any roles currently assigned will not be changed.
      - Any empty array (V([])) will clear any roles previously set.
    suboptions:
      name:
        description:
          - The name of the role to attach to this token; see M(community.general.consul_role) for more info.
          - Either this or O(roles[].id) must be specified.
        type: str
      id:
        description:
          - The ID of the role to attach to this token; see M(community.general.consul_role) for more info.
          - Either this or O(roles[].name) must be specified.
        type: str
  templated_policies:
    description:
      - The list of templated policies that should be applied to the role.
    type: list
    elements: dict
    suboptions:
      template_name:
        description:
          - The templated policy name.
        type: str
        required: true
      template_variables:
        description:
          - The templated policy variables.
          - Not all templated policies require variables.
        type: dict
  service_identities:
    type: list
    elements: dict
    description:
      - List of service identities to attach to the token.
      - If not specified, any service identities currently assigned will not be changed.
      - If the parameter is an empty array (V([])), any node identities assigned will be unassigned.
    suboptions:
      service_name:
        description:
          - The name of the service.
          - Must not be longer than 256 characters, must start and end with a lowercase alphanumeric character.
          - May only contain lowercase alphanumeric characters as well as V(-) and V(_).
        type: str
        required: true
      datacenters:
        description:
          - The datacenters the token will be effective.
          - If an empty array (V([])) is specified, the token will valid in all datacenters.
          - Including those which do not yet exist but may in the future.
        type: list
        elements: str
  node_identities:
    type: list
    elements: dict
    description:
      - List of node identities to attach to the token.
      - If not specified, any node identities currently assigned will not be changed.
      - If the parameter is an empty array (V([])), any node identities assigned will be unassigned.
    suboptions:
      node_name:
        description:
          - The name of the node.
          - Must not be longer than 256 characters, must start and end with a lowercase alphanumeric character.
          - May only contain lowercase alphanumeric characters as well as V(-) and V(_).
        type: str
        required: true
      datacenter:
        description:
          - The nodes datacenter.
          - This will result in effective token only being valid in this datacenter.
        type: str
        required: true
  local:
    description:
      - If true, indicates that the token should not be replicated globally and instead be local to the current datacenter.
    type: bool
  expiration_ttl:
    description:
      - This is a convenience field and if set will initialize the C(expiration_time). Can be specified in the form of V(60s)
        or V(5m) (that is, 60 seconds or 5 minutes, respectively). Ingored when the token is updated!
    type: str
ae  
- name: Create / Update a token by accessor_id
  community.general.consul_token:
    state: present
    accessor_id: 07a7de84-c9c7-448a-99cc-beaf682efd21
    token: 8adddd91-0bd6-d41d-ae1a-3b49cfa9a0e8
    roles:
      - name: role1
      - name: role2
    service_identities:
      - service_name: service1
        datacenters: [dc1, dc2]
    node_identities:
      - node_name: node1
        datacenter: dc1
    expiration_ttl: 50m

- name: Delete a token
  community.general.consul_token:
    state: absent
    accessor_id: 07a7de84-c9c7-448a-99cc-beaf682efd21
    token: 8adddd91-0bd6-d41d-ae1a-3b49cfa9a0e8
a+  
token:
  description: The token as returned by the Consul HTTP API.
  returned: always
  type: dict
  sample:
    AccessorID: 07a7de84-c9c7-448a-99cc-beaf682efd21
    CreateIndex: 632
    CreateTime: "2024-01-14T21:53:01.402749174+01:00"
    Description: Testing
    Hash: rj5PeDHddHslkpW7Ij4OD6N4bbSXiecXFmiw2SYXg2A=
    Local: false
    ModifyIndex: 633
    SecretID: bd380fba-da17-7cee-8576-8d6427c6c930
    ServiceIdentities: ["ServiceName": "test"]
operation:
  description: The operation performed.
  returned: changed
  type: str
  sample: update
)AnsibleModule)AUTH_ARGUMENTS_SPEC_ConsulModulec                 V   | j                  |      }|j                  |      }||y |D ci c]  }|d   |d    }}|D ci c]  }|d   |d    }}|D ]G  }|j                  d      }	|j                  d      }
|	r|
s|	|v r||	   |d<   |	r8|
s;|
|v s@||
   |d<   I y c c}w c c}w )NNameID)get)api_obj
module_objkeyapi_objsmodule_objsi
name_to_id
id_to_nameobj
identifiernames              r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/consul_token.pynormalize_link_objr      s    {{3H..%K;..67!F)QtW$7J7.67!D'1V9$7J7 )WWT]
wwvdzZ'?$Z0CKdtz'9"4(CI) 87s   B!B&c                   <     e Zd ZdZdZdgZdhZ fdZ fdZ xZ	S )ConsulTokenModulez	acl/tokentokenaccessor_idexpiration_ttlc                 `    | j                  | j                        sy t        t        |          S )N)id_from_objparamssuperr   read_object)self	__class__s    r   r#   zConsulTokenModule.read_object   s)    ,&9;;    c                     d|vrd|v r|d= t        ||d       t        ||d       d|v r|d= t        t        |   ||      S )NSecretIDRolesPoliciesExpirationTTL)r   r"   r   needs_update)r$   r   r   r%   s      r   r,   zConsulTokenModule.needs_update   sY    Z'J',A
#7J87J
; j(?+&:7JOOr&   )
__name__
__module____qualname__api_endpoint
result_keyunique_identifierscreate_only_fieldsr#   r,   __classcell__)r%   s   @r   r   r      s/    LJ'*+<
P 
Pr&   r   str)type)r   idT)r6   required)	node_name
datacenterlist)r6   elements)service_namedatacentersdict)template_nametemplate_variables)no_log)r6   r<   optionsmutually_exclusiverequired_one_of)r6   r<   rC   boolpresentabsent)defaultchoices)descriptionr   	secret_idrolespoliciestemplated_policiesnode_identitiesservice_identitieslocalr   statec                  h    t        t        dddgfgd      } t        |       }|j                          y )NrS   rH   r   T)required_ifsupports_check_mode)r   _ARGUMENT_SPECr   execute)moduleconsul_modules     r   mainr[   =  s9    x-9: F
 &f-Mr&   __main__N)
__future__r   r   r   r6   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Aansible_collections.community.general.plugins.module_utils.consulr   r   r   r   r?   NAME_ID_SPECNODE_ID_SPECSERVICE_ID_SPECTEMPLATE_POLICY_SPECrW   updater[   r-    r&   r   <module>rj      s   A @Rh0
, 5)"P P4 	5	
 -.
 540&51
 ED1(  66T"*+'( *+'( $
 
 
 v&)i-BCG$J   ) * zF r&   