
    Vh*                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZmZmZ ddlmZ  G d deee      Zy)    )absolute_importdivisionprint_functiona  
---
name: vultr
short_description: Retrieves list of instances via Vultr v2 API
description:
  - Vultr inventory plugin.
  - Retrieves list of instances via Vultr v2 API.
  - Configuration of this plugin is done with files ending with '(vultr|vultr_hosts|vultr_instances).(yaml|yml)'
version_added: '1.4.0'
author:
  - jasites (@jasites)
extends_documentation_fragment:
  - constructed
  - inventory_cache
options:
  api_endpoint:
    description:
      - URL to API endpint (without trailing slash).
      - Fallback environment variable C(VULTR_API_ENDPOINT).
    type: str
    env:
      - name: VULTR_API_ENDPOINT
    default: https://api.vultr.com/v2
  api_key:
    description:
      - API key of the Vultr API.
      - Fallback environment variable C(VULTR_API_KEY).
    type: str
    env:
      - name: VULTR_API_KEY
    required: true
  api_results_per_page:
    description:
      - When receiving large numbers of instances, specify how many instances should be returned per call to API.
      - This does not determine how many results are returned; all instances are returned according to other filters.
      - Vultr API maximum is 500.
      - Fallback environment variable C(VULTR_API_RESULTS_PER_PAGE).
    type: int
    env:
      - name: VULTR_API_RESULTS_PER_PAGE
    default: 100
  api_timeout:
    description:
      - HTTP timeout to Vultr API.
      - Fallback environment variable C(VULTR_API_TIMEOUT).
    type: int
    env:
      - name: VULTR_API_TIMEOUT
    default: 60
  attributes:
    description:
      - Instance attributes to add as host variables to each host added to inventory.
      - See U(https://www.vultr.com/api/#operation/list-instances) for valid values.
      - The I(internal_ip) attribute was added in version 1.10.0.
    type: list
    elements: str
    default:
      - id
      - region
      - label
      - plan
      - hostname
      - main_ip
      - v6_main_ip
      - tags
      - internal_ip
  filters:
    description:
      - Filter hosts with Jinja2 templates.
      - If not provided, all hosts are added to inventory.
    type: list
    elements: str
    default: []
  instance_type:
    description:
      - Type of instance.
    type: str
    default: cloud
    choices:
      - cloud
      - bare_metal
    version_added: '1.8.0'
  plugin:
    description:
      - Name of Vultr inventory plugin.
      - This should always be C(vultr.cloud.vultr).
    type: str
    choices: ['vultr.cloud.vultr']
    required: true
  variable_prefix:
    description:
      - Prefix of generated variables (e.g. C(id) becomes C(vultr_id)).
    type: str
    default: 'vultr_'
  validate_certs:
    description:
      - Validate SSL certs of the Vultr API.
    type: bool
    default: true
notes:
  - Also see the API documentation on U(https://www.vultr.com/api/).
a  
---
# File endings vultr{,_{hosts,instances}}.y{,a}ml
# All configuration done via environment variables:
plugin: vultr.cloud.vultr

# Grouping and filtering configuration in inventory file
plugin: vultr.cloud.vultr
api_key: '{{ lookup("pipe"), "./get_vultr_api_key.sh" }}'
keyed_groups:
  - key: vultr_tags | lower
    prefix: ''
    separator: ''
filters:
  - '"vpc" in vultr_tags'
  - 'vultr_plan == "vc2-2c-4gb"'

# Unless you can connect to your servers via it's vultr label,
# we suggest setting ansible_host with compose:
plugin: vultr.cloud.vultr
compose:
  ansible_host: vultr_main_ip

# Respectively for IPv6:
plugin: vultr.cloud.vultr
compose:
  ansible_host: vultr_v6_main_ip

# Prioritize IPv6 over IPv4 if available.
plugin: vultr.cloud.vultr
compose:
  ansible_host: vultr_v6_main_ip or vultr_main_ip

# Use the internal IP
plugin: vultr.cloud.vultr
compose:
  ansible_host: vultr_internal_ip

# Querying the bare metal instances
plugin: vultr.cloud.vultr
instance_type: bare_metal
z # N)AnsibleErrorAnsibleParserError)	to_native)	HTTPErrorURLError)Request)BaseInventoryPlugin	CacheableConstructable   )VULTR_USER_AGENTc                   X     e Zd ZdZdddddddZd Zd Zdd	Z fd
Zd fd	Z	 xZ
S )InventoryModulezvultr.cloud.vultr	instances)resourceresponsezbare-metalsbare_metals)cloud
bare_metalc                    g }| j                  d      }| j                  j                  |      r| j                  j                  |      }dt        dj                  |      d}t        |t        | j                  d            | j                  d            | _        | j                  d      xs d	}| j                  j                  d
j                  |             | j                  |   }dj                  | j                  d      |d   | j                  d            }d}|}	 	 | j                  j                  dj                  |             t        j                  | j                  j                  |            }	|j                  |	|d             |	d   d   d   }|dk(  r|S dj                  ||      }# t         t"        f$ r t%        d      t&        t(        f$ r}
t%        |
      d }
