
    Vh!n                         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 d dl	mZ d dlmZ d dlZd dlZ G d	 d
e      Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona0  
module: btrfs_subvolume
short_description: Manage btrfs subvolumes
version_added: "6.6.0"

description: Creates, updates and deletes btrfs subvolumes and snapshots.

options:
  automount:
    description:
      - Allow the module to temporarily mount the targeted btrfs filesystem in order to validate the current state and make
        any required changes.
    type: bool
    default: false
  default:
    description:
      - Make the subvolume specified by O(name) the filesystem's default subvolume.
    type: bool
    default: false
  filesystem_device:
    description:
      - A block device contained within the btrfs filesystem to be targeted.
      - Useful when multiple btrfs filesystems are present to specify which filesystem should be targeted.
    type: path
  filesystem_label:
    description:
      - A descriptive label assigned to the btrfs filesystem to be targeted.
      - Useful when multiple btrfs filesystems are present to specify which filesystem should be targeted.
    type: str
  filesystem_uuid:
    description:
      - A unique identifier assigned to the btrfs filesystem to be targeted.
      - Useful when multiple btrfs filesystems are present to specify which filesystem should be targeted.
    type: str
  name:
    description:
      - Name of the subvolume/snapshot to be targeted.
    required: true
    type: str
  recursive:
    description:
      - When true, indicates that parent/child subvolumes should be created/removedas necessary to complete the operation
        (for O(state=present) and O(state=absent) respectively).
    type: bool
    default: false
  snapshot_source:
    description:
      - Identifies the source subvolume for the created snapshot.
      - Infers that the created subvolume is a snapshot.
    type: str
  snapshot_conflict:
    description:
      - Policy defining behavior when a subvolume already exists at the path of the requested snapshot.
      - V(skip) - Create a snapshot only if a subvolume does not yet exist at the target location, otherwise indicate that
        no change is required. Warning, this option does not yet verify that the target subvolume was generated from a snapshot
        of the requested source.
      - V(clobber) - If a subvolume already exists at the requested location, delete it first. This option is not idempotent
        and will result in a new snapshot being generated on every execution.
      - V(error) - If a subvolume already exists at the requested location, return an error. This option is not idempotent
        and will result in an error on replay of the module.
    type: str
    choices: [skip, clobber, error]
    default: skip
  state:
    description:
      - Indicates the current state of the targeted subvolume.
    type: str
    choices: [absent, present]
    default: present

notes:
  - If any or all of the options O(filesystem_device), O(filesystem_label) or O(filesystem_uuid) parameters are provided,
    there is expected to be a matching btrfs filesystem. If none are provided and only a single btrfs filesystem exists or
    only a single btrfs filesystem is mounted, that filesystem will be used; otherwise, the module will take no action and
    return an error.
extends_documentation_fragment:
  - community.general.attributes

attributes:
  check_mode:
    support: partial
    details:
      - In some scenarios it may erroneously report intermediate subvolumes being created. After mounting, if a directory
        like file is found where the subvolume would have been created, the operation is skipped.
  diff_mode:
    support: none

author:
  - Gregory Furlong (@gnfzdz)
a  
- name: Create a @home subvolume under the root subvolume
  community.general.btrfs_subvolume:
    name: /@home
    filesystem_device: /dev/vda2

- name: Remove the @home subvolume if it exists
  community.general.btrfs_subvolume:
    name: /@home
    state: absent
    filesystem_device: /dev/vda2

- name: Create a snapshot of the root subvolume named @
  community.general.btrfs_subvolume:
    name: /@
    snapshot_source: /
    filesystem_device: /dev/vda2

- name: Create a snapshot of the root subvolume and make it the new default subvolume
  community.general.btrfs_subvolume:
    name: /@
    snapshot_source: /
    default: true
    filesystem_device: /dev/vda2

