
    Vh)                         d dl mZmZmZ eZdZdZdZ	 d dl	Z	dZd dlmZ d dlmZ  G d	 d
      Zd Zd Zd Zd ZddZd Zd Zd Zd Zedk(  r e        yy# e
$ r Y Gw xY w)    )absolute_importdivisionprint_functiona  
module: xenserver_facts
short_description: Get facts reported on xenserver
description:
  - Reads data out of XenAPI, can be used instead of multiple C(xe) commands.
author:
  - Andy Hill (@andyhky)
  - Tim Rupp (@caphrim007)
  - Robin Lee (@cheese)
extends_documentation_fragment:
  - community.general.attributes
  - community.general.attributes.facts
  - community.general.attributes.facts_module
attributes:
  check_mode:
    version_added: 3.3.0
    # This was backported to 2.5.4 and 1.3.11 as well, since this was a bugfix
options: {}
a>  
- name: Gather facts from xenserver
  community.general.xenserver_facts:

- name: Print running VMs
  ansible.builtin.debug:
    msg: "{{ item }}"
  with_items: "{{ xs_vms.keys() }}"
  when: xs_vms[item]['power_state'] == "Running"

# Which will print:
#
# TASK: [Print running VMs] ***********************************************************
# skipping: [10.13.0.22] => (item=CentOS 4.7 (32-bit))
# ok: [10.13.0.22] => (item=Control domain on host: 10.0.13.22) => {
#     "item": "Control domain on host: 10.0.13.22",
#     "msg": "Control domain on host: 10.0.13.22"
# }
FNT)distro)AnsibleModulec                   2    e Zd Zd Zed        Zed        Zy)XenServerFactsc                     dddddd| _         y )Ngeorgeoxfordbostontampa
clearwater)z5.5.0z5.6.100z6.0.0z6.1.0z6.2.0)codes)selfs    u/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/xenserver_facts.py__init__zXenServerFacts.__init__A   s    !

    c                 4    t        j                         d   }|S )N   )r   linux_distributionr   results     r   versionzXenServerFacts.versionJ   s    **,Q/r   c                 p    | j                   | j                  v r| j                  | j                      }|S d }|S )N)r   r   r   s     r   codenamezXenServerFacts.codenameO   s7    <<4::%ZZ-F  Fr   N)__name__
__module____qualname__r   propertyr   r    r   r   r	   r	   @   s/    
    r   r	   c                  f    t        j                         } | j                  j                  dd       | S )N )XenAPI
xapi_localxenapilogin_with_password)sessions    r   get_xenapi_sessionr)   Y   s)    !GNN&&r2.Nr   c                 h    | j                   j                  j                         }t        |d      }|S N
name_labelkey)r&   networkget_all_recordschange_keys)r(   recsnetworkss      r   get_networksr4   _   s+    >>!!113D4\2HOr   c                 "   | j                   j                  j                         }t        |d      }i }t	        dd      }|j                         D ]<  }|D ]5  }d|z  }|j                  dd      }|d   |k(  r|||<   (|d   |k(  s1|||<   7 > |S )	Nuuidr-   r      zeth%sethbonddevice)r&   PIFr0   r1   rangevaluesreplace)	r(   r2   pifsxs_pifs
devicenumspifr8   interface_name	bond_names	            r   get_pifsrE   e   s    >>--/Dt(DGq!J{{} ) 	)C$_N&..uf=I8}.*-'X)+%(	"	)) Nr   c                 d    | j                   j                  j                         }t        |d      S )Ntagr-   )r&   VLANr0   r1   )r(   r2   s     r   	get_vlansrI   u   s'    >>..0Dt''r   c                     i }| j                         D ]W  \  }}|	 ||      s|j                         D ]!  \  }}t        |d      s|j                  ||<   # ||||   <   ||||      d<   Y |S )zp
    Take a xapi dict, and make the keys the value of recs[ref][key].

    Preserves the ref in rec['ref']

    valueref)itemshasattrrK   )r2   r.   filter_funcnew_recsrL   rec
param_nameparam_values           r   r1   r1   z   s     HJJL (S";s+;'*yy{ 	4#J
 {G,"-"3"3J	4 !S$'S5!( Or   c                     | j                   j                  j                         }| j                   j                  j                  |d         S )zGet the hostr   )r&   hostget_all
get_record)r(   	host_recss     r   get_hostrY      s9    ##++-I>>)))A,77r   c                 n    | j                   j                  j                         }|sy t        |d      }|S r+   )r&   VMr0   r1   )r(   r2   vmss      r   get_vmsr]      1    >>,,.D
d
-CJr   c                 n    | j                   j                  j                         }|sy t        |d      }|S r+   )r&   SRr0   r1   )r(   r2   srss      r   get_srsrb      r^   r   c                     t        i d      } t        s| j                  dd       t               }	 t	               }|j                  |j                  d}t              }t        |      }t        |      }t        |      }t        |      }	|r||d	<   |r||d
<   |r||d<   |r||d<   |	r|	|d<   | j                  |       y # t
        j                  $ r}| j                  d|z         Y d }~d }~ww xY w)NT)supports_check_modeFz'python xen api required for this module)changedmsgz%s)rf   )xenserver_versionxenserver_codenamexs_vlansr@   xs_networksxs_vmsxs_srs)ansible_facts)r   HAVE_XENAPI	fail_jsonr	   r)   r$   Failurer   r   r4   rE   rI   r]   rb   	exit_json)
moduleobjr(   edatarj   r@   ri   rk   rl   s
             r   mainrv      s   248F,UV

C'$&
 ![[!llD
 w'KwG!HWFWF#Z!Y)]XX
4(7 >> 'TAX&&'s   
C C4C//C4__main__)r6   N)
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESrn   r$   ImportErroransible.module_utilsr   ansible.module_utils.basicr   r	   r)   r4   rE   rI   r1   rY   r]   rb   rv   r   r!   r   r   <module>r      s    A @(* 	K ( 4 2 (
48$)N zF s  		s   A A"!A"