
    VhU/                         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ed
z   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_function
short_description: Scaleway Function management
version_added: 6.0.0
author: Guillaume MARTINEZ (@Lunik)
description:
  - This module manages function 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 function.
    default: present
    choices:
      - present
      - absent

  namespace_id:
    type: str
    description:
      - Function 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 function.
    required: true

  description:
    description:
      - Description of the function.
    type: str
    default: ''

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

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

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

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

  runtime:
    description:
      - Runtime of the function.
      - See U(https://www.scaleway.com/en/docs/compute/functions/reference-content/functions-lifecycle/) for all available
        runtimes.
    type: str
    required: true

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

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

  handler:
    description:
      - The C(module-name.export) value in your function.
    type: str

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

  redeploy:
    description:
      - Redeploy the function if update is required.
    type: bool
    default: false
a*  
- name: Create a function
  community.general.scaleway_function:
    namespace_id: '{{ scw_function_namespace }}'
    region: fr-par
    state: present
    name: my-awesome-function
    runtime: python3
    environment_variables:
      MY_VAR: my_value
    secret_environment_variables:
      MY_SECRET_VAR: my_secret_value
  register: function_creation_task

- name: Make sure function is deleted
  community.general.scaleway_function:
    namespace_id: '{{ scw_function_namespace }}'
    region: fr-par
    state: absent
    name: my-awesome-function
a|  
function:
  description: The function information.
  returned: when O(state=present)
  type: dict
  sample:
    cpu_limit: 140
    description: Function used for testing scaleway_function ansible module
    domain_name: fnansibletestfxamabuc-fn-ansible-test.functions.fnc.fr-par.scw.cloud
    environment_variables:
      MY_VAR: my_value
    error_message: null
    handler: handler.handle
    http_option: ""
    id: ceb64dc4-4464-4196-8e20-ecef705475d3
    max_scale: 5
    memory_limit: 256
    min_scale: 0
    name: fn-ansible-test
    namespace_id: 82737d8d-0ebb-4d89-b0ad-625876eca50d
    privacy: public
    region: fr-par
    runtime: python310
    runtime_message: ""
    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runtimememory_limittimeouthandlerprivacysecret_environment_variables)redeployc                     | d   | d   | d   | d   | d   | d   | d   | d   | d	   | d
   | d   | d   t        j                  | d         d}|S )Nnamespace_idnamer   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   )r   dict_to_list)	wished_fnpayloads     w/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/scaleway_function.pypayload_from_wished_fnr#      s    !.1&! /{+{+Y'!.1Y'Y'Y'j)!*+B!C(7(D(DYOmEn(o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	functionsr   TstatuszFunction would be destroyed)resourcestable_states
force_wait/%sidpathz"Error deleting function [{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fn_listfn	fn_lookup	target_fnresponses           r"   absent_strategyrE      s   G%%k2G*12BFR2I2	){)F+,IG
zz#@AAA))9Mfj)kzzs||eio.EEzFH;;

!E!L!L  (--"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 function would be created.r   )r.   dataz Error during function creation: r0   z: 'messagez' ()r/   r1   r+   r,   r-   r   )targetwishedverifiable_mutable_attributesmutable_attributesz%Function attributes would be changed.z3Error during function attributes update: [{0}: {1}])r2   r#   r3   r4   warnpostr8   r9   infor=   r:   r5   r6   getr   r   decoder   VERIFIABLE_MUTABLE_ATTRIBUTESMUTABLE_ATTRIBUTESpatchr;   r<   )r>   r    r?   r@   rA   rB   
payload_fncreation_responser0   rD   rC   decoded_target_fnpatch_payloadfn_patch_responses                 r"   present_strategyr[     s   G%%k2G*12BFR2I2'	2J	)::  X'EFFF z" 	HH#,,*4 % 6 !##EVE[E[\aEbEVE[E[\eEfEVE[E[]C JJ  S )--7H7M7M]j-k77u7H7M7Md7S/S S7T%%)F+,I +8G8N8NO`a  PAOYZxOy9{45 :ARAKXuM_aM
 	!!G
zz#JKKK		s||eio6M'M'4 " 6 

!V!]!]))+<+A+A)+L"N 	O ))9M)ZwwCLL59T?+BBwCHHMM!!k 3s   I+)presentr   c                 X   t        j                  |        | j                  d   }| j                  d   | j                  d   | j                  d   | j                  d   | j                  d   | j                  d   | j                  d   | j                  d	   | j                  d
   | j                  d   | j                  d   | j                  d   | j                  d   | j                  d   d}t        |       }d|z  |_        t        |d      ||      \  }}| j                  ||       y )Nregionstater   r   r   r   r   r   r   function_timeoutr   r   r   r   r   )r_   r   r   r   r   r   r   r   r   r   r   r   r   r   )r3   z&functions/v1beta1/regions/%s/functions)r>   r    )r?   function)r   ensure_scaleway_secret_packageparamsr	   r8   state_strategy	exit_json)r3   r^   wished_functionr>   r?   summarys         r"   corerh   O  s   226:]]8$Fw'n5f%}}]3]];/]];/==+n5==!34==+==+MM*-!'/F!G(.6T(UO" &
!C;fDCL%og&>?CSbcGW
Ww7r$   c                  $   t               } | j                  t                      | j                  t        t        ddddg      t        dd      t        ddt              t        dd      t        dd	      t        d
      t        d
      t        dd      t        d
      t        d      t        d      t        ddddg      t        dd	      t        di 	      t        di d                   t        | d      }t        |       y )Nstrr\   r   )typedefaultchoicesT)rk   required)rk   rn   rm    )rk   rl   int)rk   publicprivateboolFdict)rk   rl   no_log)r_   r   r^   r   r   r   r   r   r   r`   r   r   r   r   r   )argument_specsupports_check_mode)r   updater
   rt   r   r   rh   )rv   r3   s     r"   mainry   l  s    *,MACDy8Y:OPut47GHut,eR0E"E"%$/u%5)% %Hi;PQ651";%)vr$%O " # F
 	Lr$   __main__N)
__future__r   r   r   rk   __metaclass__DOCUMENTATIONEXAMPLESRETURNcopyr   Cansible_collections.community.general.plugins.module_utils.scalewayr   r   r	   r
   r   r   ansible.module_utils.basicr   r6   rS   rT   r#   rE   r[   rd   rh   ry   __name__ r$   r"   <module>r      s    A @{z,
>  
 5!  3 6  
("09"z  8:8 zF r$   