
    Vh	)                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 	 d dlmZmZ dZ G d d	e
      Zd
 Zedk(  r e        yy# e$ r Y %w xY w)    )absolute_importdivisionprint_functiona  
module: one_host

short_description: Manages OpenNebula Hosts


requirements:
  - pyone

description:
  - Manages OpenNebula Hosts.
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none

options:
  name:
    description:
      - Hostname of the machine to manage.
    required: true
    type: str
  state:
    description:
      - Takes the host to the desired lifecycle state.
      - If V(absent) the host will be deleted from the cluster.
      - If V(present) the host will be created in the cluster (includes V(enabled), V(disabled) and V(offline) states).
      - If V(enabled) the host is fully operational.
      - V(disabled), for example to perform maintenance operations.
      - V(offline), host is totally offline.
    choices:
      - absent
      - present
      - enabled
      - disabled
      - offline
    default: present
    type: str
  im_mad_name:
    description:
      - The name of the information manager, this values are taken from the oned.conf with the tag name IM_MAD (name).
    default: kvm
    type: str
  vmm_mad_name:
    description:
      - The name of the virtual machine manager mad name, this values are taken from the oned.conf with the tag name VM_MAD
        (name).
    default: kvm
    type: str
  cluster_id:
    description:
      - The cluster ID.
    default: 0
    type: int
  cluster_name:
    description:
      - The cluster specified by name.
    type: str
  labels:
    description:
      - The labels for this host.
    type: list
    elements: str
  template:
    description:
      - The template or attribute changes to merge into the host template.
    aliases:
      - attributes
    type: dict

extends_documentation_fragment:
  - community.general.opennebula
  - community.general.attributes

author:
  - Rafael del Valle (@rvalle)
aZ  
- name: Create a new host in OpenNebula
  community.general.one_host:
    name: host1
    cluster_id: 1
    api_url: http://127.0.0.1:2633/RPC2

- name: Create a host and adjust its template
  community.general.one_host:
    name: host2
    cluster_name: default
    template:
      LABELS:
        - gold
        - ssd
      RESERVED_CPU: -100

)OpenNebulaModule)HOST_STATESHOST_STATUSic                   $    e Zd Zd Zd Zd Zd Zy)
HostModulec                    t        t        dd      t        g dd      t        dd      t        dd      t        d	d
      t        d      t        dd      t        ddg            }ddgg}t        j                  | ||       y )NstrT)typerequired)presentabsentenableddisabledoffliner   )choicesdefaultkvm)r   r   intr   )r   list)r   elementsdict
attributes)r   aliases)namestateim_mad_namevmm_mad_name
cluster_idcluster_namelabelstemplater"   r#   )mutually_exclusive)r   r   __init__)selfargument_specr&   s      n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/one_host.pyr'   zHostModule.__init__   s    540V`ij%75%825)Ve4v~>	
 >*
 	!!$J\]    c                 T   	 | j                   j                  j                  | j                  d      | j                  d      | j                  d      | j                  d             d| j                  d<   y# t
        $ r(}| j                  dt        |      z          Y d	}~yd	}~ww xY w)
