
    VhV)                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZmZ d dlmZ d dlmZ  G d	 d
e      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona	
  
module: stacki_host
short_description: Add or remove host to stacki front-end
description:
  - Use this module to add or remove hosts to a stacki front-end using API.
  - Information on stacki can be found at U(https://github.com/StackIQ/stacki).
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  name:
    description:
      - Name of the host to be added to Stacki.
    required: true
    type: str
  stacki_user:
    description:
      - Username for authenticating with Stacki API, but if not specified, the environment variable E(stacki_user) is used
        instead.
    required: true
    type: str
  stacki_password:
    description:
      - Password for authenticating with Stacki API, but if not specified, the environment variable E(stacki_password) is
        used instead.
    required: true
    type: str
  stacki_endpoint:
    description:
      - URL for the Stacki API Endpoint.
    required: true
    type: str
  prim_intf_mac:
    description:
      - MAC Address for the primary PXE boot network interface.
      - Currently not used by the module.
    type: str
  prim_intf_ip:
    description:
      - IP Address for the primary network interface.
      - Currently not used by the module.
    type: str
  prim_intf:
    description:
      - Name of the primary network interface.
      - Currently not used by the module.
    type: str
  force_install:
    description:
      - Set value to V(true) to force node into install state if it already exists in stacki.
    type: bool
    default: false
  state:
    description:
      - Set value to the desired state for the specified host.
    type: str
    choices: [absent, present]
    default: present
  appliance:
    description:
      - Appliance to be used in host creation.
      - Required if O(state=present) and host does not yet exist.
    type: str
    default: backend
  rack:
    description:
      - Rack to be used in host creation.
      - Required if O(state=present) and host does not yet exist.
    type: int
    default: 0
  rank:
    description:
      - Rank to be used in host creation.
      - In Stacki terminology, the rank is the position of the machine in a rack.
      - Required if O(state=present) and host does not yet exist.
    type: int
    default: 0
  network:
    description:
      - Network to be configured in the host.
      - Currently not used by the module.
    type: str
    default: private
author:
  - Hugh Ma (@bbyhuy) <Hugh.Ma@flextronics.com>
a  
- name: Add a host named test-1
  community.general.stacki_host:
    name: test-1
    stacki_user: usr
    stacki_password: pwd
    stacki_endpoint: url
    prim_intf_mac: mac_addr
    prim_intf_ip: x.x.x.x
    prim_intf: eth0

- name: Remove a host named test-1
  community.general.stacki_host:
    name: test-1
    stacki_user: usr
    stacki_password: pwd
    stacki_endpoint: url
    state: absent
a  
changed:
  description: Response to whether or not the API call completed successfully.
  returned: always
  type: bool
  sample: true

stdout:
  description: The set of responses from the commands.
  returned: always
  type: list
  sample: ['...', '...']

stdout_lines:
  description: The value of stdout split into a list.
  returned: always
  type: list
  sample: [['...', '...'], ['...'], ['...']]
N)AnsibleModuleenv_fallback)	urlencode)	fetch_urlc                   8    e Zd Zd Zd	dZd Zd Zd Zd Zd Z	y)

StackiHostc                 x   || _         |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _	        |j                  d	   | _
        |j                  d
   |j                  d   d}| j                  | j                  d      }|j                  j                  d      j                  d      }d }|D ]+  }d|v s|j                  dd      }|j!                  d      } n ||d|j                  j                  d      d}| j                  dz   }| j                  ||t#        |      d      }	|	j                  j                  d      j                  d      }