~
ww xY w)Napi_keyzapplication/jsonz
Bearer {0})zContent-Typez
User-AgentAuthorizationapi_timeoutvalidate_certs)headerstimeoutr   instance_typer   zType is: {0}z{0}/{1}?per_page={2}api_endpointr   api_results_per_page zQuerying API: {0}r   metalinksnextz{0}&cursor={1}zUnable to parse JSON response.)
get_optiontemplaris_templatetemplater   formatr   intreqdisplayvvvRESOURCES_PER_TYPEjsonloadgetextendKeyError
ValueErrorr   r
   r	   )selfr   r   r   instance_type_configr    r!   cursorreq_urlpageerrs              g/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/vultr/cloud/plugins/inventory/vultr.py_get_instanceszInventoryModule._get_instances   s   	//),<<##G,ll++G4G /*)009
 67??+;<
  $?J7../CDE//0DE-44OON+*%OO23
 	*  !4!;!;G!DEyyg!67  mJ&?!@Afg.v6R<$$*11,G  *% 	G$%EFF)$ 	*$S))	*s   $BF9 &F9 9(G1!G,,G1c                     | j                  d      }| j                  d      }| j                  d      }| j                  d      }|D ]  }|j                  d      }|s;| j                  j                  dj	                  |j                  d                   Ri }|j                         D ]  \  }	}
|	|v s|
|d	j	                  ||	      <   ! | j                  ||||      s+| j                  j                  d
j	                  |             | j                  j                  |       |j                         D ]"  \  }}| j                  j                  |||       $ | j                  | j                  d      | j                  j                  |      j                         ||       | j                  | j                  d      t               ||       | j!                  | j                  d      t               ||        y )N
attributesfiltersstrictvariable_prefixlabelz'instance ID {0} has no label, skipping.id)msgz{0}{1}zHost {0} excluded by filterscomposegroupskeyed_groups)r'   r3   r.   warningr+   items_passes_filtersr/   	inventoryadd_hostset_variable_set_composite_varsget_hostget_vars_add_host_to_composed_groupsdict_add_host_to_keyed_groups)r7   r   r@   host_filtersrB   rC   instanceinstance_labelhost_variableskvvar_namevar_vals                r=   	_populatezInventoryModule._populate   s   __\2
y1*//*;<! -	H%\\'2N!$$)R)Y)YZbZfZfgkZl)m$nN ( L1
?JKN8???A#FGL ''	   !?!F!F~!VWNN##N3%3%9%9%; O!'++NHgNO $$	*''7@@B	 --)	 **/	Q-	    c                     |r,t        |t              r|D ]  }	 | j                  ||      s y y# t        $ r2}|r%t	        dj                  ||t        |                  Y d }~ yd }~ww xY w)NFz/Could not evaluate host filter {0} for {1}: {2}T)
isinstancelist_compose	Exceptionr   r+   r   )r7   rA   	variableshostrB   r*   es          r=   rL   zInventoryModule._passes_filters%  s    z'40# !!==9=$ >!  ! 	!*MTT ( $ )!  !	!s   0	A+'A&&A+c                     d}t         t        |   |      r0|j                  d      rd}|S | j                  j                  d       |S )NF)z
vultr.yamlz	vultr.ymlzvultr_hosts.yamlzvultr_hosts.ymlzvultr_instances.yamlzvultr_instances.ymlTzSkipping due to inventory configuration file name mismatch. Valid filename endings: vultr.yaml, vultr.yml, vultr_hosts.yaml, vultr_hosts.yml, vultr_instances.yaml, vultr_instances.yml)superr   verify_fileendswithr.   r/   )r7   pathvalid	__class__s      r=   rj   zInventoryModule.verify_file7  sW    $3D9}}	     @ r_   c                 |   t         t        |   |||       | j                  |       | j	                  |      }| j                  d      xr |}| j                  d      xr | }d }|r	 | j                  |   }|| j                         }|r|| j                  |<   | j                  |       y # t        $ r d}Y Bw xY w)NcacheT)
ri   r   parse_read_config_dataget_cache_keyr'   _cacher5   r>   r^   )
r7   rM   loaderrl   rp   	cache_key	use_cacheupdate_cacher   rn   s
            r=   rq   zInventoryModule.parseN  s    ot*9fdCt$&&t,	OOG,6	w/=I	$ KK	2	 ++-I%.DKK	"y!  $#$s   )B- -B;:B;)F)T)__name__
__module____qualname__NAMEr0   r>   r^   rL   rj   rq   __classcell__)rn   s   @r=   r   r      sJ    D $#

 &%
	/*b3j$." "r_   r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESRETURNr1   ansible.errorsr   r   ansible.module_utils._textr   +ansible.module_utils.six.moves.urllib.errorr	   r
   ansible.module_utils.urlsr   ansible.plugins.inventoryr   r   r   module_utils.vultr_v2r   r    r_   r=   <module>r      s\    A @eN)V 
  ; 0 K -6 6 5t")=) t"r_   