- name: Create a snapshot of the /@ subvolume and recursively creating intermediate subvolumes as required
  community.general.btrfs_subvolume:
    name: /@snapshots/@2022_06_09
    snapshot_source: /@
    recursive: true
    filesystem_device: /dev/vda2

- name: Remove the /@ subvolume and recursively delete child subvolumes as required
  community.general.btrfs_subvolume:
    name: /@snapshots/@2022_06_09
    snapshot_source: /@
    recursive: true
    filesystem_device: /dev/vda2
a  
filesystem:
  description:
    - A summary of the final state of the targeted btrfs filesystem.
  type: dict
  returned: success
  contains:
    uuid:
      description: A unique identifier assigned to the filesystem.
      returned: success
      type: str
      sample: 96c9c605-1454-49b8-a63a-15e2584c208e
    label:
      description: An optional label assigned to the filesystem.
      returned: success
      type: str
      sample: Tank
    devices:
      description: A list of devices assigned to the filesystem.
      returned: success
      type: list
      sample:
        - /dev/sda1
        - /dev/sdb1
    default_subvolume:
      description: The ID of the filesystem's default subvolume.
      returned: success and if filesystem is mounted
      type: int
      sample: 5
    subvolumes:
      description: A list of dicts containing metadata for all of the filesystem's subvolumes.
      returned: success and if filesystem is mounted
      type: list
      elements: dict
      contains:
        id:
          description: An identifier assigned to the subvolume, unique within the containing filesystem.
          type: int
          sample: 256
        mountpoints:
          description: Paths where the subvolume is mounted on the targeted host.
          type: list
          sample: ['/home']
        parent:
          description: The identifier of this subvolume's parent.
          type: int
          sample: 5
        path:
          description: The full path of the subvolume relative to the btrfs fileystem's root.
          type: str
          sample: /@home

modifications:
  description:
    - A list where each element describes a change made to the target btrfs filesystem.
  type: list
  returned: Success
  elements: str