z
        Creates a host entry in OpenNebula
        self.one.host.allocate returns ID of a host
        Returns: True on success, fails otherwise.

        r   r!   r    r"   Tchangedz Could not allocate host, ERROR: msgN)onehostallocateget_parameterresult	Exceptionfailr   )r(   es     r*   allocate_hostzHostModule.allocate_host   s    	GHHMM""4#5#5f#=#'#5#5n#E#'#5#5m#D#'#5#5l#CE &*DKK	"   	GII<s1vEIFF	Gs   A3A6 6	B'?B""B'c                 x      j                  d fdd |t        j                  t        j                  g      S )zy
        Utility method that waits for a host state.
        Args:
            host:
            target_states:

        r1   c                  v    j                   j                  j                   j                        j                  S N)r0   r1   infoIDSTATE)r1   r(   s   r*   <lambda>z0HostModule.wait_for_host_state.<locals>.<lambda>   s$    488==+=+=dgg+F+L+L r+   c                 ,    t        |       j                  S r;   )r   r   )ss    r*   r?   z0HostModule.wait_for_host_state.<locals>.<lambda>   s    [^-@-@ r+   )invalid_states)wait_for_stater   ERRORMONITORING_ERROR)r(   r1   target_statess   `` r*   wait_for_host_statezHostModule.wait_for_host_state   s=     ""6#L#@-3>3D3DkFbFb2c # e 	er+   c                    | j                  d      }| j                  |      }| j                  d      }t        |      r,|j                  }t	        |j                        j
                  }nt        }d}|dk(  r|t        k(  rD| j                          | j                  |      }| j                  |t        j                  g       n|t        j                  t        j                  fv r| j                  d|z         n|dk(  r|t        k(  rD| j                          | j                  |      }| j                  |t        j                  g       n|t        j                  t        j                  fv rp|j                  j!                  |j"                  t$        j&                        r(| j                  |t        j                  g       d|d	<   n| j                  d
       n|t        j                  fv rn| j                  d|z         n|dk(  r|t        k(  r| j                  d       n|t        j                  t        j                  fv r]	 |j                  j!                  |j"                  t$        j                         d|d	<   | j                  |t        j                  g       n.|t        j                  fv rn| j                  d|z         n|dk(  r|t        k(  r| j                  d       n|t        j                  t        j                  fv r\	 |j                  j!                  |j"                  t$        j                         d|d	<   | j                  |t        j                  g       nc|t        j                  fv rnO| j                  d|z         n9|dk(  r4|t        k7  r+	 |j                  j-                  |j"                         d|d	<   |dk7  r| j                  d      }
|

t/               }
| j1                  d      r| j                  d      |
d<   | j3                  |j4                  |
      r1d|
i}
	 |j                  j7                  |j"                  |
d       d|d	<   |j8                  | j                  d      k7  r;	 |j:                  j=                  | j                  d      |j"                         d|d	<   | j?                          y # t(        $ r)}	| j                  dt+        |	      z          Y d }	~	|d }	~	ww xY w# t(        $ r)}	| j                  dt+        |	      z          Y d }	~	d }	~	ww xY w# t(        $ r)}	| j                  dt+        |	      z          Y d }	~	d }	~	ww xY w# t(        $ r)}	| j                  dt+        |	      z          Y d }	~	6d }	~	ww xY w# t(        $ r)}	| j                  dt+        |	      z          Y d }	~	d }	~	ww xY w)Nr   r   ABSENTr   zinvalid host state %sr.   r   Tr-   zcould not enable hostzBunknown host state %s, cowardly refusing to change state to enabler   z+absent host cannot be put in disabled statezCould not disable host, ERROR: zCunknown host state %s, cowardly refusing to change state to disabler   z-absent host cannot be placed in offline statez#Could not set host offline, ERROR: zCunknown host state %s, cowardly refusing to change state to offliner   z+Could not delete host from cluster, ERROR: r%   r$   LABELSTEMPLATE   z+Failed to update the host template, ERROR: r"   z*Failed to update the host cluster, ERROR: ) r3   get_host_by_nameboolr>   r   r   HOST_ABSENTr8   rG   	MONITOREDrD   rE   r6   DISABLEDOFFLINEr1   statusr=   r	   ENABLEDr5   r   deleter   is_parameterrequires_template_updaterK   update
CLUSTER_IDclusteraddhostexit)r(   r0   moduler4   	host_namer1   desired_statecurrent_statecurrent_state_namer7   desired_template_changess              r*   runzHostModule.run   s    &&v.	$$Y/ **73: JJM!,TZZ!8!=!='M!) I%+""$,,Y7((0E0E/FG;#4#4k6R6R"SS		58JJ	Ki'+""$,,Y7((0E0E/FG;#7#79L9L"MM88??477K,?,?@,,TK4I4I3JK(,F9%II"9I:;#8#8"99		beww	xj(+		K	L;#8#8+:M:M"NNNHHOODGG[-A-AB(,F9% ((0D0D/EF;#7#7"88		cfxx	yi'+		M	N;#8#8+:N:N"OORHHOODGG[-@-@A(,F9% ((0C0C/DE;#6#6"77		cfxx	yh&+ZHHOODGG,(,F9% H$'+'9'9*'E$'/+/6(   *595G5G5Q(2,,T]]<TU,68P+Q(ZHHOODGG-EqI(,F9%
 $"4"4\"BBYKK''(:(:<(H$''R(,F9%
 			E ! NII"Cc!f"LIMMN  ! RII"G#a&"PIQQR ! ZII"ORUVWRX"XIYYZ. ! ZII"ORUVWRX"XIYYZ ! YII"NQTUVQW"WIXXYsx   9S !9T 5*U ,U< :V1 	T&T

T	UT??U	U9U44U9<	V.V))V.1	W#:WW#N)__name__
__module____qualname__r'   r8   rG   rc    r+   r*   r   r      s    ^&$err+   r   c                  4    t               j                          y r;   )r   
run_modulerg   r+   r*   mainrj   +  s    Lr+   __main__N)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNEansible_collections.community.general.plugins.module_utils.opennebular   pyoner   r	   ImportErrorrO   r   rj   rd   rg   r+   r*   <module>rt      s}    C BM^&
 c	. f! fR zF m  		s   A   AA