
    Vh1                         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mZmZmZmZ d dlmZ dZd	Zd
 Zd Zd ZeedZd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: scaleway_container
short_description: Scaleway Container management
version_added: 6.0.0
author: Guillaume MARTINEZ (@Lunik)
description:
  - This module manages container on Scaleway account.
extends_documentation_fragment:
  - community.general.scaleway
  - community.general.scaleway_waitable_resource
  - community.general.attributes
requirements:
  - passlib[argon2] >= 1.7.4

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

options:
  state:
    type: str
    description:
      - Indicate desired state of the container.
    default: present
    choices:
      - present
      - absent

  namespace_id:
    type: str
    description:
      - Container namespace identifier.
    required: true

  region:
    type: str
    description:
      - Scaleway region to use (for example V(fr-par)).
    required: true
    choices:
      - fr-par
      - nl-ams
      - pl-waw

  name:
    type: str
    description:
      - Name of the container namespace.
    required: true

  description:
    description:
      - Description of the container namespace.
    type: str
    default: ''

  min_scale:
    description:
      - Minimum number of replicas for the container.
    type: int

  max_scale:
    description:
      - Maximum number of replicas for the container.
    type: int

  environment_variables:
    description:
      - Environment variables of the container namespace.
      - Injected in container at runtime.
    type: dict
    default: {}

  secret_environment_variables:
    description:
      - Secret environment variables of the container namespace.
      - Updating those values will not output a C(changed) state in Ansible.
      - Injected in container at runtime.
    type: dict
    default: {}

  memory_limit:
    description:
      - Resources define performance characteristics of your container.
      - They are allocated to your container at runtime.
    type: int

  container_timeout:
    description:
      - The length of time your handler can spend processing a request before being stopped.
    type: str

  privacy:
    description:
      - Privacy policies define whether a container can be executed anonymously.
      - Choose V(public) to enable anonymous execution, or V(private) to protect your container with an authentication mechanism
        provided by the Scaleway API.
    type: str
    default: public
    choices:
      - public
      - private

  registry_image:
    description:
      - The name of image used for the container.
    type: str
    required: true

  max_concurrency:
    description:
      - Maximum number of connections per container.
      - This parameter will be used to trigger autoscaling.
    type: int

  protocol:
    description:
      - Communication protocol of the container.
    type: str
    default: http1
    choices:
      - http1
      - h2c

  port:
    description:
      - Listen port used to expose the container.
    type: int

  redeploy:
    description:
      - Redeploy the container if update is required.
    type: bool
    default: false
ag  
- name: Create a container
  community.general.scaleway_container:
    namespace_id: '{{ scw_container_namespace }}'
    state: present
    region: fr-par
    name: my-awesome-container
    registry_image: rg.fr-par.scw.cloud/funcscwtestrgy2f9zw/nginx:latest
    environment_variables:
      MY_VAR: my_value
    secret_environment_variables:
      MY_SECRET_VAR: my_secret_value
  register: container_creation_task

- name: Make sure container is deleted
  community.general.scaleway_container:
    namespace_id: '{{ scw_container_namespace }}'
    state: absent
    region: fr-par
    name: my-awesome-container
a  
container:
  description: The container information.
  returned: when O(state=present)
  type: dict
  sample:
    cpu_limit: 140
    description: Container used for testing scaleway_container ansible module
    domain_name: cnansibletestgfogtjod-cn-ansible-test.functions.fnc.fr-par.scw.cloud
    environment_variables:
      MY_VAR: my_value
    error_message: null
    http_option: ""
    id: c9070eb0-d7a4-48dd-9af3-4fb139890721
    max_concurrency: 50
    max_scale: 5
    memory_limit: 256
    min_scale: 0
    name: cn-ansible-test
    namespace_id: 75e299f1-d1e5-4e6b-bc6e-4fb51cfe1e69
    port: 80
    privacy: public
    protocol: http1
    region: fr-par
    registry_image: rg.fr-par.scw.cloud/namespace-ansible-ci/nginx:latest
    secret_environment_variables:
      - key: MY_SECRET_VAR
        value: $argon2id$v=19$m=65536,t=1,p=2$tb6UwSPWx/rH5Vyxt9Ujfw$5ZlvaIjWwNDPxD9Rdght3NarJz4IETKjpvAU3mMSmFg
    status: created
    timeout: 300s
)deepcopy)SCALEWAY_REGIONSscaleway_argument_specScaleway(scaleway_waitable_resource_argument_spec%resource_attributes_should_be_changedSecretVariables)AnsibleModule)readycreatedabsent)description	min_scale	max_scaleenvironment_variablesmemory_limittimeoutprivacyregistry_imagemax_concurrencyprotocolportsecret_environment_variablesc                     | d   | d   | d   | d   | d   | d   t        j                  | d         | d   | d	   | d
   | d   | d   | d   | d   | d   d}|S )Nnamespace_idnamer   r   r   r   r   r   r   r   r   r   r   r   redeploy)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    )r   dict_to_list)	wished_cnpayloads     x/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/scaleway_container.pypayload_from_wished_cnr%      s    !.1&! /{+{+!*+B!C(7(D(DYOmEn(o!.1Y'Y'#$45$%67j)&!j)G$ N    c                    d}| j                  d      }|D ci c]  }|d   |
 }}|d   |vr|i fS ||d      }d}| j                  j                  r|ddifS | j                  |t        d       | j                  | j                  d|d	   z  z   
      }|j                  s@| j                  j                  dj                  |j                  |j                               | j                  |t               ||j                  fS c c}w )NF