target_subvolume_id:
  description:
    - The ID of the subvolume specified with the O(name) parameter, either pre-existing or created as part of module execution.
  type: int
  sample: 257
  returned: Success and subvolume exists after module execution
)BtrfsFilesystemsProviderBtrfsCommandsBtrfsModuleException)normalize_subvolume_path)AnsibleModuleNc                      e Zd ZdZdZdZdZdZdZdZ	dZ
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd0dZd Zd Zd1dZd Zd Z d  Z!d! Z"d" Z#d# Z$d$ Z%d% Z&d& Z'd' Z(d( Z)d) Z*d* Z+d+ Z,d, Z-d- Z.d. Z/d/ Z0y)2BtrfsSubvolumeModule/      createsnapshotdeletezset-default?c                 <   || _         t        |      | _        t        |      | _        | j                   j
                  d   }|t        |      nd | _        | j                   j
                  d   | _        | j                   j
                  d   | _	        | j                   j
                  d   | _
        | j                   j
                  d   | _        | j                   j
                  d   | _        | j                   j
                  d   | _        | j                   j
                  d   | _        | j                   j
                  d	   | _        | j                   j
                  d
   }|t        |      nd | _        d | _        g | _        g | _        g | _        t+               | _        y )Nnamestate	automountdefaultfilesystem_devicefilesystem_labelfilesystem_uuid	recursivesnapshot_conflictsnapshot_source)moduler    _BtrfsSubvolumeModule__btrfs_apir   _BtrfsSubvolumeModule__providerparamsr	   _BtrfsSubvolumeModule__name_BtrfsSubvolumeModule__state _BtrfsSubvolumeModule__automount_BtrfsSubvolumeModule__default(_BtrfsSubvolumeModule__filesystem_device'_BtrfsSubvolumeModule__filesystem_label&_BtrfsSubvolumeModule__filesystem_uuid _BtrfsSubvolumeModule__recursive(_BtrfsSubvolumeModule__snapshot_conflict&_BtrfsSubvolumeModule__snapshot_source!_BtrfsSubvolumeModule__filesystem&_BtrfsSubvolumeModule__required_mounts#_BtrfsSubvolumeModule__unit_of_work%_BtrfsSubvolumeModule__completed_workdict'_BtrfsSubvolumeModule__temporary_mounts)selfr   r   r   s       u/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/btrfs_subvolume.py__init__zBtrfsSubvolumeModule.__init__   s]   (026: {{!!&)8<8H.t4d{{))'2;;--k:++I6#';;#5#56I#J "&++"4"45G"H!%!3!34E!F;;--k:#';;#5#56I#J ++,,->?N]Ni!9/!Jos !!#  ""&&    c                 ~   d }	 | j                          | j                          | j                  j                  sCt	        | j
                        dkD  rP| j                          | j                  j                          n%| j                  j                  | j
                         | j                          | j                  | j                  j                          	 || j                         fS # t        $ r}|}Y d }~Yd }~ww xY w# | j                          | j                  | j                  j                          w w xY wNr   )&_BtrfsSubvolumeModule__load_filesystem+_BtrfsSubvolumeModule__prepare_unit_of_workr   
check_modelenr/   +_BtrfsSubvolumeModule__execute_unit_of_workr-   refreshr0   extend	Exception%_BtrfsSubvolumeModule__cleanup_mountsrefresh_mountpointsget_results)r3   errores      r4   runzBtrfsSubvolumeModule.run   s   	8""$'');;))t**+a///1%%--/ %%,,T-@-@A !!#  ,!!557t'')**  	E	 !!#  ,!!557 -s*   BC+ +	D 4C;6D ;D  D 9D<c                    | j                         r| j                         }n| j                         }|j                         s~| j                  st        d|j                  z        | j                  j                  rt        d|j                  z        | j                  || j                         |j                          || _        y )NzTarget filesystem uuid=%s is not currently mounted and automount=False.Mount explicitly before module execution or pass automount=TruezzTarget filesystem uuid=%s is not currently mounted. Unable to validate the currentstate while running with check_mode=True)._BtrfsSubvolumeModule__has_filesystem_criteria._BtrfsSubvolumeModule__find_matching_filesytem._BtrfsSubvolumeModule__find_default_filesystem
is_mountedr%   r   uuidr   r;   4_BtrfsSubvolumeModule__mount_subvolume_id_to_tempdir._BtrfsSubvolumeModule__BTRFS_ROOT_SUBVOLUME_IDr>   r-   )r3   
filesystems     r4   __load_filesystemz&BtrfsSubvolumeModule.__load_filesystem  s    ))+779J779J $$&##*VXbXgXghi i ''*?AKQR R 44ZA_A_`""$&r6   c                 ^    | j                   d uxs | j                  d uxs | j                  d uS N)r)   r(   r'   r3   s    r4   __has_filesystem_criteriaz.BtrfsSubvolumeModule.__has_filesystem_criteria-  sG    %%T1  AT5L5LTX5X  A\`\t\t  }A  ]A  	Ar6   c                     | j                   | j                  | j                  d}| j                  j	                  |      S )N)rL   labeldevice)r)   r(   r'   r!   get_matching_filesystem)r3   criterias     r4   __find_matching_filesytemz.BtrfsSubvolumeModule.__find_matching_filesytem0  s;    **,,..

 66x@@r6   c                    | j                   j                         }d }t        |      dk(  r|d   }n3|D cg c]  }|j                         s| }}t        |      dk(  r|d   }||S t	        dt        |      z        c c}w )N   r   zvFailed to automatically identify targeted filesystem. No explicit device indicated and found %d available filesystems.)r!   get_filesystemsr<   rK   r   )r3   filesystemsrO   xmounted_filesystemss        r4   __find_default_filesystemz.BtrfsSubvolumeModule.__find_default_filesystem8  s    oo557
{q $QJ.9"LQ\\^1"L"L&'1,03
!&SUXYdUef  #Ms   A?A?c                     | j                   dk(  rK| j                  | j                          n| j                          | j                  r| j                          y y | j                   dk(  r| j                          y y )Npresentabsent)r$   r,   0_BtrfsSubvolumeModule__prepare_subvolume_present/_BtrfsSubvolumeModule__prepare_snapshot_presentr&   *_BtrfsSubvolumeModule__prepare_set_default/_BtrfsSubvolumeModule__prepare_subvolume_absentrS   s    r4   __prepare_unit_of_workz+BtrfsSubvolumeModule.__prepare_unit_of_workL  sg    <<9$%%-002//1~~**, \\X%++- &r6   c                     | j                   j                  | j                        }|7| j                  | j                         | j	                  | j                         y y rR   )r-   get_subvolume_by_namer#   6_BtrfsSubvolumeModule__prepare_before_create_subvolume-_BtrfsSubvolumeModule__stage_create_subvolumer3   	subvolumes     r4   __prepare_subvolume_presentz0BtrfsSubvolumeModule.__prepare_subvolume_presentX  sK    %%;;DKKH	224;;?))$++6 r6   c                     | j                   j                  |      }| j                  |       | j                  r| j	                  ||       y y rR   )r-   get_nearest_subvolume+_BtrfsSubvolumeModule__stage_required_mountr*   3_BtrfsSubvolumeModule__prepare_create_intermediates)r3   subvolume_nameclosest_parents      r4   !__prepare_before_create_subvolumez6BtrfsSubvolumeModule.__prepare_before_create_subvolume^  sD    **@@P##N3//O r6   c                    |j                  | j                        }|j                  t        j                  j
                        D cg c]  }t        |      dkD  s| }}t        |      dkD  rm|j                  }|d d D ]X  }|d   t        j                  j
                  k7  rt        j                  j
                  nd}||z   |z   }| j                  |d       Z y y c c}w )Nr   r\    T)get_child_relative_pathr#   splitospathsepr<   rm   )	r3   closest_subvolumeru   relative_pathr_   missing_subvolumescurrents	separators	            r4   __prepare_create_intermediatesz3BtrfsSubvolumeModule.__prepare_create_intermediatesd  s    )AA$++N)6)<)<RWW[[)IXASQRVVWZaXX!"Q&',,G', =+22;"''+++EBGGKK2	!I-1--gt<= ' Ys   CCc                 l   | j                   j                  | j                        }| j                   j                  | j                        }|d u}|r7| j                  dk(  ry | j                  dk(  rt        d| j                  z        |t        d| j                  z        |$|j                  |j                  k(  rt        d      | j                  |       |r!| j                  dk(  r| j                  |       n|s| j                  | j                         | j                  || j                         y )NskiprD   z@Target subvolume=%s already exists and snapshot_conflict='error'z"Source subvolume %s does not existz(Snapshot source and target are the same.clobber)r-   rk   r,   r#   r+   r   idrs   4_BtrfsSubvolumeModule__prepare_delete_subvolume_treerl   ,_BtrfsSubvolumeModule__stage_create_snapshot)r3   source_subvolumero   subvolume_existss       r4   __prepare_snapshot_presentz/BtrfsSubvolumeModule.__prepare_snapshot_presentn  s   ,,BB4CYCYZ%%;;DKKH	$D0''61))W4*+mptp{p{+{||#&'KdNdNd'dee"'7':':ill'J&'QRR''(89 8 8I E00;!224;;?$$%5t{{Cr6   c                 v    | j                   j                  | j                        }|| j                  |       y y rR   )r-   rk   r#   r   rn   s     r4   __prepare_subvolume_absentz/BtrfsSubvolumeModule.__prepare_subvolume_absent  s5    %%;;DKKH	 00; !r6   c                 $   |j                         rt        d      | j                  s4t        |j	                               dkD  rt        d|j
                  z        | j                  |j                                | j                  r| j                  |      n|g}|D ]q  }|j                         rt        d|j
                  z        |j                         r&| j                  | j                  | j                         | j                  |       s y )Nz.Can not delete the filesystem's root subvolumer   zSubvolume targeted for deletion %s has children and recursive=False.Either explicitly delete the child subvolumes first or pass parameter recursive=True.z#Can not delete mounted subvolume=%s)is_filesystem_rootr   r*   r<   get_child_subvolumesr~   rs   get_parent_subvolume5_BtrfsSubvolumeModule__prepare_recursive_delete_orderrK   is_filesystem_default2_BtrfsSubvolumeModule__stage_set_default_subvolume+_BtrfsSubvolumeModule__BTRFS_ROOT_SUBVOLUMErN   -_BtrfsSubvolumeModule__stage_delete_subvolume)r3   ro   queuer   s       r4   __prepare_delete_subvolume_treez4BtrfsSubvolumeModule.__prepare_delete_subvolume_tree  s    '')&'WXXC	(F(F(H$IA$M& (CEN^^(T U U 	##I$B$B$DEDHDTDT55i@[dZe 	-A||~*+PSTSYSY+YZZ&&(2243N3NPTPnPno))!,	-r6   c                     |g}g }t        |      dkD  rO|j                         }|j                  |       |j                  |j	                                t        |      dkD  rO|j                          |S )zsReturn the subvolume and all descendents as a list, ordered so that descendents always occur before their ancestorsr   )r<   popappendr?   r   reverse)r3   ro   pendingorderednexts        r4    __prepare_recursive_delete_orderz5BtrfsSubvolumeModule.__prepare_recursive_delete_order  se    +'lQ;;=DNN4 NN44467 'lQ 	r6   c                     | j                   j                  | j                        }||j                  nd }| j                   j                  |k7  r| j                  | j                  |       y y rR   )r-   rk   r#   r   default_subvolidr   )r3   ro   subvolume_ids      r4   __prepare_set_defaultz*BtrfsSubvolumeModule.__prepare_set_default  sY    %%;;DKKH	'0'<y||$--=..t{{LI >r6   c                     |j                         @| j                  r| j                  j                  |       y t	        d|j
                  z        y )NzXThe requested changes will require the subvolume '%s' to be mounted, but automount=False)get_mounted_pathr%   r.   r   r   r~   rn   s     r4   __stage_required_mountz+BtrfsSubvolumeModule.__stage_required_mount  sc    %%'/&&--i8*  ,F  IR  IW  IW  ,W  X  X	 0r6   c                 V    | j                   j                  | j                  ||d       y)z
        Add required creation of an intermediate subvolume to the unit of work
        If intermediate is true, the action will be skipped if a directory like file is found at target
        after mounting a parent subvolume
        )actiontargetintermediateN)r/   r   1_BtrfsSubvolumeModule__CREATE_SUBVOLUME_OPERATION)r3   subvolume_pathr   s      r4   __stage_create_subvolumez-BtrfsSubvolumeModule.__stage_create_subvolume  s,     	""77$($
 	r6   c                     | j                   j                  | j                  |j                  |j                  |d       y)zDAdd creation of a snapshot from source to target to the unit of work)r   source	source_idr   N)r/   r   0_BtrfsSubvolumeModule__CREATE_SNAPSHOT_OPERATIONr~   r   )r3   r   target_subvolume_paths      r4   __stage_create_snapshotz,BtrfsSubvolumeModule.__stage_create_snapshot  s9    ""66&++),,+	$
 	r6   c                 ~    | j                   j                  | j                  |j                  |j                  d       y)z8Add deletion of the target subvolume to the unit of workr   r   	target_idN)r/   r   1_BtrfsSubvolumeModule__DELETE_SUBVOLUME_OPERATIONr~   r   rn   s     r4   __stage_delete_subvolumez-BtrfsSubvolumeModule.__stage_delete_subvolume  s2    ""77nn"$
 	r6   Nc                 V    | j                   j                  | j                  ||d       y)zDAdd update of the filesystem's default subvolume to the unit of workr   N)r/   r   6_BtrfsSubvolumeModule__SET_DEFAULT_SUBVOLUME_OPERATION)r3   r   r   s      r4   __stage_set_default_subvolumez2BtrfsSubvolumeModule.__stage_set_default_subvolume  s*    ""<<$%$
 	r6   c                    | j                          | j                  D ]  }|d   | j                  k(  r| j                  |       '|d   | j                  k(  r| j                  |       K|d   | j                  k(  r| j                  |       o|d   | j                  k(  r| j                  |       t        d|d   z         y Nr   zUnknown operation type '%s'),_BtrfsSubvolumeModule__check_required_mountsr/   r   /_BtrfsSubvolumeModule__execute_create_subvolumer   ._BtrfsSubvolumeModule__execute_create_snapshotr   /_BtrfsSubvolumeModule__execute_delete_subvolumer   4_BtrfsSubvolumeModule__execute_set_default_subvolume
ValueErrorr3   ops     r4   __execute_unit_of_workz+BtrfsSubvolumeModule.__execute_unit_of_work  s    $$&%% 
	OB(|t@@@//3H!A!AA..r2H!B!BB//3H!G!GG44R8 !>H!MNN
	Or6   c                     | j                   j                  |d         }| j                  |      s7| j                  j	                  |       | j
                  j                  |       y y Nr   )r-   get_mountpath_as_child1_BtrfsSubvolumeModule__is_existing_directory_liker    subvolume_creater0   r   r3   	operationtarget_mounted_paths      r4   __execute_create_subvolumez/BtrfsSubvolumeModule.__execute_create_subvolume  s]    "//FFyQYGZ[001DE--.AB!!((3 Fr6   c                 
   | j                   j                  |d         }|j                         }| j                   j                  |d         }| j                  j                  ||       | j                  j                  |       y )Nr   r   )r-   rk   r   r   r    subvolume_snapshotr0   r   )r3   r   r   source_mounted_pathr   s        r4   __execute_create_snapshotz.BtrfsSubvolumeModule.__execute_create_snapshot  sv    ,,BB9XCVW.??A"//FFyQYGZ[++,?ATU$$Y/r6   c                     | j                   j                  |d         }| j                  j                  |       | j                  j                  |       y r   )r-   r   r    subvolume_deleter0   r   r   s      r4   __execute_delete_subvolumez/BtrfsSubvolumeModule.__execute_delete_subvolume  sH    "//FFyQYGZ[))*=>$$Y/r6   c                    |d   }|d   }|n| j                   j                  |      }|5| j                   j                          | j                   j                  |      }|t        d|z        |j                  }| j
                  j                  | j                   j                         |       | j                  j                  |       y )Nr   r   z&Failed to find existing subvolume '%s')
