
    Vh                          d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZ dZ	 d dlZd	Zd
 Z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: packet_sshkey
short_description: Create/delete an SSH key in Packet host
description:
  - Create/delete an SSH key in Packet host.
  - API is documented at U(https://www.packet.net/help/api/#page:ssh-keys,header:ssh-keys-ssh-keys-post).
author: "Tomas Karasek (@t0mk) <tom.to.the.k@gmail.com>"
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  state:
    description:
      - Indicate desired state of the target.
    default: present
    choices: ['present', 'absent']
    type: str
  auth_token:
    description:
      - Packet API token. You can also supply it in environment variable E(PACKET_API_TOKEN).
    type: str
  label:
    description:
      - Label for the key. If you keep it empty, it will be read from key string.
    type: str
    aliases: [name]
  id:
    description:
      - UUID of the key which you want to remove.
    type: str
  fingerprint:
    description:
      - Fingerprint of the key which you want to remove.
    type: str
  key:
    description:
      - Public Key string (V({type} {base64 encoded key} {description})).
    type: str
  key_file:
    description:
      - File with the public key.
    type: path

requirements:
  - packet-python
ao  
# All the examples assume that you have your Packet API token in env var PACKET_API_TOKEN.
# You can also pass the api token in module param auth_token.

- name: Create sshkey from string
  hosts: localhost
  tasks:
    community.general.packet_sshkey:
      key: "{{ lookup('file', 'my_packet_sshkey.pub') }}"

- name: Create sshkey from file
  hosts: localhost
  tasks:
    community.general.packet_sshkey:
      label: key from file
      key_file: ~/ff.pub

- name: Remove sshkey by id
  hosts: localhost
  tasks:
    community.general.packet_sshkey:
      state: absent
      id: eef49903-7a09-4ca1-af67-4087c29ab5b6
a  
changed:
  description: True if a sshkey was created or removed.
  type: bool
  sample: true
  returned: always
sshkeys:
  description: Information about sshkeys that were created/removed.
  type: list
  sample:
    [
      {
        "fingerprint": "5c:93:74:7c:ed:07:17:62:28:75:79:23:d6:08:93:46",
        "id": "41d61bd8-3342-428b-a09c-e67bdd18a9b7",
        "key": "ssh-dss AAAAB3NzaC1kc3MAAACBA ... MdDxfmcsCslJKgoRKSmQpCwXQtN2g== user@server",
        "label": "mynewkey33"
      }
    ]
  returned: always
N)AnsibleModuleTFPACKET_API_TOKENc                 >    i }g d}|D ]  }t        | |      ||<    |S )N)idkeylabelfingerprintgetattr)sshkeysshkey_data	copy_keysnames       s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/packet_sshkey.pyserialize_sshkeyr   {   s2    K5I 2#FD1D2    c                 l    	 t        j                  | d      }t        |      | k(  S # t        $ r Y yw xY w)N   )versionF)uuidUUID
ValueErrorstr)myuuidvals     r   is_valid_uuidr      s<    ii* s8v  s   ' 	33c                     i }| j                         } | |d<   | j                         }t        |      dv rt        |      dk(  r|d   |d<   |S t        d| z        )Nr
   )      r"   r!   r   z Public key %s is in wrong format)stripsplitlen	Exception)key_strret_dictcut_keys      r   load_key_stringr*      sf    HmmoGHUOmmoG
