
    Vh#                        d Z ddlmZmZmZ eZdZdZddl	m
Z
mZ 	 ddlmZmZmZmZmZmZmZ ed        Zed        Zed	        Zed
        Zd Zd Zd Zd Zd Zd Zd Z e!dk(  r e         yy# e$ r ddlmZmZmZmZmZmZmZ Y \w xY w)z* A module for managing Infinibox clusters     )absolute_importdivisionprint_functiona
  
---
module: infini_cluster
version_added: '2.9.0'
short_description: Create, Delete and Modify Host Cluster on Infinibox
description:
    - This module creates, deletes or modifies host clusters on Infinibox.
author: David Ohlemacher (@ohlemacher)
options:
  name:
    description:
      - Cluster Name
    required: true
    type: str
  state:
    description:
      - Creates/Modifies Cluster when present, removes when absent, or provides
        details of a cluster when stat.
    required: false
    type: str
    default: present
    choices: [ "stat", "present", "absent" ]
  cluster_hosts:
    description: A list of hosts to add to a cluster when state is present.
    required: false
    type: list
    elements: dict
extends_documentation_fragment:
    - infinibox
z}
- name: Create new cluster
  infini_cluster:
    name: foo_cluster
    user: admin
    password: secret
    system: ibox001
)AnsibleModulemissing_required_lib)HAS_INFINISDKapi_wrapperinfinibox_argument_spec
get_systemget_clusterunixMillisecondsToDatemerge_two_dictsc                 |    d}| j                   j                         D ]  }|j                         }||k(  s|} |S  |S )z/Find a host by the name specified in the moduleN)hoststo_listget_name)system	host_namehosta_hosta_host_names        v/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/infinidat/infinibox/plugins/modules/infini_cluster.pyget_host_by_namer   Q   sM     D,,&&( oo')#DK
 K    c                     d}| j                   sl|j                  j                  | j                  d         }| j                  d   }|r2|D ]-  }|d   dk(  st	        ||d         }|j                  |       d}/ |S )	z Create a cluster Fname)r   cluster_hostshost_cluster_statepresentr   T)
check_modehost_clusterscreateparamsr   add_host)moduler   changedclusterr   cluster_hostr   s          r   create_clusterr)   ^   s     G&&--6==3H-Io6 - # 45B+FL4MND$$T*"G	#
 Nr   c                    d}| j                   d   }|j                  d      D cg c]  }|j                          }}|r|D ]  }|d   }||vrS|d   dk(  st        ||      }|s(d| d|j                          }	| j	                  |		       |j                  |       d
}_||v sd|d   dk(  smt        ||      }|s(d| d|j                          }	| j	                  |		       |j                  |       d
} |S c c}w )z Update a cluster Fr   r   r   r   r   zCannot find host z to add to cluster msgTabsent)r#   	get_fieldr   r   	fail_jsonr$   remove_host)
r%   r   r'   r&   module_cluster_hostsr   current_cluster_hosts_namesmodule_cluster_hostmodule_cluster_host_namer,   s
             r   update_clusterr5   n   sA    G "==9?F?P?PQX?Y"Zt4==?"Z"Z#7 	#':;'G$'/JJ&';<	I+F4LMD 12J1KK^_f_o_o_q^rs((S(1$$T*"G)-HH&';<H+F4LMD 12J1KK^_f_o_o_q^rs((S(1''-"G'	#( N- #[s   C<c                     |s&d|j                          d}| j                  |       d}| j                  s|j                          |S )z Delete a cluster Cluster 
 not foundr+   T)r   r/   r    delete)r%   r'   r,   r&   s       r   delete_clusterr:      sM     ))+,J7S!GNr   c                 *   | j                  dd      }t        |j                  dd            \  }}t        g | j                  ||      }| j                         }|D ]3  }|j                  |j                         d}|d   j                  |       5 |S )z Find fields for cluster T)
from_cache	raw_value
created_atN)r   idr>   created_at_timezone)host_idr   r   )
get_fieldsr   getdictr?   	get_hostsr   append)r'   fieldsr>   r@   
field_dictr   r   	host_dicts           r   get_cluster_fieldsrJ      s    44@F&<VZZVZ=[&\#J#::/	J E .ww
	 	7""9-. r   c                     t        |       }t        | |      }| j                  d   }|s| j                  d| d       t	        |      }t        dd      }t        ||      } | j                  d	i | y)
z Handle stat state r   r7   r8   r+   FzCluster stat foundr&   r,   N )r   r   r#   r/   rJ   rD   r   	exit_json)r%   r   r'   cluster_namerH   results         r   handle_statrQ      s~    F&&)G==(Lx~Z@A#G,J F VZ0FFvr   c                     t        |       }t        | |      }| j                  d   }|s&t        | |      }d| d}| j	                  ||       yt        | ||      }|rd| d}nd| d}| j	                  ||       y)z Handle present state r   r7   z createdrL   z updatedz required no changesN)r   r   r#   r)   rN   r5   r%   r   r'   rO   r&   r,   s         r   handle_presentrT      s    F&&)G==(L 0h/c2 9\N(3C\N*>?Cc2r   c                     t        |       }t        | |      }| j                  d   }|s	d}d| d}nt        | |      }d| d}| j	                  ||       y)z Handle absent state r   Fr7   z already absentz removedrL   N)r   r   r#   r:   rN   rS   s         r   handle_absentrV      si    F&&)G==(Lo6 1h/
W#.r   c                 *   | j                   d   }	 |dk(  rt        |        n7|dk(  rt        |        n&|dk(  rt        |        n| j	                  d|        t        |       }|j                          y# t        |       }|j                          w xY w)z Handle states statestatr   r-   z'Internal handler error. Invalid state: r+   N)r#   rQ   rT   rV   r/   r   logout)r%   rX   r   s      r   execute_stater[      s    MM'"EF?i6"h&!#J5'!RSF# F#s   AA5 5Bc                 D   | j                   d   }|dk(  ri| j                   d   }|rW|D ]Q  }	 ddg}|D ]  }||j                         vst         t        |j                               t        |      k7  rt        S yyy# t        $ r d}| j	                  |       Y vw xY w)	z* Check module parameters for logic errors rX   r   r   r   r   zgWith state present, all cluster_hosts require host_name and host_cluster_state key:values and no othersr+   N)r#   keysKeyErrorlenr/   )r%   rX   r   r   
valid_keys	valid_keyr,   s          r   check_optionsrb      s    MM'"E	o6% .."-/C!DJ%/ +	$DIIK7"*N+
 499;'3z?:& ;.     .*C $$$-	.s   A?2A??BBc                      t               } | j                  t        t        d      t        dg d      t        ddd	      
             t        | d      }t        s|j                  t        d             t        |       t        |       y)z Main T)requiredr   )rY   r   r-   )defaultchoicesFlistrD   )rd   typeelements)r   rX   r   )supports_check_mode	infinisdkr+   N)	r
   updaterD   r   r   r/   r   rb   r[   )argument_specr%   s     r   mainrn     sw    +-Mt$y2OPFVL	
 =dCF1+>?&&r   __main__N)"__doc__
__future__r   r   r   rh   __metaclass__DOCUMENTATIONEXAMPLESansible.module_utils.basicr   r   Fansible_collections.infinidat.infinibox.plugins.module_utils.infiniboxr   r	   r
   r   r   r   r   ModuleNotFoundError	infiniboxr   r)   r5   r:   rJ   rQ   rT   rV   r[   rb   rn   __name__rM   r   r   <module>rz      s    1
 C B> K  * 	 	    @  ( 3$/".0( zF o  	  	s   A1 1B
B