
    Vhv"                        d dl mZ dZdZ	 d dlZdZd dlmZ d dl	m
Z
mZ d d	lmZ d d
lmZ d dlZ G d de
e      Zy# e$ r dZY 4w xY w)    )annotationsa  
name: opennebula
author:
  - Kristian Feldsam (@feldsam)
short_description: OpenNebula inventory source
version_added: "3.8.0"
extends_documentation_fragment:
  - constructed
description:
  - Get inventory hosts from OpenNebula cloud.
  - Uses an YAML configuration file ending with either C(opennebula.yml) or C(opennebula.yaml)
    to set parameter values.
  - Uses O(api_authfile), C(~/.one/one_auth), or E(ONE_AUTH) pointing to a OpenNebula credentials file.
options:
  plugin:
    description: Token that ensures this is a source file for the 'opennebula' plugin.
    type: string
    required: true
    choices: [community.general.opennebula]
  api_url:
    description:
      - URL of the OpenNebula RPC server.
      - It is recommended to use HTTPS so that the username/password are not
        transferred over the network unencrypted.
      - If not set then the value of the E(ONE_URL) environment variable is used.
    env:
      - name: ONE_URL
    required: true
    type: string
  api_username:
    description:
      - Name of the user to login into the OpenNebula RPC server. If not set
        then the value of the E(ONE_USERNAME) environment variable is used.
    env:
      - name: ONE_USERNAME
    type: string
  api_password:
    description:
      - Password or a token of the user to login into OpenNebula RPC server.
      - If not set, the value of the E(ONE_PASSWORD) environment variable is used.
    env:
      - name: ONE_PASSWORD
    required: false
    type: string
  api_authfile:
    description:
      - If both O(api_username) or O(api_password) are not set, then it will try
        authenticate with ONE auth file. Default path is C(~/.one/one_auth).
      - Set environment variable E(ONE_AUTH) to override this path.
    env:
      - name: ONE_AUTH
    required: false
    type: string
  hostname:
    description: Field to match the hostname. Note V(v4_first_ip) corresponds to the first IPv4 found on VM.
    type: string
    default: v4_first_ip
    choices:
      - v4_first_ip
      - v6_first_ip
      - name
  filter_by_label:
    description: Only return servers filtered by this label.
    type: string
  group_by_labels:
    description: Create host groups by vm labels
    type: bool
    default: true
a  
# inventory_opennebula.yml file in YAML format
# Example command line: ansible-inventory --list -i inventory_opennebula.yml

# Pass a label filter to the API
plugin: community.general.opennebula
api_url: https://opennebula:2633/RPC2
filter_by_label: Cache
NTF)AnsibleError)BaseInventoryPluginConstructable)make_unsafe)
namedtuplec                  T     e Zd ZdZ fdZd Zd Zd Zd Zd
dZ	d Z
d fd		Z xZS )InventoryModulezcommunity.general.opennebulac                X    d}t         t        |   |      r|j                  d      rd}|S )NF)zopennebula.yamlzopennebula.ymlT)superr
   verify_fileendswith)selfpathvalid	__class__s      r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/inventory/opennebula.pyr   zInventoryModule.verify_filel   s.    $3D9}}BC    c                Z   | j                  d      }| j                  d      }| j                  d      }| j                  d      }|s|s|>t        j                  j                  t        j                  j                  d      dd      }	 t        |d      5 }|j                         j                         }d d d        j                  d	      \  }}t        dd      } ||||      S # 1 sw Y   4xY w# t        t        f$ r t        d
| d      t        $ r t        d| d      w xY w)Napi_urlapi_usernameapi_passwordapi_authfileHOMEz.oneone_authr:z)Could not find or read ONE_AUTH file at ''z,Error occurs when reading ONE_AUTH file at 'auth)urlusernamepassword)
get_optionosr   joinenvirongetopenreadrstripsplitOSErrorIOErrorr   	Exceptionr   )r   r    r!   r"   authfilefp
authstringauth_paramss           r   _get_connection_infoz$InventoryModule._get_connection_infos   s$   ooi(??>2??>2??>277<<

v(>
S_(C( 4B!#!1!1!3J4%/%5%5c%:"( !)HIsXII4 4 W% \"%NxjXY#Z[[ _"%QRZQ[[\#]^^_s$   
C4 C(5C4 (C1-C4 46D*c                    |j                   j                  d      }t        |t              r|g}|D ]  }|j                  d      s|d   c S  y)NNICIPFTEMPLATEr'   
isinstancedictr   vmnicnets       r   _get_vm_ipv4zInventoryModule._get_vm_ipv4   sN    kkooe$c4 %C 	!Cwwt}4y 	! r   c                    |j                   j                  d      }t        |t              r|g}|D ]  }|j                  d      s|d   c S  y)Nr5   
IP6_GLOBALFr7   r;   s       r   _get_vm_ipv6zInventoryModule._get_vm_ipv6   sP    kkooe$c4 %C 	)Cww|$<((	) r   c                \   | j                         }|j                  r|j                  st        d      t	        j
                  |j                  |j                   d|j                         }	 |j                  j                  dddd      }|S # t        $ r}t        d|       d }~ww xY w)Nz9API Credentials missing. Check OpenNebula inventory file.r   )session   z(Something happened during XML-RPC call: )
