
    Vh9                         d dl mZmZmZ eZdZdZdZd dl	m
Z
mZmZmZ d dlmZ d dlZdZd	d
gZddgZ G d de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: lxd_project
short_description: Manage LXD projects
version_added: 4.8.0
description:
  - Management of LXD projects.
author: "Raymond Chang (@we10710aa)"
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  name:
    description:
      - Name of the project.
    required: true
    type: str
  description:
    description:
      - Description of the project.
    type: str
  config:
    description:
      - 'The config for the project (for example V({"features.profiles": "true"})).'
      - See U(https://documentation.ubuntu.com/lxd/en/latest/api/#/projects/project_get).
      - If the project already exists and its "config" value in metadata obtained from C(GET /1.0/projects/<name>)
        U(https://documentation.ubuntu.com/lxd/en/latest/api/#/projects/project_get)
        are different, then this module tries to apply the configurations U(https://documentation.ubuntu.com/lxd/en/latest/api/#/projects/project_put).
    type: dict
  new_name:
    description:
      - A new name of a project.
      - If this parameter is specified a project will be renamed to this name.
      - See U(https://documentation.ubuntu.com/lxd/en/latest/api/#/projects/project_post).
    required: false
    type: str
  merge_project:
    description:
      - Merge the configuration of the present project with the new desired configuration, instead of replacing it. If configuration
        is the same after merged, no change will be made.
    required: false
    default: false
    type: bool
  state:
    choices:
      - present
      - absent
    description:
      - Define the state of a project.
    required: false
    default: present
    type: str
  url:
    description:
      - The Unix domain socket path or the https URL for the LXD server.
    required: false
    default: unix:/var/lib/lxd/unix.socket
    type: str
  snap_url:
    description:
      - The Unix domain socket path when LXD is installed by snap package manager.
    required: false
    default: unix:/var/snap/lxd/common/lxd/unix.socket
    type: str
  client_key:
    description:
      - The client certificate key file path.
      - If not specified, it defaults to C($HOME/.config/lxc/client.key).
    required: false
    aliases: [key_file]
    type: path
  client_cert:
    description:
      - The client certificate file path.
      - If not specified, it defaults to C($HOME/.config/lxc/client.crt).
    required: false
    aliases: [cert_file]
    type: path
  trust_password:
    description:
      - The client trusted password.
      - 'You need to set this password on the LXD server before running this module using the following command: C(lxc config
        set core.trust_password <some random password>) See U(https://www.stgraber.org/2016/04/18/lxd-api-direct-interaction/).'
      - If O(trust_password) is set, this module send a request for authentication before sending any requests.
    required: false
    type: str
notes:
  - Projects must have a unique name. If you attempt to create a project with a name that already existed in the users namespace
    the module will simply return as "unchanged".
al  
# An example for creating a project
- hosts: localhost
  connection: local
  tasks:
    - name: Create a project
      community.general.lxd_project:
        name: ansible-test-project
        state: present
        config: {}
        description: my new project

# An example for renaming a project
- hosts: localhost
  connection: local
  tasks:
    - name: Rename ansible-test-project to ansible-test-project-new-name
      community.general.lxd_project:
        name: ansible-test-project
        new_name: ansible-test-project-new-name
        state: present
        config: {}
        description: my new project
aR  
old_state:
  description: The old state of the project.
  returned: success
  type: str
  sample: "absent"
logs:
  description: The logs of requests and responses.
  returned: when ansible-playbook is invoked with -vvvv.
  type: list
  elements: dict
  contains:
    type:
      description: Type of actions performed, currently only V(sent request).
      type: str
      sample: "sent request"
    request:
      description: HTTP request sent to LXD server.
      type: dict
      contains:
        method:
          description: Method of HTTP request.
          type: str
          sample: "GET"
        url:
          description: URL path of HTTP request.
          type: str
          sample: "/1.0/projects/test-project"
        json:
          description: JSON body of HTTP request.
          type: str
          sample: "(too long to be placed here)"
        timeout:
          description: Timeout of HTTP request, V(null) if unset.
          type: int
          sample: null
    response:
      description: HTTP response received from LXD server.
      type: dict
      contains:
        json:
          description: JSON of HTTP response.
          type: str
          sample: "(too long to be placed here)"
actions:
  description: List of actions performed for the project.
  returned: success
  type: list
  elements: str
  sample: ["create"]
)	LXDClientLXDClientExceptiondefault_key_filedefault_cert_file)AnsibleModuleNzunix:/var/lib/lxd/unix.socketpresentabsentconfigdescriptionc                   d    e Zd Zd Zd Zd Zed        Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zy)LXDProjectManagementc                    || _         | j                   j                  d   | _        | j                          | j                   j                  d   | _        | j                   j                  j                  dd      | _        | j                   j                  j                  d      | _        | j                  t               | _        | j                   j                  j                  d      | _	        | j                  t               | _	        | j                   j                  dk\  | _        	 | j                   j                  d   t        k7  r| j                   j                  d   | _        nt        j                   j#                  | j                   j                  d	   j%                  d
d            r| j                   j                  d	   | _        n| j                   j                  d   | _        	 t-        | j                  | j                  | j                  | j                        | _        | j                   j                  j                  dd      | _        g | _        y# t&        $ r0}| j                   j)                  |j*                         Y d}~d}~ww xY w# t0        $ r0}| j                   j)                  |j*                         Y d}~d}~ww xY w)zManagement of LXC projects via Ansible.

        :param module: Processed Ansible Module.
        :type module: ``object``
        namestatenew_nameN
client_keyclient_cert   urlsnap_urlzunix: )msg)key_file	cert_filedebugtrust_password)moduleparamsr   _build_configr   getr   r   r   r   r	   
_verbosityr   ANSIBLE_LXD_DEFAULT_URLr   ospathexistsreplace	Exception	fail_jsonr   r   clientr   r   actions)selfr    es      q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/lxd_project.py__init__zLXDProjectManagement.__init__   s    KK&&v.	[[''0
**..z4@**..|<== ,.DM++//>>>!.0DN[[++q0
	-{{!!%(,CC;;--e4 2 2: > F FwPR ST;;--j9;;--e4	-#4==DNNjjDK #kk00445EtL  	-KK!!aee!,,	- " 	-KK!!aee!,,	-s1   (CI +<J 	J#&JJ	K&K

Kc                     i | _         t        D ]:  }| j                  j                  j	                  |d       }|,|| j                   |<   < y )N)r   CONFIG_PARAMSr    r!   r#   )r.   attr	param_vals      r0   r"   z"LXDProjectManagement._build_config   sH    ! 	.D**..tT:I$$-D!	.    c                 r    | j                   j                  ddj                  | j                        dg      S )NGET/1.0/projects/{0}i  )ok_error_codes)r,   doformatr   r.   s    r0   _get_project_jsonz&LXDProjectManagement._get_project_json   s6    {{~~&--dii85  
 	
