
    Vh ,                     r    d dl mZmZmZ eZdZdZdZd dl	m
Z
 d Zd Zd Zd	 Zd
 Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
author:
  - Kairo Araujo (@kairoaraujo)
module: aix_lvg
short_description: Manage LVM volume groups on AIX
description:
  - This module creates, removes or resize volume groups on AIX LVM.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  force:
    description:
      - Force volume group creation.
    type: bool
    default: false
  pp_size:
    description:
      - The size of the physical partition in megabytes.
    type: int
  pvs:
    description:
      - List of comma-separated devices to use as physical devices in this volume group.
      - Required when creating or extending (V(present) state) the volume group.
      - If not informed reducing (V(absent) state) the volume group will be removed.
    type: list
    elements: str
  state:
    description:
      - Control if the volume group exists and volume group AIX state varyonvg V(varyon) or varyoffvg V(varyoff).
    type: str
    choices: [absent, present, varyoff, varyon]
    default: present
  vg:
    description:
      - The name of the volume group.
    type: str
    required: true
  vg_type:
    description:
      - The type of the volume group.
    type: str
    choices: [big, normal, scalable]
    default: normal
notes:
  - AIX will permit remove VG only if all LV/Filesystems are not busy.
  - Module does not modify PP size for already present volume group.
a  
- name: Create a volume group datavg
  community.general.aix_lvg:
    vg: datavg
    pp_size: 128
    vg_type: scalable
    state: present

- name: Removing a volume group datavg
  community.general.aix_lvg:
    vg: datavg
    state: absent

- name: Extending rootvg
  community.general.aix_lvg:
    vg: rootvg
    pvs: hdisk1
    state: present

- name: Reducing rootvg
  community.general.aix_lvg:
    vg: rootvg
    pvs: hdisk1
    state: absent
z # )AnsibleModulec                    | j                  dd      }| j                  |g      \  }}}|dk7  r| j                  d|||       |D ]  }i }|j                         D ]  }	|	j	                         }
|
d   ||
d   <    ||j                         vr| j                  d|z         ||   d	k(  rl| j                  d
d      }| j                  |dd|z  ddg      \  }}}|dk7  r| j                  d|||       d|v r| j                  d|z         d|z  }d|fc S |||   k7  r| j                  d|d||   d       d|d||   d}d|fc S  y)a2  
    Function to validate if the physical volume (PV) is not already in use by
    another volume group or Oracle ASM.

    :param module: Ansible module argument spec.
    :param vg: Volume group name.
    :param pvs: Physical volume list.
    :return: [bool, message] or module.fail_json for errors.
    lspvTr   z Failed executing 'lspv' command.msgrcstdoutstderr   z#Physical volume '%s' doesn't exist.r
   Nonelquerypvz-hz/dev/%s2010z"Failed executing lquerypv command.ORCLDISKz3Physical volume '%s' is already used by Oracle ASM.z&Physical volume '%s' is ok to be used.zPhysical volume 'z%' is in use by another volume group ''.z#' is already used by volume group 'FN)get_bin_pathrun_command	fail_json
