
    Vh+%                        d dl mZ dZdZd dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ  G d de      Zy)    )annotationsa  
name: online
author:
  - Remy Leone (@remyleone)
short_description: Scaleway (previously Online SAS or Online.net) inventory source
description:
  - Get inventory hosts from Scaleway (previously Online SAS or Online.net).
options:
  plugin:
    description: token that ensures this is a source file for the 'online' plugin.
    type: string
    required: true
    choices: ['online', 'community.general.online']
  oauth_token:
    required: true
    description: Online OAuth token.
    type: string
    env:
      # in order of precedence
      - name: ONLINE_TOKEN
      - name: ONLINE_API_KEY
      - name: ONLINE_OAUTH_TOKEN
  hostnames:
    description: List of preference about what to use as an hostname.
    type: list
    elements: string
    default:
      - public_ipv4
    choices:
      - public_ipv4
      - private_ipv4
      - hostname
  groups:
    description: List of groups.
    type: list
    elements: string
    choices:
      - location
      - offer
      - rpn
z
# online_inventory.yml file in YAML format
# Example command line: ansible-inventory --list -i online_inventory.yml

plugin: community.general.online
hostnames:
  - public_ipv4
groups:
  - location
  - offer
  - rpn
N)version)AnsibleError)open_url)BaseInventoryPlugin)to_text)__version__)urljoin)make_unsafec                       e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zed        Zd Zd Zd Zd fd	Z xZS )InventoryModulezcommunity.general.onlinezhttps://api.online.netc                    	 |d   d   d   S # t         t        t        f$ r | j                  j	                  d       Y y w xY w)Nnetworkipr   zLAn error happened while extracting public IPv4 address. Information skipped.KeyError	TypeError
IndexErrordisplaywarningself
host_infoss     n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/inventory/online.pyextract_public_ipv4z#InventoryModule.extract_public_ipv4P   sH    	i(.q11)Z0 	LL  !op	   
 /??c                    	 |d   d   d   S # t         t        t        f$ r | j                  j	                  d       Y y w xY w)Nr   privater   zMAn error happened while extracting private IPv4 address. Information skipped.r   r   s     r   extract_private_ipv4z$InventoryModule.extract_private_ipv4W   sH    	i(3A66)Z0 	LL  !pq	r   c                t    	 |d   d   S # t         t        f$ r | j                  j                  d       Y y w xY w)Nosnamez@An error happened while extracting OS name. Information skipped.r   r   r   r   r   s     r   extract_os_namezInventoryModule.extract_os_name^   sA    	d#F++)$ 	LL  !cd	   
 *77c                t    	 |d   d   S # t         t        f$ r | j                  j                  d       Y y w xY w)Nr!   r   zCAn error happened while extracting OS version. Information skipped.r#   r   s     r   extract_os_versionz"InventoryModule.extract_os_versione   sA    	d#I..)$ 	LL  !fg	r%   c                n    	 |d   S # t         t        f$ r | j                  j                  d       Y y w xY w)NhostnamezAAn error happened while extracting hostname. Information skipped.r#   r   s     r   extract_hostnamez InventoryModule.extract_hostnamel   s<    	j)))$ 	LL  !de	    *44c                t    	 |d   d   S # t         t        f$ r | j                  j                  d       Y y w xY w)Nlocation
datacenterzLAn error happened while extracting datacenter location. Information skipped.r#   r   s     r   extract_locationz InventoryModule.extract_locations   sA    	j),77)$ 	LL  !op	r%   c                n    	 |d   S # t         t        f$ r | j                  j                  d       Y y w xY w)NofferzIAn error happened while extracting commercial offer. Information skipped.r#   r   s     r   extract_offerzInventoryModule.extract_offerz   s<    	g&&)$ 	LL  !lm	r+   c                    	 | j                   |d      S # t        t        f$ r | j                  j	                  d       Y y w xY w)NidzHAn error happened while extracting RPN information. Information skipped.)rpn_lookup_cacher   r   r   r   r   s     r   extract_rpnzInventoryModule.extract_rpn   sG    	((D)9::)$ 	LL  !kl	s    *A Ac                ^   	 t        || j                        }	 t        |j                         d      }	 t        j                  |      S # t        $ r(}| j                  j	                  d|        Y d }~y d }~ww xY w# t        $ r t        d      w xY w# t        $ r t        d      w xY w)N)headersz"An error happened while fetching: surrogate_or_strict)errorsz9Incorrect encoding of fetched payload from Online serverszIncorrect JSON payload)r   r8   	Exceptionr   r   r   readUnicodeErrorr   jsonloads
ValueError)r   urlresponseeraw_datas        r   _fetch_informationz"InventoryModule._fetch_information   s    	T\\:H
	\x}}7LMH	9::h''  	LL  #EcU!KL	  	\Z[[	\
  	9788	9s-   A A? B 	A<A77A<?BB,c                B    i }| D ]  }|d   D ]  }|d   ||d   <     |S )Nmembersr"   r4    )rpn_listlookuprpnmembers       r   extract_rpn_lookup_cachez(InventoryModule.extract_rpn_lookup_cache   sB     	3Ci. 3'*6{vd|$3	3     c           	        d}|D ]+  }| j                   j                  ||t        ||                - | j                  |      rl| j                   j                  |dt        | j                  |                   | j                   j                  |dt        | j                  |                   | j	                  |      r6| j                   j                  |dt        | j	                  |                   | j                  |      r6| j                   j                  |dt        | j                  |                   | j                  |      r7| j                   j                  |dt        | j                  |                   y y )N)