r6   c                     | d   dk(  ryy)Ntypeerrorr   r    )	resp_jsons    r0   _project_json_to_module_statez2LXDProjectManagement._project_json_to_module_state   s    V'r6   c                    | j                   dk(  r| j                  dk(  r;| j                  | j                          y | j                  j                  dd       y | j                  )| j                  | j                  k7  r| j                          | j                         r| j                          y y | j                   dk(  rK| j                  dk(  r;| j                  | j                          y | j                  j                  dd       y y y )Nr   r   zQnew_name must not be set when the project does not exist and the state is presentF)r   changedzRnew_name must not be set when the project exists and the specified state is absent)r   	old_stater   _create_projectr    r+   r   _rename_project_needs_to_apply_project_configs_apply_project_configs_delete_projectr=   s    r0   _update_projectz$LXDProjectManagement._update_project  s    ::"~~)==(((*KK))o % * ' ==,$))1K((*779//1 :ZZ8#~~*==(((*KK))p % * '	 + $r6   c                     | j                   j                         }| j                  |d<   | j                  j	                  dd|       | j
                  j                  d       y )Nr   POSTz/1.0/projectscreate)r   copyr   r,   r;   r-   appendr.   r   s     r0   rH   z$LXDProjectManagement._create_project  sG    !!#vv7H%r6   c                     d| j                   i}| j                  j                  ddj                  | j                        |       | j
                  j                  d       | j                   | _        y )Nr   rO   r9   rename)r   r,   r;   r<   r   r-   rR   rS   s     r0   rI   z$LXDProjectManagement._rename_project"  sQ    $--(v299$))DfMH%MM	r6   c                     || j                   vry| j                  d   j                  |d       }| j                   |   |k7  S )NFmetadata)r   old_project_jsonr#   )r.   keyold_configss      r0   _needs_to_change_project_configz4LXDProjectManagement._needs_to_change_project_config(  sB    dkk!++J7;;CF{{3;..r6   c                 J    | j                  d      xs | j                  d      S )Nr   r   )r[   r=   s    r0   rJ   z4LXDProjectManagement._needs_to_apply_project_configs.  s)    00: @00?	