r-   rk   r>   r   r   r    subvolume_set_defaultget_any_mountpointr0   r   r3   r   r   r   target_subvolumes        r4   __execute_set_default_subvolumez4BtrfsSubvolumeModule.__execute_set_default_subvolume  s    8$k*	#00FFvN'!!))+#'#4#4#J#J6#R '*+SV\+\]],//	..t/@/@/S/S/UW`a$$Y/r6   c                     t         j                  j                  |      xrM t         j                  j                  |      xs, t        j                  |      j
                  | j                  k(  S rR   )r}   r~   existsisdirstatst_ino3_BtrfsSubvolumeModule__BTRFS_SUBVOLUME_INODE_NUMBER)r3   r~   s     r4   __is_existing_directory_likez1BtrfsSubvolumeModule.__is_existing_directory_like  sN    ww~~d# 
GGMM$ HGGDM  D$G$GG	
r6   c                     | j                  | j                        }t        |      dkD  rH|D ](  }| j                  | j                  |j
                         * | j                  j                          y y r8   )._BtrfsSubvolumeModule__filter_child_subvolumesr.   r<   rM   r-   r   rB   )r3   filteredro   s      r4   __check_required_mountsz,BtrfsSubvolumeModule.__check_required_mounts  sf    11$2H2HIx=1% V	44T5F5F	UV113 r6   c                     g }d}t        |d       }|D ]=  }||j                  dt        |       |k(  r!|j                  |       |j                  }? |S )zaFilter the provided list of subvolumes to remove any that are a child of another item in the listNc                     | j                   S rR   )r~   )r_   s    r4   <lambda>z@BtrfsSubvolumeModule.__filter_child_subvolumes.<locals>.<lambda>%  s
    166 r6   )keyr   )sortedr~   r<   r   )r3   
