
    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mZ d Zd Zd	 Zd
 Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: scaleway_sshkey
short_description: Scaleway SSH keys management module
author: Remy Leone (@remyleone)
description:
  - This module manages SSH keys on Scaleway account (U(https://developer.scaleway.com)).
extends_documentation_fragment:
  - community.general.scaleway
  - community.general.attributes

attributes:
  check_mode:
    support: full
  diff_mode:
    support: none

options:
  state:
    type: str
    description:
      - Indicate desired state of the SSH key.
    default: present
    choices:
      - present
      - absent
  ssh_pub_key:
    type: str
    description:
      - The public SSH key as a string to add.
    required: true
  api_url:
    type: str
    description:
      - Scaleway API URL.
    default: 'https://account.scaleway.com'
    aliases: ['base_url']
a  
- name: "Add SSH key"
  community.general.scaleway_sshkey:
    ssh_pub_key: "ssh-rsa AAAA..."
    state: "present"

- name: "Delete SSH key"
  community.general.scaleway_sshkey:
    ssh_pub_key: "ssh-rsa AAAA..."
    state: "absent"

- name: "Add SSH key with explicit token"
  community.general.scaleway_sshkey:
    ssh_pub_key: "ssh-rsa AAAA..."
    state: "present"
    oauth_token: "6ecd2c9b-6f4f-44d4-a187-61a92078d08c"
z
data:
  description: This is only present when O(state=present).
  returned: when O(state=present)
  type: dict
  sample:
    {
      "ssh_public_keys": [
        {
          "key": "ssh-rsa AAAA...."
        }
      ]
    }
)AnsibleModuleenv_fallback)scaleway_argument_specScalewayc                 V    | d   d   d   d   d   }|D cg c]  }|d   	 }}|S c c}w )Norganizationsr   usersssh_public_keyskey )raw_organization_dictssh_key_listssh_keyssh_key_lookups       u/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/scaleway_sshkey.pyextract_present_sshkeysr   [   sD    (9!<WEaHIZ[L4@AgenANA Bs   &c                 $    | d   d   d   d   d   S )Nr   r   r   idr   )r   s    r   extract_user_idr   a   s!     1!4W=a@FF    c                 8    d| D cg c]  }d|i c}i}|S c c}w )Nr   r   r   )
ssh_lookupr   ssh_lists      r   sshkey_user_patchr   e   s0    !/9$;(+ &+CL $; <HO$;s   c                    | j                   d   }| j                   d   }t        |       }|j                  d      }|j                  }|j                  }|j
                  s/| j                  dj                  ||j                  d                t        |      }g }	 t        |      }|d
v r||v r| j                  d       | j                  r| j                  d       |j                  |       t        |      }
|j!                  d|z  |
      }|j
                  r| j                  d|j                  	       | j                  dj                  |j                  |j                               y |dv r||vr| j                  d       | j                  r| j                  d       |j#                  |       t        |      }
|j!                  d|z  |
      }|j
                  r| j                  d|j                  	       | j                  dj                  |j                  |j                               y y # t        t        f$ r}	| j                  dd	       Y d }	~	d }	~	ww xY w)Nssh_pub_keystater   z Error getting ssh key [{0}: {1}]message)msgFz0Error while extracting present SSH keys from API)changeddata)present)r#   Tz	/users/%s)r$   z!Error creating ssh key [{0}: {1}])absentz!Error deleting ssh key [{0}: {1}])paramsr	   getstatus_codejsonok	fail_jsonformatr   r   KeyError
IndexError	exit_json
check_modeappendr   patchremove)moduler   r    account_apiresponser)   organization_jsonuser_idpresent_sshkeysepayloads              r   corer=   k   sO   --.KMM'"E6"K/H&&K ;;?FFy13 	4 /0GOa12CD /)U+ T*{+#O4$$[7%:$I;;T>@GG  (--1 	2 
+	o-U+T*{+#O4$$[7%:$I;;T>@GG  (--1 	2 
- j! a-_``as   H< <I)I$$I)c                      t               } | j                  t        t        dddg      t        d      t        t        dgfddg	      
             t	        | d      }t        |       y )Nr%   r&   )defaultchoicesT)requiredSCW_API_URLzhttps://account.scaleway.combase_url)fallbackr?   aliases)r    r   api_url)argument_specsupports_check_mode)r   updatedictr   r   r=   )rG   r5   s     r   mainrK      si    *,M9x.CD$'|m_=Gepzo{| 
 # F
 	Lr   __main__N)
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   r   Cansible_collections.community.general.plugins.module_utils.scalewayr   r	   r   r   r   r=   rK   __name__r   r   r   <module>rV      sa    A @$L$
 C pG72t zF r   