r6   c                     |j                         }|j                         D ]?  \  }}t        |t              r%|j	                  |i       }| j                  ||       ;|||<   A |S )aH   Return a new dict that merge two dict,
        with values in source dict overwrite destination dict

        Args:
            dict(source): source dict
            dict(destination): destination dict
        Kwargs:
            None
        Raises:
            None
        Returns:
            dict(destination): merged dict)rQ   items
isinstancedict
setdefault_merge_dicts)r.   sourcedestinationresultrY   valuenodes          r0   rb   z!LXDProjectManagement._merge_dicts4  sg     !!# ,,. 	$JC%&((b1!!%.#s	$ r6   c                    t               }| j                  d   j                         }t        D ]
  }||   ||<    | j                  j
                  d   r"| j                  | j                  |      }||k(  ry| j                  j                         }| j                  j                  ddj                  | j                        |       | j                  j                  d       y)a   Selection of the procedure: rebuild or merge

        The standard behavior is that all information not contained
        in the play is discarded.

        If "merge_project" is provides in the play and "True", then existing
        configurations from the project and new ones defined are merged.

        Args:
            None
        Kwargs:
            None
        Raises:
            None
        Returns:
            NonerW   merge_projectNPUTr9   apply_projects_configs)r`   rX   rQ   r3   r    r!   rb   r   r,   r;   r<   r   r-   rR   )r.   
old_configold_metadatar4   r   s        r0   rK   z+LXDProjectManagement._apply_project_configsK  s    " V
,,Z8==?! 	2D+D1Jt	2 ;;o.&&t{{J?F# [[%%'Fu188CVL45r6   c                     | j                   j                  ddj                  | j                               | j                  j                  d       y )NDELETEr9   delete)r,   r;   r<   r   r-   rR   r=   s    r0   rL   z$LXDProjectManagement._delete_projectm  s6    x!4!;!;DII!FGH%r6   c                    	 | j                   %| j                  j                  | j                          | j                         | _        | j                  | j                        | _        | j                          t        | j                        dkD  }|| j                  | j                  d}| j                  j                  r| j                  j                  |d<    | j                  j                  di | y# t        $ r}t        | j                        dkD  }|j                  || j                  d}| j                  j                  r|j                   d   |d<    | j                  j"                  di | Y d}~yd}~ww xY w)zRun the main method.Nr   )rF   rG   r-   logs)r   rF   r-   rB   )r   r,   authenticater>   rX   rD   rG   rM   lenr-   r   rr   r    	exit_jsonr   r   kwargsr+   )r.   state_changedresult_jsonr/   fail_paramss        r0   runzLXDProjectManagement.runq  s<   	1"".(()<)<=$($:$:$<D!!??%%'DN  "-1M(!^^<<K
 {{  &*kk&6&6F#!DKK!!0K0! 		1-1Muu(<<K
 {{  &'hhv&6F#!DKK!!0K0		1s   C3C6 6	E??A6E::E?N)__name__
__module____qualname__r1   r"   r>   staticmethodrD   rM   rH   rI   r[   rJ   rb   rK   rL   rz   rB   r6   r0   r   r      sT    &P.
  
'.&"/
. 6D&1r6   r   c                  ~   t        t        t        dd      t        d      t        d      t        d      t        dd      t        t        d	
      t        dt              t        dd      t        ddg      t        ddg      t        dd            d      } t	        |       }|j                          y)zAnsible Main module.strT)r@   required)r@   r`   boolF)r@   defaultr   )choicesr   z)unix:/var/snap/lxd/common/lxd/unix.socketr'   r   )r@   aliasesr   )r@   no_log)r   r   r   r   ri   r   r   r   r   r   r   )argument_specsupports_check_mode)r    N)r
   r`   PROJECTS_STATESr%   r   rz   )r    
lxd_manages     r0   mainr     s          '! / C # $  U48M'
P "S*FX &V4JNNr6   __main__)
__future__r   r   r   r@   __metaclass__DOCUMENTATIONEXAMPLESRETURN>ansible_collections.community.general.plugins.module_utils.lxdr   r   r   r	   ansible.module_utils.basicr
   r&   r%   r   r3   objectr   r   r{   rB   r6   r0   <module>r      s    A @\|22
h  5 	 :  x m
F16 F1R0f zF r6   