d }|
D ]C  }d|v r|j                  dd      }d|v sj                  dd      d   }|j!                  d      }E ||d|	j                  j                  d      d| _        y ) Nnamerackrank	appliance	prim_intfprim_intf_ipnetworkprim_intf_macstacki_endpointstacki_userstacki_password)USERNAMEPASSWORDGET)methodz
Set-Cookie;	csrftokenz
csrftoken= 
z!application/x-www-form-urlencoded)r   X-CSRFTokenContent-typeCookiez/loginPOST)headerspayloadr   	sessionidz
sessionid=   zapplication/json)r   r    r&   r!   r"   )moduleparamshostnamer   r   r   r   r   r   r   endpoint
do_requestr$   getsplitreplacerstripr   header)selfr)   
auth_credscred_acookie_ainit_csrftokencr2   login_endpoint	login_reqcookie_fr   fr&   s                 q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/stacki_host.py__init__zStackiHost.__init__   s/   f-MM&)	MM&)	{3{3"MM.9}}Y/#]]?;&78"(--">"(--0A"BD
 u=>>%%l399#> 	Aa!"<!<!/!6!6v!>		  .n"EQWQ_Q_QcQcdpQqs 1 OONFIV`LajpOq	$$((6<<SA	 	5AaIIlB7	aGGL!4R8	%,,V4		5 %.&/$-'9!*!2!2!6!6|!D	F    Nc                     t        | j                  ||||      \  }}|d   dk7  r | j                  j                  d|d          |S )N)datar$   r   status   Fmsg)changedrD   )r	   r)   	fail_json)r3   urlr%   r$   r   resinfos          r=   r-   zStackiHost.do_request   sI    dkk3WgV\]	T>S KK!!%T%[!A
r?   c                     | j                  | j                  t        j                  ddi      | j                  d      }| j
                  |j                         v S )Ncmdz	list hostr#   r%   r$   r   )r-   r,   jsondumpsr2   r+   read)r3   rH   s     r=   stack_check_hostzStackiHost.stack_check_host   sG    oodmmTZZ@T5U_c_j_jsyoz}}
**r?   c                     | j                  | j                  t        j                  ddi      | j                  d       | j                  | j                  t        j                  ddi      | j                  d       y )NrK   zsync configr#   rL   zsync host config)r-   r,   rM   rN   r2   )r3   s    r=   
stack_synczStackiHost.stack_sync   s_    tzz5-:P/Q[_[f[fouvtzz5BT:U/V`d`k`ktz{r?   c                    ddj                  | j                        i}| j                  | j                  t	        j
                  |      | j                  d       d}| j                          ||d<   dj                  d      |d	<   y )
NrK   z set host boot {0} action=installr#   rL   TrE   api call successfulr   stdout)	formatr+   r-   r,   rM   rN   r2   rR   r1   r3   resultrA   rE   s       r=   stack_force_installzStackiHost.stack_force_install   sp    9@@OPtzz$/?]cd#y077?xr?   c                 d   t               }d}dj                  | j                  | j                  | j                  | j
                        |d<   | j                  | j                  t        j                  |      | j                  d       | j                          ||d<   dj                  d      |d	<   y )
NFz,add host {0} rack={1} rank={2} appliance={3}rK   r#   rL   rE   rT   r   rU   )dictrV   r+   r   r   r   r-   r,   rM   rN   r2   rR   r1   rW   s       r=   	stack_addzStackiHost.stack_add   s    vDVDMM499diiH 	Utzz$/?]cd#y077?xr?   c                    t               }dj                  | j                        |d<   | j                  | j                  t        j                  |      | j                  d       | j                          d|d<   dj                  d      |d	<   y )
Nzremove host {0}rK   r#   rL   TrE   rT   r   rU   )
r[   rV   r+   r-   r,   rM   rN   r2   rR   r1   )r3   rX   rA   s      r=   stack_removezStackiHost.stack_remove   sr    v'VDMM" 	Utzz$/?]cd y077?xr?   )NNN)
__name__
__module____qualname__r>   r-   rP   rR   rY   r\   r^    r?   r=   r   r      s+    0Fd+|@@
@r?   r   c                  H   t        t        t        ddddg      t        dd      t        dd	      t        dd	      t        dd
	      t        d      t        d      t        dd	      t        d      t        ddt        dgf      t        ddt        dgfd      t        ddt        dgf      t        dd	            d      } ddi}t               }t	        |       }|j                         }| j                  d   dk(  r#|r!| j                  d   r|j                  |       n| j                  d   dk(  r3|r1| j                  d   s"dj                  | j                  d         |d<   n| j                  d   dk(  rk|sidD ]#  }| j                  |   r|j                  |       % t        |      dkD  r!| j                  dj                  |             |j                  |       n%| j                  d   dk(  r|r|j                  |        | j                  di | y ) Nstrpresentabsent)typedefaultchoicesT)rg   requiredintr   )rg   rh   backend)rg   privater   )rg   rj   fallbackr   )rg   rj   rn   no_logr   boolF)stater   r   r   r   r   r   r   r   r   r   r   force_install)argument_specsupports_check_moderE   rq   rr   z<{0} already exists. Set 'force_install' to true to bootstrapr   rU   )r   r   r   r   r   r   r   zmissing required arguments: {0})rD   rb   )r   r[   r   listr   rP   r*   rY   rV   appendlenrF   r\   r^   	exit_json)r)   rX   missing_paramsstackihost_existsparams         r=   mainr}      s   E9x>ST5405!,5!,y9&5)eY7E*%$,Q^P_A`a edlUfTgEhquv edlUfTgEhiFE:
 "!F& FVNF))+K }}W*{v}}_?]""6*	w	9	,V]]SbEcYVFMM&)* 	x 
w	9	,[k 	-E=='%%e,	- ~"!B!I!I.!YZ 	w	8	+F#Fvr?   __main__)
__future__r   r   r   rg   __metaclass__DOCUMENTATIONEXAMPLESRETURNrM   ansible.module_utils.basicr   r   +ansible.module_utils.six.moves.urllib.parser   ansible.module_utils.urlsr	   objectr   r}   r_   rb   r?   r=   <module>r      sc    A @Yv(
(  B A /e@ e@P.b zF r?   