r3   r!   r"   r   pyone	OneServerr    vmpoolinfoextendedr.   )r   r   
one_clientvm_pooles        r   _get_vm_poolzInventoryModule._get_vm_pool   s    ((*$--Z[[dmm_Admm_;]^J	O ''44RRCG   	O!I!MNN	Os   /B 	B+B&&B+c                H   | j                         }g }|j                  D ]z  }|j                  }g }|j                  j                  d      r|j                  j                  d      D cg c]/  }|dk(  s&|dk(  s!|j	                         s|j                         s.|1 }}dj                  |      }|j                  dd      }|j                  dd      }|j                  d      }|||vr|j                  |d<   |j                  |d<   t        |j                  d	      r<|j                  j                  r&|j                  j                  d
   j                  |d<   ||d<   | j                  |      |d<   | j!                  |      |d<   |j#                  |       } |S c c}w )NLABELS,-  _nameidHISTORYrF   hostv4_first_ipv6_first_ip)rO   VMUSER_TEMPLATEr'   isalnumisspacer%   replacer+   NAMEIDhasattrHISTORY_RECORDSrY   HOSTNAMEr?   rB   append)r   label_filterrM   resultr<   serverlabelsss           r   _retrieve_serversz!InventoryModule._retrieve_servers   s   ##% ** 	"B%%FF##H-%'%5%5%9%9(%CzqCxSTX[S[_`_h_h_jnonwnwny!zzS1S1c* 'v-WWF6N55F4Lr))95":L:L:T:T!#!3!3!;!;B!?!H!Hv%F8$($5$5b$9F=!$($5$5b$9F=!MM&!1	"4 + {s   )/FFc                   | j                  d      }| j                  d      }| j                  d      }| j                  j                  d       | j                  d      }| j                  |      }|D ]q  }t	        |      }|d   }|rH|d   rC|d   D ];  }| j                  j                  |       | j                  j                  ||	       = | j                  j                  |d	       |j                         D ]"  \  }	}
| j                  j                  ||	|
       $ |dk7  r | j                  j                  |d
||          |j                  d      r | j                  j                  |d|d          | j                  | j                  d      |||       | j                  | j                  d      |||       | j                  | j                  d      |||       t y )Nhostnamegroup_by_labelsstrictall)groupfilter_by_labelrW   rQ   )rZ   rs   ansible_hostSSH_PORTansible_portcompose)rq   groupskeyed_groups)r#   	inventory	add_grouprm   r   add_hostitemsset_variabler'   _set_composite_vars_add_host_to_composed_groups_add_host_to_keyed_groups)r   hostname_preferencerp   rq   rt   serversrj   ro   label	attributevalues              r   	_populatezInventoryModule._populate   s   "ooj9//*;<* 	  u -//*;<((9 	mF (Ff~H6(#3#H- HENN,,5,9NN+++GH NN###?$*LLN H 	5++HiGH #f,++HnfM`Fabzz*%++HnfZFXY $$T__Y%?Z`$a --dooh.GQYbh-i **4??>+JFT\ek*l7	mr   c                    t         st        d      t        t        |   |||       | j                  |       | j                          y )Nz3OpenNebula Inventory plugin requires pyone to work!)r   )	HAS_PYONEr   r   r
   parse_read_config_datar   )r   r{   loaderr   cacher   s        r   r   zInventoryModule.parse   s@    TUUot*9fdCD)r   )N)T)__name__
__module____qualname__rb   r   r3   r?   rB   rO   rm   r   r   __classcell__)r   s   @r   r
   r
   i   s9    )DJ,

  D%mN r   r
   )
__future__r   DOCUMENTATIONEXAMPLESrH   r   ImportErroransible.errorsr   ansible.plugins.inventoryr   r   Aansible_collections.community.general.plugins.plugin_utils.unsafer   collectionsr   r$   r
    r   r   <module>r      s_    #DLI ( H Y " 	X)= X  Is   = AA