r1   r4   r)   r-   	boot_modepowerlast_reboot	anti_ddoshardware_watchsupport)r   public_ipv4ansible_hostos_name
os_version)	inventoryset_variabler   r   r   r$   r'   )r   r)   r   targeted_attributes	attributes        r   _fill_host_variablesz$InventoryModule._fill_host_variables   s[   
 - 	aINN'')[T]I^=_`	a ##z#:NN''-TMeMeq{MeM|A}~NN''.+dNfNfr|NfN}B~$$
$;NN''-TMfMfr|MfM}A~:6NN'')[I]I]isI]It=uv""j"9NN'',DL`L`lvL`Lw@xy :rN   c                j    |D ].  } | j                   |   |      s | j                   |   |      c S  y )N)
extractors)r   r   hostname_preferencesprefs       r   _filter_hostzInventoryModule._filter_host   s?    ( 	9D$tt$Z0,tt,Z88	9 rN   c                l   | j                  ||      }|sy t        |      }| j                  j                  |       | j	                  ||       |D ]_  } | j
                  |   |      }|s y t        |      }| j                  j                  |       | j                  j                  ||       a y )N)r   ra   )host)r)   r   )group)rf   re   )rc   r   rZ   add_hostr^   group_extractors	add_group)r   r   ra   group_preferencesr)   grf   s          r   do_server_inventoryz#InventoryModule.do_server_inventory   s    $$
:N % P x(X.!!8
!K" 		@A,D))!,Z8E&ENN$$5$1NN##%h#?		@rN   c                <   t         t        |   |||       | j                  |       | j	                  d      }| j	                  d      }| j	                  d      }|g }| j
                  | j                  | j                  d| _        | j                  | j                  | j                  d| _        d| dt         d	t        j                  d
d      d    dd| _        t#        t        j$                  d      }| j'                  |      }	d|v rBt#        t        j$                  d      }
| j'                  |
      }| j)                  |      | _        |	D ]E  }t#        t        j$                  |      }| j'                  |      }|2| j-                  |||       G y )N)pathoauth_token	hostnamesgroups)rV   private_ipv4r)   )r-   r1   rK   zBearer zansible z Python     r   zapplication/json)Authorizationz
User-AgentzContent-typezapi/v1/server)rA   rK   zapi/v1/rpn/group)r   ra   rj   )superr   parse_read_config_data
get_optionr   r   r*   r`   r/   r2   r6   rh   ansible_versionpython_versionsplitr8   r
   API_ENDPOINTrE   rM   r5   rl   )r   rZ   loaderrn   cachetokenra   rj   servers_urlservers_api_pathrpn_groups_urlrI   server_api_path
server_urlraw_server_info	__class__s                  r   rw   zInventoryModule.parse   s   ot*9fdCD).#{; OOH5$ "  33 55--
 --''##!
  'ug.$_$5Xn>R>RSVXY>Z[\>]=^_.
 o::OL22{2C%%$_%A%ACUVN..>.BH$($A$A($KD!/ 
	JO !=!=OJ"55*5EO&$$:N7H % J
	JrN   )T)__name__
__module____qualname__NAMEr}   r   r   r$   r'   r*   r/   r2   r6   rE   staticmethodrM   r^   rc   rl   rw   __classcell__)r   s   @r   r   r   L   sk    %D+L9"  z:@2/J /JrN   r   )
__future__r   DOCUMENTATIONEXAMPLESr>   sysr   r{   ansible.errorsr   ansible.module_utils.urlsr   ansible.plugins.inventoryr   +ansible.module_utils.common.text.convertersr   $ansible.module_utils.ansible_releaser	   rz   +ansible.module_utils.six.moves.urllib.parser
   Aansible_collections.community.general.plugins.plugin_utils.unsafer   r   rH   rN   r   <module>r      sG    #(T  ) ' . 9 ? O ? YBJ) BJrN   