subvolumesr   lastr   r   s         r4   __filter_child_subvolumesz.BtrfsSubvolumeModule.__filter_child_subvolumes!  s_    )9: 	!D|499Qs4y#9T#A%yy	! r6   c                    | j                   j                  s| j                  s/t        d| j                  d| j                   j                        d|j                  |fz  }|| j
                  v r| j
                  |   S |j                  d   }t        j                  d      }|| j
                  |<   | j                   j                  dd	      }|d
d|z  ||g}| j                   j                  |d      }|S )Nz>Unable to temporarily mount required subvolumeswith automount=z and check_mode=z%s:%dr   z/tmp)dirmountTrequiredz-oznoatime,subvolid=%d)check_rc)r   r;   r%   r   rL   r2   devicestempfilemkdtempget_bin_pathrun_command)	r3   rO   subvolid	cache_keyrW   
mountpointr   commandresults	            r4   __mount_subvolume_id_to_tempdirz4BtrfsSubvolumeModule.__mount_subvolume_id_to_tempdir-  s    ;;!!)9)9&PTP`P`bfbmbmbxbx(z { { z99	///**955##A&%%&1
-7	*((4(@$ 5 @&*U((4(@r6   c                 ~    | j                   j                         D ]   }| j                  | j                   |          " y rR   )r2   keys$_BtrfsSubvolumeModule__cleanup_mount)r3   r   s     r4   __cleanup_mountsz%BtrfsSubvolumeModule.__cleanup_mountsB  s9    **//1 	?C  !8!8!=>	?r6   c                     | j                   j                  dd      }| j                   j                  ||g      }|d   dk(  r;| j                   j                  dd      }| j                   j                  ||g       y y )NumountTr   r   rmdir)r   r   r   )r3   r  r  r  r  s        r4   __cleanup_mountz$BtrfsSubvolumeModule.__cleanup_mountF  st    ))(T)B((&*)=>!9>KK,,Wt,DEKK##UJ$78 r6   c                    | j                   j                  | j                        }t        t	        | j
                        dkD  | j                   j                         | j                         ||j                        S d       S )Nr   )changedrO   modificationstarget_subvolume_id)	r-   rk   r#   r1   r<   r0   get_summary2_BtrfsSubvolumeModule__get_formatted_modificationsr   )r3   r   s     r4   rC   z BtrfsSubvolumeModule.get_resultsN  sw    ""88E--.2((446<<>.4.@	
 	
 GK	
 	
r6   c                 ^    | j                   D cg c]  }| j                  |       c}S c c}w rR   )r0   ._BtrfsSubvolumeModule__format_operation_resultr   s     r4   __get_formatted_modificationsz2BtrfsSubvolumeModule.__get_formatted_modificationsW  s'    =A=R=RSr..r2SSSs   *c                 .   |d   }|| j                   k(  r| j                  |      S || j                  k(  r| j                  |      S || j                  k(  r| j                  |      S || j                  k(  r| j                  |      S t        d|d   z        r   )	r   5_BtrfsSubvolumeModule__format_create_subvolume_resultr   4_BtrfsSubvolumeModule__format_create_snapshot_resultr   5_BtrfsSubvolumeModule__format_delete_subvolume_resultr   :_BtrfsSubvolumeModule__format_set_default_subvolume_resultr   )r3   r   action_types      r4   __format_operation_resultz.BtrfsSubvolumeModule.__format_operation_resultZ  s    )$;;;88CCD<<<77	BBD===88CCDBBB==iHH:Yx=PPQQr6   c                     |d   }| j                   j                  |      }||j                  n| j                  }d|d|dS )Nr   zCreated subvolume '' ()r-   rk   r   +_BtrfsSubvolumeModule__UNKNOWN_SUBVOLUME_ID)r3   r   r   r   r   s        r4    __format_create_subvolume_resultz5BtrfsSubvolumeModule.__format_create_subvolume_resultg  sG    8$,,BB6J+;+G$''TMhMh	06	BBr6   c           	          |d   }|d   }|d   }| j                   j                  |      }||j                  n| j                  }d|d|d|d|d	S )Nr   r   r   zCreated snapshot 'r  z) from 'r   r!  )r3   r   r   r   r   r   r   s          r4   __format_create_snapshot_resultz4BtrfsSubvolumeModule.__format_create_snapshot_resultm  sa    8$k*	8$,,BB6J+;+G$''TMhMh	>DiQWYbccr6   c                 (    |d   }|d   }d|d|dS )Nr   r   zDeleted subvolume 'r  r    )r3   r   r   r   s       r4    __format_delete_subvolume_resultz5BtrfsSubvolumeModule.__format_delete_subvolume_resultv  s!    8$k*	06	BBr6   c                     |d   }d|v r|d   }n5| j                   j                  |      }||j                  n| j                  }d|d|dS )Nr   r   zUpdated default subvolume to 'r  r   r!  r   s        r4   %__format_set_default_subvolume_resultz:BtrfsSubvolumeModule.__format_set_default_subvolume_result{  s[    8$)#!+.I#00FFvN/?/K(++QUQlQlI;A9MMr6   )FrR   )1__name__
__module____qualname__r   rN   r   r   r   r   r   r"  r5   rF   r9   rH   rI   rJ   r:   re   rl   rt   rf   rh   r   r   rg   rs   rm   r   r   r   r=   r   r   r   r   r   r   r   rM   rA   r  rC   r  r  r  r  r  r  r'  r6   r4   r   r      s      !%("#+ ",#+ (5% )6+0',AA(
.7P=D4<
-&	JX
O400
0&
4	*?9
TRCdC
Nr6   r   c                     t        t        ddd      t        ddd      t        dd      t        dd      t        dd      t        dd      t        dd      t        ddd	d	d
g      t        dd      t        dddg d      
      } t        | d      }t        |      }|j                         \  }}| |j                  t        |      fi | y  |j                  di | y )NboolF)typer   r   r~   )r0  r   strT)r0  r   rc   rd   )r0  r   r   choicesr   )r   r   rD   )
r   r   r   r   r   r   r   r   r   r   )argument_specsupports_check_moder'  )r1   r
   r   rF   	fail_jsonr1  	exit_json)module_argsr   ro   rD   r  s        r4   
run_moduler8    s    FUEB&5%@FU;559%%8ut,FE2y9V^J_`%%8EE6SopK ! F
 %V,IMMOME6U.v."6"r6   c                      t                y rR   )r8  r'  r6   r4   mainr:    s    Lr6   __main__)
__future__r   r   r   r0  __metaclass__DOCUMENTATIONEXAMPLESRETURN@ansible_collections.community.general.plugins.module_utils.btrfsr   r   r   r	   ansible.module_utils.basicr
   r}   r   objectr   r8  r:  r+  r'  r6   r4   <module>rD     st    C BYv&PA
F K  K e 4 	 kN6 kN\#6 zF r6   