containersr   TstatuszContainer would be destroyed)resourcestable_states
force_wait/%sidpathz#Error deleting container [{0}: {1}]msgr*   r+   )fetch_all_resourcesmodule
check_mode!wait_to_complete_state_transitionSTABLE_STATESdeleteapi_pathok	fail_jsonformatstatus_codejson)apir"   changedcn_listcn	cn_lookup	target_cnresponses           r$   absent_strategyrG     s   G%%l3G*12BFR2I2	){)F+,IG
zz#ABBB))9Mfj)kzzs||eio.EEzFH;;

!F!M!M  (--"1 	2 ))9M)ZHMM!!# 3s   Dc                    d}| j                  d      }|D ci c]  }|d   |
 }}t        |      }|d   |vrd}| j                  j                  r|ddifS |d= | j	                  |       | j                  | j                  |      }|j                  sLd	|j                  d
   d|j                  d   d|j                  d}| j                  j                  |       | j                  |j                  t               | j                  | j                  d|j                  d   z  z         }	||	j                  fS ||d      }
t        |
      }t        j                   |d   |d         |d<   t#        ||t$        t$              }|s||
fS d}| j                  j                  r|ddifS | j'                  | j                  d|
d   z  z   |      }|j                  sC| j                  j                  dj)                  |j*                  |j                  d                | j                  |
t               | j                  | j                  d|
d   z  z         }	||	j                  fS c c}w )NFr(   r   Tr)   zA container would be created.r    )r0   dataz!Error during container creation: r2   z: 'messagez' ()r1   r3   r-   r.   r/   r   )targetwishedverifiable_mutable_attributesmutable_attributesz&Container attributes would be changed.z4Error during container attributes update: [{0}: {1}])r4   r%   r5   r6   warnpostr:   r;   infor?   r<   r7   r8   getr   r   decoder   MUTABLE_ATTRIBUTESpatchr=   r>   )r@   r"   rA   rB   rC   rD   
payload_cncreation_responser2   rF   rE   decoded_target_cnpatch_payloadcn_patch_responses                 r$   present_strategyr\     s   G%%l3G*12BFR2I2'	2J	)::  X'FGGG z" 	HH#,,*4 % 6 !##FWF\F\]bFcFWF\F\]fFgFWF\F\^C JJ  S )--7H7M7M]j-k77u7H7M7Md7S/S S7T%%)F+,I +8G8N8NO`a  PAOYZxOy9{459ARAKXjM_aM
 	!!G
zz#KLLL		s||eio6M'M'4 " 6 

!W!^!^))+<+A+A)+L"N 	O ))9M)ZwwCLL59T?+BBwCHHMM!!i 3s   I+)presentr   c                    t        j                  |        | j                  d   }i d| j                  d   d| j                  d   d| j                  d   d| j                  d   d| j                  d   d| j                  d   d| j                  d   d	| j                  d	   d
| j                  d
   d| j                  d   d| j                  d   d| j                  d   d| j                  d   d| j                  d   d| j                  d   d| j                  d   }t        |       }d|z  |_        t        |d      ||      \  }}| j                  ||       y )Nregionstater   r   r   r   r   r   r   r   r   container_timeoutr   r   r   r   r   r    )r5   z(containers/v1beta1/regions/%s/containers)r@   r"   )rA   	container)r   ensure_scaleway_secret_packageparamsr	   r:   state_strategy	exit_json)r5   r_   wished_containerr@   rA   summarys         r$   coreri   \  s   226:]]8$Fw'n5 	f% 	v}}]3	
 	V]];/ 	V]];/ 	 /F!G 	'6T(U 	n5 	6==!45 	6==+ 	&--(89 	6==):; 	FMM*- 	f%  	FMM*-!& &
!C=FCL%&6w&?@STdeGW
W8r&   c                     t               } | j                  t                      | j                  t        d&i dt        ddddg      dt        dd      d	t        ddt        
      dt        dd      dt        dd      dt        d      dt        d      dt        d      dt        d      dt        ddddg      dt        dd      dt        d      dt        ddddg      dt        d      dt        dd       d!t        d"i       d#t        d"i d$             t        | d%      }t        |       y )'Nr`   strr]   r   )typedefaultchoicesr   T)rl   requiredr_   )rl   ro   rn   r   r    )rl   rm   r   int)rl   r   r   ra   r   publicprivater   r   r   http1h2cr   r    boolFr   dictr   )rl   rm   no_log)argument_specsupports_check_mode )r   updater
   rw   r   r   ri   )ry   r5   s     r$   mainr}   {  sw   *,MACD y8Y:OPut4 7GH ut,	
 eR0 E" E" u% E* %Hi;PQ 6 %( 5'GU;KL u 651  #;!" &*vr$%O# & # F
 	Lr&   __main__N)
__future__r   r   r   rl   __metaclass__DOCUMENTATIONEXAMPLESRETURNcopyr   Cansible_collections.community.general.plugins.module_utils.scalewayr   r   r	   r
   r   r   ansible.module_utils.basicr   r8   rU   r%   rG   r\   re   ri   r}   __name__r{   r&   r$   <module>r      s    A @HT,
@  
 5  ,"08"x  9>< zF r&   