splitlinessplitkeys)modulevgpvslspv_cmdr   current_lspvr   pv	lspv_listlinepv_datalquerypv_cmdcurrent_lquerypvr
   s                 m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/aix_lvg.py_validate_pvr(   _   s    ""640H%118*=Bf	Qw?B|djk 	 ++- 	/DjjlG$+AJIgaj!	/
 Y^^%%!F!KLR=F" "..z4@L+1+=+=|TS\_aSacgim>n+o(B &Qw  %IbYirx y--  !VY[![\:R?C9 9R= cegpqsgt!uvNPR[\^R_`cz=    c                 6   | j                  dd      }| j                  |dg      \  }}}|dk7  r| j                  d|z         | j                  |g      \  }}}|dk7  r| j                  d|z         ||v r||vr	d|z  }d|fS ||v r	d	|z  }d|fS d
|z  }d|fS )z
    Check the current state of volume group.

    :param module: Ansible module argument spec.
    :param vg: Volume Group name.
    :return: True (VG in varyon state) or False (VG in varyoff state) or
             None (VG does not exist), message.
    lsvgTz-or   zFailed executing '%s' command.r   z&Volume group '%s' is in varyoff state.Fz%Volume group '%s' is in varyon state.z!Volume group '%s' does not exist.N)r   r   r   )r   r   lsvg_cmdr   current_active_vgserrcurrent_all_vgsr
   s           r'   _validate_vgr0      s     ""640H"("4"4h5E"FBC	Qw=HI%118*=B	Qw=HI	_+=!=6;cz	5:Sy
-
2C9r)   c                    ddd}dddd}t        | ||      \  }	}
|	sd}||
fS |\  }}
|du rd}||
fS |du rUd}d}
| j                  s<| j                  d	d      }| j                  ||g|z         \  }}}|d
k7  rd}d|z  }
||
fS d|z  }
||
fS |_d}d}
| j                  sF| j                  dd      }| j                  |||   |||   d|g|z         \  }}}|d
k7  rd}d|z  }
||
fS d|z  }
||
fS y)z# Creates or extend a volume group. z-f )TFz-Bz-S)normalbigscalableFTextendvgr   z'Extending volume group '%s' has failed.zVolume group '%s' extended.Nmkvgz-yz"Creating volume group '%s' failed.zVolume group '%s' created.)r(   
check_moder   r   )r   r   r   pp_sizevg_typeforcevg_validation	force_optvg_optpv_stater
   changedvg_stateextendvg_cmdr   outputr.   mkvg_cmds                     r'   create_extend_vgrE      s   
 I F !S1MHc|!MHc5|	T	  !..z4@L$00,1Cc1IJOBQw?"D|#+b0|		  **648H$00(F7OWV_`eVfhlnp1qtw1wxOBQw:R?|#*R/| 
r)   c                 `   |\  }}|du rd}||fS |d}||fS || j                  dd      }| j                  |d|g      \  }}	}
|dk7  r| j                  d|z         g }|	j                         dd  D ]$  }|j	                  |j                         d          & d	|z  }n|}d
dj                  |      d|d}t        |      dk  rd}d}||fS d}d}| j                  sJ| j                  dd      }| j                  |d|g|z         \  }}}|dk7  r| j                  d|z  |||       |}||fS )NFr+   Tz-pr   z Failing to execute '%s' command.r   r   zVolume group '%s' removed.zPhysical volume(s) ' z' removed from Volume group 'r   zNo physical volumes to remove.r2   reducevgz-dfzUnable to remove '%s'.r	   )	r   r   r   r   appendr   joinlenr8   )r   r   r   r<   rA   r
   r@   r,   r   current_pvsr.   pvs_to_remover#   
reduce_msgreducevg_cmdr   r   s                    r'   	reduce_vgrP      s   !MHc5|		| { &&vt4%118T22FGK7!Ch!NO**,QR0 	2D  a1	2 2B6
SVS[S[\iSjlno
 =Q.|G
C**:t<#//ub0IM0YZFF7!9B!>2f]cd
CC<r)   c                    |\  }}|| j                  |       |dk(  re|du rd}||fS d}d}| j                  sB| j                  dd      }| j                  ||g      \  }}	}
|dk7  r| j                  d||
	       d
|z  }||fS |dk(  rf|du rd}||fS d}d}| j                  sC| j                  dd      }| j                  ||g      \  }}	}|dk7  r| j                  d||	|       d|z  }||fS y )Nr   varyonTFr2   varyonvgr   zCommand 'varyonvg' failed.)r
   r   r.   z!Varyon volume group %s completed.varyoff	varyoffvgzCommand 'varyoffvg' failed.r	   z"Varyoff volume group %s completed.)r   r8   r   r   )r   r   stater<   rA   r
   r@   varyonvg_cmdr   varyonvg_outr.   r   s               r'   state_vgrY     sM   !MHcS!tGC<  !..z4@L$*$6$6b7I$J!BcQw  %Abc R1B6|	)	uGC<  !..{DAL'-'9'9<:L'M$BfQw  %BrR^gm n2R7| 
r)   c                     t        t        t        dd      t        d      t        dd      t        dd	g d
      t        dd      t        ddg d            d      } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }|d}nd|z  }t        | |      }|d	k(  r.|sd}d}	| j	                  |	       nLt        | ||||||      \  }}	n7|dk(  rt        | |||      \  }}	n |dk(  s|dk(  rt        | |||      \  }}	nd}d}	| j                  ||	|        y )!NboolF)typedefaultint)r\   liststr)r\   elementspresent)absentrb   rT   rR   )r\   r]   choicesT)r\   requiredr3   )r4   r3   r5   )r;   r9   r   rV   r   r:   )argument_specsupports_check_moder;   r9   r   rV   r   r:   r2   z-s %sz#pvs is required to state 'present'.r   rc   rR   rT   zUnexpected state)r@   r
   rV   )	r   dictparamsr0   r   rE   rP   rY   	exit_json)
r   r;   r9   r   rV   r   r:   r<   r@   r
   s
             r'   mainrk   A  sp   FE2e$&51E9>hi.eX?\]
 !
F MM'"EmmI&G
--
CMM'"E	t	BmmI&GG# ,M	G7C%+FBWguVcdLGS	(	 S-@	(	ey0E=A  
W#U;r)   __main__N)
__future__r   r   r   r\   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   r(   r0   rE   rP   rY   rk   __name__ r)   r'   <module>ru      sc    A @3j4 
 4.b<8v-`%P-<` zF r)   