7|vw<1 '
HW O :WDEEr   c                 ^   | j                   j                  d      }|rt        |      st        d|z        g d}i |D ]<  }| j                   j                  |      | j                   j                  |      |<   > | j                   j                  d      rt	        | j                   j                  d            5 }t        |j                               }d d d        d   d<   | j                   j                  d      |j                  d      r|d   d<   fd}|S # 1 sw Y   LxY w)Nr	   zsshkey ID %s is not valid UUID)r   r   r	   r
   key_filer
   r   c                 \     dv r j                   d   k(  S t         fdD              S )Nr
   c              3   B   K   | ]  }|   t        |      k(    y w)Nr   ).0fkselect_dicts     r   	<genexpr>z8get_sshkey_selector.<locals>.selector.<locals>.<genexpr>   s      L1{1~A6Ls   )r
   all)r1   r2   s   `r   selectorz%get_sshkey_selector.<locals>.selector   s2    K55K... LLLLr   )paramsgetr   r&   openr*   read)modulekey_idselecting_fieldsr0   _file
loaded_keyr5   r2   s          @r   get_sshkey_selectorr?      s   ]]t$FV$<vEFF<K 2==Q+#]]..q1KN2 }}$&--##J/0 	7E(6J	7'.E==W%-~~g&'1'':G$M O	7 	7s   >D##D,c           
         t        |      }|j                         }t        ||      }d}| dk(  r*|g k(  r>i }|j                  j	                  d      rFt        |j                  j	                  d            5 }t        |j                               }d d d        |j                  j	                  d      r$t        |j                  j	                  d            }|j                  j	                  d      r|j                  j	                  d      |d<   dD ]  }	|	|vsd|	z  }
t        |
       g }|j                  |d   |d         }d}|j                  |       n|D ]  }	 |j                          d} ||D cg c]  }t        |       c}dS # 1 sw Y   xY w# t        $ r2}d	|j                  d
|j                  d| d|}
t        |
      d }~ww xY wc c}w )NFpresentr,   r
   r   )r   r
   zIf you want to ensure a key is present, you must supply both a label and a key string, either in module params, or in a key file. %s is missingTzwhile trying to remove sshkey z, id  z, got error: )changedsshkeys)r?   list_ssh_keysfilterr6   r7   r8   r*   r9   r&   create_ssh_keyappenddeleter   r	   r   )target_stater:   packet_connr5   existing_sshkeysmatching_sshkeysrC   newkeyr0   param_msgnew_key_responser1   es                 r   act_on_sshkeysrS      s   "6*H"002h(89Gy r! F}}  ,&--++J78 7A,QVVX6F7}}  '():):5)AB}}  )"(--"3"3G"<w) *&M $$D $D/)*  "*99w 0G##$45 " 	&A&
	& 1ABA$Q'B ?7 72  & !$$a9  o%	& Cs*   7F F9GF	G&-GGc                  *   t        t        t        ddgd      t        t        j                  j	                  t
              d      t        ddg      t        d	      t        d	      t        dd
      t        d	            g d      } t        s| j                  d       | j                  j	                  d      sdt
        z  }| j                  |       | j                  j	                  d      }t        j                  |      }| j                  j	                  d      }|dv r	  | j                  di t        || |       y | j                  d|z         y # t        $ r(}| j                  dt        |      z         Y d }~y d }~ww xY w)NrA   absent)choicesdefaultT)rW   no_logr   r   )typealiases)rY   )rY   rX   path)state
auth_tokenr   r	   r   r
   r,   ))r   r	   )r   r   )r	   r   )r
   r   )r
   r	   )r,   r
   )argument_specmutually_exclusivezpacket required for this module)msgr]   z[if Packet API token is not in environment variable %s, the auth_token parameter is required)r]   r\   )rA   rU   zfailed to set sshkey state: %sz'%s is not a valid state for this module )r   dictosenvironr7   PACKET_API_TOKEN_ENV_VARHAS_PACKET_SDK	fail_jsonr6   packetManager	exit_jsonrS   r&   r   )r:   	_fail_msgr]   rK   r\   rR   s         r   mainrl      sh   	84iHBJJNN3K$L#')EF84%(%-v&	

F* >?==\*<-.	 	Y'""<0J..J7KMMg&E%%	LFJ~eV[IJ 	FNO  	L!ACF!JKK	Ls   -E! !	F*FF__main__)
__future__r   r   r   rY   __metaclass__DOCUMENTATIONEXAMPLESRETURNrc   r   ansible.module_utils.basicr   rf   rh   ImportErrorre   r   r   r*   r?   rS   rl   __name__ra   r   r   <module>rv      s    A @1f2
* 
  4
 . 
:-`+P\ zF   Ns   A
 
AA